sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v3/scaffolds/internal/templates/controllers/controller.go (about) 1 /* 2 Copyright 2020 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 controllers 18 19 import ( 20 "path/filepath" 21 22 log "github.com/sirupsen/logrus" 23 24 "sigs.k8s.io/kubebuilder/v3/pkg/machinery" 25 ) 26 27 var _ machinery.Template = &Controller{} 28 29 // Controller scaffolds the file that defines the controller for a CRD or a builtin resource 30 // nolint:maligned 31 type Controller struct { 32 machinery.TemplateMixin 33 machinery.MultiGroupMixin 34 machinery.BoilerplateMixin 35 machinery.ResourceMixin 36 37 ControllerRuntimeVersion string 38 39 Force bool 40 } 41 42 // SetTemplateDefaults implements file.Template 43 func (f *Controller) SetTemplateDefaults() error { 44 if f.Path == "" { 45 if f.MultiGroup && f.Resource.Group != "" { 46 f.Path = filepath.Join("controllers", "%[group]", "%[kind]_controller.go") 47 } else { 48 f.Path = filepath.Join("controllers", "%[kind]_controller.go") 49 } 50 } 51 f.Path = f.Resource.Replacer().Replace(f.Path) 52 log.Println(f.Path) 53 54 f.TemplateBody = controllerTemplate 55 56 if f.Force { 57 f.IfExistsAction = machinery.OverwriteFile 58 } else { 59 f.IfExistsAction = machinery.Error 60 } 61 62 return nil 63 } 64 65 //nolint:lll 66 const controllerTemplate = `{{ .Boilerplate }} 67 68 package {{ if and .MultiGroup .Resource.Group }}{{ .Resource.PackageName }}{{ else }}controllers{{ end }} 69 70 import ( 71 "context" 72 "k8s.io/apimachinery/pkg/runtime" 73 ctrl "sigs.k8s.io/controller-runtime" 74 "sigs.k8s.io/controller-runtime/pkg/client" 75 "sigs.k8s.io/controller-runtime/pkg/log" 76 {{ if not (isEmptyStr .Resource.Path) -}} 77 {{ .Resource.ImportAlias }} "{{ .Resource.Path }}" 78 {{- end }} 79 ) 80 81 // {{ .Resource.Kind }}Reconciler reconciles a {{ .Resource.Kind }} object 82 type {{ .Resource.Kind }}Reconciler struct { 83 client.Client 84 Scheme *runtime.Scheme 85 } 86 87 //+kubebuilder:rbac:groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }},verbs=get;list;watch;create;update;patch;delete 88 //+kubebuilder:rbac:groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }}/status,verbs=get;update;patch 89 //+kubebuilder:rbac:groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }}/finalizers,verbs=update 90 91 // Reconcile is part of the main kubernetes reconciliation loop which aims to 92 // move the current state of the cluster closer to the desired state. 93 // TODO(user): Modify the Reconcile function to compare the state specified by 94 // the {{ .Resource.Kind }} object against the actual cluster state, and then 95 // perform operations to make the cluster state reflect the state specified by 96 // the user. 97 // 98 // For more details, check Reconcile and its Result here: 99 // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@{{ .ControllerRuntimeVersion }}/pkg/reconcile 100 func (r *{{ .Resource.Kind }}Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { 101 _ = log.FromContext(ctx) 102 103 // TODO(user): your logic here 104 105 return ctrl.Result{}, nil 106 } 107 108 // SetupWithManager sets up the controller with the Manager. 109 func (r *{{ .Resource.Kind }}Reconciler) SetupWithManager(mgr ctrl.Manager) error { 110 return ctrl.NewControllerManagedBy(mgr). 111 {{ if not (isEmptyStr .Resource.Path) -}} 112 For(&{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}). 113 {{- else -}} 114 // Uncomment the following line adding a pointer to an instance of the controlled resource as an argument 115 // For(). 116 {{- end }} 117 Complete(r) 118 } 119 `