|
| 1 | +package image |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/tar" |
| 5 | + "bytes" |
| 6 | + "compress/gzip" |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "errors" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "io/fs" |
| 13 | + "iter" |
| 14 | + "os" |
| 15 | + "path/filepath" |
| 16 | + "regexp" |
| 17 | + "slices" |
| 18 | + "strings" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/containers/image/v5/docker/reference" |
| 22 | + "github.com/containers/image/v5/types" |
| 23 | + ocispecv1 "github.com/opencontainers/image-spec/specs-go/v1" |
| 24 | + "gopkg.in/yaml.v2" |
| 25 | + "helm.sh/helm/v3/pkg/chart" |
| 26 | + "helm.sh/helm/v3/pkg/chart/loader" |
| 27 | + "helm.sh/helm/v3/pkg/registry" |
| 28 | +) |
| 29 | + |
| 30 | +func hasChart(imgCloser types.ImageCloser) bool { |
| 31 | + config := imgCloser.ConfigInfo() |
| 32 | + return config.MediaType == registry.ConfigMediaType |
| 33 | +} |
| 34 | + |
| 35 | +func pullChart(ctx context.Context, ownerID string, srcRef reference.Named, canonicalRef reference.Canonical, imgSrc types.ImageSource, imgRef types.ImageReference, cache Cache) (fs.FS, time.Time, error) { |
| 36 | + imgDigest := canonicalRef.Digest() |
| 37 | + raw, _, err := imgSrc.GetManifest(ctx, &imgDigest) |
| 38 | + if err != nil { |
| 39 | + return nil, time.Time{}, fmt.Errorf("get OCI helm chart manifest; %w", err) |
| 40 | + } |
| 41 | + |
| 42 | + chartManifest := ocispecv1.Manifest{} |
| 43 | + if err := json.Unmarshal(raw, &chartManifest); err != nil { |
| 44 | + return nil, time.Time{}, fmt.Errorf("unmarshaling chart manifest; %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + if len(chartManifest.Layers) == 0 { |
| 48 | + return nil, time.Time{}, fmt.Errorf("manifest has no layers; expected at least one chart layer") |
| 49 | + } |
| 50 | + |
| 51 | + layerIter := iter.Seq[LayerData](func(yield func(LayerData) bool) { |
| 52 | + for i, layer := range chartManifest.Layers { |
| 53 | + ld := LayerData{Index: i, MediaType: layer.MediaType} |
| 54 | + if layer.MediaType == registry.ChartLayerMediaType { |
| 55 | + var contents []byte |
| 56 | + contents, ld.Err = os.ReadFile(filepath.Join( |
| 57 | + imgRef.PolicyConfigurationIdentity(), "blobs", |
| 58 | + "sha256", chartManifest.Layers[i].Digest.Encoded()), |
| 59 | + ) |
| 60 | + ld.Reader = bytes.NewBuffer(contents) |
| 61 | + } |
| 62 | + // Ignore the Helm provenance data layer |
| 63 | + if layer.MediaType == registry.ProvLayerMediaType { |
| 64 | + continue |
| 65 | + } |
| 66 | + if !yield(ld) { |
| 67 | + return |
| 68 | + } |
| 69 | + } |
| 70 | + }) |
| 71 | + |
| 72 | + return cache.Store(ctx, ownerID, srcRef, canonicalRef, ocispecv1.Image{}, layerIter) |
| 73 | +} |
| 74 | + |
| 75 | +func IsValidChart(chart *chart.Chart) error { |
| 76 | + if chart.Metadata == nil { |
| 77 | + return errors.New("chart metadata is missing") |
| 78 | + } |
| 79 | + if chart.Metadata.Name == "" { |
| 80 | + return errors.New("chart name is required") |
| 81 | + } |
| 82 | + if chart.Metadata.Version == "" { |
| 83 | + return errors.New("chart version is required") |
| 84 | + } |
| 85 | + return chart.Metadata.Validate() |
| 86 | +} |
| 87 | + |
| 88 | +type chartInspectionResult struct { |
| 89 | + // templatesExist is set to true if the templates |
| 90 | + // directory exists in the chart archive |
| 91 | + templatesExist bool |
| 92 | + // chartfileExists is set to true if the Chart.yaml |
| 93 | + // file exists in the chart archive |
| 94 | + chartfileExists bool |
| 95 | +} |
| 96 | + |
| 97 | +func inspectChart(data []byte, metadata *chart.Metadata) (chartInspectionResult, error) { |
| 98 | + gzReader, err := gzip.NewReader(bytes.NewReader(data)) |
| 99 | + if err != nil { |
| 100 | + return chartInspectionResult{}, err |
| 101 | + } |
| 102 | + defer gzReader.Close() |
| 103 | + |
| 104 | + report := chartInspectionResult{} |
| 105 | + tarReader := tar.NewReader(gzReader) |
| 106 | + for { |
| 107 | + header, err := tarReader.Next() |
| 108 | + if err == io.EOF { |
| 109 | + if !report.chartfileExists && !report.templatesExist { |
| 110 | + return report, errors.New("neither Chart.yaml nor templates directory were found") |
| 111 | + } |
| 112 | + |
| 113 | + if !report.chartfileExists { |
| 114 | + return report, errors.New("the Chart.yaml file was not found") |
| 115 | + } |
| 116 | + |
| 117 | + if !report.templatesExist { |
| 118 | + return report, errors.New("templates directory not found") |
| 119 | + } |
| 120 | + |
| 121 | + return report, nil |
| 122 | + } |
| 123 | + |
| 124 | + if strings.HasSuffix(header.Name, filepath.Join("templates", filepath.Base(header.Name))) { |
| 125 | + report.templatesExist = true |
| 126 | + } |
| 127 | + |
| 128 | + if filepath.Base(header.Name) == "Chart.yaml" { |
| 129 | + report.chartfileExists = true |
| 130 | + if err := loadMetadataArchive(tarReader, metadata); err != nil { |
| 131 | + return report, err |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func loadMetadataArchive(r io.Reader, metadata *chart.Metadata) error { |
| 138 | + if metadata == nil { |
| 139 | + return nil |
| 140 | + } |
| 141 | + |
| 142 | + content, err := io.ReadAll(r) |
| 143 | + if err != nil { |
| 144 | + return fmt.Errorf("reading Chart.yaml; %w", err) |
| 145 | + } |
| 146 | + |
| 147 | + if err := yaml.Unmarshal(content, metadata); err != nil { |
| 148 | + return fmt.Errorf("unmarshaling Chart.yaml; %w", err) |
| 149 | + } |
| 150 | + |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +func IsBundleSourceChart(bundleFS fs.FS, metadata *chart.Metadata) (bool, error) { |
| 155 | + var chartPath string |
| 156 | + files, _ := fs.ReadDir(bundleFS, ".") |
| 157 | + for _, file := range files { |
| 158 | + if slices.Contains([]string{".tar.gz", ".tgz"}, filepath.Ext(file.Name())) { |
| 159 | + chartPath = file.Name() |
| 160 | + break |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + chartData, err := fs.ReadFile(bundleFS, chartPath) |
| 165 | + if err != nil { |
| 166 | + return false, err |
| 167 | + } |
| 168 | + |
| 169 | + result, err := inspectChart(chartData, metadata) |
| 170 | + if err != nil { |
| 171 | + return false, err |
| 172 | + } |
| 173 | + |
| 174 | + return (result.templatesExist && result.chartfileExists), nil |
| 175 | +} |
| 176 | + |
| 177 | +type ChartOption func(*chart.Chart) |
| 178 | + |
| 179 | +func WithInstallNamespace(namespace string) ChartOption { |
| 180 | + re := regexp.MustCompile(`{{\W+\.Release\.Namespace\W+}}`) |
| 181 | + |
| 182 | + return func(chrt *chart.Chart) { |
| 183 | + for i, template := range chrt.Templates { |
| 184 | + chrt.Templates[i].Data = re.ReplaceAll(template.Data, []byte(namespace)) |
| 185 | + } |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +func LoadChartFSWithOptions(bundleFS fs.FS, filename string, options ...ChartOption) (*chart.Chart, error) { |
| 190 | + ch, err := loadChartFS(bundleFS, filename) |
| 191 | + if err != nil { |
| 192 | + return nil, err |
| 193 | + } |
| 194 | + |
| 195 | + return enrichChart(ch, options...) |
| 196 | +} |
| 197 | + |
| 198 | +func enrichChart(chart *chart.Chart, options ...ChartOption) (*chart.Chart, error) { |
| 199 | + if chart == nil { |
| 200 | + return nil, fmt.Errorf("chart can not be nil") |
| 201 | + } |
| 202 | + for _, f := range options { |
| 203 | + f(chart) |
| 204 | + } |
| 205 | + return chart, nil |
| 206 | +} |
| 207 | + |
| 208 | +var LoadChartFS = loadChartFS |
| 209 | + |
| 210 | +// loadChartFS loads a chart archive from a filesystem of |
| 211 | +// type fs.FS with the provided filename |
| 212 | +func loadChartFS(bundleFS fs.FS, filename string) (*chart.Chart, error) { |
| 213 | + if filename == "" { |
| 214 | + return nil, fmt.Errorf("chart file name was not provided") |
| 215 | + } |
| 216 | + |
| 217 | + tarball, err := fs.ReadFile(bundleFS, filename) |
| 218 | + if err != nil { |
| 219 | + return nil, fmt.Errorf("reading chart %s; %+v", filename, err) |
| 220 | + } |
| 221 | + return loader.LoadArchive(bytes.NewBuffer(tarball)) |
| 222 | +} |
0 commit comments