Skip to content

Commit b569fb8

Browse files
committed
Add support for deploying OCI helm charts in OLM v1
* added support for deploying OCI helm charts which sits behind the HelmChartSupport feature gate * extend the Cache Store() method to allow storing of Helm charts * inspect chart archive contents * added MediaType to the LayerData struct Signed-off-by: Edmund Ochieng <[email protected]>
1 parent b152c7b commit b569fb8

File tree

10 files changed

+757
-6
lines changed

10 files changed

+757
-6
lines changed

internal/operator-controller/applier/helm.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ import (
2626

2727
ocv1 "github.com/operator-framework/operator-controller/api/v1"
2828
"github.com/operator-framework/operator-controller/internal/operator-controller/authorization"
29+
"github.com/operator-framework/operator-controller/internal/operator-controller/features"
2930
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle/source"
3031
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/preflights/crdupgradesafety"
3132
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/util"
33+
imageutil "github.com/operator-framework/operator-controller/internal/shared/util/image"
3234
)
3335

3436
const (
@@ -209,6 +211,17 @@ func (h *Helm) buildHelmChart(bundleFS fs.FS, ext *ocv1.ClusterExtension) (*char
209211
if err != nil {
210212
return nil, err
211213
}
214+
if features.OperatorControllerFeatureGate.Enabled(features.HelmChartSupport) {
215+
meta := new(chart.Metadata)
216+
if ok, _ := imageutil.IsBundleSourceChart(bundleFS, meta); ok {
217+
return imageutil.LoadChartFSWithOptions(
218+
bundleFS,
219+
fmt.Sprintf("%s-%s.tgz", meta.Name, meta.Version),
220+
imageutil.WithInstallNamespace(ext.Spec.Namespace),
221+
)
222+
}
223+
}
224+
212225
return h.BundleToHelmChartConverter.ToHelmChart(source.FromFS(bundleFS), ext.Spec.Namespace, watchNamespace)
213226
}
214227

internal/operator-controller/features/features.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
SyntheticPermissions featuregate.Feature = "SyntheticPermissions"
1717
WebhookProviderCertManager featuregate.Feature = "WebhookProviderCertManager"
1818
WebhookProviderOpenshiftServiceCA featuregate.Feature = "WebhookProviderOpenshiftServiceCA"
19+
HelmChartSupport featuregate.Feature = "HelmChartSupport"
1920
)
2021

2122
var operatorControllerFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{
@@ -63,6 +64,14 @@ var operatorControllerFeatureGates = map[featuregate.Feature]featuregate.Feature
6364
PreRelease: featuregate.Alpha,
6465
LockToDefault: false,
6566
},
67+
68+
// HelmChartSupport enables support for installing,
69+
// updating and uninstalling Helm Charts via Cluster Extensions.
70+
HelmChartSupport: {
71+
Default: false,
72+
PreRelease: featuregate.Alpha,
73+
LockToDefault: false,
74+
},
6675
}
6776

6877
var OperatorControllerFeatureGate featuregate.MutableFeatureGate = featuregate.NewFeatureGate()

internal/shared/util/image/cache.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@ import (
1616
"github.com/containers/image/v5/docker/reference"
1717
"github.com/opencontainers/go-digest"
1818
ocispecv1 "github.com/opencontainers/image-spec/specs-go/v1"
19+
"helm.sh/helm/v3/pkg/chart"
20+
"helm.sh/helm/v3/pkg/registry"
1921
"sigs.k8s.io/controller-runtime/pkg/log"
2022

2123
errorutil "github.com/operator-framework/operator-controller/internal/shared/util/error"
2224
fsutil "github.com/operator-framework/operator-controller/internal/shared/util/fs"
2325
)
2426

2527
type LayerData struct {
26-
Reader io.Reader
27-
Index int
28-
Err error
28+
MediaType string
29+
Reader io.Reader
30+
Index int
31+
Err error
2932
}
3033

