Skip to content

Commit

Permalink
adding support for scaffolding a componentconfig type
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Hein <[email protected]>
  • Loading branch information
christopherhein committed Nov 7, 2020
1 parent 367fa11 commit ac37786
Show file tree
Hide file tree
Showing 14 changed files with 252 additions and 56 deletions.
11 changes: 11 additions & 0 deletions pkg/model/file/funcmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package file

import (
"fmt"
"hash/fnv"
"strings"
"text/template"
)
Expand All @@ -26,5 +28,14 @@ func DefaultFuncMap() template.FuncMap {
return template.FuncMap{
"title": strings.Title,
"lower": strings.ToLower,
"hash": hash,
}
}

func hash(s string) (string, error) {
hasher := fnv.New32a()
if _, err := hasher.Write([]byte(s)); err != nil {
return "", err
}
return fmt.Sprintf("%x", hasher.Sum(nil)), nil
}
24 changes: 3 additions & 21 deletions pkg/plugin/v2/scaffolds/internal/templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ package templates

import (
"fmt"
"hash/fnv"
"path/filepath"
"text/template"

"sigs.k8s.io/kubebuilder/pkg/model/file"
)

const defaultMainPath = "main.go"

var _ file.Template = &Main{}
var _ file.UseCustomFuncMap = &Main{}

// Main scaffolds a file that defines the controller manager entry point
type Main struct {
Expand All @@ -53,21 +50,6 @@ func (f *Main) SetTemplateDefaults() error {
return nil
}

func hash(s string) (string, error) {
hasher := fnv.New32a()
if _, err := hasher.Write([]byte(s)); err != nil {
return "", err
}
return fmt.Sprintf("%x", hasher.Sum(nil)), nil
}

// GetFuncMap implements file.UseCustomFuncMap
func (f *Main) GetFuncMap() template.FuncMap {
fm := file.DefaultFuncMap()
fm["hash"] = hash
return fm
}

var _ file.Inserter = &MainUpdater{}

// MainUpdater updates main.go to run Controllers
Expand Down Expand Up @@ -241,13 +223,13 @@ func main() {
"Enabling this will ensure there is only one active controller manager.")
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
LeaderElection: enableLeaderElection,
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "{{ hash .Repo }}.{{ .Domain }}",
})
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/plugin/v3/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type initSubcommand struct {
// flags
fetchDeps bool
skipGoVersionCheck bool
componentConfig bool
}

