Skip to content

Commit 561c1d9

Browse files
authored
Merge pull request kubernetes#2929 from spiffxp/fix-kepctl-remote-load
kepctl query: fix remote KEP loading
2 parents 6944420 + 8165794 commit 561c1d9

File tree

3 files changed

+111
-86
lines changed

3 files changed

+111
-86
lines changed

pkg/proposal/promote.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func Promote(opts *PromoteOpts) error {
5151

5252
logrus.Infof("Updating KEP %s/%s", opts.SIG, opts.Name)
5353

54-
p, err := r.ReadKEP(opts.SIG, opts.Name)
54+
p, err := r.LoadLocalKEP(opts.SIG, opts.Name)
5555
if err != nil {
5656
return fmt.Errorf("unable to load KEP for promotion: %s", err)
5757
}

pkg/repo/query.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (r *Repo) Query(opts *QueryOpts) ([]*api.Proposal, error) {
9999
}
100100
}
101101

102-
allKEPs := make([]*api.Proposal, 0, 10)
102+
allKEPs := []*api.Proposal{}
103103
// load the KEPs for each listed SIG
104104
for _, sig := range opts.Groups {
105105
// KEPs in the local filesystem
@@ -112,7 +112,7 @@ func (r *Repo) Query(opts *QueryOpts) ([]*api.Proposal, error) {
112112

113113
// Open PRs; existing KEPs with open PRs will be shown twice
114114
if opts.IncludePRs {
115-
prKeps, err := r.loadKEPPullRequests(sig)
115+
prKeps, err := r.LoadPullRequestKEPs(sig)
116116
if err != nil {
117117
logrus.Warnf("error searching for KEP PRs from %s: %s", sig, err)
118118
}
@@ -132,7 +132,7 @@ func (r *Repo) Query(opts *QueryOpts) ([]*api.Proposal, error) {
132132
allowedApprover := sliceToMap(opts.Approver)
133133
allowedParticipant := sliceToMap(opts.Participant)
134134

135-
results := make([]*api.Proposal, 0, 10)
135+
results := []*api.Proposal{}
136136
for _, k := range allKEPs {
137137
if k == nil {
138138
return nil, errors.New("one of the KEPs in query was nil")

pkg/repo/repo.go

Lines changed: 107 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ type Repo struct {
7070

7171
// Templates
7272
ProposalTemplate []byte
73+
74+
// Temporary caches
75+
// a local git clone of remoteOrg/remoteRepo
76+
gitRepo *git.Repo
77+
// all open pull requests for remoteOrg/remoteRepo
78+
allPRs []*github.PullRequest
7379
}
7480

7581
// New returns a new repo client configured to use the the normal os.Stdxxx and Filesystem
@@ -179,10 +185,8 @@ func (r *Repo) SetGitHubToken(tokenFile string) error {
179185
if err != nil {
180186
return err
181187
}
182-
183188
r.Token = strings.Trim(string(token), "\n\r")
184189
}
185-
186190
return nil
187191
}
188192

@@ -199,10 +203,7 @@ func (r *Repo) getProposalTemplate() ([]byte, error) {
199203
}
200204

201205
func (r *Repo) findLocalKEPMeta(sig string) ([]string, error) {
202-
sigPath := filepath.Join(
203-
r.ProposalPath,
204-
sig,
205-
)
206+
sigPath := filepath.Join(r.ProposalPath, sig)
206207

207208
keps := []string{}
208209

@@ -234,6 +235,10 @@ func (r *Repo) findLocalKEPMeta(sig string) ([]string, error) {
234235

235236
if info.Name() == ProposalMetadataFilename {
236237
logrus.Debugf("adding %s as KEP metadata", info.Name())
238+
path, err = filepath.Rel(r.BasePath, path)
239+
if err != nil {
240+
return err
241+
}
237242
keps = append(keps, path)
238243
return filepath.SkipDir
239244
}
@@ -263,65 +268,88 @@ func (r *Repo) LoadLocalKEPs(sig string) ([]*api.Proposal, error) {
263268
logrus.Debugf("loading the following local KEPs: %v", files)
264269

265270
var allKEPs []*api.Proposal
266-
for _, k := range files {
267-
if filepath.Ext(k) == ".yaml" {
268-
kep, err := r.loadKEPFromYaml(k)
269-
if err != nil {
270-
return nil, errors.Wrapf(
271-
err,
272-
"reading KEP %s from yaml",
273-
k,
274-
)
275-
}
276-
277-
allKEPs = append(allKEPs, kep)
271+
for _, kepYamlPath := range files {
272+
kep, err := r.loadKEPFromYaml(r.BasePath, kepYamlPath)
273+
if err != nil {
274+
return nil, errors.Wrapf(
275+
err,
276+
"reading KEP %s from yaml",
277+
kepYamlPath,
278+
)
278279
}
280+
281+
allKEPs = append(allKEPs, kep)
279282
}
280283

281284
logrus.Debugf("returning %d local KEPs", len(allKEPs))
282285

283286
return allKEPs, nil
284287
}
285288

286-
func (r *Repo) loadKEPPullRequests(sig string) ([]*api.Proposal, error) {
289+
func (r *Repo) LoadLocalKEP(sig, name string) (*api.Proposal, error) {
290+
kepPath := filepath.Join(
291+
ProposalPathStub,
292+
sig,
293+
name,
294+
ProposalMetadataFilename,
295+
)
296+
297+
_, err := os.Stat(kepPath)
298+
if err != nil {
299+
return nil, errors.Wrapf(err, "getting file info for %s", kepPath)
300+
}
301+
302+
return r.loadKEPFromYaml(r.BasePath, kepPath)
303+
}
304+
305+
func (r *Repo) LoadPullRequestKEPs(sig string) ([]*api.Proposal, error) {
306+
// Initialize github client
307+
logrus.Debugf("Initializing github client to load PRs for sig: %v", sig)
287308
var auth *http.Client
288309
ctx := context.Background()
289310
if r.Token != "" {
290311
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: r.Token})
291312
auth = oauth2.NewClient(ctx, ts)
292313
}
293-
294314
gh := github.NewClient(auth)
295-
allPulls := []*github.PullRequest{}
296-
opt := &github.PullRequestListOptions{
297-
ListOptions: github.ListOptions{
298-
PerPage: 100,
299-
},
300-
}
301315

302-
for {
303-
pulls, resp, err := gh.PullRequests.List(
304-
ctx,
305-
remoteOrg,
306-
remoteRepo,
307-
opt,
308-
)
309-
if err != nil {
310-
return nil, err
311-
}
316+
// Fetch list of all PRs if none exists
317+
if r.allPRs == nil {
318+
logrus.Debugf("Initializing list of all PRs for %v/%v", remoteOrg, remoteRepo)
319+
r.allPRs = []*github.PullRequest{}
312320

313-
allPulls = append(allPulls, pulls...)
314-
if resp.NextPage == 0 {
315-
break
321+
opt := &github.PullRequestListOptions{
322+
ListOptions: github.ListOptions{
323+
PerPage: 100,
324+
},
316325
}
317326

318-
opt.Page = resp.NextPage
327+
for {
328+
pulls, resp, err := gh.PullRequests.List(
329+
ctx,
330+
remoteOrg,
331+
remoteRepo,
332+
opt,
333+
)
334+
if err != nil {
335+
return nil, err
336+
}
337+
338+
r.allPRs = append(r.allPRs, pulls...)
339+
if resp.NextPage == 0 {
340+
break
341+
}
342+
343+
opt.Page = resp.NextPage
344+
}
319345
}
320346

321-
kepPRs := make([]*github.PullRequest, 10)
322-
for _, pr := range allPulls {
347+
// Find KEP PRs for the given sig
348+
kepPRs := []*github.PullRequest{}
349+
sigLabel := strings.Replace(sig, "-", "/", 1)
350+
logrus.Debugf("Searching list of %v PRs for %v/%v with labels: [%v, %v]", len(r.allPRs), remoteOrg, remoteRepo, sigLabel, proposalLabel)
351+
for _, pr := range r.allPRs {
323352
foundKind, foundSIG := false, false
324-
sigLabel := strings.Replace(sig, "-", "/", 1)
325353

326354
for _, l := range pr.Labels {
327355
if *l.Name == proposalLabel {
@@ -337,31 +365,37 @@ func (r *Repo) loadKEPPullRequests(sig string) ([]*api.Proposal, error) {
337365
continue
338366
}
339367

368+
logrus.Debugf("Found #%v", pr.GetHTMLURL())
369+
340370
kepPRs = append(kepPRs, pr)
341371
}
372+
logrus.Debugf("Found %v PRs for %v/%v with labels: [%v, %v]", len(kepPRs), remoteOrg, remoteRepo, sigLabel, proposalLabel)
342373

343374
if len(kepPRs) == 0 {
344375
return nil, nil
345376
}
346377

347-
// Pull a temporary clone of the repo
348-
g, err := git.NewClient()
349-
if err != nil {
350-
return nil, err
351-
}
378+
// Pull a temporary clone of the repo if none already exists
379+
if r.gitRepo == nil {
380+
g, err := git.NewClient()
381+
if err != nil {
382+
return nil, err
383+
}
352384

353-
g.SetCredentials("", func() []byte { return []byte{} })
354-
g.SetRemote(krgh.GitHubURL)
385+
g.SetCredentials("", func() []byte { return []byte{} })
386+
g.SetRemote(krgh.GitHubURL)
355387

356-
repo, err := g.Clone(remoteOrg, remoteRepo)
357-
if err != nil {
358-
return nil, err
388+
r.gitRepo, err = g.Clone(remoteOrg, remoteRepo)
389+
if err != nil {
390+
return nil, err
391+
}
359392
}
360393

361394
// read out each PR, and create a Proposal for each KEP that is
362395
// touched by a PR. This may result in multiple versions of the same KEP.
363396
var allKEPs []*api.Proposal
364397
for _, pr := range kepPRs {
398+
logrus.Debugf("Getting list of files for %v", pr.GetHTMLURL())
365399
files, _, err := gh.PullRequests.ListFiles(
366400
context.Background(),
367401
remoteOrg,
@@ -395,14 +429,20 @@ func (r *Repo) loadKEPPullRequests(sig string) ([]*api.Proposal, error) {
395429
continue
396430
}
397431

398-
err = repo.CheckoutPullRequest(pr.GetNumber())
432+
err = r.gitRepo.CheckoutPullRequest(pr.GetNumber())
399433
if err != nil {
400434
return nil, err
401435
}
402436

403437
// read all these KEPs
404438
for k := range kepNames {
405-
kep, err := r.ReadKEP(sig, k)
439+
kepPath := filepath.Join(
440+
ProposalPathStub,
441+
sig,
442+
k,
443+
ProposalMetadataFilename,
444+
)
445+
kep, err := r.loadKEPFromYaml(r.gitRepo.Directory(), kepPath)
406446
if err != nil {
407447
logrus.Warnf("error reading KEP %v: %v", k, err)
408448
} else {
@@ -415,26 +455,13 @@ func (r *Repo) loadKEPPullRequests(sig string) ([]*api.Proposal, error) {
415455
return allKEPs, nil
416456
}
417457

418-
func (r *Repo) ReadKEP(sig, name string) (*api.Proposal, error) {
419-
kepPath := filepath.Join(
420-
r.ProposalPath,
421-
sig,
422-
name,
423-
ProposalMetadataFilename,
424-
)
425-
426-
_, err := os.Stat(kepPath)
427-
if err != nil {
428-
return nil, errors.Wrapf(err, "getting file info for %s", kepPath)
429-
}
430-
431-
return r.loadKEPFromYaml(kepPath)
432-
}
433-
434-
func (r *Repo) loadKEPFromYaml(kepPath string) (*api.Proposal, error) {
435-
b, err := ioutil.ReadFile(kepPath)
458+
// loadKEPFromYaml will return a Proposal from a kep.yaml at the given kepPath
459+
// within the given repoPath, or an error if the Proposal is invalid
460+
func (r *Repo) loadKEPFromYaml(repoPath, kepPath string) (*api.Proposal, error) {
461+
fullKEPPath := filepath.Join(repoPath, kepPath)
462+
b, err := ioutil.ReadFile(fullKEPPath)
436463
if err != nil {
437-
return nil, fmt.Errorf("unable to read KEP metadata: %s", err)
464+
return nil, fmt.Errorf("unable to read KEP metadata for %s: %w", fullKEPPath, err)
438465
}
439466

440467
var p api.Proposal
@@ -444,24 +471,22 @@ func (r *Repo) loadKEPFromYaml(kepPath string) (*api.Proposal, error) {
444471
}
445472

446473
p.Name = filepath.Base(filepath.Dir(kepPath))
474+
prrApprovalPath := filepath.Join(repoPath, ProposalPathStub, PRRApprovalPathStub)
447475

448476
// Read the PRR approval file and add any listed PRR approvers in there
449477
// to the PRR approvers list in the KEP. this is a hack while we transition
450478
// away from PRR approvers listed in kep.yaml
451479
handler := r.PRRHandler
452-
err = kepval.ValidatePRR(&p, handler, r.PRRApprovalPath)
480+
err = kepval.ValidatePRR(&p, handler, prrApprovalPath)
453481
if err != nil {
454482
logrus.Errorf(
455483
"%v",
456484
errors.Wrapf(err, "validating PRR for %s", p.Name),
457485
)
458486
} else {
459-
prrPath := filepath.Dir(kepPath)
460-
prrPath = filepath.Dir(prrPath)
461-
sig := filepath.Base(prrPath)
462-
prrPath = filepath.Join(
463-
filepath.Dir(prrPath),
464-
PRRApprovalPathStub,
487+
sig := filepath.Base(filepath.Dir(filepath.Dir(kepPath)))
488+
prrPath := filepath.Join(
489+
prrApprovalPath,
465490
sig,
466491
p.Number+".yaml",
467492
)
@@ -484,7 +509,7 @@ func (r *Repo) loadKEPFromYaml(kepPath string) (*api.Proposal, error) {
484509
if err != nil {
485510
logrus.Errorf(
486511
"%v",
487-
errors.Wrapf(err, "getting PRR approver for %s stage", p.Stage),
512+
fmt.Errorf("getting PRR approver for stage '%s' for kep %s: %w", p.Stage, fullKEPPath, err),
488513
)
489514
}
490515

0 commit comments

Comments
 (0)