Skip to content

Commit

Permalink
update kustomize scaffolding to work with kustomize 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Liujingfang1 committed Feb 28, 2019
1 parent 4228ca5 commit ae6ee76
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 30 deletions.
2 changes: 2 additions & 0 deletions cmd/init_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ func (o *projectOptions) runInit() {
&manager.Config{Image: imgName},
&project.GitIgnore{},
&project.Kustomize{},
&project.KustomizeRBAC{},
&project.KustomizeManager{},
&project.KustomizeImagePatch{},
&project.KustomizePrometheusMetricsPatch{},
&project.KustomizeAuthProxyPatch{},
Expand Down
18 changes: 3 additions & 15 deletions pkg/scaffold/project/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,9 @@ namePrefix: {{.Prefix}}-
#commonLabels:
# someName: someValue
# Each entry in this list must resolve to an existing
# resource definition in YAML. These are the resource
# files that kustomize reads, modifies and emits as a
# YAML string, with resources separated by document
# markers ("---").
resources:
- ../rbac/rbac_role.yaml
- ../rbac/rbac_role_binding.yaml
- ../manager/manager.yaml
# Comment the following 3 lines if you want to disable
# the auth proxy (https://github.com/brancz/kube-rbac-proxy)
# which protects your /metrics endpoint.
- ../rbac/auth_proxy_service.yaml
- ../rbac/auth_proxy_role.yaml
- ../rbac/auth_proxy_role_binding.yaml
bases:
- ../rbac
- ../manager
patches:
- manager_image_patch.yaml
Expand Down
44 changes: 44 additions & 0 deletions pkg/scaffold/project/kustomize_manager_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package project

import (
"path/filepath"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
)

var _ input.File = &KustomizeManager{}

// KustomizeManager scaffolds the Kustomization file in manager folder.
type KustomizeManager struct {
input.Input
}

// GetInput implements input.File
func (c *KustomizeManager) GetInput() (input.Input, error) {
if c.Path == "" {
c.Path = filepath.Join("config", "manager", "kustomization.yaml")
}
c.TemplateBody = kustomizeManagerTemplate
c.Input.IfExistsAction = input.Error
return c.Input, nil
}

var kustomizeManagerTemplate = `resources:
- manager.yaml
`
51 changes: 51 additions & 0 deletions pkg/scaffold/project/kustomize_rbac_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package project

import (
"path/filepath"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
)

var _ input.File = &KustomizeRBAC{}

// KustomizeRBAC scaffolds the Kustomization file in rbac folder.
type KustomizeRBAC struct {
input.Input
}

// GetInput implements input.File
func (c *KustomizeRBAC) GetInput() (input.Input, error) {
if c.Path == "" {
c.Path = filepath.Join("config", "rbac", "kustomization.yaml")
}
c.TemplateBody = kustomizeRBACTemplate
c.Input.IfExistsAction = input.Error
return c.Input, nil
}

var kustomizeRBACTemplate = `resources:
- rbac_role.yaml
- rbac_role_binding.yaml
# Comment the following 3 lines if you want to disable
# the auth proxy (https://github.com/brancz/kube-rbac-proxy)
# which protects your /metrics endpoint.
- auth_proxy_service.yaml
- auth_proxy_role.yaml
- auth_proxy_role_binding.yaml
`
34 changes: 34 additions & 0 deletions pkg/scaffold/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,40 @@ Copyright %s Example Owners.
})
})

Describe("scaffolding a RBAC Kustomization", func() {
BeforeEach(func() {
goldenPath = filepath.Join("config", "rbac", "kustomization.yaml")
writeToPath = goldenPath
})
Context("with rbac", func() {
It("should match the golden file", func() {
instance := &KustomizeRBAC{}
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())

// Verify the contents matches the golden file.
Expect(result.Actual.String()).To(BeEquivalentTo(result.Golden))
})
})
})

Describe("scaffolding a manager Kustomization", func() {
BeforeEach(func() {
goldenPath = filepath.Join("config", "manager", "kustomization.yaml")
writeToPath = goldenPath
})
Context("with manager", func() {
It("should match the golden file", func() {
instance := &KustomizeManager{}
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())

// Verify the contents matches the golden file.
Expect(result.Actual.String()).To(BeEquivalentTo(result.Golden))
})
})
})

Describe("scaffolding a Kustomize image patch", func() {
BeforeEach(func() {
goldenPath = filepath.Join("config", "default", "manager_image_patch.yaml")
Expand Down
18 changes: 3 additions & 15 deletions test/project/config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,9 @@ namePrefix: project-
#commonLabels:
# someName: someValue

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

patches:
- manager_image_patch.yaml
Expand Down
2 changes: 2 additions & 0 deletions test/project/config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resources:
- manager.yaml
9 changes: 9 additions & 0 deletions test/project/config/rbac/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resources:
- rbac_role.yaml
- rbac_role_binding.yaml
# Comment the following 3 lines if you want to disable
# the auth proxy (https://github.com/brancz/kube-rbac-proxy)
# which protects your /metrics endpoint.
- auth_proxy_service.yaml
- auth_proxy_role.yaml
- auth_proxy_role_binding.yaml

0 comments on commit ae6ee76

Please sign in to comment.