var (
Expand Down Expand Up @@ -86,6 +87,8 @@ func (p *initSubcommand) BindFlags(fs *pflag.FlagSet) {
fs.StringVar(&p.license, "license", "apache2",
"license to use to boilerplate, may be one of 'apache2', 'none'")
fs.StringVar(&p.owner, "owner", "", "owner to add to the copyright")
fs.BoolVar(&p.componentConfig, "component-config", false,
"create a versioned ComponentConfig file, may be 'true' or 'false'")

// project args
fs.StringVar(&p.config.Repo, "repo", "", "name to use for go module (e.g., github.com/user/repo), "+
Expand Down Expand Up @@ -141,7 +144,7 @@ func (p *initSubcommand) Validate() error {
}

func (p *initSubcommand) GetScaffolder() (cmdutil.Scaffolder, error) {
return scaffolds.NewInitScaffolder(p.config, p.license, p.owner), nil
return scaffolds.NewInitScaffolder(p.config, p.license, p.owner, p.componentConfig), nil
}

func (p *initSubcommand) PostScaffold() error {
Expand Down
19 changes: 13 additions & 6 deletions pkg/plugin/v3/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (

const (
// ControllerRuntimeVersion is the kubernetes-sigs/controller-runtime version to be used in the project
ControllerRuntimeVersion = "v0.6.3"
ControllerRuntimeVersion = "v0.7.0-alpha.6"
// ControllerToolsVersion is the kubernetes-sigs/controller-tools version to be used in the project
ControllerToolsVersion = "v0.4.0"
// KustomizeVersion is the kubernetes-sigs/kustomize version to be used in the project
Expand All @@ -53,15 +53,17 @@ type initScaffolder struct {
boilerplatePath string
license string
owner string
componentConfig bool
}

// NewInitScaffolder returns a new Scaffolder for project initialization operations
func NewInitScaffolder(config *config.Config, license, owner string) cmdutil.Scaffolder {
func NewInitScaffolder(config *config.Config, license, owner string, componentConfig bool) cmdutil.Scaffolder {
return &initScaffolder{
config: config,
boilerplatePath: filepath.Join("hack", "boilerplate.go.txt"),
license: license,
owner: owner,
componentConfig: componentConfig,
}
}

Expand Down Expand Up @@ -101,11 +103,14 @@ func (s *initScaffolder) scaffold() error {
&templates.GitIgnore{},
&rbac.AuthProxyRole{},
&rbac.AuthProxyRoleBinding{},
&kdefault.ManagerAuthProxyPatch{},
&kdefault.ManagerAuthProxyPatch{ComponentConfig: s.componentConfig},
&rbac.AuthProxyService{},
&rbac.AuthProxyClientRole{},
&manager.Config{Image: imageName},
&templates.Main{},
&manager.Config{
Image: imageName,
ComponentConfig: s.componentConfig,
},
&templates.Main{ComponentConfig: s.componentConfig},
&templates.GoMod{ControllerRuntimeVersion: ControllerRuntimeVersion},
&templates.Makefile{
Image: imageName,
Expand All @@ -116,13 +121,15 @@ func (s *initScaffolder) scaffold() error {
},
&templates.Dockerfile{},
&templates.DockerIgnore{},
&kdefault.Kustomization{},
&templates.ComponentConfig{},
&kdefault.Kustomization{ComponentConfig: s.componentConfig},
&kdefault.ManagerWebhookPatch{},
&rbac.RoleBinding{},
&rbac.LeaderElectionRole{},
&rbac.LeaderElectionRoleBinding{},
&rbac.Kustomization{},
&manager.Kustomization{},
&manager.ComponentConfig{},
&webhook.Kustomization{},
&webhook.KustomizeConfig{},
&webhook.Service{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ var _ = BeforeSuite(func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join({{ .BaseDirectoryRelativePath }}, "config", "crd", "bases")},
WebhookInstallOptions: envtest.WebhookInstallOptions{
DirectoryPaths: []string{filepath.Join({{ .BaseDirectoryRelativePath }}, "config", "webhook")},
Paths: []string{filepath.Join({{ .BaseDirectoryRelativePath }}, "config", "webhook")},
},
}
Expand All @@ -174,7 +174,7 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
%s
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
Expand Down
55 changes: 55 additions & 0 deletions pkg/plugin/v3/scaffolds/internal/templates/componentconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2020 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 templates

import (
"sigs.k8s.io/kubebuilder/pkg/model/file"
)

var _ file.Template = &ComponentConfig{}

// ComponentConfig scaffolds the ComponentConfig file in manager folder.
type ComponentConfig struct {
file.TemplateMixin
file.DomainMixin
file.RepositoryMixin
}

// SetTemplateDefaults implements input.Template
func (f *ComponentConfig) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = "config.yaml"
}

f.TemplateBody = componentConfigTemplate

f.IfExistsAction = file.Error

return nil
}

const componentConfigTemplate = `apiVersion: controller-runtime.sigs.k8s.io/v1alpha1
kind: ControllerManagerConfiguration
metrics:
bindAddress: :8080
webhook:
port: 9443
leaderElection:
leaderElect: false
resourceLock: configmaps
resourceName: {{ hash .Repo }}.{{ .Domain }}",
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2020 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 kdefault

import (
"path/filepath"

"sigs.k8s.io/kubebuilder/pkg/model/file"
)

var _ file.Template = &ManagerConfigPatch{}

// ManagerConfigPatch scaffolds a ManagerConfigPatch for a Resource
type ManagerConfigPatch struct {
file.TemplateMixin
}

// SetTemplateDefaults implements input.Template
func (f *ManagerConfigPatch) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "default", "manager_config_patch.yaml")
}

f.TemplateBody = managerConfigPatchTemplate

return nil
}

const managerConfigPatchTemplate = `apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: manager
volumeMounts:
- name: config
mountPath: /config.yaml
subPath: config.yaml
volumes:
- name: config
configMap:
name: controller-config
`
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var _ file.Template = &Kustomization{}
type Kustomization struct {
file.TemplateMixin
file.ProjectNameMixin

ComponentConfig bool
}

// SetTemplateDefaults implements file.Template
Expand Down Expand Up @@ -70,11 +72,15 @@ bases:
#- ../prometheus
patchesStrategicMerge:
# Protect the /metrics endpoint by putting it behind auth.
# If you want your controller-manager to expose the /metrics
# endpoint w/o any authn/z, please comment the following line.
# Protect the /metrics endpoint by putting it behind auth.
# If you want your controller-manager to expose the /metrics
# endpoint w/o any authn/z, please comment the following line.
- manager_auth_proxy_patch.yaml
# Mount the controller config file for loading manager configurations
# through a ComponentConfig type
{{ if not .ComponentConfig }}#{{ end }}- manager_config_patch.yaml
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- manager_webhook_patch.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ var _ file.Template = &ManagerAuthProxyPatch{}
// ManagerAuthProxyPatch scaffolds a file that defines the patch that enables prometheus metrics for the manager
type ManagerAuthProxyPatch struct {
file.TemplateMixin

// ComponentConfig defines that this should be configured with a file
ComponentConfig bool
}

// SetTemplateDefaults implements file.Template
Expand All @@ -42,7 +45,7 @@ func (f *ManagerAuthProxyPatch) SetTemplateDefaults() error {
return nil
}

const kustomizeAuthProxyPatchTemplate = `# This patch inject a sidecar container which is a HTTP proxy for the
const kustomizeAuthProxyPatchTemplate = `# This patch inject a sidecar container which is a HTTP proxy for the
# controller manager, it performs RBAC authorization against the Kubernetes API using SubjectAccessReviews.
apiVersion: apps/v1
kind: Deployment
Expand All @@ -63,8 +66,10 @@ spec:
ports:
- containerPort: 8443
name: https
{{- if not .ComponentConfig }}
- name: manager
args:
- "--metrics-addr=127.0.0.1:8080"
- "--enable-leader-election"
{{- end }}
`
Loading

0 comments on commit ac37786

Please sign in to comment.