-
Notifications
You must be signed in to change notification settings - Fork 630
Experimental features configuration and validation #5214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
knative-prow-robot
merged 7 commits into
knative:main
from
slinkydeveloper:experimental_flags
May 26, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d9d757e
Experimental features
slinkydeveloper 86dd450
godoc
slinkydeveloper 41d8599
ValidateAnnotations
slinkydeveloper e9facb5
Copyright
slinkydeveloper c946a5d
Now the `ValidateAPIField` can validate nested fields
slinkydeveloper fbf82c2
Added the directory and the action for the experimental features e2e/…
slinkydeveloper f093286
Removed custom build tag and using just e2e tag
slinkydeveloper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| core/configmaps/experimental-features.yaml | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Copyright 2021 The Knative Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| apiVersion: v1 | ||
| kind: ConfigMap | ||
| metadata: | ||
| name: config-experimental-features | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
| namespace: knative-eventing | ||
| labels: | ||
| eventing.knative.dev/release: devel | ||
| knative.dev/config-propagation: original | ||
| knative.dev/config-category: eventing | ||
| data: | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| Copyright 2021 The Knative Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package experimental | ||
|
slinkydeveloper marked this conversation as resolved.
|
||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "reflect" | ||
| "strings" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "knative.dev/pkg/apis" | ||
| ) | ||
|
|
||
| // ValidateAPIFields checks that the experimental features fields are disabled if the experimental flag is disabled. | ||
| // experimentalFields can contain a string with dots, to identify sub-structs, like "Destination.Ref.APIVersion" | ||
| func ValidateAPIFields(ctx context.Context, featureName string, object interface{}, experimentalFields ...string) (errs *apis.FieldError) { | ||
| obj := reflect.ValueOf(object) | ||
| obj = reflect.Indirect(obj) | ||
| if obj.Kind() != reflect.Struct { | ||
| return nil | ||
| } | ||
|
|
||
| // If feature not enabled, let's check the field is not used | ||
| if !FromContext(ctx).IsEnabled(featureName) { | ||
| for _, fieldName := range experimentalFields { | ||
| fieldVal := walk(obj, strings.Split(fieldName, ".")...) | ||
|
|
||
| if !fieldVal.IsZero() { | ||
| errs = errs.Also(&apis.FieldError{ | ||
| Message: fmt.Sprintf("Disallowed field because the experimental feature '%s' is disabled", featureName), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove experimental |
||
| Paths: []string{fmt.Sprintf("%s.%s", obj.Type().Name(), fieldName)}, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return errs | ||
| } | ||
|
|
||
| // ValidateAnnotations checks that the experimental features annotations are disabled if the experimental flag is disabled | ||
| func ValidateAnnotations(ctx context.Context, featureName string, object metav1.Object, experimentalAnnotations ...string) (errs *apis.FieldError) { | ||
| // If feature not enabled, let's check the annotation is not used | ||
| if !FromContext(ctx).IsEnabled(featureName) { | ||
| for _, annotation := range experimentalAnnotations { | ||
| if _, ok := object.GetAnnotations()[annotation]; ok { | ||
| errs = errs.Also(&apis.FieldError{ | ||
| Message: fmt.Sprintf("Disallowed annotation because the experimental feature '%s' is disabled", featureName), | ||
| Paths: []string{annotation}, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return errs | ||
| } | ||
|
|
||
| func walk(value reflect.Value, paths ...string) reflect.Value { | ||
| switch value.Kind() { | ||
| case reflect.Struct: | ||
| newVal := value.FieldByName(paths[0]) | ||
| if len(paths) == 1 { | ||
| return newVal | ||
| } | ||
| return walk(value.FieldByName(paths[0]), paths[1:]...) | ||
| case reflect.Ptr: | ||
| return walk(reflect.Indirect(value), paths...) | ||
| default: | ||
| return reflect.Zero(value.Type()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| /* | ||
| Copyright 2021 The Knative Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package experimental | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "knative.dev/pkg/apis" | ||
| duckv1 "knative.dev/pkg/apis/duck/v1" | ||
|
|
||
| eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1" | ||
| ) | ||
|
|
||
| const flagName = "my-flag" | ||
|
|
||
| func TestValidateAPIFields(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| flags Flags | ||
| featureName string | ||
| object interface{} | ||
| experimentalFields []string | ||
| wantErrs *apis.FieldError | ||
| }{ | ||
| { | ||
| name: "invalid input", | ||
| featureName: flagName, | ||
| flags: map[string]bool{ | ||
| flagName: true, | ||
| }, | ||
| object: []string{}, | ||
| experimentalFields: []string{"Filter"}, | ||
| }, | ||
| { | ||
| name: "enabled flag", | ||
| featureName: flagName, | ||
| flags: map[string]bool{ | ||
| flagName: true, | ||
| }, | ||
| object: eventingv1.TriggerSpec{ | ||
| Broker: "blabla", | ||
| Subscriber: duckv1.Destination{ | ||
| URI: apis.HTTP("example.com"), | ||
| }, | ||
| Filter: &eventingv1.TriggerFilter{}, | ||
| }, | ||
| experimentalFields: []string{"Filter"}, | ||
| }, | ||
| { | ||
| name: "disabled pointer flag", | ||
| featureName: flagName, | ||
| flags: map[string]bool{ | ||
| flagName: false, | ||
| }, | ||
| object: eventingv1.TriggerSpec{ | ||
| Broker: "blabla", | ||
| Subscriber: duckv1.Destination{ | ||
| URI: apis.HTTP("example.com"), | ||
| }, | ||
| Filter: &eventingv1.TriggerFilter{}, | ||
| }, | ||
| experimentalFields: []string{"Filter"}, | ||
| wantErrs: &apis.FieldError{ | ||
| Message: fmt.Sprintf("Disallowed field because the experimental feature '%s' is disabled", flagName), | ||
| Paths: []string{"TriggerSpec.Filter"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "disabled nested string flag", | ||
| featureName: flagName, | ||
| flags: map[string]bool{ | ||
| flagName: false, | ||
| }, | ||
| object: eventingv1.TriggerSpec{ | ||
| Broker: "blabla", | ||
| Subscriber: duckv1.Destination{ | ||
| Ref: &duckv1.KReference{ | ||
| Namespace: "abc", | ||
| }, | ||
| }, | ||
| }, | ||
| experimentalFields: []string{"Subscriber.Ref.Namespace"}, | ||
| wantErrs: &apis.FieldError{ | ||
| Message: fmt.Sprintf("Disallowed field because the experimental feature '%s' is disabled", flagName), | ||
| Paths: []string{"Subscriber.Ref.Namespace"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "enabled nested string flag", | ||
| featureName: flagName, | ||
| flags: map[string]bool{ | ||
| flagName: true, | ||
| }, | ||
| object: eventingv1.TriggerSpec{ | ||
| Broker: "blabla", | ||
| Subscriber: duckv1.Destination{ | ||
| Ref: &duckv1.KReference{ | ||
| Namespace: "abc", | ||
| }, | ||
| }, | ||
| }, | ||
| experimentalFields: []string{"Subscriber.Ref.Namespace"}, | ||
| }, | ||
| { | ||
| name: "disabled map flag", | ||
| featureName: flagName, | ||
| flags: map[string]bool{ | ||
| flagName: false, | ||
| }, | ||
| object: &eventingv1.TriggerFilter{ | ||
| Attributes: map[string]string{}, | ||
| }, | ||
| experimentalFields: []string{"Attributes"}, | ||
| wantErrs: &apis.FieldError{ | ||
| Message: fmt.Sprintf("Disallowed field because the experimental feature '%s' is disabled", flagName), | ||
| Paths: []string{"TriggerFilter.Attributes"}, | ||
| }, | ||
| }} | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ctx := ToContext(context.Background(), tt.flags) | ||
|
|
||
| res := ValidateAPIFields(ctx, tt.featureName, tt.object, tt.experimentalFields...) | ||
| if tt.wantErrs == nil { | ||
| require.Nil(t, res) | ||
| } else { | ||
| require.Error(t, res, tt.wantErrs.Error()) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateAnnotations(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| flags Flags | ||
| featureName string | ||
| object metav1.Object | ||
| experimentalFields []string | ||
| wantErrs *apis.FieldError | ||
| }{{ | ||
| name: "enabled flag", | ||
| featureName: flagName, | ||
| flags: map[string]bool{ | ||
| flagName: true, | ||
| }, | ||
| object: &eventingv1.Broker{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Annotations: map[string]string{ | ||
| "dev.knative/myfancyannotation": "blabla", | ||
| }, | ||
| }, | ||
| }, | ||
| experimentalFields: []string{"dev.knative/myfancyannotation"}, | ||
| }, | ||
| { | ||
| name: "disabled flag", | ||
| featureName: flagName, | ||
| flags: map[string]bool{ | ||
| flagName: false, | ||
| }, | ||
| object: &eventingv1.Broker{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Annotations: map[string]string{ | ||
| "dev.knative/myfancyannotation": "blabla", | ||
| }, | ||
| }, | ||
| }, | ||
| experimentalFields: []string{"dev.knative/myfancyannotation"}, | ||
| wantErrs: &apis.FieldError{ | ||
| Message: fmt.Sprintf("Disallowed field because the experimental feature '%s' is disabled", flagName), | ||
| Paths: []string{"dev.knative/myfancyannotation"}, | ||
| }, | ||
| }} | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ctx := ToContext(context.Background(), tt.flags) | ||
|
|
||
| res := ValidateAnnotations(ctx, tt.featureName, tt.object, tt.experimentalFields...) | ||
| if tt.wantErrs == nil { | ||
| require.Nil(t, res) | ||
| } else { | ||
| require.Error(t, res, tt.wantErrs.Error()) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| Copyright 2021 The Knative Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package experimental | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| ) | ||
|
|
||
| // Flags is a map containing all the enabled/disabled flags for the experimental features. | ||
| // Missing entry in the map means feature is equal to feature not enabled. | ||
| type Flags map[string]bool | ||
|
|
||
| // IsEnabled returns true if the feature is enabled | ||
| func (e Flags) IsEnabled(featureName string) bool { | ||
| return e != nil && e[featureName] | ||
| } | ||
|
|
||
| // NewFlagsConfigFromMap creates a Flags from the supplied Map | ||
| func NewFlagsConfigFromMap(data map[string]string) (Flags, error) { | ||
| flags := Flags{} | ||
|
|
||
| for k, v := range data { | ||
| if strings.HasPrefix(k, "_") { | ||
| // Ignore all the keys starting with _ | ||
| continue | ||
| } | ||
| sanitizedKey := strings.TrimSpace(k) | ||
| if strings.EqualFold(v, "true") { | ||
| flags[sanitizedKey] = true | ||
| } else if strings.EqualFold(v, "false") { | ||
| flags[sanitizedKey] = false | ||
| } else { | ||
| return Flags{}, fmt.Errorf("cannot parse the boolean flag '%s' = '%s'. Allowed values: [true, false]", k, v) | ||
| } | ||
| } | ||
|
|
||
| return flags, nil | ||
| } | ||
|
|
||
| // NewFlagsConfigFromConfigMap creates a Flags from the supplied configMap | ||
| func NewFlagsConfigFromConfigMap(config *corev1.ConfigMap) (Flags, error) { | ||
| return NewFlagsConfigFromMap(config.Data) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we call this just
features.yaml, to be consistent with Knative Serving? A feature can be Alpha, ie experimental. A feature can also be stable and still be disabled if a vendor doesn't want it for X reason.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a comment in the doc about that. I don't like mixing up the concepts, I would prefer that if a feature still needs a flag to enable/disable, it even after GA, that flag should live somewhere else, wherever it makes more sense to do so. e.g. a feature related to broker config should live in the broker config map.
This approach of keeping it separate from experimental features also fits in the various scoping of configurations we already implement for every component. I don't want experimental features to end up re-doing that.