-
Notifications
You must be signed in to change notification settings - Fork 64
✨ Add support for deploying OCI helm charts in OLM v1 #1971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
OchiengEd
wants to merge
1
commit into
operator-framework:main
Choose a base branch
from
OchiengEd:helm_explorations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
package image | ||
|
||
import ( | ||
"archive/tar" | ||
"bytes" | ||
"compress/gzip" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"io/fs" | ||
"iter" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"slices" | ||
"strings" | ||
"time" | ||
|
||
"github.com/containers/image/v5/docker/reference" | ||
"github.com/containers/image/v5/types" | ||
ocispecv1 "github.com/opencontainers/image-spec/specs-go/v1" | ||
"gopkg.in/yaml.v2" | ||
"helm.sh/helm/v3/pkg/chart" | ||
"helm.sh/helm/v3/pkg/chart/loader" | ||
"helm.sh/helm/v3/pkg/registry" | ||
) | ||
|
||
func hasChart(imgCloser types.ImageCloser) bool { | ||
config := imgCloser.ConfigInfo() | ||
return config.MediaType == registry.ConfigMediaType | ||
} | ||
|
||
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) { | ||
imgDigest := canonicalRef.Digest() | ||
raw, _, err := imgSrc.GetManifest(ctx, &imgDigest) | ||
if err != nil { | ||
return nil, time.Time{}, fmt.Errorf("get OCI helm chart manifest; %w", err) | ||
OchiengEd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
chartManifest := ocispecv1.Manifest{} | ||
if err := json.Unmarshal(raw, &chartManifest); err != nil { | ||
return nil, time.Time{}, fmt.Errorf("unmarshaling chart manifest; %w", err) | ||
} | ||
|
||
if len(chartManifest.Layers) == 0 { | ||
return nil, time.Time{}, fmt.Errorf("manifest has no layers; expected at least one chart layer") | ||
} | ||
|
||
layerIter := iter.Seq[LayerData](func(yield func(LayerData) bool) { | ||
for i, layer := range chartManifest.Layers { | ||
ld := LayerData{Index: i, MediaType: layer.MediaType} | ||
if layer.MediaType == registry.ChartLayerMediaType { | ||
var contents []byte | ||
contents, ld.Err = os.ReadFile(filepath.Join( | ||
imgRef.PolicyConfigurationIdentity(), "blobs", | ||
"sha256", chartManifest.Layers[i].Digest.Encoded()), | ||
) | ||
ld.Reader = bytes.NewBuffer(contents) | ||
} | ||
// Ignore the Helm provenance data layer | ||
if layer.MediaType == registry.ProvLayerMediaType { | ||
continue | ||
} | ||
if !yield(ld) { | ||
return | ||
} | ||
} | ||
}) | ||
|
||
return cache.Store(ctx, ownerID, srcRef, canonicalRef, ocispecv1.Image{}, layerIter) | ||
} | ||
|
||
func IsValidChart(chart *chart.Chart) error { | ||
if chart.Metadata == nil { | ||
return errors.New("chart metadata is missing") | ||
} | ||
if chart.Metadata.Name == "" { | ||
return errors.New("chart name is required") | ||
} | ||
if chart.Metadata.Version == "" { | ||
return errors.New("chart version is required") | ||
} | ||
return chart.Metadata.Validate() | ||
} | ||
|
||
type chartInspectionResult struct { | ||
// templatesExist is set to true if the templates | ||
// directory exists in the chart archive | ||
templatesExist bool | ||
// chartfileExists is set to true if the Chart.yaml | ||
// file exists in the chart archive | ||
chartfileExists bool | ||
} | ||
|
||
func inspectChart(data []byte, metadata *chart.Metadata) (chartInspectionResult, error) { | ||
gzReader, err := gzip.NewReader(bytes.NewReader(data)) | ||
if err != nil { | ||
return chartInspectionResult{}, err | ||
} | ||
defer gzReader.Close() | ||
|
||
report := chartInspectionResult{} | ||
tarReader := tar.NewReader(gzReader) | ||
for { | ||
header, err := tarReader.Next() | ||
if err == io.EOF { | ||
if !report.chartfileExists && !report.templatesExist { | ||
return report, errors.New("neither Chart.yaml nor templates directory were found") | ||
} | ||
|
||
if !report.chartfileExists { | ||
return report, errors.New("the Chart.yaml file was not found") | ||
} | ||
|
||
if !report.templatesExist { | ||
return report, errors.New("templates directory not found") | ||
} | ||
|
||
return report, nil | ||
} | ||
|
||
if strings.HasSuffix(header.Name, filepath.Join("templates", filepath.Base(header.Name))) { | ||
report.templatesExist = true | ||
} | ||
|
||
if filepath.Base(header.Name) == "Chart.yaml" { | ||
report.chartfileExists = true | ||
if err := loadMetadataArchive(tarReader, metadata); err != nil { | ||
return report, err | ||
} | ||
} | ||
} | ||
} | ||
|
||
func loadMetadataArchive(r io.Reader, metadata *chart.Metadata) error { | ||
if metadata == nil { | ||
return nil | ||
} | ||
|
||
content, err := io.ReadAll(r) | ||
if err != nil { | ||
return fmt.Errorf("reading Chart.yaml; %w", err) | ||
} | ||
|
||
if err := yaml.Unmarshal(content, metadata); err != nil { | ||
return fmt.Errorf("unmarshaling Chart.yaml; %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func IsBundleSourceChart(bundleFS fs.FS, metadata *chart.Metadata) (bool, error) { | ||
var chartPath string | ||
files, _ := fs.ReadDir(bundleFS, ".") | ||
for _, file := range files { | ||
if slices.Contains([]string{".tar.gz", ".tgz"}, filepath.Ext(file.Name())) { | ||
chartPath = file.Name() | ||
break | ||
} | ||
} | ||
|
||
chartData, err := fs.ReadFile(bundleFS, chartPath) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
result, err := inspectChart(chartData, metadata) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return (result.templatesExist && result.chartfileExists), nil | ||
} | ||
|
||
type ChartOption func(*chart.Chart) | ||
|
||
func WithInstallNamespace(namespace string) ChartOption { | ||
re := regexp.MustCompile(`{{\W+\.Release\.Namespace\W+}}`) | ||
|
||
return func(chrt *chart.Chart) { | ||
for i, template := range chrt.Templates { | ||
chrt.Templates[i].Data = re.ReplaceAll(template.Data, []byte(namespace)) | ||
} | ||
} | ||
} | ||
|
||
func LoadChartFSWithOptions(bundleFS fs.FS, filename string, options ...ChartOption) (*chart.Chart, error) { | ||
ch, err := loadChartFS(bundleFS, filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return enrichChart(ch, options...) | ||
} | ||
|
||
func enrichChart(chart *chart.Chart, options ...ChartOption) (*chart.Chart, error) { | ||
if chart == nil { | ||
return nil, fmt.Errorf("chart can not be nil") | ||
} | ||
for _, f := range options { | ||
f(chart) | ||
} | ||
return chart, nil | ||
} | ||
|
||
var LoadChartFS = loadChartFS | ||
|
||
// loadChartFS loads a chart archive from a filesystem of | ||
// type fs.FS with the provided filename | ||
func loadChartFS(bundleFS fs.FS, filename string) (*chart.Chart, error) { | ||
if filename == "" { | ||
return nil, fmt.Errorf("chart file name was not provided") | ||
} | ||
|
||
tarball, err := fs.ReadFile(bundleFS, filename) | ||
if err != nil { | ||
return nil, fmt.Errorf("reading chart %s; %+v", filename, err) | ||
} | ||
return loader.LoadArchive(bytes.NewBuffer(tarball)) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.