/
filters.go
295 lines (244 loc) · 7.93 KB
/
filters.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
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
285
286
287
288
289
290
291
292
293
294
295
package manifestmerge
import (
"fmt"
"github.com/openshift/api/tools/codegen/pkg/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/sets"
"os"
"path"
"path/filepath"
kyaml "sigs.k8s.io/yaml"
"strings"
)
func AllKnownFeatureSets(payloadFeatureGatePath string) (sets.String, error) {
allFeatureSets := sets.String{}
allFeatureSets.Insert("CustomNoUpgrade") // this one won't have a rendered version since we don't know the gates
featureSetManifestFiles, err := os.ReadDir(payloadFeatureGatePath)
if err != nil {
return nil, fmt.Errorf("cannot read FeatureSetManifestDir: %w", err)
}
for _, currFeatureSetManifestFile := range featureSetManifestFiles {
featureGateFilename := filepath.Join(payloadFeatureGatePath, currFeatureSetManifestFile.Name())
featureGateBytes, err := os.ReadFile(featureGateFilename)
if err != nil {
return nil, fmt.Errorf("unable to read %q: %w", featureGateFilename, err)
}
// use unstructured to pull this information to avoid vendoring openshift/api
featureGateMap := map[string]interface{}{}
if err := kyaml.Unmarshal(featureGateBytes, &featureGateMap); err != nil {
return nil, fmt.Errorf("unable to parse featuregate %q: %w", featureGateFilename, err)
}
uncastFeatureGate := unstructured.Unstructured{
Object: featureGateMap,
}
currFeatureSet, _, _ := unstructured.NestedString(uncastFeatureGate.Object, "spec", "featureSet")
if len(currFeatureSet) == 0 {
currFeatureSet = "Default"
}
allFeatureSets.Insert(currFeatureSet)
}
return allFeatureSets, nil
}
func FilterForFeatureSet(payloadFeatureGatePath, clusterProfile, featureSetName string) (ManifestFilter, error) {
if featureSetName == "CustomNoUpgrade" {
return &AndManifestFilter{
filters: []ManifestFilter{
&CustomNoUpgrade{},
&ClusterProfileFilter{
clusterProfile: clusterProfile,
},
},
}, nil
}
allKnownFeatureSets, err := AllKnownFeatureSets(payloadFeatureGatePath)
if err != nil {
return nil, fmt.Errorf("failed reading featuresets from %q", payloadFeatureGatePath)
}
if !allKnownFeatureSets.Has(featureSetName) {
return nil, fmt.Errorf("unrecognized featureset name %q", featureSetName)
}
clusterProfileShortName, err := utils.ClusterProfileToShortName(clusterProfile)
if err != nil {
return nil, fmt.Errorf("unrecognized clusterprofile name %q: %w", clusterProfile, err)
}
featureGateFilename := path.Join(payloadFeatureGatePath, fmt.Sprintf("featureGate-%s-%s.yaml", clusterProfileShortName, featureSetName))
enabledFeatureGatesSet := sets.NewString()
featureGateBytes, err := os.ReadFile(featureGateFilename)
if err != nil {
return nil, err
}
// use unstructured to pull this information to avoid vendoring openshift/api
uncastFeatureGate := map[string]interface{}{}
if err := kyaml.Unmarshal(featureGateBytes, &uncastFeatureGate); err != nil {
return nil, fmt.Errorf("unable to parse featuregate %q: %w", featureGateFilename, err)
}
uncastFeatureGateSlice, _, err := unstructured.NestedSlice(uncastFeatureGate, "status", "featureGates")
if err != nil {
return nil, fmt.Errorf("no slice found %w", err)
}
enabledFeatureGates, _, err := unstructured.NestedSlice(uncastFeatureGateSlice[0].(map[string]interface{}), "enabled")
if err != nil {
return nil, fmt.Errorf("no enabled found %w", err)
}
for _, currGate := range enabledFeatureGates {
featureGateName, _, err := unstructured.NestedString(currGate.(map[string]interface{}), "name")
if err != nil {
return nil, fmt.Errorf("no gate name found %w", err)
}
enabledFeatureGatesSet.Insert(featureGateName)
}
return &AndManifestFilter{
filters: []ManifestFilter{
&ForFeatureGates{
allowedFeatureGates: enabledFeatureGatesSet,
},
&ClusterProfileFilter{
clusterProfile: clusterProfile,
},
},
}, nil
}
type ManifestFilter interface {
UseManifest([]byte) (bool, error)
}
type AllFeatureGates struct{}
func (*AllFeatureGates) UseManifest([]byte) (bool, error) {
return true, nil
}
type CustomNoUpgrade struct{}
func (*CustomNoUpgrade) UseManifest([]byte) (bool, error) {
return true, nil
}
func (f *CustomNoUpgrade) String() string {
return fmt.Sprintf("CustomNoUpgrade")
}
type ForFeatureGates struct {
allowedFeatureGates sets.String
}
func (f *ForFeatureGates) UseManifest(data []byte) (bool, error) {
partialObject := &metav1.PartialObjectMetadata{}
if err := kyaml.Unmarshal(data, partialObject); err != nil {
return false, err
}
manifestFeatureGates := featureGatesFromManifest(partialObject)
if len(manifestFeatureGates) == 0 || manifestFeatureGates.Has("") {
// always include ungated manifests
return true, nil
}
return manifestFeatureGates.HasAny(f.allowedFeatureGates.UnsortedList()...), nil
}
func (f *ForFeatureGates) String() string {
return fmt.Sprintf("featureGates/%d", len(f.allowedFeatureGates))
}
func featureGatesFromManifest(manifest metav1.Object) sets.String {
ret := sets.String{}
for existingAnnotation := range manifest.GetAnnotations() {
if strings.HasPrefix(existingAnnotation, "feature-gate.release.openshift.io/") {
featureGateName := strings.TrimPrefix(existingAnnotation, "feature-gate.release.openshift.io/")
ret.Insert(featureGateName)
}
}
return ret
}
type ClusterProfileFilter struct {
clusterProfile string
}
func (f *ClusterProfileFilter) UseManifest(data []byte) (bool, error) {
partialObject := &metav1.PartialObjectMetadata{}
if err := kyaml.Unmarshal(data, partialObject); err != nil {
return false, err
}
// if there's no preferenceinclude everywhere
if !utils.HasClusterProfilePreference(partialObject.GetAnnotations()) {
return true, nil
}
forThisProfile := partialObject.GetAnnotations()[f.clusterProfile] == "true"
return forThisProfile, nil
}
func (f *ClusterProfileFilter) UseCRD(metadata crdForFeatureSet) bool {
return metadata.clusterProfile == f.clusterProfile
}
func (f *ClusterProfileFilter) String() string {
return fmt.Sprintf("clusterProfile=%v", f.clusterProfile)
}
type AndManifestFilter struct {
filters []ManifestFilter
}
func (f *AndManifestFilter) UseManifest(data []byte) (bool, error) {
for _, curr := range f.filters {
ret, err := curr.UseManifest(data)
if err != nil {
return false, err
}
if !ret {
return false, nil
}
}
return true, nil
}
func (f *AndManifestFilter) String() string {
str := []string{}
for _, curr := range f.filters {
str = append(str, fmt.Sprintf("%v", curr))
}
return strings.Join(str, " AND ")
}
type CRDFilter interface {
UseCRD(metadata crdForFeatureSet) bool
}
type AndCRDFilter struct {
filters []CRDFilter
}
func (f *AndCRDFilter) UseCRD(metadata crdForFeatureSet) bool {
for _, curr := range f.filters {
ret := curr.UseCRD(metadata)
if !ret {
return false
}
}
return true
}
func (f *AndCRDFilter) String() string {
str := []string{}
for _, curr := range f.filters {
str = append(str, fmt.Sprintf("%v", curr))
}
return strings.Join(str, " AND ")
}
type FeatureSetFilter struct {
featureSetName string
}
func (f *FeatureSetFilter) UseManifest(data []byte) (bool, error) {
partialObject := &metav1.PartialObjectMetadata{}
if err := kyaml.Unmarshal(data, partialObject); err != nil {
return false, err
}
forThisFeatureSet := partialObject.GetAnnotations()["release.openshift.io/feature-set"] == f.featureSetName
return forThisFeatureSet, nil
}
func (f *FeatureSetFilter) UseCRD(metadata crdForFeatureSet) bool {
return metadata.featureSet == f.featureSetName
}
func (f *FeatureSetFilter) String() string {
return fmt.Sprintf("featureSetName=%v", f.featureSetName)
}
type HasData struct {
}
func (f *HasData) UseCRD(metadata crdForFeatureSet) bool {
return metadata.noData == false
}
func (f *HasData) String() string {
return "HasData"
}
type Everything struct {
}
func (f *Everything) UseManifest(data []byte) (bool, error) {
return true, nil
}
func (f *Everything) UseCRD(metadata crdForFeatureSet) bool {
return true
}
func (f *Everything) String() string {
return "Everything"
}