Skip to content

Commit b870100

Browse files
author
Per Goncalves da Silva
committed
Improve .spec.config unmarshallig
Signed-off-by: Per Goncalves da Silva <[email protected]>
1 parent 9eac616 commit b870100

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

internal/operator-controller/applier/provider.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package applier
33
import (
44
"crypto/sha256"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"io/fs"
89

910
"helm.sh/helm/v3/pkg/chart"
1011
"k8s.io/apimachinery/pkg/util/sets"
1112
"k8s.io/apimachinery/pkg/util/validation"
1213
"sigs.k8s.io/controller-runtime/pkg/client"
14+
"sigs.k8s.io/yaml"
1315

1416
"github.com/operator-framework/api/pkg/operators/v1alpha1"
1517

@@ -70,7 +72,7 @@ func (r *RegistryV1ManifestProvider) Get(bundleFS fs.FS, ext *ocv1.ClusterExtens
7072

7173
watchNamespace, err := r.getWatchNamespace(ext)
7274
if err != nil {
73-
return nil, err
75+
return nil, fmt.Errorf("invalid bundle configuration: %w", err)
7476
}
7577

7678
if watchNamespace != "" {
@@ -92,8 +94,9 @@ func (r *RegistryV1ManifestProvider) getWatchNamespace(ext *ocv1.ClusterExtensio
9294
cfg := struct {
9395
WatchNamespace string `json:"watchNamespace"`
9496
}{}
95-
if err := json.Unmarshal(ext.Spec.Config.Inline.Raw, &cfg); err != nil {
96-
return "", fmt.Errorf("invalid bundle configuration: %w", err)
97+
// Using k8s.io/yaml package as that is able to handle both json and yaml
98+
if err := yaml.Unmarshal(ext.Spec.Config.Inline.Raw, &cfg); err != nil {
99+
return "", errors.New("inline config is not a valid JSON/YAML object")
97100
}
98101
watchNamespace = cfg.WatchNamespace
99102
} else {

internal/operator-controller/applier/provider_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,66 @@ func Test_RegistryV1ManifestProvider_WebhookSupport(t *testing.T) {
188188
})
189189
}
190190

191+
func Test_RegistryV1ManifestProvider_ConfigUnmarshalling(t *testing.T) {
192+
for _, tc := range []struct {
193+
name string
194+
configBytes []byte
195+
expectedErrMessage string
196+
}{
197+
{
198+
name: "accepts json config",
199+
configBytes: []byte(`{"watchNamespace": "some-namespace"}`),
200+
},
201+
{
202+
name: "accepts yaml config",
203+
configBytes: []byte(`watchNamespace: some-namespace`),
204+
},
205+
{
206+
name: "rejects invalid json",
207+
configBytes: []byte(`{"hello`),
208+
expectedErrMessage: `inline config is not a valid JSON/YAML object`,
209+
},
210+
{
211+
name: "rejects valid json that isn't of object type",
212+
configBytes: []byte(`true`),
213+
expectedErrMessage: `inline config is not a valid JSON/YAML object`,
214+
},
215+
} {
216+
t.Run(tc.name, func(t *testing.T) {
217+
provider := applier.RegistryV1ManifestProvider{
218+
BundleRenderer: render.BundleRenderer{
219+
ResourceGenerators: []render.ResourceGenerator{
220+
func(rv1 *bundle.RegistryV1, opts render.Options) ([]client.Object, error) {
221+
return nil, nil
222+
},
223+
},
224+
},
225+
IsSingleOwnNamespaceEnabled: true,
226+
}
227+
228+
bundleFS := bundlefs.Builder().WithPackageName("test").
229+
WithCSV(clusterserviceversion.Builder().WithInstallModeSupportFor(v1alpha1.InstallModeTypeSingleNamespace).Build()).Build()
230+
231+
_, err := provider.Get(bundleFS, &ocv1.ClusterExtension{
232+
Spec: ocv1.ClusterExtensionSpec{
233+
Namespace: "install-namespace",
234+
Config: &ocv1.ClusterExtensionConfig{
235+
ConfigType: ocv1.ClusterExtensionConfigTypeInline,
236+
Inline: &apiextensionsv1.JSON{
237+
Raw: tc.configBytes,
238+
},
239+
},
240+
},
241+
})
242+
if tc.expectedErrMessage != "" {
243+
require.Contains(t, err.Error(), tc.expectedErrMessage)
244+
} else {
245+
require.NoError(t, err)
246+
}
247+
})
248+
}
249+
}
250+
191251
func Test_RegistryV1ManifestProvider_SingleOwnNamespaceSupport(t *testing.T) {
192252
t.Run("rejects bundles without AllNamespaces install mode when Single/OwnNamespace install mode support is disabled", func(t *testing.T) {
193253
provider := applier.RegistryV1ManifestProvider{

0 commit comments

Comments
 (0)