github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/internal/packager/helm/images.go (about)

     1  package helm
     2  
     3  import (
     4  	"github.com/Racer159/jackal/src/pkg/message"
     5  	"github.com/defenseunicorns/pkg/helpers"
     6  	"github.com/goccy/go-yaml"
     7  	"helm.sh/helm/v3/pkg/chart/loader"
     8  	"helm.sh/helm/v3/pkg/chartutil"
     9  )
    10  
    11  // ChartImages captures the structure of the helm.sh/images annotation within the Helm chart.
    12  type ChartImages []struct {
    13  	// Name of the image.
    14  	Name string `yaml:"name"`
    15  	// Image with tag.
    16  	Image string `yaml:"image"`
    17  	// Condition specifies the values to determine if the image is included or not.
    18  	Condition string `yaml:"condition"`
    19  	// Dependency is the subchart that contains the image, if empty its the parent chart.
    20  	Dependency string `yaml:"dependency"`
    21  }
    22  
    23  // FindAnnotatedImagesForChart attempts to parse any image annotations found in a chart archive or directory.
    24  func FindAnnotatedImagesForChart(chartPath string, values chartutil.Values) (images []string, err error) {
    25  	// Load a new chart.
    26  	chart, err := loader.Load(chartPath)
    27  	if err != nil {
    28  		return images, err
    29  	}
    30  	values = helpers.MergeMapRecursive(chart.Values, values)
    31  
    32  	imageAnnotation := chart.Metadata.Annotations["helm.sh/images"]
    33  
    34  	var chartImages ChartImages
    35  
    36  	err = yaml.Unmarshal([]byte(imageAnnotation), &chartImages)
    37  	if err != nil {
    38  		return images, err
    39  	}
    40  
    41  	for _, i := range chartImages {
    42  		// Only include the image if the current values/condition specify it should be included
    43  		if i.Condition != "" {
    44  			value, err := values.PathValue(i.Condition)
    45  			message.Debugf("%#v - %#v - %#v\n", value, i.Condition, err)
    46  			// We intentionally ignore the error here because the key could be missing from the values.yaml
    47  			if err == nil && value == true {
    48  				images = append(images, i.Image)
    49  			}
    50  		} else {
    51  			images = append(images, i.Image)
    52  		}
    53  	}
    54  
    55  	return images, nil
    56  }