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 <me@chrishein.com>
  • Loading branch information
christopherhein committed Nov 7, 2020
1 parent a4ff1b6 commit 28c55cf
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 55 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 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 @@ -236,13 +218,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 @@ -46,6 +46,7 @@ type initPlugin struct {
// flags
fetchDeps bool
skipGoVersionCheck bool
componentConfig bool
}

var (
Expand Down Expand Up @@ -87,6 +88,8 @@ func (p *initPlugin) 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 @@ -142,7 +145,7 @@ func (p *initPlugin) Validate() error {
}

func (p *initPlugin) GetScaffolder() (scaffold.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 *initPlugin) PostScaffold() error {
Expand Down
20 changes: 14 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.3.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) scaffold.Scaffolder {
func NewInitScaffolder(config *config.Config, license, owner string, componentConfig bool) scaffold.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.AuthProxyPatch{},
&kdefault.AuthProxyPatch{ComponentConfig: s.componentConfig},
&rbac.AuthProxyService{},
&rbac.ClientClusterRole{},
&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 @@ -115,14 +120,17 @@ func (s *initScaffolder) scaffold() error {
ControllerRuntimeVersion: ControllerRuntimeVersion,
},
&templates.Dockerfile{},
&templates.ComponentConfig{},
&templates.DockerignoreFile{},
&kdefault.Kustomize{},
&kdefault.Kustomize{ComponentConfig: s.componentConfig},
&kdefault.ManagerWebhookPatch{},
&kdefault.ManagerConfigPatch{},
&rbac.ManagerRoleBinding{},
&rbac.LeaderElectionRole{},
&rbac.LeaderElectionRoleBinding{},
&rbac.KustomizeRBAC{},
&manager.Kustomization{},
&manager.ComponentConfig{},
&webhook.Kustomization{},
&webhook.KustomizeConfigWebhook{},
&webhook.Service{},
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
Expand Up @@ -92,7 +92,7 @@ type {{ .Resource.Kind }}Reconciler struct {
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@{{ .ControllerRuntimeVersion }}/pkg/reconcile
func (r *{{ .Resource.Kind }}Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
func (r *{{ .Resource.Kind }}Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = context.Background()
_ = r.Log.WithValues("{{ .Resource.Kind | lower }}", req.NamespacedName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ var _ file.Template = &AuthProxyPatch{}
// prometheus metrics for manager Pod.
type AuthProxyPatch struct {
file.TemplateMixin

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

// SetTemplateDefaults implements input.Template
Expand All @@ -43,7 +46,7 @@ func (f *AuthProxyPatch) 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 @@ -64,8 +67,10 @@ spec:
ports:
- containerPort: 8443
name: https
- name: manager
{{- if not .ComponentConfig }}
- name: manager
args:
- "--metrics-addr=127.0.0.1:8080"
- "--enable-leader-election"
{{- end }}
`
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 = &Kustomize{}
type Kustomize struct {
file.TemplateMixin
file.ProjectNameMixin

ComponentConfig bool
}

// SetTemplateDefaults implements input.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 .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
Loading

0 comments on commit 28c55cf

Please sign in to comment.