Skip to content

Commit 6c41176

Browse files
committed
api: add Stage and Status types
1 parent c57c443 commit 6c41176

File tree

11 files changed

+98
-54
lines changed

11 files changed

+98
-54
lines changed

api/approval.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,29 @@ func (prr *PRRApproval) Validate() error {
5454
return nil
5555
}
5656

57-
func (prr *PRRApproval) ApproverForStage(stage string) (string, error) {
58-
isValidStage := IsOneOf(stage, ValidStages)
59-
if !isValidStage {
60-
return "", ErrKEPStageIsInvalid(stage)
57+
func (prr *PRRApproval) ApproverForStage(stage Stage) (string, error) {
58+
if err := stage.IsValid(); err != nil {
59+
return "", err
6160
}
6261

6362
if prr.Alpha == nil && prr.Beta == nil && prr.Stable == nil {
6463
return "", ErrPRRMilestonesAllEmpty
6564
}
6665

6766
switch stage {
68-
case "alpha":
67+
case AlphaStage:
6968
if prr.Alpha == nil {
7069
return "", ErrPRRMilestoneIsNil
7170
}
7271

7372
return prr.Alpha.Approver, nil
74-
case "beta":
73+
case BetaStage:
7574
if prr.Beta == nil {
7675
return "", ErrPRRMilestoneIsNil
7776
}
7877

7978
return prr.Beta.Approver, nil
80-
case "stable":
79+
case StableStage:
8180
if prr.Stable == nil {
8281
return "", ErrPRRMilestoneIsNil
8382
}

api/approval_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var (
4646
func TestPRRApproval_ApproverForStage(t *testing.T) {
4747
testcases := []struct {
4848
name string
49-
stage string
49+
stage api.Stage
5050
prr *api.PRRApproval
5151
wantApprover string
5252
wantErr bool

api/error.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
package api
1818

1919
import (
20-
"fmt"
21-
2220
"github.com/pkg/errors"
2321
)
2422

@@ -37,14 +35,3 @@ var (
3735
"an unknown error occurred while trying to determine a PRR approver",
3836
)
3937
)
40-
41-
// KEP errors
42-
func ErrKEPStageIsInvalid(stage string) error {
43-
return errors.New(
44-
fmt.Sprintf(
45-
"the specified stage (%s) should be one of the following: %v",
46-
stage,
47-
ValidStages,
48-
),
49-
)
50-
}

api/proposal.go

Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,66 @@ import (
3030
"k8s.io/enhancements/pkg/yaml"
3131
)
3232

33-
var ValidStages = []string{
34-
"alpha",
35-
"beta",
36-
"stable",
33+
type Stage string
34+
35+
const (
36+
AlphaStage Stage = "alpha"
37+
BetaStage Stage = "beta"
38+
StableStage Stage = "stable"
39+
)
40+
41+
var ValidStages = []Stage{
42+
AlphaStage,
43+
BetaStage,
44+
StableStage,
45+
}
46+
47+
func (s Stage) String() string {
48+
return string(s)
49+
}
50+
51+
func (s Stage) IsValid() error {
52+
for _, s2 := range ValidStages {
53+
if s == s2 {
54+
return nil
55+
}
56+
}
57+
return fmt.Errorf("invalid stage: %v, should be one of %v", s, ValidStages)
58+
}
59+
60+
type Status string
61+
62+
const (
63+
ProvisionalStatus Status = "provisional"
64+
ImplementableStatus Status = "implementable"
65+
ImplementedStatus Status = "implemented"
66+
DeferredStatus Status = "deferred"
67+
RejectedStatus Status = "rejected"
68+
WithdrawnStatus Status = "withdrawn"
69+
ReplacedStatus Status = "replaced"
70+
)
71+
72+
var ValidStatuses = []Status{
73+
ProvisionalStatus,
74+
ImplementableStatus,
75+
ImplementedStatus,
76+
DeferredStatus,
77+
RejectedStatus,
78+
WithdrawnStatus,
79+
ReplacedStatus,
80+
}
81+
82+
func (s Status) String() string {
83+
return string(s)
84+
}
85+
86+
func (s Status) IsValid() error {
87+
for _, s2 := range ValidStatuses {
88+
if s == s2 {
89+
return nil
90+
}
91+
}
92+
return fmt.Errorf("invalid status: %v, should be one of %v", s, ValidStatuses)
3793
}
3894

3995
type Proposals []*Proposal
@@ -59,12 +115,12 @@ type Proposal struct {
59115
Editor string `json:"editor" yaml:"editor,omitempty"`
60116
CreationDate string `json:"creationDate" yaml:"creation-date"`
61117
LastUpdated string `json:"lastUpdated" yaml:"last-updated"`
62-
Status string `json:"status" yaml:"status" validate:"required"`
118+
Status Status `json:"status" yaml:"status" validate:"required"`
63119
SeeAlso []string `json:"seeAlso" yaml:"see-also,omitempty"`
64120
Replaces []string `json:"replaces" yaml:"replaces,omitempty"`
65121
SupersededBy []string `json:"supersededBy" yaml:"superseded-by,omitempty"`
66122

67-
Stage string `json:"stage" yaml:"stage"`
123+
Stage Stage `json:"stage" yaml:"stage"`
68124
LatestMilestone string `json:"latestMilestone" yaml:"latest-milestone"`
69125
Milestone Milestone `json:"milestone" yaml:"milestone"`
70126

@@ -85,6 +141,18 @@ func (p *Proposal) IsMissingStage() bool {
85141
return p.Stage == ""
86142
}
87143

144+
type Milestone struct {
145+
Alpha string `json:"alpha" yaml:"alpha"`
146+
Beta string `json:"beta" yaml:"beta"`
147+
Stable string `json:"stable" yaml:"stable"`
148+
Deprecated string `json:"deprecated" yaml:"deprecated,omitempty"`
149+
Removed string `json:"removed" yaml:"removed,omitempty"`
150+
}
151+
152+
type FeatureGate struct {
153+
Name string `json:"name" yaml:"name"`
154+
Components []string `json:"components" yaml:"components"`
155+
}
88156
type KEPHandler Parser
89157

90158
// TODO(api): Make this a generic parser for all `Document` types
@@ -194,19 +262,6 @@ func (k *KEPHandler) Validate(p *Proposal) []error {
194262
return allErrs
195263
}
196264

197-
type Milestone struct {
198-
Alpha string `json:"alpha" yaml:"alpha"`
199-
Beta string `json:"beta" yaml:"beta"`
200-
Stable string `json:"stable" yaml:"stable"`
201-
Deprecated string `json:"deprecated" yaml:"deprecated,omitempty"`
202-
Removed string `json:"removed" yaml:"removed,omitempty"`
203-
}
204-
205-
type FeatureGate struct {
206-
Name string `json:"name" yaml:"name"`
207-
Components []string `json:"components" yaml:"components"`
208-
}
209-
210265
func hash(s string) string {
211266
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
212267
}

pkg/kepctl/commands/create.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/pkg/errors"
2121
"github.com/spf13/cobra"
2222

23+
"k8s.io/enhancements/api"
2324
"k8s.io/enhancements/pkg/proposal"
2425
"k8s.io/enhancements/pkg/repo"
2526
)
@@ -89,7 +90,7 @@ func addCreate(topLevel *cobra.Command) {
8990
&co.State,
9091
"state",
9192
"s",
92-
"provisional",
93+
api.ProvisionalStatus.String(),
9394
"KEP State",
9495
)
9596

pkg/kepval/approval.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func isPRRRequired(kep *api.Proposal) (required, missingMilestone, missingStage
104104
missingMilestone = kep.IsMissingMilestone()
105105
missingStage = kep.IsMissingStage()
106106

107-
if kep.Status != "implementable" {
107+
if kep.Status != api.ImplementableStatus {
108108
required = false
109109
return required, missingMilestone, missingStage, nil
110110
}

pkg/output/output.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ func DefaultPrintConfigs(names ...string) []PrintConfig {
210210

211211
return k.OwningSIG
212212
}},
213-
"Stage": {"Stage", func(k *api.Proposal) string { return k.Stage }},
214-
"Status": {"Status", func(k *api.Proposal) string { return k.Status }},
213+
"Stage": {"Stage", func(k *api.Proposal) string { return k.Stage.String() }},
214+
"Status": {"Status", func(k *api.Proposal) string { return k.Status.String() }},
215215
"Title": {"Title", func(k *api.Proposal) string {
216216
if k.PRNumber == "" {
217217
return k.Title

pkg/proposal/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func populateProposal(p *api.Proposal, opts *CreateOpts) {
124124
p.Name = opts.Name
125125

126126
if opts.State != "" {
127-
p.Status = opts.State
127+
p.Status = api.Status(opts.State)
128128
}
129129

130130
now := time.Now()

pkg/proposal/promote.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121

2222
"github.com/sirupsen/logrus"
23+
"k8s.io/enhancements/api"
2324
"k8s.io/enhancements/pkg/repo"
2425
)
2526

@@ -56,7 +57,7 @@ func Promote(opts *PromoteOpts) error {
5657
return fmt.Errorf("unable to load KEP for promotion: %s", err)
5758
}
5859

59-
p.Stage = opts.Stage
60+
p.Stage = api.Stage(opts.Stage)
6061
p.LatestMilestone = opts.Release
6162
p.LastUpdated = opts.Release
6263

pkg/repo/query.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ func (r *Repo) Query(opts *QueryOpts) ([]*api.Proposal, error) {
140140

141141
logrus.Debugf("current KEP: %v", k)
142142

143-
if len(opts.Status) > 0 && !allowedStatus[k.Status] {
143+
if len(opts.Status) > 0 && !allowedStatus[k.Status.String()] {
144144
continue
145145
}
146-
if len(opts.Stage) > 0 && !allowedStage[k.Stage] {
146+
if len(opts.Stage) > 0 && !allowedStage[k.Stage.String()] {
147147
continue
148148
}
149149
if len(opts.PRRApprover) > 0 && !atLeastOne(k.PRRApprovers, allowedPRR) {

0 commit comments

Comments
 (0)