-
Notifications
You must be signed in to change notification settings - Fork 590
/
features.go
162 lines (135 loc) · 5.14 KB
/
features.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
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 feature
import (
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
)
// Flag is a string value which can be either Enabled, Disabled, or Allowed.
type Flag string
const (
// Enabled turns on an optional behavior.
Enabled Flag = "Enabled"
// Disabled turns off an optional behavior.
Disabled Flag = "Disabled"
// Allowed neither explicitly disables or enables a behavior.
// eg. allow a client to control behavior with an annotation or allow a new value through validation.
Allowed Flag = "Allowed"
// Strict is only applicable to the TransportEncryption feature.
// The following applies:
// - Addressables must not accept events to non-HTTPS endpoints
// - Addressables must only advertise HTTPS endpoints
Strict Flag = "Strict"
// Permissive is only applicable to the TransportEncryption feature.
// The following applies:
// - Addressables should accept events at both HTTP and HTTPS endpoints
// - Addressables should advertise both HTTP and HTTPS endpoints
// - Producers should prefer to send events to HTTPS endpoints, if available
Permissive Flag = "Permissive"
)
// 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]Flag
func newDefaults() Flags {
return map[string]Flag{
KReferenceGroup: Disabled,
DeliveryRetryAfter: Disabled,
DeliveryTimeout: Enabled,
KReferenceMapping: Disabled,
NewTriggerFilters: Enabled,
TransportEncryption: Disabled,
OIDCAuthentication: Disabled,
EvenTypeAutoCreate: Disabled,
NewAPIServerFilters: Disabled,
}
}
// IsEnabled returns true if the feature is enabled
func (e Flags) IsEnabled(featureName string) bool {
return e != nil && e[featureName] == Enabled
}
// IsDisabled returns true if the feature is disabled
func (e Flags) IsDisabled(featureName string) bool {
return e != nil && e[featureName] == Disabled
}
// IsAllowed returns true if the feature is enabled or allowed
func (e Flags) IsAllowed(featureName string) bool {
return e.IsEnabled(featureName) || (e != nil && e[featureName] == Allowed)
}
// IsPermissiveTransportEncryption returns true if the TransportEncryption feature is in Permissive mode.
func (e Flags) IsPermissiveTransportEncryption() bool {
return e != nil && e[TransportEncryption] == Permissive
}
// IsStrictTransportEncryption returns true if the TransportEncryption feature is in Strict mode.
func (e Flags) IsStrictTransportEncryption() bool {
return e != nil && e[TransportEncryption] == Strict
}
// IsDisabledTransportEncryption returns true if the TransportEncryption feature is in Disabled mode.
func (e Flags) IsDisabledTransportEncryption() bool {
return e != nil && e[TransportEncryption] == Disabled
}
func (e Flags) IsOIDCAuthentication() bool {
return e != nil && e[OIDCAuthentication] == Enabled
}
func (e Flags) IsCrossNamespaceEventLinks() bool {
return e != nil && e[CrossNamespaceEventLinks] == Enabled
}
func (e Flags) String() string {
return fmt.Sprintf("%+v", map[string]Flag(e))
}
func (e Flags) NodeSelector() map[string]string {
// Check if NodeSelector is not nil
if e == nil {
return map[string]string{}
}
nodeSelectorMap := make(map[string]string)
for k, v := range e {
if strings.Contains(k, NodeSelectorLabel) {
key := strings.TrimPrefix(k, NodeSelectorLabel)
value := strings.TrimSpace(string(v))
nodeSelectorMap[key] = value
}
}
return nodeSelectorMap
}
// NewFlagsConfigFromMap creates a Flags from the supplied Map
func NewFlagsConfigFromMap(data map[string]string) (Flags, error) {
flags := newDefaults()
for k, v := range data {
if strings.HasPrefix(k, "_") {
// Ignore all the keys starting with _
continue
}
sanitizedKey := strings.TrimSpace(k)
if strings.EqualFold(v, string(Allowed)) {
flags[sanitizedKey] = Allowed
} else if strings.EqualFold(v, string(Disabled)) {
flags[sanitizedKey] = Disabled
} else if strings.EqualFold(v, string(Enabled)) {
flags[sanitizedKey] = Enabled
} else if k == TransportEncryption && strings.EqualFold(v, string(Permissive)) {
flags[sanitizedKey] = Permissive
} else if k == TransportEncryption && strings.EqualFold(v, string(Strict)) {
flags[sanitizedKey] = Strict
} else if strings.Contains(k, NodeSelectorLabel) {
flags[sanitizedKey] = Flag(v)
} else {
return flags, fmt.Errorf("cannot parse the feature flag '%s' = '%s'", k, v)
}
}
return flags, nil
}
// NewFlagsConfigFromConfigMap creates a Flags from the supplied configMap
func NewFlagsConfigFromConfigMap(config *corev1.ConfigMap) (Flags, error) {
return NewFlagsConfigFromMap(config.Data)
}