Skip to content

Commit e76ef66

Browse files
authored
Merge pull request kubernetes#2346 from justaugustus/api
api: Initial commit
2 parents 2b936f1 + bc7062d commit e76ef66

File tree

8 files changed

+118
-95
lines changed

8 files changed

+118
-95
lines changed

api/proposal.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
type Proposals []*Proposal
20+
21+
func (p *Proposals) AddProposal(proposal *Proposal) {
22+
*p = append(*p, proposal)
23+
}
24+
25+
type Proposal struct {
26+
ID string `json:"id"`
27+
PRNumber string `json:"prNumber,omitempty"`
28+
Name string `json:"name,omitempty"`
29+
30+
Title string `json:"title" yaml:"title"`
31+
Number string `json:"kep-number" yaml:"kep-number"`
32+
Authors []string `json:"authors" yaml:",flow"`
33+
OwningSIG string `json:"owningSig" yaml:"owning-sig"`
34+
ParticipatingSIGs []string `json:"participatingSigs" yaml:"participating-sigs,flow,omitempty"`
35+
Reviewers []string `json:"reviewers" yaml:",flow"`
36+
Approvers []string `json:"approvers" yaml:",flow"`
37+
PRRApprovers []string `json:"prrApprovers" yaml:"prr-approvers,flow"`
38+
Editor string `json:"editor" yaml:"editor,omitempty"`
39+
CreationDate string `json:"creationDate" yaml:"creation-date"`
40+
LastUpdated string `json:"lastUpdated" yaml:"last-updated"`
41+
Status string `json:"status" yaml:"status"`
42+
SeeAlso []string `json:"seeAlso" yaml:"see-also,omitempty"`
43+
Replaces []string `json:"replaces" yaml:"replaces,omitempty"`
44+
SupersededBy []string `json:"supersededBy" yaml:"superseded-by,omitempty"`
45+
46+
Stage string `json:"stage" yaml:"stage"`
47+
LatestMilestone string `json:"latestMilestone" yaml:"latest-milestone"`
48+
Milestone Milestone `json:"milestone" yaml:"milestone"`
49+
50+
FeatureGates []FeatureGate `json:"featureGates" yaml:"feature-gates"`
51+
DisableSupported bool `json:"disableSupported" yaml:"disable-supported"`
52+
Metrics []string `json:"metrics" yaml:"metrics"`
53+
54+
Filename string `json:"-" yaml:"-"`
55+
Error error `json:"-" yaml:"-"`
56+
Contents string `json:"markdown" yaml:"-"`
57+
}
58+
59+
type Milestone struct {
60+
Alpha string `json:"alpha" yaml:"alpha"`
61+
Beta string `json:"beta" yaml:"beta"`
62+
Stable string `json:"stable" yaml:"stable"`
63+
}
64+
65+
type FeatureGate struct {
66+
Name string `json:"name" yaml:"name"`
67+
Components []string `json:"components" yaml:"components"`
68+
}

cmd/kepify/main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"path/filepath"
2626
"strings"
2727

28+
"k8s.io/enhancements/api"
2829
"k8s.io/enhancements/pkg/kepval/keps"
2930
)
3031

@@ -104,8 +105,8 @@ func findMarkdownFiles(dirPath *string) ([]string, error) {
104105
return files, err
105106
}
106107