3134
type Cache interface {
@@ -128,8 +131,15 @@ func (a *diskCache) Store(ctx context.Context, ownerID string, srcRef reference.
128131
if layer.Err != nil {
129132
return fmt.Errorf("error reading layer[%d]: %w", layer.Index, layer.Err)
130133
}
131-
if _, err := archive.Apply(ctx, dest, layer.Reader, applyOpts...); err != nil {
132-
return fmt.Errorf("error applying layer[%d]: %w", layer.Index, err)
134+
switch layer.MediaType {
135+
case registry.ChartLayerMediaType:
136+
if err := storeChartLayer(dest, layer); err != nil {
137+
return err
138+
}
139+
default:
140+
if _, err := archive.Apply(ctx, dest, layer.Reader, applyOpts...); err != nil {
141+
return fmt.Errorf("error applying layer[%d]: %w", layer.Index, err)
142+
}
133143
}
134144
l.Info("applied layer", "layer", layer.Index)
135145
}
@@ -147,6 +157,29 @@ func (a *diskCache) Store(ctx context.Context, ownerID string, srcRef reference.
147157
return os.DirFS(dest), modTime, nil
148158
}
149159

160+
func storeChartLayer(path string, layer LayerData) error {
161+
data, err := io.ReadAll(layer.Reader)
162+
if err != nil {
163+
return fmt.Errorf("error reading layer[%d]: %w", layer.Index, layer.Err)
164+
}
165+
meta := new(chart.Metadata)
166+
_, err = inspectChart(data, meta)
167+
if err != nil {
168+
return fmt.Errorf("inspecting chart layer: %w", err)
169+
}
170+
filename := filepath.Join(path,
171+
fmt.Sprintf("%s-%s.tgz", meta.Name, meta.Version),
172+
)
173+
chart, err := os.Create(filename)
174+
if err != nil {
175+
return fmt.Errorf("inspecting chart layer: %w", err)
176+
}
177+
defer chart.Close()
178+
179+
_, err = chart.Write(data)
180+
return err
181+
}
182+
150183
func (a *diskCache) Delete(_ context.Context, ownerID string) error {
151184
return fsutil.DeleteReadOnlyRecursive(a.ownerIDPath(ownerID))
152185
}

internal/shared/util/image/cache_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package image
22

33
import (
44
"archive/tar"
5+
"bytes"
56
"context"
67
"errors"
78
"io"
@@ -20,6 +21,7 @@ import (
2021
ocispecv1 "github.com/opencontainers/image-spec/specs-go/v1"
2122
"github.com/stretchr/testify/assert"
2223
"github.com/stretchr/testify/require"
24+
"helm.sh/helm/v3/pkg/registry"
2325

2426
fsutil "github.com/operator-framework/operator-controller/internal/shared/util/fs"
2527
)
@@ -211,6 +213,22 @@ func TestDiskCacheStore(t *testing.T) {
211213
assert.ErrorContains(t, err, "error applying layer")
212214
},
213215
},
216+
{
217+
name: "returns no error if layer read contains helm chart",
218+
ownerID: myOwner,
219+
srcRef: myTaggedRef,
220+
canonicalRef: myCanonicalRef,
221+
layers: func() iter.Seq[LayerData] {
222+
sampleChart := filepath.Join("../../../../", "testdata", "charts", "sample-chart-0.1.0.tgz")
223+
data, _ := os.ReadFile(sampleChart)
224+
return func(yield func(LayerData) bool) {
225+
yield(LayerData{Reader: bytes.NewBuffer(data), MediaType: registry.ChartLayerMediaType})
226+
}
227+
}(),
228+
expect: func(t *testing.T, cache *diskCache, fsys fs.FS, modTime time.Time, err error) {
229+
require.NoError(t, err)
230+
},
231+
},
214232
{
215233
name: "no error and an empty FS returned when there are no layers",
216234
ownerID: myOwner,

internal/shared/util/image/helm.go

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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

Comments
 (0)