Skip to content

Commit 9963df0

Browse files
(feat): Improved Error Messages for watchNamespace Configuration
1 parent 6ef62de commit 9963df0

2 files changed

Lines changed: 7 additions & 75 deletions

File tree

internal/operator-controller/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,11 @@ func formatSingleError(errUnit jsonschema.OutputUnit) string {
267267
// Unknown/additional field
268268
fieldName := extractFieldNameFromMessage(errUnit.Error)
269269
if fieldName != "" {
270+
// Special case: if watchNamespace is rejected as unknown, it means only
271+
// AllNamespaces install mode is supported
272+
if fieldName == "watchNamespace" {
273+
return fmt.Sprintf("field %q not allowed: only AllNamespaces install mode is supported", fieldName)
274+
}
270275
return fmt.Sprintf("unknown field %q", fieldName)
271276
}
272277
return "unknown field"

internal/operator-controller/config/config_test.go

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,10 @@ func Test_UnmarshalConfig(t *testing.T) {
8080
expectedErrMessage: `got object, want string`,
8181
},
8282
{
83-
name: "rejects with unknown field when install modes {AllNamespaces}",
83+
name: "rejects with descriptive message when install modes {AllNamespaces}",
8484
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces},
8585
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
86-
expectedErrMessage: `unknown field "watchNamespace"`,
87-
},
88-
{
89-
name: "rejects with unknown field when install modes {MultiNamespace}",
90-
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeMultiNamespace},
91-
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
92-
expectedErrMessage: `unknown field "watchNamespace"`,
93-
},
94-
{
95-
name: "reject with unknown field when install modes {AllNamespaces, MultiNamespace}",
96-
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces, v1alpha1.InstallModeTypeMultiNamespace},
97-
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
98-
expectedErrMessage: `unknown field "watchNamespace"`,
86+
expectedErrMessage: `field "watchNamespace" not allowed: only AllNamespaces install mode is supported`,
9987
},
10088
{
10189
name: "reject with required field when install modes {OwnNamespace} and watchNamespace is null",
@@ -109,18 +97,6 @@ func Test_UnmarshalConfig(t *testing.T) {
10997
rawConfig: []byte(`{}`),
11098
expectedErrMessage: `required field "watchNamespace" is missing`,
11199
},
112-
{
113-
name: "reject with required field when install modes {MultiNamespace, OwnNamespace} and watchNamespace is null",
114-
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeMultiNamespace, v1alpha1.InstallModeTypeOwnNamespace},
115-
rawConfig: []byte(`{"watchNamespace": null}`),
116-
expectedErrMessage: `required field "watchNamespace" is missing`,
117-
},
118-
{
119-
name: "reject with required field when install modes {MultiNamespace, OwnNamespace} and watchNamespace is missing",
120-
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeMultiNamespace, v1alpha1.InstallModeTypeOwnNamespace},
121-
rawConfig: []byte(`{}`),
122-
expectedErrMessage: `required field "watchNamespace" is missing`,
123-
},
124100
{
125101
name: "accepts when install modes {SingleNamespace} and watchNamespace != install namespace",
126102
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace},
@@ -135,13 +111,6 @@ func Test_UnmarshalConfig(t *testing.T) {
135111
installNamespace: "install-ns",
136112
expectedWatchNamespace: ptr.To("some-namespace"),
137113
},
138-
{
139-
name: "accepts when install modes {MultiNamespace, SingleNamespace} and watchNamespace != install namespace",
140-
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeMultiNamespace, v1alpha1.InstallModeTypeSingleNamespace},
141-
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
142-
installNamespace: "install-ns",
143-
expectedWatchNamespace: ptr.To("some-namespace"),
144-
},
145114
{
146115
name: "accepts when install modes {OwnNamespace, SingleNamespace} and watchNamespace != install namespace",
147116
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeOwnNamespace, v1alpha1.InstallModeTypeSingleNamespace},
@@ -163,13 +132,6 @@ func Test_UnmarshalConfig(t *testing.T) {
163132
installNamespace: "some-namespace",
164133
expectedErrMessage: "invalid configuration:",
165134
},
166-
{
167-
name: "rejects when install modes {MultiNamespace, SingleNamespace} and watchNamespace == install namespace",
168-
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeMultiNamespace, v1alpha1.InstallModeTypeSingleNamespace},
169-
rawConfig: []byte(`{"watchNamespace": "some-namespace"}`),
170-
installNamespace: "some-namespace",
171-
expectedErrMessage: "invalid configuration:",
172-
},
173135
{
174136
name: "accepts when install modes {AllNamespaces, OwnNamespace} and watchNamespace == install namespace",
175137
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces, v1alpha1.InstallModeTypeOwnNamespace},
@@ -205,13 +167,6 @@ func Test_UnmarshalConfig(t *testing.T) {
205167
installNamespace: "not-some-namespace",
206168
expectedErrMessage: `required field "watchNamespace" is missing`,
207169
},
208-
{
209-
name: "rejects with required field error when install modes {SingleNamespace, MultiNamespace} and watchNamespace is nil",
210-
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace, v1alpha1.InstallModeTypeMultiNamespace},
211-
rawConfig: []byte(`{"watchNamespace": null}`),
212-
installNamespace: "not-some-namespace",
213-
expectedErrMessage: `required field "watchNamespace" is missing`,
214-
},
215170
{
216171
name: "rejects with required field error when install modes {SingleNamespace} and watchNamespace is missing",
217172
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace},
@@ -226,48 +181,20 @@ func Test_UnmarshalConfig(t *testing.T) {
226181
installNamespace: "not-some-namespace",
227182
expectedErrMessage: `required field "watchNamespace" is missing`,
228183
},
229-
{
230-
name: "rejects with required field error when install modes {SingleNamespace, MultiNamespace} and watchNamespace is missing",
231-
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace, v1alpha1.InstallModeTypeMultiNamespace},
232-
rawConfig: []byte(`{}`),
233-
installNamespace: "not-some-namespace",
234-
expectedErrMessage: `required field "watchNamespace" is missing`,
235-
},
236-
{
237-
name: "rejects with required field error when install modes {SingleNamespace, OwnNamespace, MultiNamespace} and watchNamespace is nil",
238-
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeSingleNamespace, v1alpha1.InstallModeTypeOwnNamespace, v1alpha1.InstallModeTypeMultiNamespace},
239-
rawConfig: []byte(`{"watchNamespace": null}`),
240-
installNamespace: "not-some-namespace",
241-
expectedErrMessage: `required field "watchNamespace" is missing`,
242-
},
243184
{
244185
name: "accepts null watchNamespace when install modes {AllNamespaces, OwnNamespace} and watchNamespace is nil",
245186
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces, v1alpha1.InstallModeTypeOwnNamespace},
246187
rawConfig: []byte(`{"watchNamespace": null}`),
247188
installNamespace: "not-some-namespace",
248189
expectedWatchNamespace: nil,
249190
},
250-
{
251-
name: "accepts null watchNamespace when install modes {AllNamespaces, OwnNamespace, MultiNamespace} and watchNamespace is nil",
252-
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces, v1alpha1.InstallModeTypeOwnNamespace, v1alpha1.InstallModeTypeMultiNamespace},
253-
rawConfig: []byte(`{"watchNamespace": null}`),
254-
installNamespace: "not-some-namespace",
255-
expectedWatchNamespace: nil,
256-
},
257191
{
258192
name: "accepts no watchNamespace when install modes {AllNamespaces, OwnNamespace} and watchNamespace is nil",
259193
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces, v1alpha1.InstallModeTypeOwnNamespace},
260194
rawConfig: []byte(`{}`),
261195
installNamespace: "not-some-namespace",
262196
expectedWatchNamespace: nil,
263197
},
264-
{
265-
name: "accepts no watchNamespace when install modes {AllNamespaces, OwnNamespace, MultiNamespace} and watchNamespace is nil",
266-
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeAllNamespaces, v1alpha1.InstallModeTypeOwnNamespace, v1alpha1.InstallModeTypeMultiNamespace},
267-
rawConfig: []byte(`{}`),
268-
installNamespace: "not-some-namespace",
269-
expectedWatchNamespace: nil,
270-
},
271198
{
272199
name: "skips validation when installNamespace is empty for OwnNamespace only",
273200
supportedInstallModes: []v1alpha1.InstallModeType{v1alpha1.InstallModeTypeOwnNamespace},

0 commit comments

Comments
 (0)