Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --update flag to zarf dev find-images #3296

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ func init() {

devFindImagesCmd.Flags().StringVar(&pkgConfig.FindImagesOpts.RegistryURL, "registry-url", defaultRegistry, lang.CmdDevFlagRegistry)

// update zarf.yaml with found image in place
devFindImagesCmd.Flags().BoolVar(&pkgConfig.FindImagesOpts.UpdatePackageDefinition, "update", false, lang.CmdDevFlagFindImagesUpdate)

devLintCmd.Flags().StringToStringVar(&pkgConfig.CreateOpts.SetVariables, "set", v.GetStringMapString(common.VPkgCreateSet), lang.CmdPackageCreateFlagSet)
devLintCmd.Flags().StringVarP(&pkgConfig.CreateOpts.Flavor, "flavor", "f", v.GetString(common.VPkgCreateFlavor), lang.CmdPackageCreateFlagFlavor)
devTransformGitLinksCmd.Flags().StringVar(&pkgConfig.InitOpts.GitServer.PushUsername, "git-account", types.ZarfGitPushUser, lang.CmdDevFlagGitAccount)
Expand Down
1 change: 1 addition & 0 deletions src/config/lang/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ $ zarf package pull oci://ghcr.io/defenseunicorns/packages/dos-games:1.0.0 -a sk
CmdDevFlagRegistry = "Override the ###ZARF_REGISTRY### value"
CmdDevFlagFindImagesWhy = "Prints the source manifest for the specified image"
CmdDevFlagFindImagesSkipCosign = "Skip searching for cosign artifacts related to discovered images"
CmdDevFlagFindImagesUpdate = "Updates zarf.yaml with found images"

CmdDevLintShort = "Lints the given package for valid schema and recommended practices"
CmdDevLintLong = "Verifies the package schema, checks if any variables won't be evaluated, and checks for unpinned images/repos/files"
Expand Down
53 changes: 52 additions & 1 deletion src/pkg/packager/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"errors"
"fmt"
"github.com/goccy/go-yaml/parser"
"github.com/zarf-dev/zarf/src/pkg/logger"
"os"
"path/filepath"
Expand Down Expand Up @@ -76,7 +77,57 @@ func (p *Packager) FindImages(ctx context.Context) (map[string][]string, error)
}
p.cfg.Pkg = pkg

return p.findImages(ctx)
images, err := p.findImages(ctx)
if err != nil {
return nil, err
}
if p.cfg.FindImagesOpts.UpdatePackageDefinition {
zarfYamlBytes, err := os.ReadFile(p.layout.ZarfYAML)
if err != nil {
return nil, err
}
zarfYaml := v1alpha1.ZarfPackage{}
err = yaml.Unmarshal(zarfYamlBytes, &zarfYaml)
if err != nil {
return nil, err
}
// yamlpath support of goccy/go-yaml only has index-based lookup, so we need to figure out index
componentToIndex := map[string]int{}
for i, component := range zarfYaml.Components {
componentToIndex[component.Name] = i
}

astFile, err := parser.ParseFile(p.layout.ZarfYAML, parser.ParseComments)
if err != nil {
return nil, err
}
for componentName, images := range images {
sortedImages := append([]string{}, images...)
sort.Strings(sortedImages)
componentMerge := map[string]interface{}{}
componentMerge["images"] = sortedImages
componentNode, err := yaml.ValueToNode(componentMerge, yaml.IndentSequence(true))
if err != nil {
return nil, err
}

path, err := yaml.PathString(fmt.Sprintf("$.components[%d]", componentToIndex[componentName]))
if err != nil {
return nil, err
}
err = path.MergeFromNode(astFile, componentNode)
if err != nil {
return nil, err
}
}
updatedZarfYaml := astFile.String()
err = os.WriteFile(layout.ZarfYAML, []byte(updatedZarfYaml), os.ModePerm)
if err != nil {
return nil, err
}
}
return images, err

}

// TODO: Refactor to return output string instead of printing inside of function.
Expand Down
2 changes: 2 additions & 0 deletions src/types/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type ZarfFindImagesOptions struct {
Why string
// Optionally skip lookup of cosign artifacts when finding images
SkipCosign bool
// Update the images in zarf.yaml in place
UpdatePackageDefinition bool
}

// ZarfDeployOptions tracks the user-defined preferences during a package deploy.
Expand Down