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