Skip to content

Commit 4ac14c6

Browse files
committed
pkg/repo: Initial cleanup on KEP creation logic
Signed-off-by: Stephen Augustus <[email protected]>
1 parent dfd76be commit 4ac14c6

File tree

3 files changed

+155
-68
lines changed

3 files changed

+155
-68
lines changed

pkg/repo/repo.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -531,32 +531,3 @@ func (r *Repo) loadKEPFromOldStyle(kepPath string) (*api.Proposal, error) {
531531
kep.Name = filepath.Base(kepPath)
532532
return kep, nil
533533
}
534-
535-
func (r *Repo) WriteKEP(kep *api.Proposal) error {
536-
b, err := yaml.Marshal(kep)
537-
if err != nil {
538-
return fmt.Errorf("KEP is invalid: %s", err)
539-
}
540-
541-
if mkErr := os.MkdirAll(
542-
filepath.Join(
543-
r.ProposalPath,
544-
kep.OwningSIG,
545-
kep.Name,
546-
),
547-
os.ModePerm,
548-
); mkErr != nil {
549-
return errors.Wrapf(mkErr, "creating KEP directory")
550-
}
551-
552-
newPath := filepath.Join(
553-
r.ProposalPath,
554-
kep.OwningSIG,
555-
kep.Name,
556-
ProposalMetadataFilename,
557-
)
558-
559-
fmt.Fprintf(r.Out, "writing KEP to %s\n", newPath)
560-
561-
return ioutil.WriteFile(newPath, b, os.ModePerm)
562-
}

pkg/repo/write.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 repo
18+
19+
import (
20+
"fmt"
21+
"io/ioutil"
22+
"os"
23+
"path/filepath"
24+
25+
"github.com/pkg/errors"
26+
"gopkg.in/yaml.v3"
27+
"k8s.io/enhancements/api"
28+
)
29+
30+
func (r *Repo) WriteKEP(kep *api.Proposal) error {
31+
b, err := yaml.Marshal(kep)
32+
if err != nil {
33+
return fmt.Errorf("KEP is invalid: %s", err)
34+
}
35+
36+
sig := kep.OwningSIG
37+
kepName := kep.Name
38+
39+
if sig == "" {
40+
return errors.New("owning SIG must be populated")
41+
}
42+
43+
if kepName == "" {
44+
return errors.New("KEP name must be populated")
45+
}
46+
47+
if mkErr := os.MkdirAll(
48+
filepath.Join(
49+
r.ProposalPath,
50+
sig,
51+
kepName,
52+
),
53+
os.ModePerm,
54+
); mkErr != nil {
55+
return errors.Wrapf(mkErr, "creating KEP directory")
56+
}
57+
58+
newPath := filepath.Join(
59+
r.ProposalPath,
60+
sig,
61+
kepName,
62+
ProposalMetadataFilename,
63+
)
64+
65+
fmt.Fprintf(r.Out, "writing KEP to %s\n", newPath)
66+
67+
return ioutil.WriteFile(newPath, b, os.ModePerm)
68+
}

pkg/proposal/create_test.go renamed to pkg/repo/write_test.go

Lines changed: 87 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package proposal
17+
package repo_test
1818

