Skip to content

Commit f4d6220

Browse files
authored
Merge pull request kubernetes#2409 from justaugustus/validate
api: Update field validations for `Document`s
2 parents 05570d4 + 82b616d commit f4d6220

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1850
-531
lines changed

api/apifakes/fake_document.go

Lines changed: 102 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/apifakes/fake_file.go

Lines changed: 117 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/approval.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package api
18+
19+
import (
20+
"bufio"
21+
"bytes"
22+
"io"
23+
24+
"github.com/go-playground/validator/v10"
25+
"github.com/pkg/errors"
26+
"gopkg.in/yaml.v3"
27+
)
28+
29+
type PRRApprovals []*PRRApproval
30+
31+
func (p *PRRApprovals) AddPRRApproval(prrApproval *PRRApproval) {
32+
*p = append(*p, prrApproval)
33+
}
34+
35+
type PRRApproval struct {
36+
Number string `json:"kep-number" yaml:"kep-number" validate:"required"`
37+
38+
// TODO: Need to validate these milestone pointers are not nil
39+
Alpha *PRRMilestone `json:"alpha" yaml:"alpha,omitempty"`
40+
Beta *PRRMilestone `json:"beta" yaml:"beta,omitempty"`
41+
Stable *PRRMilestone `json:"stable" yaml:"stable,omitempty"`
42+
43+
// TODO(api): Move to separate struct for handling document parsing
44+
Error error `json:"-" yaml:"-"`
45+
}
46+
47+
func (prr *PRRApproval) Validate() error {
48+
v := validator.New()
49+
if err := v.Struct(prr); err != nil {
50+
return errors.Wrap(err, "running validation")
51+
}
52+
53+
return nil
54+
}
55+
56+
func (prr *PRRApproval) ApproverForStage(stage string) (string, error) {
57+
isValidStage := IsOneOf(stage, ValidStages)
58+
if !isValidStage {
59+
return "", ErrKEPStageIsInvalid(stage)
60+
}
61+
62+
if prr.Alpha == nil && prr.Beta == nil && prr.Stable == nil {
63+
return "", ErrPRRMilestonesAllEmpty
64+
}
65+
66+
switch stage {
67+
case "alpha":
68+
if prr.Alpha == nil {
69+
return "", ErrPRRMilestoneIsNil
70+
}
71+
72+
return prr.Alpha.Approver, nil
73+
case "beta":
74+
if prr.Beta == nil {
75+
return "", ErrPRRMilestoneIsNil
76+
}
77+
78+
return prr.Beta.Approver, nil
79+
case "stable":
80+
if prr.Stable == nil {
81+
return "", ErrPRRMilestoneIsNil
82+
}
83+
84+
return prr.Stable.Approver, nil
85+
}
86+
87+
return "", ErrPRRApproverUnknown
88+
}
89+
90+
// TODO(api): Can we refactor the proposal `Milestone` to retrieve this?
91+
type PRRMilestone struct {
92+
Approver string `json:"approver" yaml:"approver" validate:"required"`
93+
}
94+
95+
type PRRHandler Parser
96+
97+
func NewPRRHandler() (*PRRHandler, error) {
98+
handler := &PRRHandler{}
99+
100+
approvers, err := FetchPRRApprovers()
101+
if err != nil {
102+
return nil, errors.Wrap(err, "fetching PRR approvers")
103+
}
104+
105+
handler.PRRApprovers = approvers
106+
107+
return handler, nil
108+
}
109+
110+
// TODO(api): Make this a generic parser for all `Document` types
111+
func (p *PRRHandler) Parse(in io.Reader) (*PRRApproval, error) {
112+
scanner := bufio.NewScanner(in)
113+
var body bytes.Buffer
114+
for scanner.Scan() {
115+
line := scanner.Text() + "\n"
116+
body.WriteString(line)
117+
}
118+
119+
approval := &PRRApproval{}
120+
if err := scanner.Err(); err != nil {
121+
return approval, errors.Wrap(err, "reading file")
122+
}
123+
124+
if err := yaml.Unmarshal(body.Bytes(), &approval); err != nil {
125+
p.Errors = append(p.Errors, errors.Wrap(err, "error unmarshalling YAML"))
126+
return approval, errors.Wrap(err, "unmarshalling YAML")
127+
}
128+
129+
if valErr := approval.Validate(); valErr != nil {
130+
p.Errors = append(p.Errors, errors.Wrap(valErr, "validating PRR"))
131+
return approval, errors.Wrap(valErr, "validating PRR")
132+
}
133+
134+
return approval, nil
135+
}

0 commit comments

Comments
 (0)