Skip to content

Commit e5ed50e

Browse files
authored
Merge pull request kubernetes#2010 from johnbelamaric/kepctl-nested-dirs
kepctl: fix query when KEPs are nested in directories
2 parents 5739c63 + 48dd8b3 commit e5ed50e

File tree

4 files changed

+70
-41
lines changed

4 files changed

+70
-41
lines changed

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,7 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
10601060
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
10611061
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
10621062
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
1063+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
10631064
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
10641065
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
10651066
github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=

pkg/kepctl/kepctl.go

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,19 @@ func validateKEP(p *keps.Proposal) error {
162162
return nil
163163
}
164164

165-
func findLocalKEPs(repoPath string, sig string) ([]string, error) {
165+
func findLocalKEPMeta(repoPath, sig string) ([]string, error) {
166166
rootPath := filepath.Join(
167167
repoPath,
168168
"keps",
169169
sig)
170170

171171
keps := []string{}
172+
173+
// if the sig doesn't have a dir, it has no KEPs
174+
if _, err := os.Stat(rootPath); os.IsNotExist(err) {
175+
return keps, nil
176+
}
177+
172178
err := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
173179
if err != nil {
174180
return err
@@ -177,7 +183,7 @@ func findLocalKEPs(repoPath string, sig string) ([]string, error) {
177183
return nil
178184
}
179185
if info.Name() == "kep.yaml" {
180-
keps = append(keps, filepath.Base(filepath.Dir(path)))
186+
keps = append(keps, path)
181187
return filepath.SkipDir
182188
}
183189
if filepath.Ext(path) != ".md" {
@@ -186,14 +192,42 @@ func findLocalKEPs(repoPath string, sig string) ([]string, error) {
186192
if info.Name() == "README.md" {
187193
return nil
188194
}
189-
keps = append(keps, info.Name()[0:len(info.Name())-3])
195+
keps = append(keps, path)
190196
return nil
191197
})
192198

193199
return keps, err
194200
}
195201

196-
func (c *Client) findKEPPullRequests(sig string) ([]*keps.Proposal, error) {
202+
func (c *Client) loadLocalKEPs(repoPath, sig string) ([]*keps.Proposal) {
203+
// KEPs in the local filesystem
204+
files, err := findLocalKEPMeta(repoPath, sig)
205+
if err != nil {
206+
fmt.Fprintf(c.Err, "error searching for local KEPs from %s: %s\n", sig, err)
207+
}
208+
209+
var allKEPs []*keps.Proposal
210+
for _, k := range files {
211+
if filepath.Ext(k) == ".yaml" {
212+
kep, err := c.loadKEPFromYaml(k)
213+
if err != nil {
214+
fmt.Fprintf(c.Err, "error reading KEP %s: %s\n", k, err)
215+
} else {
216+
allKEPs = append(allKEPs, kep)
217+
}
218+
} else {
219+
kep, err := c.loadKEPFromOldStyle(k)
220+
if err != nil {
221+
fmt.Fprintf(c.Err, "error reading KEP %s: %s\n", k, err)
222+
} else {
223+
allKEPs = append(allKEPs, kep)
224+
}
225+
}
226+
}
227+
return allKEPs
228+
}
229+
230+
func (c *Client) loadKEPPullRequests(sig string) ([]*keps.Proposal, error) {
197231
var auth *http.Client
198232
ctx := context.Background()
199233
if c.Token != "" {
@@ -301,19 +335,10 @@ func (c *Client) readKEP(repoPath string, sig, name string) (*keps.Proposal, err
301335
sig,
302336
name,
303337
"kep.yaml")
338+
304339
_, err := os.Stat(kepPath)
305340
if err == nil {
306-
b, err := ioutil.ReadFile(kepPath)
307-
if err != nil {
308-
return nil, fmt.Errorf("unable to read KEP metadata: %s", err)
309-
}
310-
var p keps.Proposal
311-
err = yaml.Unmarshal(b, &p)
312-
if err != nil {
313-
return nil, fmt.Errorf("unable to load KEP metadata: %s", err)
314-
}
315-
p.Name = name
316-
return &p, nil
341+
return c.loadKEPFromYaml(kepPath)
317342
}
318343

319344
// No kep.yaml, treat as old-style KEP
@@ -322,6 +347,25 @@ func (c *Client) readKEP(repoPath string, sig, name string) (*keps.Proposal, err
322347
"keps",
323348
sig,
324349
name) + ".md"
350+
351+
return c.loadKEPFromOldStyle(kepPath)
352+
}
353+
354+
func (c *Client) loadKEPFromYaml(kepPath string) (*keps.Proposal, error) {
355+
b, err := ioutil.ReadFile(kepPath)
356+
if err != nil {
357+
return nil, fmt.Errorf("unable to read KEP metadata: %s", err)
358+
}
359+
var p keps.Proposal
360+
err = yaml.Unmarshal(b, &p)
361+
if err != nil {
362+
return nil, fmt.Errorf("unable to load KEP metadata: %s", err)
363+
}
364+
p.Name = filepath.Base(filepath.Dir(kepPath))
365+
return &p, nil
366+
}
367+
368+
func (c *Client) loadKEPFromOldStyle(kepPath string) (*keps.Proposal, error) {
325369
b, err := ioutil.ReadFile(kepPath)
326370
if err != nil {
327371
return nil, fmt.Errorf("no kep.yaml, but failed to read as old-style KEP: %s", err)
@@ -333,7 +377,7 @@ func (c *Client) readKEP(repoPath string, sig, name string) (*keps.Proposal, err
333377
if kep.Error != nil {
334378
return nil, fmt.Errorf("kep is invalid: %s", kep.Error)
335379
}
336-
kep.Name = name + ".md"
380+
kep.Name = filepath.Base(kepPath)
337381
return kep, nil
338382
}
339383

pkg/kepctl/kepctl_test.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestValidate(t *testing.T) {
4141
{
4242
name: "invalid kep fails valdiate for owning-sig",
4343
file: "testdata/invalid-kep.yaml",
44-
err: fmt.Errorf(`kep is invalid: error validating KEP metadata: "owning-sig" must be one of (committee-code-of-conduct,committee-product-security,committee-steering,sig-api-machinery,sig-apps,sig-architecture,sig-auth,sig-autoscaling,sig-cli,sig-cloud-provider,sig-cluster-lifecycle,sig-contributor-experience,sig-docs,sig-instrumentation,sig-multicluster,sig-network,sig-node,sig-release,sig-scalability,sig-scheduling,sig-service-catalog,sig-storage,sig-testing,sig-ui,sig-usability,sig-windows,ug-big-data,ug-vmware-users,wg-api-expression,wg-component-standard,wg-data-protection,wg-iot-edge,wg-k8s-infra,wg-lts,wg-machine-learning,wg-multitenancy,wg-naming,wg-policy,wg-security-audit) but it is a string: sig-awesome`),
44+
err: fmt.Errorf(`kep is invalid: error validating KEP metadata: "owning-sig" must be one of (committee-code-of-conduct,committee-product-security,committee-steering,sig-api-machinery,sig-apps,sig-architecture,sig-auth,sig-autoscaling,sig-cli,sig-cloud-provider,sig-cluster-lifecycle,sig-contributor-experience,sig-docs,sig-instrumentation,sig-multicluster,sig-network,sig-node,sig-release,sig-scalability,sig-scheduling,sig-security,sig-service-catalog,sig-storage,sig-testing,sig-ui,sig-usability,sig-windows,ug-big-data,ug-vmware-users,wg-api-expression,wg-component-standard,wg-data-protection,wg-iot-edge,wg-k8s-infra,wg-lts,wg-multitenancy,wg-naming,wg-policy,wg-security-audit) but it is a string: sig-awesome`),
4545
},
4646
}
4747

@@ -70,29 +70,25 @@ func TestFindLocalKEPs(t *testing.T) {
7070
}{
7171
{
7272
"sig-architecture",
73-
[]string{"123-newstyle", "20200115-kubectl-diff"},
73+
[]string{"123-newstyle", "20200115-kubectl-diff.md"},
7474
},
7575
{
7676
"sig-sig",
7777
[]string{},
7878
},
7979
}
8080

81+
c, _ := New("testdata")
8182
for i, tc := range testcases {
82-
k, err := findLocalKEPs("testdata", tc.sig)
83-
if err != nil {
84-
t.Errorf("Test case %d: expected no error but got %s", i, err)
85-
continue
86-
}
83+
k := c.loadLocalKEPs("testdata", tc.sig)
8784
if len(k) != len(tc.keps) {
88-
t.Errorf("Test case %d: expected %s but got %s", i, tc.keps, k)
85+
t.Errorf("Test case %d: expected %d but got %d", i, len(tc.keps), len(k))
8986
continue
9087
}
9188
for j, kn := range k {
92-
if kn != tc.keps[j] {
93-
t.Errorf("Test case %d: expected %s but got %s", i, tc.keps[j], kn)
89+
if kn.Name != tc.keps[j] {
90+
t.Errorf("Test case %d: expected %s but got %s", i, tc.keps[j], kn.Name)
9491
}
9592
}
9693
}
97-
findLocalKEPs("testdata", "sig-architecture")
9894
}

pkg/kepctl/query.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,11 @@ func (c *Client) Query(opts QueryOpts) error {
6666
// load the KEPs for each listed SIG
6767
for _, sig := range opts.SIG {
6868
// KEPs in the local filesystem
69-
names, err := findLocalKEPs(repoPath, sig)
70-
if err != nil {
71-
fmt.Fprintf(c.Err, "error searching for local KEPs from %s: %s\n", sig, err)
72-
}
73-
74-
for _, k := range names {
75-
kep, err := c.readKEP(repoPath, sig, k)
76-
if err != nil {
77-
fmt.Fprintf(c.Err, "error reading KEP %s: %s\n", k, err)
78-
} else {
79-
allKEPs = append(allKEPs, kep)
80-
}
81-
}
69+
allKEPs = append(allKEPs, c.loadLocalKEPs(repoPath, sig)...)
8270

8371
// Open PRs; existing KEPs with open PRs will be shown twice
8472
if opts.IncludePRs {
85-
prKeps, err := c.findKEPPullRequests(sig)
73+
prKeps, err := c.loadKEPPullRequests(sig)
8674
if err != nil {
8775
fmt.Fprintf(c.Err, "error searching for KEP PRs from %s: %s\n", sig, err)
8876
}

0 commit comments

Comments
 (0)