Skip to content

Instantly share code, notes, and snippets.

@Noxsios
Created October 20, 2023 21:56
Show Gist options
  • Save Noxsios/86dc832e127a2a31577f233cb70724dc to your computer and use it in GitHub Desktop.
Save Noxsios/86dc832e127a2a31577f233cb70724dc to your computer and use it in GitHub Desktop.
func (p *Packager) updateComponentImagesInplace(index int, images []string, cm goyaml.CommentMap) error {
component := p.cfg.Pkg.Components[index]
for imageIdx, image := range component.Images {
commentsPath := fmt.Sprintf("$.components[%d].images[%d]", index, imageIdx)
imageComments := cm[commentsPath]
left, err := transform.ParseImageRef(image)
if err != nil {
return err
}
var right transform.Image
for _, newImage := range images {
right, err = transform.ParseImageRef(newImage)
if err != nil {
return err
}
if left.Host == right.Host && left.Path == right.Path && left.Name == right.Name {
break
}
}
// If the image is not found, remove it, unless it has a special comment
if right == (transform.Image{}) {
ignore := false
for _, comment := range imageComments {
if comment.Position == goyaml.CommentLinePosition {
if helpers.SliceContains(comment.Texts, "zarf-find-images:ignore") {
// do nothing
ignore = true
}
}
}
if !ignore {
// remove the image
component.Images = append(component.Images[:imageIdx], component.Images[imageIdx+1:]...)
continue
// TODO: add a comment to the component that the image was removed
// and figure out how to shift the comments up
}
}
// If left == right, do nothing
if left == right {
continue
}
// If the registry and repository are the same, update the tag
if left.TagOrDigest != right.TagOrDigest {
// check the comment map for a do not update comment
ignore := false
for _, comment := range imageComments {
if comment.Position == goyaml.CommentLinePosition {
if helpers.SliceContains(comment.Texts, "zarf-find-images:ignore") {
// do nothing
ignore = true
}
}
}
if !ignore {
component.Images[imageIdx] = right.String()
continue
}
}
// Otherwise, append the new image
component.Images = append(component.Images, right.String())
}
p.cfg.Pkg.Components[index] = component
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment