-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathproposal.go
254 lines (215 loc) · 7.08 KB
/
proposal.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
/*
Copyright 2021 The Kubernetes 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 api
import (
"bufio"
"bytes"
"crypto/md5"
"fmt"
"io"
"strings"
"github.com/go-playground/validator/v10"
"k8s.io/enhancements/pkg/yaml"
)
type Stage string
const (
AlphaStage Stage = "alpha"
BetaStage Stage = "beta"
StableStage Stage = "stable"
Deprecated Stage = "deprecated"
Disabled Stage = "disabled"
Removed Stage = "removed"
)
var ValidStages = []Stage{
AlphaStage,
BetaStage,
StableStage,
Deprecated,
Disabled,
Removed,
}
func (s Stage) IsValid() error {
for _, s2 := range ValidStages {
if s == s2 {
return nil
}
}
return fmt.Errorf("invalid stage: %v, should be one of %v", s, ValidStages)
}
type Status string
const (
ProvisionalStatus Status = "provisional"
ImplementableStatus Status = "implementable"
ImplementedStatus Status = "implemented"
DeferredStatus Status = "deferred"
RejectedStatus Status = "rejected"
WithdrawnStatus Status = "withdrawn"
ReplacedStatus Status = "replaced"
)
var ValidStatuses = []Status{
ProvisionalStatus,
ImplementableStatus,
ImplementedStatus,
DeferredStatus,
RejectedStatus,
WithdrawnStatus,
ReplacedStatus,
}
func (s Status) IsValid() error {
for _, s2 := range ValidStatuses {
if s == s2 {
return nil
}
}
return fmt.Errorf("invalid status: %v, should be one of %v", s, ValidStatuses)
}
type Proposals []*Proposal
func (p *Proposals) AddProposal(proposal *Proposal) {
*p = append(*p, proposal)
}
// TODO(api): json fields are not using consistent casing
type Proposal struct {
ID string `json:"id"`
PRNumber string `json:"prNumber,omitempty"`
Name string `json:"name,omitempty"`
Title string `json:"title" yaml:"title" validate:"required"`
Number string `json:"kepNumber" yaml:"kep-number" validate:"required"`
Authors []string `json:"authors" yaml:",flow" validate:"required"`
OwningSIG string `json:"owningSig" yaml:"owning-sig" validate:"required"`
ParticipatingSIGs []string `json:"participatingSigs" yaml:"participating-sigs,flow,omitempty"`
Reviewers []string `json:"reviewers" yaml:",flow"`
Approvers []string `json:"approvers" yaml:",flow" validate:"required"`
Editor string `json:"editor" yaml:"editor,omitempty"`
CreationDate string `json:"creationDate" yaml:"creation-date"`
LastUpdated string `json:"lastUpdated" yaml:"last-updated"`
Status Status `json:"status" yaml:"status" validate:"required"`
SeeAlso []string `json:"seeAlso" yaml:"see-also,omitempty"`
Replaces []string `json:"replaces" yaml:"replaces,omitempty"`
SupersededBy []string `json:"supersededBy" yaml:"superseded-by,omitempty"`
Stage Stage `json:"stage" yaml:"stage"`
LatestMilestone string `json:"latestMilestone" yaml:"latest-milestone"`
Milestone Milestone `json:"milestone" yaml:"milestone"`
FeatureGates []FeatureGate `json:"featureGates" yaml:"feature-gates"`
DisableSupported bool `json:"disableSupported" yaml:"disable-supported"`
Metrics []string `json:"metrics" yaml:"metrics"`
Filename string `json:"-" yaml:"-"`
Error error `json:"-" yaml:"-"`
Contents string `json:"markdown" yaml:"-"`
}
func (p *Proposal) IsMissingMilestone() bool {
return p.LatestMilestone == ""
}
func (p *Proposal) IsMissingStage() bool {
return p.Stage == ""
}
type Milestone struct {
Alpha string `json:"alpha" yaml:"alpha"`
Beta string `json:"beta" yaml:"beta"`
Stable string `json:"stable" yaml:"stable"`
Deprecated string `json:"deprecated" yaml:"deprecated,omitempty"`
Removed string `json:"removed" yaml:"removed,omitempty"`
Disabled string `json:"disabled" yaml:"disabled,omitempty"`
}
type FeatureGate struct {
Name string `json:"name" yaml:"name"`
Components []string `json:"components" yaml:"components"`
}
type KEPHandler Parser
// TODO(api): Make this a generic parser for all `Document` types
func (k *KEPHandler) Parse(in io.Reader) (*Proposal, error) {
scanner := bufio.NewScanner(in)
count := 0
metadata := []byte{}
var body bytes.Buffer
for scanner.Scan() {
line := scanner.Text() + "\n"
if strings.Contains(line, "---") {
count++
continue
}
if count == 1 {
metadata = append(metadata, []byte(line)...)
} else {
body.WriteString(line)
}
}
kep := &Proposal{
Contents: body.String(),
}
if err := scanner.Err(); err != nil {
return kep, fmt.Errorf("reading file: %w", err)
}
// this file is just the KEP metadata
if count == 0 {
metadata = body.Bytes()
kep.Contents = ""
}
if err := yaml.UnmarshalStrict(metadata, &kep); err != nil {
k.Errors = append(k.Errors, fmt.Errorf("error unmarshalling YAML: %w", err))
return kep, fmt.Errorf("unmarshalling YAML: %w", err)
}
if err := k.validateStruct(kep); err != nil {
k.Errors = append(k.Errors, err)
return kep, fmt.Errorf("validating KEP: %w", err)
}
kep.ID = hash(kep.OwningSIG + ":" + kep.Title)
return kep, nil
}
// validateStruct returns an error if the given Proposal has invalid fields
// as defined by struct tags, or nil if there are no invalid fields
func (k *KEPHandler) validateStruct(p *Proposal) error {
v := validator.New()
return v.Struct(p)
}
// validateGroups returns errors for each invalid group (e.g. SIG) in the given
// Proposal, or nil if there are no invalid groups
func (k *KEPHandler) validateGroups(p *Proposal) []error {
var errs []error
validGroups := make(map[string]bool)
for _, g := range k.Groups {
validGroups[g] = true
}
for _, g := range p.ParticipatingSIGs {
if _, ok := validGroups[g]; !ok {
errs = append(errs, fmt.Errorf("invalid participating-sig: %s", g))
}
}
if _, ok := validGroups[p.OwningSIG]; !ok {
errs = append(errs, fmt.Errorf("invalid owning-sig: %s", p.OwningSIG))
}
return errs
}
// Validate returns errors for each reason the given proposal is invalid,
// or nil if it is valid
func (k *KEPHandler) Validate(p *Proposal) []error {
var allErrs []error
if err := k.validateStruct(p); err != nil {
allErrs = append(allErrs, fmt.Errorf("struct-based validation: %w", err))
}
if errs := k.validateGroups(p); errs != nil {
allErrs = append(allErrs, errs...)
}
if err := p.Status.IsValid(); err != nil {
allErrs = append(allErrs, err)
}
if err := p.Stage.IsValid(); err != nil {
allErrs = append(allErrs, err)
}
if p.Status == ImplementedStatus && p.Stage != StableStage {
allErrs = append(allErrs, fmt.Errorf("status:implemented implies stage:stable but found: %v", p.Stage))
}
return allErrs
}
func hash(s string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
}