Skip to content

Commit 49e9983

Browse files
authored
Merge pull request #614 from Liujingfang1/kustomize
update kustomize scaffolding to work with kustomize 2.0
2 parents 67cfb94 + ae6ee76 commit 49e9983

File tree

8 files changed

+148
-30
lines changed

8 files changed

+148
-30
lines changed

cmd/init_project.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ func (o *projectOptions) runInit() {
142142
&manager.Config{Image: imgName},
143143
&project.GitIgnore{},
144144
&project.Kustomize{},
145+
&project.KustomizeRBAC{},
146+
&project.KustomizeManager{},
145147
&project.KustomizeImagePatch{},
146148
&project.KustomizePrometheusMetricsPatch{},
147149
&project.KustomizeAuthProxyPatch{},

pkg/scaffold/project/kustomize.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,9 @@ namePrefix: {{.Prefix}}-
6565
#commonLabels:
6666
# someName: someValue
6767
68-
# Each entry in this list must resolve to an existing
69-
# resource definition in YAML. These are the resource
70-
# files that kustomize reads, modifies and emits as a
71-
# YAML string, with resources separated by document
72-
# markers ("---").
73-
resources:
74-
- ../rbac/rbac_role.yaml
75-
- ../rbac/rbac_role_binding.yaml
76-
- ../manager/manager.yaml
77-
# Comment the following 3 lines if you want to disable
78-
# the auth proxy (https://github.com/brancz/kube-rbac-proxy)
79-
# which protects your /metrics endpoint.
80-
- ../rbac/auth_proxy_service.yaml
81-
- ../rbac/auth_proxy_role.yaml
82-
- ../rbac/auth_proxy_role_binding.yaml
68+
bases:
69+
- ../rbac
70+
- ../manager
8371
8472
patches:
8573
- manager_image_patch.yaml
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2019 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 project
18+
19+
import (
20+
"path/filepath"
21+
22+
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
23+
)
24+
25+
var _ input.File = &KustomizeManager{}
26+
27+
// KustomizeManager scaffolds the Kustomization file in manager folder.
28+
type KustomizeManager struct {
29+
input.Input
30+
}
31+
32+
// GetInput implements input.File
33+
func (c *KustomizeManager) GetInput() (input.Input, error) {
34+
if c.Path == "" {
35+
c.Path = filepath.Join("config", "manager", "kustomization.yaml")
36+
}
37+
c.TemplateBody = kustomizeManagerTemplate
38+
c.Input.IfExistsAction = input.Error
39+
return c.Input, nil
40+
}
41+
42+
var kustomizeManagerTemplate = `resources:
43+
- manager.yaml
44+
`
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2019 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 project
18+
19+
import (
20+
"path/filepath"
21+
22+
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
23+
)
24+
25+
var _ input.File = &KustomizeRBAC{}
26+
27+
// KustomizeRBAC scaffolds the Kustomization file in rbac folder.
28+
type KustomizeRBAC struct {
29+
input.Input
30+
}
31+
32+
// GetInput implements input.File
33+
func (c *KustomizeRBAC) GetInput() (input.Input, error) {
34+
if c.Path == "" {
35+
c.Path = filepath.Join("config", "rbac", "kustomization.yaml")
36+
}
37+
c.TemplateBody = kustomizeRBACTemplate
38+
c.Input.IfExistsAction = input.Error
39+
return c.Input, nil
40+
}
41+
42+
var kustomizeRBACTemplate = `resources:
43+
- rbac_role.yaml
44+
- rbac_role_binding.yaml
45+
# Comment the following 3 lines if you want to disable
46+
# the auth proxy (https://github.com/brancz/kube-rbac-proxy)
47+
# which protects your /metrics endpoint.
48+
- auth_proxy_service.yaml
49+
- auth_proxy_role.yaml
50+
- auth_proxy_role_binding.yaml
51+
`

pkg/scaffold/project/project_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,40 @@ Copyright %s Example Owners.
205205
})
206206
})
207207

208+
Describe("scaffolding a RBAC Kustomization", func() {
209+
BeforeEach(func() {
210+
goldenPath = filepath.Join("config", "rbac", "kustomization.yaml")
211+
writeToPath = goldenPath
212+
})
213+
Context("with rbac", func() {
214+
It("should match the golden file", func() {
215+
instance := &KustomizeRBAC{}
216+
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
217+
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())
218+
219+
// Verify the contents matches the golden file.
220+
Expect(result.Actual.String()).To(BeEquivalentTo(result.Golden))
221+
})
222+
})
223+
})
224+
225+
Describe("scaffolding a manager Kustomization", func() {
226+
BeforeEach(func() {
227+
goldenPath = filepath.Join("config", "manager", "kustomization.yaml")
228+
writeToPath = goldenPath
229+
})
230+
Context("with manager", func() {
231+
It("should match the golden file", func() {
232+
instance := &KustomizeManager{}
233+
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
234+
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())
235+
236+
// Verify the contents matches the golden file.
237+
Expect(result.Actual.String()).To(BeEquivalentTo(result.Golden))
238+
})
239+
})
240+
})
241+
208242
Describe("scaffolding a Kustomize image patch", func() {
209243
BeforeEach(func() {
210244
goldenPath = filepath.Join("config", "default", "manager_image_patch.yaml")

test/project/config/default/kustomization.yaml

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,9 @@ namePrefix: project-
1212
#commonLabels:
1313
# someName: someValue
1414

15-
# Each entry in this list must resolve to an existing
16-
# resource definition in YAML. These are the resource
17-
# files that kustomize reads, modifies and emits as a
18-
# YAML string, with resources separated by document
19-
# markers ("---").
20-
resources:
21-
- ../rbac/rbac_role.yaml
22-
- ../rbac/rbac_role_binding.yaml
23-
- ../manager/manager.yaml
24-
# Comment the following 3 lines if you want to disable
25-
# the auth proxy (https://github.com/brancz/kube-rbac-proxy)
26-
# which protects your /metrics endpoint.
27-
- ../rbac/auth_proxy_service.yaml
28-
- ../rbac/auth_proxy_role.yaml
29-
- ../rbac/auth_proxy_role_binding.yaml
15+
bases:
16+
- ../rbac
17+
- ../manager
3018

3119
patches:
3220
- manager_image_patch.yaml
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
resources:
2+
- manager.yaml
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
resources:
2+
- rbac_role.yaml
3+
- rbac_role_binding.yaml
4+
# Comment the following 3 lines if you want to disable
5+
# the auth proxy (https://github.com/brancz/kube-rbac-proxy)
6+
# which protects your /metrics endpoint.
7+
- auth_proxy_service.yaml
8+
- auth_proxy_role.yaml
9+
- auth_proxy_role_binding.yaml

0 commit comments

Comments
 (0)