Skip to content

Commit

Permalink
Enable image verification upto Max Level
Browse files Browse the repository at this point in the history
Signed-off-by: Nitishkumar Singh <nitishkumarsingh71@gmail.com>

changed max to depth

Signed-off-by: Nitishkumar Singh <nitishkumarsingh71@gmail.com>
  • Loading branch information
nitishkumar71 authored and alexellis committed Sep 4, 2022
1 parent 3965bae commit 01c3ce4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
12 changes: 11 additions & 1 deletion cmd/chart/verify.go
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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)
}
Expand Down
31 changes: 15 additions & 16 deletions pkg/helm/io.go
Expand Up @@ -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{}
Expand All @@ -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
}

0 comments on commit 01c3ce4

Please sign in to comment.