107-
func parseFiles(files []string) (keps.Proposals, error) {
108-
var proposals keps.Proposals
108+
func parseFiles(files []string) (api.Proposals, error) {
109+
var proposals api.Proposals
109110
for _, filename := range files {
110111
parser := &keps.Parser{}
111112
file, err := os.Open(filename)
@@ -124,7 +125,7 @@ func parseFiles(files []string) (keps.Proposals, error) {
124125
return proposals, nil
125126
}
126127

127-
func printJSONOutput(filePath string, proposals keps.Proposals) error {
128+
func printJSONOutput(filePath string, proposals api.Proposals) error {
128129
fmt.Printf("Output file: %s\n", filePath)
129130
file, err := os.Create(filePath)
130131
if err != nil {

pkg/kepctl/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"strings"
2626
"time"
2727

28-
"k8s.io/enhancements/pkg/kepval/keps"
28+
"k8s.io/enhancements/api"
2929
)
3030

3131
type CreateOpts struct {
@@ -82,7 +82,7 @@ func (c *Client) Create(opts CreateOpts) error {
8282
return nil
8383
}
8484

85-
func updateTemplate(t *keps.Proposal, opts CreateOpts) {
85+
func updateTemplate(t *api.Proposal, opts CreateOpts) {
8686
if opts.State != "" {
8787
t.Status = opts.State
8888
}
@@ -134,7 +134,7 @@ func updatePersonReference(names []string) []string {
134134
return persons
135135
}
136136

137-
func (c *Client) createKEP(kep *keps.Proposal, opts CreateOpts) error {
137+
func (c *Client) createKEP(kep *api.Proposal, opts CreateOpts) error {
138138

139139
fmt.Fprintf(c.Out, "Generating new KEP %s in %s ===>\n", opts.Name, opts.SIG)
140140

pkg/kepctl/create_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import (
2525

2626
"github.com/stretchr/testify/assert"
2727
"github.com/stretchr/testify/require"
28-
"k8s.io/enhancements/pkg/kepval/keps"
28+
29+
"k8s.io/enhancements/api"
2930
"sigs.k8s.io/yaml"
3031
)
3132

@@ -87,7 +88,7 @@ func TestWriteKep(t *testing.T) {
8788
c := newTestClient(t, repoPath)
8889
b, err := ioutil.ReadFile(tc.kepFile)
8990
require.NoError(t, err)
90-
var p keps.Proposal
91+
var p api.Proposal
9192
err = yaml.Unmarshal(b, &p)
9293
require.NoError(t, err)
9394
tc.opts.CommonArgs.RepoPath = repoPath

pkg/kepctl/kepctl.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"golang.org/x/oauth2"
3636
"gopkg.in/yaml.v2"
3737

38+
"k8s.io/enhancements/api"
3839
"k8s.io/enhancements/pkg/kepval/keps"
3940
"k8s.io/test-infra/prow/git"
4041
)
@@ -127,8 +128,8 @@ func (c *Client) SetGitHubToken(opts CommonArgs) error {
127128
// getKepTemplate reads the kep.yaml template from the local
128129
// (per c.RepoPath) k/enhancements, but this could be replaced with a
129130
// template via packr or fetched from Github?
130-
func (c *Client) getKepTemplate(repoPath string) (*keps.Proposal, error) {
131-
var p keps.Proposal
131+
func (c *Client) getKepTemplate(repoPath string) (*api.Proposal, error) {
132+
var p api.Proposal
132133
path := filepath.Join(repoPath, "keps", "NNNN-kep-template", "kep.yaml")
133134
b, err := ioutil.ReadFile(path)
134135
if err != nil {
@@ -148,7 +149,7 @@ func (c *Client) getReadmeTemplate(repoPath string) ([]byte, error) {
148149
return ioutil.ReadFile(path)
149150
}
150151

151-
func validateKEP(p *keps.Proposal) error {
152+
func validateKEP(p *api.Proposal) error {
152153
b, err := yaml.Marshal(p)
153154
if err != nil {
154155
return err
@@ -200,14 +201,14 @@ func findLocalKEPMeta(repoPath, sig string) ([]string, error) {
200201
return keps, err
201202
}
202203

203-
func (c *Client) loadLocalKEPs(repoPath, sig string) ([]*keps.Proposal) {
204+
func (c *Client) loadLocalKEPs(repoPath, sig string) []*api.Proposal {
204205
// KEPs in the local filesystem
205206
files, err := findLocalKEPMeta(repoPath, sig)
206207
if err != nil {
207208
fmt.Fprintf(c.Err, "error searching for local KEPs from %s: %s\n", sig, err)
208209
}
209210

210-
var allKEPs []*keps.Proposal
211+
var allKEPs []*api.Proposal
211212
for _, k := range files {
212213
if filepath.Ext(k) == ".yaml" {
213214
kep, err := c.loadKEPFromYaml(k)
@@ -228,7 +229,7 @@ func (c *Client) loadLocalKEPs(repoPath, sig string) ([]*keps.Proposal) {
228229
return allKEPs
229230
}
230231

231-
func (c *Client) loadKEPPullRequests(sig string) ([]*keps.Proposal, error) {
232+
func (c *Client) loadKEPPullRequests(sig string) ([]*api.Proposal, error) {
232233
var auth *http.Client
233234
ctx := context.Background()
234235
if c.Token != "" {
@@ -289,7 +290,7 @@ func (c *Client) loadKEPPullRequests(sig string) ([]*keps.Proposal, error) {
289290

290291
// read out each PR, and create a Proposal for each KEP that is
291292
// touched by a PR. This may result in multiple versions of the same KEP.
292-
var allKEPs []*keps.Proposal
293+
var allKEPs []*api.Proposal
293294
for _, pr := range kepPRs {
294295
files, _, err := gh.PullRequests.ListFiles(context.Background(), "kubernetes", "enhancements",
295296
pr.GetNumber(), &github.ListOptions{})
@@ -338,7 +339,7 @@ func (c *Client) loadKEPPullRequests(sig string) ([]*keps.Proposal, error) {
338339
return allKEPs, nil
339340
}
340341

341-
func (c *Client) readKEP(repoPath string, sig, name string) (*keps.Proposal, error) {
342+
func (c *Client) readKEP(repoPath string, sig, name string) (*api.Proposal, error) {
342343
kepPath := filepath.Join(
343344
repoPath,
344345
"keps",
@@ -361,12 +362,12 @@ func (c *Client) readKEP(repoPath string, sig, name string) (*keps.Proposal, err
361362
return c.loadKEPFromOldStyle(kepPath)
362363
}
363364

364-
func (c *Client) loadKEPFromYaml(kepPath string) (*keps.Proposal, error) {
365+
func (c *Client) loadKEPFromYaml(kepPath string) (*api.Proposal, error) {
365366
b, err := ioutil.ReadFile(kepPath)
366367
if err != nil {
367368
return nil, fmt.Errorf("unable to read KEP metadata: %s", err)
368369
}
369-
var p keps.Proposal
370+
var p api.Proposal
370371
err = yaml.Unmarshal(b, &p)
371372
if err != nil {
372373
return nil, fmt.Errorf("unable to load KEP metadata: %s", err)
@@ -375,7 +376,7 @@ func (c *Client) loadKEPFromYaml(kepPath string) (*keps.Proposal, error) {
375376
return &p, nil
376377
}
377378

378-
func (c *Client) loadKEPFromOldStyle(kepPath string) (*keps.Proposal, error) {
379+
func (c *Client) loadKEPFromOldStyle(kepPath string) (*api.Proposal, error) {
379380
b, err := ioutil.ReadFile(kepPath)
380381
if err != nil {
381382
return nil, fmt.Errorf("no kep.yaml, but failed to read as old-style KEP: %s", err)
@@ -391,7 +392,7 @@ func (c *Client) loadKEPFromOldStyle(kepPath string) (*keps.Proposal, error) {
391392
return kep, nil
392393
}
393394

394-
func (c *Client) writeKEP(kep *keps.Proposal, opts CommonArgs) error {
395+
func (c *Client) writeKEP(kep *api.Proposal, opts CommonArgs) error {
395396
path, err := c.findEnhancementsRepo(opts)
396397
if err != nil {
397398
return fmt.Errorf("unable to write KEP: %s", err)
@@ -417,39 +418,39 @@ func (c *Client) writeKEP(kep *keps.Proposal, opts CommonArgs) error {
417418

418419
type PrintConfig interface {
419420
Title() string
420-
Value(*keps.Proposal) string
421+
Value(*api.Proposal) string
421422
}
422423

423424
type printConfig struct {
424425
title string
425-
valueFunc func(*keps.Proposal) string
426+
valueFunc func(*api.Proposal) string
426427
}
427428

428429
func (p *printConfig) Title() string { return p.title }
429-
func (p *printConfig) Value(k *keps.Proposal) string {
430+
func (p *printConfig) Value(k *api.Proposal) string {
430431
return p.valueFunc(k)
431432
}
432433

433434
var defaultConfig = map[string]printConfig{
434-
"Authors": {"Authors", func(k *keps.Proposal) string { return strings.Join(k.Authors, ", ") }},
435-
"LastUpdated": {"Updated", func(k *keps.Proposal) string { return k.LastUpdated }},
436-
"SIG": {"SIG", func(k *keps.Proposal) string {
435+
"Authors": {"Authors", func(k *api.Proposal) string { return strings.Join(k.Authors, ", ") }},
436+
"LastUpdated": {"Updated", func(k *api.Proposal) string { return k.LastUpdated }},
437+
"SIG": {"SIG", func(k *api.Proposal) string {
437438
if strings.HasPrefix(k.OwningSIG, "sig-") {
438439
return k.OwningSIG[4:]
439440
} else {
440441
return k.OwningSIG
441442
}
442443
}},
443-
"Stage": {"Stage", func(k *keps.Proposal) string { return k.Stage }},
444-
"Status": {"Status", func(k *keps.Proposal) string { return k.Status }},
445-
"Title": {"Title", func(k *keps.Proposal) string {
444+
"Stage": {"Stage", func(k *api.Proposal) string { return k.Stage }},
445+
"Status": {"Status", func(k *api.Proposal) string { return k.Status }},
446+
"Title": {"Title", func(k *api.Proposal) string {
446447
if k.PRNumber == "" {
447448
return k.Title
448449
} else {
449450
return "PR#" + k.PRNumber + " - " + k.Title
450451
}
451452
}},
452-
"Link": {"Link", func(k *keps.Proposal) string {
453+
"Link": {"Link", func(k *api.Proposal) string {
453454
if k.PRNumber == "" {
454455
return "https://git.k8s.io/enhancements/keps/" + k.OwningSIG + "/" + k.Name
455456
} else {
@@ -468,7 +469,7 @@ func DefaultPrintConfigs(names ...string) []PrintConfig {
468469
return configs
469470
}
470471

471-
func (c *Client) PrintTable(configs []PrintConfig, proposals []*keps.Proposal) {
472+
func (c *Client) PrintTable(configs []PrintConfig, proposals []*api.Proposal) {
472473
if len(configs) == 0 {
473474
return
474475
}
@@ -493,7 +494,7 @@ func (c *Client) PrintTable(configs []PrintConfig, proposals []*keps.Proposal) {
493494
}
494495

495496
// PrintYAML outputs keps array as YAML to c.Out
496-
func (c *Client) PrintYAML(proposals []*keps.Proposal) {
497+
func (c *Client) PrintYAML(proposals []*api.Proposal) {
497498
data, err := yaml.Marshal(proposals)
498499
if err != nil {
499500
fmt.Fprintf(c.Err, "error printing keps as YAML: %s", err)
@@ -504,7 +505,7 @@ func (c *Client) PrintYAML(proposals []*keps.Proposal) {
504505
}
505506

506507
// PrintJSON outputs keps array as YAML to c.Out
507-
func (c *Client) PrintJSON(proposals []*keps.Proposal) {
508+
func (c *Client) PrintJSON(proposals []*api.Proposal) {
508509
data, err := json.Marshal(proposals)
509510
if err != nil {
510511
fmt.Fprintf(c.Err, "error printing keps as JSON: %s", err)

pkg/kepctl/kepctl_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import (
2424
"github.com/stretchr/testify/assert"
2525
"github.com/stretchr/testify/require"
2626
"gopkg.in/yaml.v2"
27-
"k8s.io/enhancements/pkg/kepval/keps"
27+
28+
"k8s.io/enhancements/api"
2829
)
2930

3031
func TestValidate(t *testing.T) {
@@ -49,7 +50,7 @@ func TestValidate(t *testing.T) {
4950
t.Run(tc.name, func(t *testing.T) {
5051
b, err := ioutil.ReadFile(tc.file)
5152
require.NoError(t, err)
52-
var p keps.Proposal
53+
var p api.Proposal
5354
err = yaml.Unmarshal(b, &p)
5455
require.NoError(t, err)
5556
err = validateKEP(&p)

pkg/kepctl/query.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
"github.com/pkg/errors"
2424

25-
"k8s.io/enhancements/pkg/kepval/keps"
25+
"k8s.io/enhancements/api"
2626
"k8s.io/enhancements/pkg/kepval/util"
2727
)
2828

@@ -101,7 +101,7 @@ func (c *Client) Query(opts QueryOpts) error {
101101

102102
c.SetGitHubToken(opts.CommonArgs)
103103

104-
var allKEPs []*keps.Proposal
104+
var allKEPs []*api.Proposal
105105
// load the KEPs for each listed SIG
106106
for _, sig := range opts.SIG {
107107
// KEPs in the local filesystem
@@ -126,7 +126,7 @@ func (c *Client) Query(opts QueryOpts) error {
126126
allowedAuthor := sliceToMap(opts.Author)
127127
allowedApprover := sliceToMap(opts.Approver)
128128

129-
var keep []*keps.Proposal
129+
var keps []*api.Proposal
130130
for _, k := range allKEPs {
131131
if len(opts.Status) > 0 && !allowedStatus[k.Status] {
132132
continue
@@ -143,16 +143,16 @@ func (c *Client) Query(opts QueryOpts) error {
143143
if len(opts.Approver) > 0 && !atLeastOne(k.Approvers, allowedApprover) {
144144
continue
145145
}
146-
keep = append(keep, k)
146+
keps = append(keps, k)
147147
}
148148

149149
switch opts.Output {
150150
case "table":
151-
c.PrintTable(DefaultPrintConfigs("LastUpdated", "Stage", "Status", "SIG", "Authors", "Title", "Link"), keep)
151+
c.PrintTable(DefaultPrintConfigs("LastUpdated", "Stage", "Status", "SIG", "Authors", "Title", "Link"), keps)
152152
case "yaml":
153-
c.PrintYAML(keep)
153+
c.PrintYAML(keps)
154154
case "json":
155-
c.PrintJSON(keep)
155+
c.PrintJSON(keps)
156156
default:
157157
// this check happens as a validation step in cobra as well
158158
// added it for additional verbosity

0 commit comments

Comments
 (0)