Skip to content

Commit 828fd6a

Browse files
author
Per Goncalves da Silva
committed
Update applier to take watchNamespace configuration from the extension
Signed-off-by: Per Goncalves da Silva <[email protected]>
1 parent 211786d commit 828fd6a

File tree

3 files changed

+65
-40
lines changed

3 files changed

+65
-40
lines changed

internal/operator-controller/applier/helm_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package applier_test
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"io"
78
"os"
89
"testing"
@@ -16,6 +17,7 @@ import (
1617
"helm.sh/helm/v3/pkg/storage/driver"
1718
corev1 "k8s.io/api/core/v1"
1819
rbacv1 "k8s.io/api/rbac/v1"
20+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1921
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2022
featuregatetesting "k8s.io/component-base/featuregate/testing"
2123
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -567,8 +569,13 @@ func TestApply_InstallationWithSingleOwnNamespaceInstallSupportEnabled(t *testin
567569
testExt := &ocv1.ClusterExtension{
568570
ObjectMeta: metav1.ObjectMeta{
569571
Name: "testExt",
570-
Annotations: map[string]string{
571-
applier.AnnotationClusterExtensionWatchNamespace: expectedWatchNamespace,
572+
},
573+
Spec: ocv1.ClusterExtensionSpec{
574+
Config: &ocv1.ClusterExtensionConfig{
575+
ConfigType: ocv1.ClusterExtensionConfigTypeInline,
576+
Inline: &apiextensionsv1.JSON{
577+
Raw: []byte(fmt.Sprintf(`{"watchNamespace":"%s"}`, expectedWatchNamespace)),
578+
},
572579
},
573580
},
574581
}
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
package applier
22

33
import (
4+
"encoding/json"
45
"fmt"
56

6-
corev1 "k8s.io/api/core/v1"
77
"k8s.io/apimachinery/pkg/util/validation"
88

99
ocv1 "github.com/operator-framework/operator-controller/api/v1"
1010
"github.com/operator-framework/operator-controller/internal/operator-controller/features"
1111
)
1212

13-
const (
14-
AnnotationClusterExtensionWatchNamespace = "olm.operatorframework.io/watch-namespace"
15-
)
16-
1713
// GetWatchNamespace determines the watch namespace the ClusterExtension should use
1814
// Note: this is a temporary artifice to enable gated use of single/own namespace install modes
1915
// for registry+v1 bundles. This will go away once the ClusterExtension API is updated to include
2016
// (opaque) runtime configuration.
2117
func GetWatchNamespace(ext *ocv1.ClusterExtension) (string, error) {
22-
if features.OperatorControllerFeatureGate.Enabled(features.SingleOwnNamespaceInstallSupport) {
23-
if ext != nil && ext.Annotations[AnnotationClusterExtensionWatchNamespace] != "" {
24-
watchNamespace := ext.Annotations[AnnotationClusterExtensionWatchNamespace]
25-
if errs := validation.IsDNS1123Subdomain(watchNamespace); len(errs) > 0 {
26-
return "", fmt.Errorf("invalid watch namespace '%s': namespace must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character", watchNamespace)
27-
}
28-
return ext.Annotations[AnnotationClusterExtensionWatchNamespace], nil
18+
cfg := struct {
19+
WatchNamespace string `json:"watchNamespace"`
20+
}{}
21+
22+
if features.OperatorControllerFeatureGate.Enabled(features.SingleOwnNamespaceInstallSupport) && ext.Spec.Config != nil && ext.Spec.Config.Inline != nil {
23+
if err := json.Unmarshal(ext.Spec.Config.Inline.Raw, &cfg); err != nil {
24+
return "", fmt.Errorf("invalid bundle configuration: %w", err)
2925
}
26+
if errs := validation.IsDNS1123Subdomain(cfg.WatchNamespace); len(errs) > 0 {
27+
return "", fmt.Errorf("invalid watch namespace '%s': namespace must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character", cfg.WatchNamespace)
28+
}
29+
return cfg.WatchNamespace, nil
3030
}
31-
return corev1.NamespaceAll, nil
31+
32+
return "", nil
3233
}

internal/operator-controller/applier/watchnamespace_test.go

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,31 @@ import (
55

66
"github.com/stretchr/testify/require"
77
corev1 "k8s.io/api/core/v1"
8+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
910
featuregatetesting "k8s.io/component-base/featuregate/testing"
1011

11-
v1 "github.com/operator-framework/operator-controller/api/v1"
12+
ocv1 "github.com/operator-framework/operator-controller/api/v1"
1213
"github.com/operator-framework/operator-controller/internal/operator-controller/applier"
1314
"github.com/operator-framework/operator-controller/internal/operator-controller/features"
1415
)
1516

1617
func TestGetWatchNamespacesWhenFeatureGateIsDisabled(t *testing.T) {
17-
watchNamespace, err := applier.GetWatchNamespace(&v1.ClusterExtension{
18+
watchNamespace, err := applier.GetWatchNamespace(&ocv1.ClusterExtension{
1819
ObjectMeta: metav1.ObjectMeta{
1920
Name: "extension",
20-
Annotations: map[string]string{
21-
"olm.operatorframework.io/watch-namespace": "watch-namespace",
21+
},
22+
Spec: ocv1.ClusterExtensionSpec{
23+
Config: &ocv1.ClusterExtensionConfig{
24+
ConfigType: ocv1.ClusterExtensionConfigTypeInline,
25+
Inline: &apiextensionsv1.JSON{
26+
Raw: []byte(`{"watchNamespace":"watch-namespace"}`),
27+
},
2228
},
2329
},
24-
Spec: v1.ClusterExtensionSpec{},
2530
})
2631
require.NoError(t, err)
27-
t.Log("Check watchNamespace is '' even if the annotation is set")
32+
t.Log("Check watchNamespace is '' even if the configuration is set")
2833
require.Equal(t, corev1.NamespaceAll, watchNamespace)
2934
}
3035

@@ -34,57 +39,69 @@ func TestGetWatchNamespace(t *testing.T) {
3439
for _, tt := range []struct {
3540
name string
3641
want string
37-
csv *v1.ClusterExtension
42+
csv *ocv1.ClusterExtension
3843
expectError bool
3944
}{
4045
{
41-
name: "cluster extension does not have watch namespace annotation",
46+
name: "cluster extension does not configure a watch namespace",
4247
want: corev1.NamespaceAll,
43-
csv: &v1.ClusterExtension{
48+
csv: &ocv1.ClusterExtension{
4449
ObjectMeta: metav1.ObjectMeta{
4550
Name: "extension",
4651
Annotations: nil,
4752
},
48-
Spec: v1.ClusterExtensionSpec{},
53+
Spec: ocv1.ClusterExtensionSpec{},
4954
},
5055
expectError: false,
5156
}, {
52-
name: "cluster extension has valid namespace annotation",
57+
name: "cluster extension configures a watch namespace",
5358
want: "watch-namespace",
54-
csv: &v1.ClusterExtension{
59+
csv: &ocv1.ClusterExtension{
5560
ObjectMeta: metav1.ObjectMeta{
5661
Name: "extension",
57-
Annotations: map[string]string{
58-
"olm.operatorframework.io/watch-namespace": "watch-namespace",
62+
},
63+
Spec: ocv1.ClusterExtensionSpec{
64+
Config: &ocv1.ClusterExtensionConfig{
65+
ConfigType: ocv1.ClusterExtensionConfigTypeInline,
66+
Inline: &apiextensionsv1.JSON{
67+
Raw: []byte(`{"watchNamespace":"watch-namespace"}`),
68+
},
5969
},
6070
},
61-
Spec: v1.ClusterExtensionSpec{},
6271
},
6372
expectError: false,
6473
}, {
65-
name: "cluster extension has invalid namespace annotation: multiple watch namespaces",
74+
name: "cluster extension configures an invalid watchNamespace: multiple watch namespaces",
6675
want: "",
67-
csv: &v1.ClusterExtension{
76+
csv: &ocv1.ClusterExtension{
6877
ObjectMeta: metav1.ObjectMeta{
6978
Name: "extension",
70-
Annotations: map[string]string{
71-
"olm.operatorframework.io/watch-namespace": "watch-namespace,watch-namespace2,watch-namespace3",
79+
},
80+
Spec: ocv1.ClusterExtensionSpec{
81+
Config: &ocv1.ClusterExtensionConfig{
82+
ConfigType: ocv1.ClusterExtensionConfigTypeInline,
83+
Inline: &apiextensionsv1.JSON{
84+
Raw: []byte(`{"watchNamespace":"watch-namespace,watch-namespace2,watch-namespace3"}`),
85+
},
7286
},
7387
},
74-
Spec: v1.ClusterExtensionSpec{},
7588
},
7689
expectError: true,
7790
}, {
78-
name: "cluster extension has invalid namespace annotation: invalid name",
91+
name: "cluster extension configures an invalid watchNamespace: invalid name",
7992
want: "",
80-
csv: &v1.ClusterExtension{
93+
csv: &ocv1.ClusterExtension{
8194
ObjectMeta: metav1.ObjectMeta{
8295
Name: "extension",
83-
Annotations: map[string]string{
84-
"olm.operatorframework.io/watch-namespace": "watch-namespace-",
96+
},
97+
Spec: ocv1.ClusterExtensionSpec{
98+
Config: &ocv1.ClusterExtensionConfig{
99+
ConfigType: ocv1.ClusterExtensionConfigTypeInline,
100+
Inline: &apiextensionsv1.JSON{
101+
Raw: []byte(`{"watchNamespace":"watch-namespace-"}`),
102+
},
85103
},
86104
},
87-
Spec: v1.ClusterExtensionSpec{},
88105
},
89106
expectError: true,
90107
},

0 commit comments

Comments
 (0)