Skip to content

Commit 797bddb

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 <ochienged@gmail.com>
1 parent 8f81c23 commit 797bddb

File tree

10 files changed

+600
-6
lines changed

10 files changed

+600
-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: 40 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,31 @@ 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+
if _, err := chart.Write(data); err != nil {
180+
return fmt.Errorf("copying chart to the destination %s; %w", path, err)
181+
}
182+
return nil
183+
}
184+
150185
func (a *diskCache) Delete(_ context.Context, ownerID string) error {
151186
return fsutil.DeleteReadOnlyRecursive(a.ownerIDPath(ownerID))
152187
}

internal/shared/util/image/cache_test.go

Lines changed: 49 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
)
@@ -585,6 +587,53 @@ func TestDiskCacheGarbageCollection(t *testing.T) {
585587
}
586588
}
587589

590+
func Test_storeChartLayer(t *testing.T) {
591+
tmpDir := t.TempDir()
592+
testdataCharts := filepath.Join("../../../../", "testdata", "charts")
593+
type args struct {
594+
path string
595+
layer LayerData
596+
}
597+
type want struct {
598+
err error
599+
filename string
600+
}
601+
tests := []struct {
602+
name string
603+
args args
604+
want want
605+
}{
606+
{
607+
name: "store helm chart layer to the filesystem",
608+
args: args{
609+
path: tmpDir,
610+
layer: func() LayerData {
611+
chart, _ := os.ReadFile(filepath.Join(testdataCharts, "sample-chart-0.1.0.tgz"))
612+
return LayerData{
613+
Index: 0,
614+
MediaType: registry.ChartLayerMediaType,
615+
Reader: bytes.NewBuffer(chart),
616+
Err: nil,
617+
}
618+
}(),
619+
},
620+
want: want{
621+
err: nil,
622+
filename: filepath.Join(tmpDir, "sample-chart-0.1.0.tgz"),
623+
},
624+
},
625+
}
626+
627+
for _, tc := range tests {
628+
t.Run(tc.name, func(t *testing.T) {
629+
err := storeChartLayer(tc.args.path, tc.args.layer)
630+
require.NoError(t, err, "write chart layer to the filesystem")
631+
_, err = os.Stat(tc.want.filename)
632+
require.NoError(t, err, "chart should exist in the filesystem")
633+
})
634+
}
635+
}
636+
588637
func mustParseCanonical(t *testing.T, s string) reference.Canonical {
589638
n, err := reference.ParseNamed(s)
590639
require.NoError(t, err)

0 commit comments

Comments
 (0)