|
| 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 templates |
| 18 | + |
| 19 | +import ( |
| 20 | + "path/filepath" |
| 21 | + |
| 22 | + "sigs.k8s.io/kubebuilder/v3/pkg/model/file" |
| 23 | +) |
| 24 | + |
| 25 | +var _ file.Template = &Controller{} |
| 26 | + |
| 27 | +// Controller scaffolds the file that defines the controller for a CRD or a builtin resource |
| 28 | +// nolint:maligned |
| 29 | +type Controller struct { |
| 30 | + file.TemplateMixin |
| 31 | + file.MultiGroupMixin |
| 32 | + file.BoilerplateMixin |
| 33 | + file.ResourceMixin |
| 34 | +} |
| 35 | + |
| 36 | +// SetTemplateDefaults implements file.Template |
| 37 | +func (f *Controller) SetTemplateDefaults() error { |
| 38 | + if f.Path == "" { |
| 39 | + if f.MultiGroup { |
| 40 | + f.Path = filepath.Join("controllers", "%[group]", "%[kind]_controller.go") |
| 41 | + } else { |
| 42 | + f.Path = filepath.Join("controllers", "%[kind]_controller.go") |
| 43 | + } |
| 44 | + } |
| 45 | + f.Path = f.Resource.Replacer().Replace(f.Path) |
| 46 | + |
| 47 | + f.TemplateBody = controllerTemplate |
| 48 | + |
| 49 | + f.IfExistsAction = file.Overwrite |
| 50 | + |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +//nolint:lll |
| 55 | +const controllerTemplate = `{{ .Boilerplate }} |
| 56 | +
|
| 57 | +package controllers |
| 58 | +
|
| 59 | +import ( |
| 60 | + "github.com/go-logr/logr" |
| 61 | + "k8s.io/apimachinery/pkg/runtime" |
| 62 | + ctrl "sigs.k8s.io/controller-runtime" |
| 63 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 64 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 65 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 66 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 67 | + "sigs.k8s.io/controller-runtime/pkg/source" |
| 68 | + "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon" |
| 69 | + "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/status" |
| 70 | + "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative" |
| 71 | +
|
| 72 | + {{ .Resource.ImportAlias }} "{{ .Resource.Path }}" |
| 73 | +) |
| 74 | +
|
| 75 | +var _ reconcile.Reconciler = &{{ .Resource.Kind }}Reconciler{} |
| 76 | +
|
| 77 | +// {{ .Resource.Kind }}Reconciler reconciles a {{ .Resource.Kind }} object |
| 78 | +type {{ .Resource.Kind }}Reconciler struct { |
| 79 | + client.Client |
| 80 | + Log logr.Logger |
| 81 | + Scheme *runtime.Scheme |
| 82 | +
|
| 83 | + declarative.Reconciler |
| 84 | +} |
| 85 | +
|
| 86 | +//+kubebuilder:rbac:groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }},verbs=get;list;watch;create;update;patch;delete |
| 87 | +//+kubebuilder:rbac:groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }}/status,verbs=get;update;patch |
| 88 | +
|
| 89 | +// SetupWithManager sets up the controller with the Manager. |
| 90 | +func (r *{{ .Resource.Kind }}Reconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 91 | + addon.Init() |
| 92 | +
|
| 93 | + labels := map[string]string{ |
| 94 | + "k8s-app": "{{ lower .Resource.Kind }}", |
| 95 | + } |
| 96 | +
|
| 97 | + watchLabels := declarative.SourceLabel(mgr.GetScheme()) |
| 98 | +
|
| 99 | + if err := r.Reconciler.Init(mgr, &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}, |
| 100 | + declarative.WithObjectTransform(declarative.AddLabels(labels)), |
| 101 | + declarative.WithOwner(declarative.SourceAsOwner), |
| 102 | + declarative.WithLabels(watchLabels), |
| 103 | + declarative.WithStatus(status.NewBasic(mgr.GetClient())), |
| 104 | + // TODO: add an application to your manifest: declarative.WithObjectTransform(addon.TransformApplicationFromStatus), |
| 105 | + // TODO: add an application to your manifest: declarative.WithManagedApplication(watchLabels), |
| 106 | + declarative.WithObjectTransform(addon.ApplyPatches), |
| 107 | + ); err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | +
|
| 111 | + c, err := controller.New("{{ lower .Resource.Kind }}-controller", mgr, controller.Options{Reconciler: r}) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | +
|
| 116 | + // Watch for changes to {{ .Resource.Kind }} |
| 117 | + err = c.Watch(&source.Kind{Type: &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}}, &handler.EnqueueRequestForObject{}) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | +
|
| 122 | + // Watch for changes to deployed objects |
| 123 | + _, err = declarative.WatchAll(mgr.GetConfig(), c, r, watchLabels) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | +
|
| 128 | + return nil |
| 129 | +} |
| 130 | +` |
0 commit comments