diff --git a/cmd/chart/verify.go b/cmd/chart/verify.go index e9a4c6e95..79bac856f 100644 --- a/cmd/chart/verify.go +++ b/cmd/chart/verify.go @@ -36,6 +36,15 @@ autoscaler ghcr.io/openfaasltd/autoscaler:0.2.5 command.Flags().StringP("file", "f", "", "Path to values.yaml file") command.Flags().BoolP("verbose", "v", false, "Verbose output") + command.Flags().IntP("depth", "d", 3, "how many levels deep into the YAML structure to walk looking for image: tags") + + command.PreRunE = func(cmd *cobra.Command, args []string) error { + _, err := cmd.Flags().GetInt("depth") + if err != nil { + return fmt.Errorf("error with --depth usage: %s", err) + } + return nil + } command.RunE = func(cmd *cobra.Command, args []string) error { file, err := cmd.Flags().GetString("file") @@ -44,6 +53,7 @@ autoscaler ghcr.io/openfaasltd/autoscaler:0.2.5 } verbose, _ := cmd.Flags().GetBool("verbose") + depth, _ := cmd.Flags().GetInt("depth") if len(file) == 0 { return fmt.Errorf("flag --file is required") @@ -62,7 +72,7 @@ autoscaler ghcr.io/openfaasltd/autoscaler:0.2.5 return err } - filtered := helm.FilterImages(values) + filtered := helm.FilterImagesUptoDepth(values, depth) if len(filtered) == 0 { return fmt.Errorf("no images found in %s", file) } diff --git a/pkg/helm/io.go b/pkg/helm/io.go index cb6642d21..6b1ad4834 100644 --- a/pkg/helm/io.go +++ b/pkg/helm/io.go @@ -8,7 +8,7 @@ import ( ) // ValuesMap is an alias for map[string]interface{} -type ValuesMap map[string]interface{} +type ValuesMap map[interface{}]interface{} // Load a values.yaml file and return a ValuesMap with the keys // and values from the YAML file as a map[string]interface{} @@ -28,28 +28,27 @@ func Load(yamlPath string) (ValuesMap, error) { return values, nil } -// FilterImages takes a ValuesMap and returns a map of images that -// were found at the top level, or one level down with keys of -// "image: " -func FilterImages(values ValuesMap) map[string]string { +// FilterImagesUptoDepth takes a ValuesMap and returns a map of images that +// were found upto max level +func FilterImagesUptoDepth(values ValuesMap, depth int) map[string]string { images := map[string]string{} for k, v := range values { - - // Match anything at the top level called "image: ..." if k == "image" { - images[k] = v.(string) + imageUrl := v.(string) + images[imageUrl] = imageUrl } - // Match anything at one level down i.e. "gateway.image: ..." - if c, ok := v.(map[interface{}]interface{}); ok && c != nil { - for kk, vv := range c { - if kk == "image" { - images[k] = vv.(string) - } - } + if c, ok := v.(ValuesMap); ok && depth > 0 { + images = mergeMaps(images, FilterImagesUptoDepth(c, depth-1)) } } - return images } + +func mergeMaps(original, latest map[string]string) map[string]string { + for k, v := range latest { + original[k] = v + } + return original +}