Skip to content

Commit 523604d

Browse files
committed
pkg/repo: Debugging for query logic
Signed-off-by: Stephen Augustus <[email protected]>
1 parent 121f349 commit 523604d

File tree

6 files changed

+82
-20
lines changed

6 files changed

+82
-20
lines changed

cmd/kepctl/main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ limitations under the License.
1616

1717
package main
1818

19-
import "k8s.io/enhancements/pkg/kepctl/commands"
19+
import (
20+
"github.com/sirupsen/logrus"
21+
22+
"k8s.io/enhancements/pkg/kepctl/commands"
23+
)
2024

2125
func main() {
22-
commands.New().Execute()
26+
if err := commands.New().Execute(); err != nil {
27+
logrus.Fatalf("error during command execution: %v", err)
28+
}
2329
}

pkg/repo/output.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ var defaultConfig = map[string]printConfig{
7272
}
7373

7474
func DefaultPrintConfigs(names ...string) []PrintConfig {
75-
configs := make([]PrintConfig, 10)
75+
configs := make([]PrintConfig, 0, 10)
7676
for _, n := range names {
7777
// copy to allow it to be tweaked by the caller
7878
c := defaultConfig[n]
@@ -90,7 +90,7 @@ func (r *Repo) PrintTable(configs []PrintConfig, proposals []*api.Proposal) {
9090

9191
table := tablewriter.NewWriter(r.Out)
9292

93-
headers := make([]string, 10)
93+
headers := make([]string, 0, 10)
9494
for _, c := range configs {
9595
headers = append(headers, c.Title())
9696
}

pkg/repo/query.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"regexp"
2222

2323
"github.com/pkg/errors"
24+
"github.com/sirupsen/logrus"
2425

2526
"k8s.io/enhancements/api"
2627
)
@@ -113,11 +114,16 @@ func (r *Repo) Query(opts *QueryOpts) error {
113114
return errors.Wrapf(tokenErr, "setting GitHub token")
114115
}
115116

116-
allKEPs := make([]*api.Proposal, 10)
117+
allKEPs := make([]*api.Proposal, 0, 10)
117118
// load the KEPs for each listed SIG
118119
for _, sig := range opts.Groups {
119120
// KEPs in the local filesystem
120-
allKEPs = append(allKEPs, r.LoadLocalKEPs(sig)...)
121+
localKEPs, err := r.LoadLocalKEPs(sig)
122+
if err != nil {
123+
return errors.Wrap(err, "loading local KEPs")
124+
}
125+
126+
allKEPs = append(allKEPs, localKEPs...)
121127

122128
// Open PRs; existing KEPs with open PRs will be shown twice
123129
if opts.IncludePRs {
@@ -131,15 +137,23 @@ func (r *Repo) Query(opts *QueryOpts) error {
131137
}
132138
}
133139

140+
logrus.Debugf("all KEPs collected: %v", allKEPs)
141+
134142
// filter the KEPs by criteria
135143
allowedStatus := sliceToMap(opts.Status)
136144
allowedStage := sliceToMap(opts.Stage)
137145
allowedPRR := sliceToMap(opts.PRRApprover)
138146
allowedAuthor := sliceToMap(opts.Author)
139147
allowedApprover := sliceToMap(opts.Approver)
140148

141-
keps := make([]*api.Proposal, 10)
149+
results := make([]*api.Proposal, 0, 10)
142150
for _, k := range allKEPs {
151+
if k == nil {
152+
return errors.New("one of the KEPs in query was nil")
153+
}
154+
155+
logrus.Debugf("current KEP: %v", k)
156+
143157
if len(opts.Status) > 0 && !allowedStatus[k.Status] {
144158
continue
145159
}
@@ -156,7 +170,7 @@ func (r *Repo) Query(opts *QueryOpts) error {
156170
continue
157171
}
158172

159-
keps = append(keps, k)
173+
results = append(results, k)
160174
}
161175

162176
switch opts.Output {
@@ -171,12 +185,12 @@ func (r *Repo) Query(opts *QueryOpts) error {
171185
"Title",
172186
"Link",
173187
),
174-
keps,
188+
results,
175189
)
176190
case "yaml":
177-
r.PrintYAML(keps)
191+
r.PrintYAML(results)
178192
case "json":
179-
r.PrintJSON(keps)
193+
r.PrintJSON(results)
180194
default:
181195
// this check happens as a validation step in cobra as well
182196
// added it for additional verbosity

pkg/repo/repo.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
"github.com/google/go-github/v33/github"
3232
"github.com/pkg/errors"
33+
"github.com/sirupsen/logrus"
3334
"golang.org/x/oauth2"
3435
"gopkg.in/yaml.v3"
3536

@@ -195,15 +196,30 @@ func (r *Repo) findLocalKEPMeta(sig string) ([]string, error) {
195196
err := filepath.Walk(
196197
sigPath,
197198
func(path string, info os.FileInfo, err error) error {
199+
logrus.Debugf("processing filename %s", info.Name())
200+
198201
if err != nil {
199202
return err
200203
}
201204

205+
if ignore(info.Name(), "yaml", "md") {
206+
return nil
207+
}
208+
209+
// true if the file is a symlink
210+
if info.Mode()&os.ModeSymlink != 0 {
211+
// Assume symlink from old KEP location to new. The new location
212+
// will be processed separately, so no need to process it here.
213+
logrus.Debugf("%s is a symlink", info.Name())
214+
return nil
215+
}
216+
202217
if !info.Mode().IsRegular() {
203218
return nil
204219
}
205220

206221
if info.Name() == ProposalMetadataFilename {
222+
logrus.Debugf("adding %s as KEP metadata", info.Name())
207223
keps = append(keps, path)
208224
return filepath.SkipDir
209225
}
@@ -216,6 +232,7 @@ func (r *Repo) findLocalKEPMeta(sig string) ([]string, error) {
216232
return nil
217233
}
218234

235+
logrus.Debugf("adding %s as KEP metadata", info.Name())
219236
keps = append(keps, path)
220237
return nil
221238
},
@@ -224,33 +241,49 @@ func (r *Repo) findLocalKEPMeta(sig string) ([]string, error) {
224241
return keps, err
225242
}
226243

227-
func (r *Repo) LoadLocalKEPs(sig string) []*api.Proposal {
244+
func (r *Repo) LoadLocalKEPs(sig string) ([]*api.Proposal, error) {
228245
// KEPs in the local filesystem
229246
files, err := r.findLocalKEPMeta(sig)
230247
if err != nil {
231-
fmt.Fprintf(r.Err, "error searching for local KEPs from %s: %s\n", sig, err)
248+
return nil, errors.Wrapf(
249+
err,
250+
"searching for local KEPs from %s",
251+
sig,
252+
)
232253
}
233254

255+
logrus.Debugf("loading the following local KEPs: %v", files)
256+
234257
var allKEPs []*api.Proposal
235258
for _, k := range files {
236259
if filepath.Ext(k) == ".yaml" {
237260
kep, err := r.loadKEPFromYaml(k)
238261
if err != nil {
239-
fmt.Fprintf(r.Err, "error reading KEP %s: %s\n", k, err)
262+
return nil, errors.Wrapf(
263+
err,
264+
"reading KEP %s from yaml",
265+
k,
266+
)
240267
} else {
241268
allKEPs = append(allKEPs, kep)
242269
}
243270
} else {
244271
kep, err := r.loadKEPFromOldStyle(k)
245272
if err != nil {
246-
fmt.Fprintf(r.Err, "error reading KEP %s: %s\n", k, err)
273+
return nil, errors.Wrapf(
274+
err,
275+
"reading KEP %s from markdown",
276+
k,
277+
)
247278
} else {
248279
allKEPs = append(allKEPs, kep)
249280
}
250281
}
251282
}
252283

253-
return allKEPs
284+
logrus.Debugf("returning %d local KEPs", len(allKEPs))
285+
286+
return allKEPs, nil
254287
}
255288

256289
func (r *Repo) loadKEPPullRequests(sig string) ([]*api.Proposal, error) {

pkg/repo/repo_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ func TestFindLocalKEPs(t *testing.T) {
8484
require.Nil(t, repoErr)
8585

8686
for i, tc := range testcases {
87-
k := r.LoadLocalKEPs(tc.sig)
87+
k, err := r.LoadLocalKEPs(tc.sig)
88+
require.Nil(t, err)
89+
8890
if len(k) != len(tc.keps) {
8991
t.Errorf("Test case %d: expected %d but got %d", i, len(tc.keps), len(k))
9092
continue

pkg/repo/validate.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,41 @@ func (r *Repo) Validate() (
4444
err = filepath.Walk(
4545
kepDir,
4646
func(path string, info os.FileInfo, err error) error {
47+
logrus.Debugf("processing filename %s", info.Name())
48+
4749
if err != nil {
4850
return err
4951
}
5052

5153
if info.IsDir() {
54+
if info.Name() == PRRApprovalPathStub {
55+
return filepath.SkipDir
56+
}
57+
5258
return nil
5359
}
5460

55-
dir := filepath.Dir(path)
5661
// true if the file is a symlink
5762
if info.Mode()&os.ModeSymlink != 0 {
5863
// Assume symlink from old KEP location to new. The new location
5964
// will be processed separately, so no need to process it here.
6065
return nil
6166
}
6267

68+
dir := filepath.Dir(path)
69+
6370
metadataFilename := ProposalMetadataFilename
6471
metadataFilepath := filepath.Join(dir, metadataFilename)
6572
if _, err := os.Stat(metadataFilepath); err == nil {
66-
// There is kep metadata file in this directory, only that one should be processed.
73+
// There is KEP metadata file in this directory, only that one should be processed.
6774
if info.Name() == metadataFilename {
6875
files = append(files, metadataFilepath)
6976
}
7077

7178
return nil
7279
}
7380

74-
if ignore(info.Name()) {
81+
if ignore(info.Name(), "yaml", "md") {
7582
return nil
7683
}
7784

0 commit comments

Comments
 (0)