forked from openshift/api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatureset-markdown.go
More file actions
321 lines (279 loc) · 9.84 KB
/
Copy pathfeatureset-markdown.go
File metadata and controls
321 lines (279 loc) · 9.84 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package main
import (
"bytes"
"context"
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/openshift/api/tools/codegen/pkg/utils"
"github.com/spf13/pflag"
"io"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/sets"
"os"
"path/filepath"
kyaml "sigs.k8s.io/yaml"
"sort"
"strings"
"time"
"github.com/spf13/cobra"
)
type FeatureSetOptions struct {
In io.Reader
Out io.Writer
ErrOut io.Writer
FeatureSetManifestDir string
OutputFile string
Verify bool
}
func NewFeatureSetOptions(in io.Reader, out, errOut io.Writer) *FeatureSetOptions {
return &FeatureSetOptions{
In: in,
Out: out,
ErrOut: errOut,
FeatureSetManifestDir: filepath.Join("payload-manifests", "featuregates"),
OutputFile: "features.md",
}
}
func NewFeatureSetFlagsCommand(in io.Reader, out, errOut io.Writer) *cobra.Command {
o := NewFeatureSetOptions(in, out, errOut)
cmd := &cobra.Command{
Use: "featureset-markdown",
Short: "featureset-markdown generates a markdown document summarizing current featuregate status.",
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFn()
if err := o.Validate(); err != nil {
return err
}
return o.Run(ctx)
},
}
o.AddFlags(cmd.Flags())
return cmd
}
func (o *FeatureSetOptions) Validate() error {
if len(o.FeatureSetManifestDir) == 0 {
return fmt.Errorf("--featureset-manifest-path is required")
}
if _, err := os.ReadDir(o.FeatureSetManifestDir); err != nil {
return fmt.Errorf("--featureset-manifest-path cannot be read: %w", err)
}
if len(o.OutputFile) == 0 {
return fmt.Errorf("--output-file is required")
}
return nil
}
func (o *FeatureSetOptions) AddFlags(flags *pflag.FlagSet) {
flags.StringVar(&o.FeatureSetManifestDir, "featureset-manifest-path", o.FeatureSetManifestDir, "path to directory containing the FeatureGate YAMLs for each FeatureSet,ClusterProfile tuple.")
flags.StringVar(&o.OutputFile, "output-file", o.OutputFile, "path to markdown file detailing FeatureGates.")
flags.BoolVar(&o.Verify, "verify", o.Verify, "Verify the content has not changed.")
}
func init() {
rootCmd.AddCommand(NewFeatureSetFlagsCommand(os.Stdin, os.Stdout, os.Stderr))
}
func (o *FeatureSetOptions) Run(ctx context.Context) error {
allClusterProfiles, allFeatureSets, _, byClusterProfilebyFeatureSet, err := readFeatureGate(ctx, o.FeatureSetManifestDir)
if err != nil {
return err
}
cols := []columnTuple{}
md := utils.NewMarkdown("FeatureGate Summary")
md.NextTableColumn()
md.Exact("FeatureGate ")
for _, featureSet := range allFeatureSets.List() {
for _, clusterProfile := range allClusterProfiles.List() {
cols = append(cols, columnTuple{
clusterProfile: clusterProfile,
featureSet: featureSet,
})
md.NextTableColumn()
md.Exact(fmt.Sprintf("%v on %v ", featureSet, clusterProfile))
}
}
md.EndTableRow()
md.NextTableColumn()
md.Exact("------ ")
for i := 0; i < len(cols); i++ {
md.NextTableColumn()
md.Exact("--- ")
}
md.EndTableRow()
orderedFeatureGates := getOrderedFeatureGates(byClusterProfilebyFeatureSet)
for _, featureGate := range orderedFeatureGates {
md.NextTableColumn()
md.Exact(featureGate)
for _, col := range cols {
currFeatureGateInfo := byClusterProfilebyFeatureSet[col.clusterProfile][col.featureSet]
md.NextTableColumn()
if currFeatureGateInfo.enabled.Has(featureGate) {
md.Exact("<span style=\"background-color: #519450\">Enabled</span> ")
} else {
//md.Exact(" ")
}
}
md.EndTableRow()
}
if o.Verify {
actualContent, err := os.ReadFile(o.OutputFile)
if err != nil {
return fmt.Errorf("failed to verify: %w", err)
}
expectedContent := md.ExactBytes()
if bytes.Equal(actualContent, expectedContent) {
return nil
}
return fmt.Errorf("actual content not match: %v", cmp.Diff(expectedContent, actualContent))
}
if err := os.WriteFile(o.OutputFile, md.ExactBytes(), 0644); err != nil {
return err
}
return nil
}
func getOrderedFeatureGates(info map[string]map[string]*featureGateInfo) []string {
counts := map[string]int{}
for _, byClusterProfile := range info {
for _, byFeature := range byClusterProfile {
for _, featureGate := range byFeature.enabled.List() {
counts[featureGate] = counts[featureGate] + 1
}
for _, featureGate := range byFeature.disabled.List() {
counts[featureGate] = counts[featureGate] + 0
}
}
}
toSort := []stringCount{}
for name, count := range counts {
toSort = append(toSort, stringCount{
name: name,
count: count,
})
}
sort.Sort(byCount(toSort))
ret := []string{}
for _, curr := range toSort {
ret = append(ret, curr.name)
}
return ret
}
type stringCount struct {
name string
count int
}
type byCount []stringCount
func (a byCount) Len() int { return len(a) }
func (a byCount) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byCount) Less(i, j int) bool {
if a[i].count < a[j].count {
return true
}
if a[i].count > a[j].count {
return false
}
if strings.Compare(a[i].name, a[j].name) < 0 {
return true
}
return false
}
type columnTuple struct {
clusterProfile string
featureSet string
}
type featureGateInfo struct {
clusterProfile string
featureSet string
enabled sets.String
disabled sets.String
allFeatureGates map[string]bool
}
func readFeatureGate(ctx context.Context, featureSetManifestDir string) (sets.String, sets.String, sets.String, map[string]map[string]*featureGateInfo, error) {
allClusterProfiles := sets.String{}
allFeatureSets := sets.String{}
allFeatureGates := sets.String{}
clusterProfileToFeatureSetToFeatureGates := map[string]map[string]*featureGateInfo{}
featureSetManifestFile, err := os.ReadDir(featureSetManifestDir)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("cannot read FeatureSetManifestDir: %w", err)
}
for _, currFeatureSetManifestFile := range featureSetManifestFile {
currFeatureGateInfo := &featureGateInfo{
enabled: sets.String{},
disabled: sets.String{},
allFeatureGates: map[string]bool{},
}
featureGateFilename := filepath.Join(featureSetManifestDir, currFeatureSetManifestFile.Name())
featureGateBytes, err := os.ReadFile(featureGateFilename)
if err != nil {
return nil, nil, nil, 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, nil, nil, nil, fmt.Errorf("unable to parse featuregate %q: %w", featureGateFilename, err)
}
uncastFeatureGate := unstructured.Unstructured{
Object: featureGateMap,
}
clusterProfiles := clusterOperatorClusterProfilesFrom(uncastFeatureGate.GetAnnotations())
if len(clusterProfiles) != 1 {
return nil, nil, nil, nil, fmt.Errorf("expected exactly one clusterProfile from %q: %v", featureGateFilename, clusterProfiles.List())
}
clusterProfileShortName, err := utils.ClusterProfileToShortName(clusterProfiles.List()[0])
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("unrecognized clusterprofile name %q: %w", clusterProfiles.List()[0], err)
}
currFeatureGateInfo.clusterProfile = clusterProfileShortName
allClusterProfiles.Insert(currFeatureGateInfo.clusterProfile)
currFeatureGateInfo.featureSet, _, _ = unstructured.NestedString(uncastFeatureGate.Object, "spec", "featureSet")
if len(currFeatureGateInfo.featureSet) == 0 {
currFeatureGateInfo.featureSet = "Default"
}
allFeatureSets.Insert(currFeatureGateInfo.featureSet)
uncastFeatureGateSlice, _, err := unstructured.NestedSlice(uncastFeatureGate.Object, "status", "featureGates")
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("no slice found %w", err)
}
enabledFeatureGates, _, err := unstructured.NestedSlice(uncastFeatureGateSlice[0].(map[string]interface{}), "enabled")
if err != nil {
return nil, nil, nil, 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, nil, nil, nil, fmt.Errorf("no gate name found %w", err)
}
currFeatureGateInfo.enabled.Insert(featureGateName)
currFeatureGateInfo.allFeatureGates[featureGateName] = true
allFeatureGates.Insert(featureGateName)
}
disabledFeatureGates, _, err := unstructured.NestedSlice(uncastFeatureGateSlice[0].(map[string]interface{}), "disabled")
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("no enabled found %w", err)
}
for _, currGate := range disabledFeatureGates {
featureGateName, _, err := unstructured.NestedString(currGate.(map[string]interface{}), "name")
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("no gate name found %w", err)
}
currFeatureGateInfo.disabled.Insert(featureGateName)
currFeatureGateInfo.allFeatureGates[featureGateName] = false
allFeatureGates.Insert(featureGateName)
}
existing, ok := clusterProfileToFeatureSetToFeatureGates[currFeatureGateInfo.clusterProfile]
if !ok {
existing = map[string]*featureGateInfo{}
clusterProfileToFeatureSetToFeatureGates[currFeatureGateInfo.clusterProfile] = existing
}
existing[currFeatureGateInfo.featureSet] = currFeatureGateInfo
clusterProfileToFeatureSetToFeatureGates[currFeatureGateInfo.clusterProfile] = existing
}
return allClusterProfiles, allFeatureSets, allFeatureGates, clusterProfileToFeatureSetToFeatureGates, nil
}
func clusterOperatorClusterProfilesFrom(annotations map[string]string) sets.String {
ret := sets.NewString()
for k, v := range annotations {
if strings.HasPrefix(k, "include.release.openshift.io/") && v == "false-except-for-the-config-operator" {
ret.Insert(k)
}
}
return ret
}