sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v2/scaffolds/internal/templates/controllers/controller.go (about) 1 /* 2 Copyright 2018 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 { 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 controllers 69 70 import ( 71 "context" 72 "github.com/go-logr/logr" 73 "k8s.io/apimachinery/pkg/runtime" 74 ctrl "sigs.k8s.io/controller-runtime" 75 "sigs.k8s.io/controller-runtime/pkg/client" 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 Log logr.Logger 85 Scheme *runtime.Scheme 86 } 87 88 //+kubebuilder:rbac:groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }},verbs=get;list;watch;create;update;patch;delete 89 //+kubebuilder:rbac:groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }}/status,verbs=get;update;patch 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(req ctrl.Request) (ctrl.Result, error) { 101 _ = context.Background() 102 _ = r.Log.WithValues("{{ .Resource.Kind | lower }}", req.NamespacedName) 103 104 // TODO(user): your logic here 105 106 return ctrl.Result{}, nil 107 } 108 109 // SetupWithManager sets up the controller with the Manager. 110 func (r *{{ .Resource.Kind }}Reconciler) SetupWithManager(mgr ctrl.Manager) error { 111 return ctrl.NewControllerManagedBy(mgr). 112 {{ if not (isEmptyStr .Resource.Path) -}} 113 For(&{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}). 114 {{- else -}} 115 // Uncomment the following line adding a pointer to an instance of the controlled resource as an argument 116 // For(). 117 {{- end }} 118 Complete(r) 119 } 120 `