sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v4/scaffolds/internal/templates/controllers/controller.go (about)

     1  /*
     2  Copyright 2022 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("internal", "controller", "%[group]", "%[kind]_controller.go")
    47  		} else {
    48  			f.Path = filepath.Join("internal", "controller", "%[kind]_controller.go")
    49  		}
    50  	}
    51  
    52  	f.Path = f.Resource.Replacer().Replace(f.Path)
    53  	log.Println(f.Path)
    54  
    55  	f.TemplateBody = controllerTemplate
    56  
    57  	if f.Force {
    58  		f.IfExistsAction = machinery.OverwriteFile
    59  	} else {
    60  		f.IfExistsAction = machinery.Error
    61  	}
    62  
    63  	return nil
    64  }
    65  
    66  //nolint:lll
    67  const controllerTemplate = `{{ .Boilerplate }}
    68  
    69  package {{ if and .MultiGroup .Resource.Group }}{{ .Resource.PackageName }}{{ else }}controller{{ end }}
    70  
    71  import (
    72  	"context"
    73  	"k8s.io/apimachinery/pkg/runtime"
    74  	ctrl "sigs.k8s.io/controller-runtime"
    75  	"sigs.k8s.io/controller-runtime/pkg/client"
    76  	"sigs.k8s.io/controller-runtime/pkg/log"
    77  	{{ if not (isEmptyStr .Resource.Path) -}}
    78  	{{ .Resource.ImportAlias }} "{{ .Resource.Path }}"
    79  	{{- end }}
    80  )
    81  
    82  // {{ .Resource.Kind }}Reconciler reconciles a {{ .Resource.Kind }} object
    83  type {{ .Resource.Kind }}Reconciler struct {
    84  	client.Client
    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  //+kubebuilder:rbac:groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }}/finalizers,verbs=update
    91  
    92  // Reconcile is part of the main kubernetes reconciliation loop which aims to
    93  // move the current state of the cluster closer to the desired state.
    94  // TODO(user): Modify the Reconcile function to compare the state specified by
    95  // the {{ .Resource.Kind }} object against the actual cluster state, and then
    96  // perform operations to make the cluster state reflect the state specified by
    97  // the user.
    98  //
    99  // For more details, check Reconcile and its Result here:
   100  // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@{{ .ControllerRuntimeVersion }}/pkg/reconcile
   101  func (r *{{ .Resource.Kind }}Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
   102  	_ = log.FromContext(ctx)
   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  `