Skip to content

Commit 6093559

Browse files
author
Per Goncalves da Silva
committed
Refactor converter to use validated bundle config
Signed-off-by: Per Goncalves da Silva <[email protected]>
1 parent a715af3 commit 6093559

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

internal/operator-controller/rukpak/convert/helm.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
"helm.sh/helm/v3/pkg/chart"
99

10-
bundlepkg "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle"
10+
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle/bundlecfg"
1111
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle/source"
1212
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/render"
1313
)
@@ -24,15 +24,6 @@ func (r *BundleToHelmChartConverter) ToHelmChart(bundle source.BundleSource, ins
2424
return nil, err
2525
}
2626

27-
opts := []render.Option{
28-
render.WithCertificateProvider(r.CertificateProvider),
29-
}
30-
if config != nil {
31-
if watchNs, ok := config[bundlepkg.BundleConfigWatchNamespaceKey].(string); ok {
32-
opts = append(opts, render.WithTargetNamespaces(watchNs))
33-
}
34-
}
35-
3627
if len(rv1.CSV.Spec.APIServiceDefinitions.Owned) > 0 {
3728
return nil, fmt.Errorf("unsupported bundle: apiServiceDefintions are not supported")
3829
}
@@ -49,6 +40,18 @@ func (r *BundleToHelmChartConverter) ToHelmChart(bundle source.BundleSource, ins
4940
return nil, fmt.Errorf("unsupported bundle: webhookDefinitions are not supported")
5041
}
5142

43+
opts := []render.Option{
44+
render.WithCertificateProvider(r.CertificateProvider),
45+
}
46+
47+
bundleConfig, err := bundlecfg.Unmarshall(rv1, installNamespace, config)
48+
if err != nil {
49+
return nil, err
50+
}
51+
if bundleConfig != nil && bundleConfig.WatchNamespace != "" {
52+
opts = append(opts, render.WithTargetNamespaces(bundleConfig.WatchNamespace))
53+
}
54+
5255
objs, err := r.BundleRenderer.Render(rv1, installNamespace, opts...)
5356

5457
if err != nil {

internal/operator-controller/rukpak/convert/helm_test.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ func Test_BundleToHelmChartConverter_ToHelmChart_ReturnsBundleSourceFailures(t *
3232
require.Contains(t, err.Error(), "some error")
3333
}
3434

35+
func Test_BundleToHelmChartConverter_ToHelmChart_ReturnsBundleConfigFailures(t *testing.T) {
36+
converter := convert.BundleToHelmChartConverter{}
37+
b := source.FromBundle(
38+
bundle.RegistryV1{
39+
CSV: MakeCSV(WithInstallModeSupportFor(v1alpha1.InstallModeTypeAllNamespaces)),
40+
},
41+
)
42+
config := map[string]interface{}{
43+
bundle.BundleConfigWatchNamespaceKey: "some-namespace",
44+
}
45+
_, err := converter.ToHelmChart(b, "install-namespace", config)
46+
require.Error(t, err)
47+
require.Contains(t, err.Error(), "'watchNamespace' not allowed")
48+
}
49+
3550
func Test_BundleToHelmChartConverter_ToHelmChart_ReturnsBundleRendererFailures(t *testing.T) {
3651
converter := convert.BundleToHelmChartConverter{
3752
BundleRenderer: render.BundleRenderer{
@@ -45,12 +60,12 @@ func Test_BundleToHelmChartConverter_ToHelmChart_ReturnsBundleRendererFailures(t
4560

4661
b := source.FromBundle(
4762
bundle.RegistryV1{
48-
CSV: MakeCSV(WithInstallModeSupportFor(v1alpha1.InstallModeTypeAllNamespaces)),
63+
CSV: MakeCSV(WithInstallModeSupportFor(v1alpha1.InstallModeTypeSingleNamespace)),
4964
},
5065
)
5166

5267
config := map[string]interface{}{
53-
bundle.BundleConfigWatchNamespaceKey: "",
68+
bundle.BundleConfigWatchNamespaceKey: "some-namespace",
5469
}
5570
_, err := converter.ToHelmChart(b, "install-namespace", config)
5671
require.Error(t, err)
@@ -121,22 +136,22 @@ func Test_BundleToHelmChartConverter_ToHelmChart_WebhooksWithCertProvider(t *tes
121136
b := source.FromBundle(
122137
bundle.RegistryV1{
123138
CSV: MakeCSV(
124-
WithInstallModeSupportFor(v1alpha1.InstallModeTypeAllNamespaces),
139+
WithInstallModeSupportFor(v1alpha1.InstallModeTypeSingleNamespace),
125140
WithWebhookDefinitions(v1alpha1.WebhookDescription{}),
126141
),
127142
},
128143
)
129144

130145
config := map[string]interface{}{
131-
bundle.BundleConfigWatchNamespaceKey: "",
146+
bundle.BundleConfigWatchNamespaceKey: "some-namespace",
132147
}
133148
_, err := converter.ToHelmChart(b, "install-namespace", config)
134149
require.NoError(t, err)
135150
}
136151

137152
func Test_BundleToHelmChartConverter_ToHelmChart_BundleRendererIntegration(t *testing.T) {
138153
expectedInstallNamespace := "install-namespace"
139-
expectedWatchNamespace := ""
154+
expectedWatchNamespace := "some-namespaces"
140155
expectedCertProvider := FakeCertProvider{}
141156

142157
converter := convert.BundleToHelmChartConverter{
@@ -156,7 +171,7 @@ func Test_BundleToHelmChartConverter_ToHelmChart_BundleRendererIntegration(t *te
156171

157172
b := source.FromBundle(
158173
bundle.RegistryV1{
159-
CSV: MakeCSV(WithInstallModeSupportFor(v1alpha1.InstallModeTypeAllNamespaces)),
174+
CSV: MakeCSV(WithInstallModeSupportFor(v1alpha1.InstallModeTypeSingleNamespace)),
160175
},
161176
)
162177

@@ -186,7 +201,7 @@ func Test_BundleToHelmChartConverter_ToHelmChart_Success(t *testing.T) {
186201
bundle.RegistryV1{
187202
CSV: MakeCSV(
188203
WithAnnotations(map[string]string{"foo": "bar"}),
189-
WithInstallModeSupportFor(v1alpha1.InstallModeTypeAllNamespaces),
204+
WithInstallModeSupportFor(v1alpha1.InstallModeTypeAllNamespaces, v1alpha1.InstallModeTypeSingleNamespace),
190205
),
191206
Others: []unstructured.Unstructured{
192207
*ToUnstructuredT(t, &corev1.Service{
@@ -203,7 +218,7 @@ func Test_BundleToHelmChartConverter_ToHelmChart_Success(t *testing.T) {
203218
)
204219

205220
config := map[string]interface{}{
206-
bundle.BundleConfigWatchNamespaceKey: "",
221+
bundle.BundleConfigWatchNamespaceKey: "some-namespace",
207222
}
208223
chart, err := converter.ToHelmChart(b, "install-namespace", config)
209224
require.NoError(t, err)

0 commit comments

Comments
 (0)