1919
import (
2020
"bytes"
@@ -26,6 +26,7 @@ import (
2626
"github.com/stretchr/testify/require"
2727

2828
"k8s.io/enhancements/api"
29+
"k8s.io/enhancements/pkg/repo"
2930
"sigs.k8s.io/yaml"
3031
)
3132

@@ -34,55 +35,83 @@ func TestWriteKep(t *testing.T) {
3435
name string
3536
kepFile string
3637
repoPath string
37-
opts CreateOpts
38+
kepName string
39+
sig string
3840
expectedPath string
3941
expectError bool
4042
}{
4143
{
42-
name: "simple kep",
43-
kepFile: "testdata/valid-kep.yaml",
44-
repoPath: "enhancements",
45-
opts: CreateOpts{
46-
CommonArgs: CommonArgs{
47-
Name: "1010-test",
48-
SIG: "sig-auth",
49-
},
50-
},
44+
name: "simple KEP",
45+
kepFile: "testdata/valid-kep.yaml",
46+
repoPath: "enhancements",
47+
kepName: "1010-test",
48+
sig: "sig-auth",
5149
expectedPath: filepath.Join("enhancements", "keps", "sig-auth", "1010-test"),
5250
expectError: false,
5351
},
5452
{
55-
name: "opts repo path works",
56-
kepFile: "testdata/valid-kep.yaml",
57-
repoPath: "",
58-
opts: CreateOpts{
59-
CommonArgs: CommonArgs{
60-
Name: "1011-test",
61-
SIG: "sig-architecture",
62-
RepoPath: "enhancementz",
63-
},
64-
},
65-
expectedPath: filepath.Join("enhancementz", "keps", "sig-architecture", "1011-test"),
66-
expectError: false,
53+
name: "missing KEP name",
54+
kepFile: "testdata/valid-kep.yaml",
55+
repoPath: "enhancements",
56+
sig: "sig-auth",
57+
expectedPath: filepath.Join("enhancements", "keps", "sig-auth", "1010-test"),
58+
expectError: true,
59+
},
60+
{
61+
name: "missing owning SIG",
62+
kepFile: "testdata/valid-kep.yaml",
63+
repoPath: "enhancements",
64+
kepName: "1010-test",
65+
expectedPath: filepath.Join("enhancements", "keps", "sig-auth", "1010-test"),
66+
expectError: true,
6767
},
6868
}
6969

7070
for _, tc := range testcases {
7171
t.Run(tc.name, func(t *testing.T) {
7272
tempDir, err := ioutil.TempDir("", "")
73+
mkErr := os.MkdirAll(
74+
filepath.Join(
75+
tempDir,
76+
tc.repoPath,
77+
repo.ProposalPathStub,
78+
repo.PRRApprovalPathStub,
79+
),
80+
os.ModePerm,
81+
)
82+
83+
require.Nil(t, mkErr)
84+
85+
templatePath := filepath.Join(
86+
tempDir,
87+
tc.repoPath,
88+
repo.ProposalPathStub,
89+
repo.ProposalTemplatePathStub,
90+
)
91+
92+
mkErr = os.MkdirAll(
93+
templatePath,
94+
os.ModePerm,
95+
)
96+
97+
require.Nil(t, mkErr)
98+
99+
templateFile := filepath.Join(templatePath, repo.ProposalFilename)
100+
emptyTemplate, fileErr := os.Create(templateFile)
101+
require.Nil(t, fileErr)
102+
emptyTemplate.Close()
103+
73104
defer func() {
74105
t.Logf("cleanup!")
75106
err := os.RemoveAll(tempDir)
76107
if err != nil {
77108
t.Logf("error cleaning up test: %s", err)
78109
}
79110
}()
111+
80112
require.NoError(t, err)
81-
repoPath := tc.repoPath
82-
if repoPath == "" {
83-
repoPath = tc.opts.RepoPath
84-
}
85113

114+
repoPath := tc.repoPath
86115
repoPath = filepath.Join(tempDir, repoPath)
87116
c := newTestClient(t, repoPath)
88117

@@ -93,8 +122,17 @@ func TestWriteKep(t *testing.T) {
93122
err = yaml.Unmarshal(b, &p)
94123
require.NoError(t, err)
95124

96-
tc.opts.CommonArgs.RepoPath = repoPath
97-
err = c.writeKEP(&p, &tc.opts.CommonArgs)
125+
p.OwningSIG = tc.sig
126+
p.Name = tc.kepName
127+
128+
err = c.r.WriteKEP(&p)
129+
130+
files, readErr := ioutil.ReadDir(c.r.ProposalPath)
131+
require.Nil(t, readErr)
132+
133+
for _, f := range files {
134+
t.Logf(f.Name())
135+
}
98136

99137
if tc.expectError {
100138
require.Error(t, err)
@@ -106,7 +144,7 @@ func TestWriteKep(t *testing.T) {
106144
require.NoError(t, err)
107145
require.NotNil(t, dirStat)
108146
require.True(t, dirStat.IsDir())
109-
p := filepath.Join(computedPath, "kep.yaml")
147+
p := filepath.Join(computedPath, repo.ProposalMetadataFilename)
110148
fileStat, err := os.Stat(p)
111149
require.NoError(t, err)
112150
require.NotNil(t, fileStat)
@@ -118,38 +156,48 @@ func TestWriteKep(t *testing.T) {
118156
type testClient struct {
119157
T *testing.T
120158
b *bytes.Buffer
121-
*Client
159+
r *repo.Repo
122160
}
123161

124162
func newTestClient(t *testing.T, repoPath string) testClient {
125163
b := &bytes.Buffer{}
126164
tc := testClient{
127165
T: t,
128166
b: b,
129-
Client: &Client{
130-
RepoPath: repoPath,
131-
Out: b,
132-
},
133167
}
134168

169+
r, err := repo.New(repoPath)
170+
require.Nil(t, err)
171+
172+
r.Out = b
173+
tc.r = r
174+
135175
// TODO: Parameterize
136-
tc.addTemplate("kep.yaml")
137-
tc.addTemplate("README.md")
176+
tc.addTemplate(repo.ProposalMetadataFilename)
177+
tc.addTemplate(repo.ProposalFilename)
178+
138179
return tc
139180
}
140181

141182
func (tc *testClient) addTemplate(file string) {
142-
src := filepath.Join("testdata", "templates", file)
183+
src := filepath.Join(
184+
"testdata",
185+
repo.ProposalPathStub,
186+
repo.ProposalTemplatePathStub,
187+
file,
188+
)
189+
143190
data, err := ioutil.ReadFile(src)
144191
if err != nil {
145192
tc.T.Fatal(err)
146193
}
147194

148-
dirPath := filepath.Join(tc.Client.RepoPath, "keps", "NNNN-kep-template")
195+
dirPath := filepath.Join(tc.r.BasePath, repo.ProposalPathStub, repo.ProposalTemplatePathStub)
149196
err = os.MkdirAll(dirPath, os.ModePerm)
150197
if err != nil {
151198
tc.T.Fatal(err)
152199
}
200+
153201
dest := filepath.Join(dirPath, file)
154202
tc.T.Logf("Writing %s to %s", file, dest)
155203
err = ioutil.WriteFile(dest, data, os.ModePerm)

0 commit comments

Comments
 (0)