-
Notifications
You must be signed in to change notification settings - Fork 606
Expand file tree
/
Copy pathutil.go
More file actions
284 lines (237 loc) · 8.69 KB
/
util.go
File metadata and controls
284 lines (237 loc) · 8.69 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package features
import (
"fmt"
"net/url"
"strings"
configv1 "github.com/openshift/api/config/v1"
"k8s.io/apimachinery/pkg/util/sets"
)
// FeatureGateDescription is a golang-only interface used to contains details for a feature gate.
type FeatureGateDescription struct {
// FeatureGateAttributes is the information that appears in the API
FeatureGateAttributes configv1.FeatureGateAttributes
// OwningJiraComponent is the jira component that owns most of the impl and first assignment for the bug.
// This is the team that owns the feature long term.
OwningJiraComponent string
// ResponsiblePerson is the person who is on the hook for first contact. This is often, but not always, a team lead.
// It is someone who can make the promise on the behalf of the team.
ResponsiblePerson string
// OwningProduct is the product that owns the lifecycle of the gate.
OwningProduct OwningProduct
// EnhancementPR is the PR for the enhancement.
EnhancementPR string
}
type FeatureGateEnabledDisabled struct {
Enabled []FeatureGateDescription
Disabled []FeatureGateDescription
}
type ClusterProfileName string
var (
Hypershift = ClusterProfileName("include.release.openshift.io/ibm-cloud-managed")
SelfManaged = ClusterProfileName("include.release.openshift.io/self-managed-high-availability")
AllClusterProfiles = []ClusterProfileName{Hypershift, SelfManaged}
)
type OwningProduct string
var (
ocpSpecific = OwningProduct("OCP")
kubernetes = OwningProduct("Kubernetes")
)
type featureGateEnableOption func(s *featureGateStatus)
type versionOperator string
var (
equal = versionOperator("=")
greaterThan = versionOperator(">")
greaterThanOrEqual = versionOperator(">=")
lessThan = versionOperator("<")
lessThanOrEqual = versionOperator("<=")
)
func inVersion(version uint64, op versionOperator) featureGateEnableOption {
return func(s *featureGateStatus) {
switch op {
case equal:
s.version.Insert(version)
case greaterThan:
for v := version + 1; v <= maxOpenshiftVersion; v++ {
s.version.Insert(v)
}
case greaterThanOrEqual:
for v := version; v <= maxOpenshiftVersion; v++ {
s.version.Insert(v)
}
case lessThan:
for v := minOpenshiftVersion; v < version; v++ {
s.version.Insert(v)
}
case lessThanOrEqual:
for v := minOpenshiftVersion; v <= version; v++ {
s.version.Insert(v)
}
default:
panic(fmt.Sprintf("invalid version operator: %s", op))
}
}
}
func inClusterProfile(clusterProfile ClusterProfileName) featureGateEnableOption {
return func(s *featureGateStatus) {
s.clusterProfile.Insert(clusterProfile)
}
}
func withFeatureSet(featureSet configv1.FeatureSet) featureGateEnableOption {
return func(s *featureGateStatus) {
s.featureSets.Insert(featureSet)
}
}
func inDefault() featureGateEnableOption {
return withFeatureSet(configv1.Default)
}
func inTechPreviewNoUpgrade() featureGateEnableOption {
return withFeatureSet(configv1.TechPreviewNoUpgrade)
}
func inDevPreviewNoUpgrade() featureGateEnableOption {
return withFeatureSet(configv1.DevPreviewNoUpgrade)
}
func inCustomNoUpgrade() featureGateEnableOption {
return withFeatureSet(configv1.CustomNoUpgrade)
}
func inOKD() featureGateEnableOption {
return withFeatureSet(configv1.OKD)
}
type featureGateBuilder struct {
name string
owningJiraComponent string
responsiblePerson string
owningProduct OwningProduct
enhancementPRURL string
status []featureGateStatus
}
type featureGateStatus struct {
version sets.Set[uint64]
clusterProfile sets.Set[ClusterProfileName]
featureSets sets.Set[configv1.FeatureSet]
}
func (s *featureGateStatus) isEnabled(version uint64, clusterProfile ClusterProfileName, featureSet configv1.FeatureSet) bool {
// If either version or clusterprofile are empty, match all.
matchesVersion := len(s.version) == 0 || s.version.Has(version)
matchesClusterProfile := len(s.clusterProfile) == 0 || s.clusterProfile.Has(clusterProfile)
matchesFeatureSet := s.featureSets.Has(featureSet)
return matchesVersion && matchesClusterProfile && matchesFeatureSet
}
const (
legacyFeatureGateWithoutEnhancement = "FeatureGate predates 4.18"
)
// newFeatureGate featuregate are disabled in every FeatureSet and selectively enabled
func newFeatureGate(name string) *featureGateBuilder {
return &featureGateBuilder{
name: name,
}
}
func (b *featureGateBuilder) reportProblemsToJiraComponent(owningJiraComponent string) *featureGateBuilder {
b.owningJiraComponent = owningJiraComponent
return b
}
func (b *featureGateBuilder) contactPerson(responsiblePerson string) *featureGateBuilder {
b.responsiblePerson = responsiblePerson
return b
}
func (b *featureGateBuilder) productScope(owningProduct OwningProduct) *featureGateBuilder {
b.owningProduct = owningProduct
return b
}
func (b *featureGateBuilder) enhancementPR(url string) *featureGateBuilder {
b.enhancementPRURL = url
return b
}
func (b *featureGateBuilder) enable(opts ...featureGateEnableOption) *featureGateBuilder {
status := featureGateStatus{
version: sets.New[uint64](),
clusterProfile: sets.New[ClusterProfileName](),
featureSets: sets.New[configv1.FeatureSet](),
}
for _, opt := range opts {
opt(&status)
}
b.status = append(b.status, status)
return b
}
func (b *featureGateBuilder) register() (configv1.FeatureGateName, error) {
if len(b.name) == 0 {
return "", fmt.Errorf("missing name")
}
if len(b.owningJiraComponent) == 0 {
return "", fmt.Errorf("missing owningJiraComponent")
}
if len(b.responsiblePerson) == 0 {
return "", fmt.Errorf("missing responsiblePerson")
}
if len(b.owningProduct) == 0 {
return "", fmt.Errorf("missing owningProduct")
}
_, enhancementPRErr := url.Parse(b.enhancementPRURL)
switch {
case b.enhancementPRURL == legacyFeatureGateWithoutEnhancement:
if !legacyFeatureGates.Has(b.name) {
return "", fmt.Errorf("FeatureGate/%s is a new feature gate, not an existing one. It must have an enhancementPR with GA Graduation Criteria like https://github.com/openshift/enhancements/pull/#### or https://github.com/kubernetes/enhancements/issues/####", b.name)
}
case len(b.enhancementPRURL) == 0:
return "", fmt.Errorf("FeatureGate/%s is missing an enhancementPR with GA Graduation Criteria like https://github.com/openshift/enhancements/pull/#### or https://github.com/kubernetes/enhancements/issues/####", b.name)
case !strings.HasPrefix(b.enhancementPRURL, "https://github.com/openshift/enhancements/pull/") &&
!strings.HasPrefix(b.enhancementPRURL, "https://github.com/kubernetes/enhancements/issues/") &&
!strings.HasPrefix(b.enhancementPRURL, "https://github.com/ovn-kubernetes/ovn-kubernetes/pull/"):
return "", fmt.Errorf("FeatureGate/%s enhancementPR format is incorrect; must be like https://github.com/openshift/enhancements/pull/#### or https://github.com/kubernetes/enhancements/issues/#### or https://github.com/ovn-kubernetes/ovn-kubernetes/pull/####", b.name)
case enhancementPRErr != nil:
return "", fmt.Errorf("FeatureGate/%s is enhancementPR is invalid: %w", b.name, enhancementPRErr)
}
featureGateName := configv1.FeatureGateName(b.name)
allFeatureGates[featureGateName] = b.status
return featureGateName, nil
}
func (b *featureGateBuilder) mustRegister() configv1.FeatureGateName {
ret, err := b.register()
if err != nil {
panic(err)
}
return ret
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *FeatureGateEnabledDisabled) DeepCopyInto(out *FeatureGateEnabledDisabled) {
*out = *in
if in.Enabled != nil {
in, out := &in.Enabled, &out.Enabled
*out = make([]FeatureGateDescription, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.Disabled != nil {
in, out := &in.Disabled, &out.Disabled
*out = make([]FeatureGateDescription, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new FeatureGateEnabledDisabled.
func (in *FeatureGateEnabledDisabled) DeepCopy() *FeatureGateEnabledDisabled {
if in == nil {
return nil
}
out := new(FeatureGateEnabledDisabled)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *FeatureGateDescription) DeepCopyInto(out *FeatureGateDescription) {
*out = *in
out.FeatureGateAttributes = in.FeatureGateAttributes
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new FeatureGateDescription.
func (in *FeatureGateDescription) DeepCopy() *FeatureGateDescription {
if in == nil {
return nil
}
out := new(FeatureGateDescription)
in.DeepCopyInto(out)
return out
}