knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/codegen/cmd/injection-gen/generators/reconciler/reconciler_reconciler_stub.go (about)

     1  /*
     2  Copyright 2020 The Knative 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 reconciler
    18  
    19  import (
    20  	"io"
    21  
    22  	"k8s.io/gengo/v2/generator"
    23  	"k8s.io/gengo/v2/namer"
    24  	"k8s.io/gengo/v2/types"
    25  	"k8s.io/klog/v2"
    26  )
    27  
    28  // reconcilerReconcilerStubGenerator produces a file of the stub of how to
    29  // implement the reconciler.
    30  type reconcilerReconcilerStubGenerator struct {
    31  	generator.GoGenerator
    32  	outputPackage  string
    33  	imports        namer.ImportTracker
    34  	typeToGenerate *types.Type
    35  
    36  	reconcilerPkg string
    37  }
    38  
    39  var _ generator.Generator = (*reconcilerReconcilerStubGenerator)(nil)
    40  
    41  func (g *reconcilerReconcilerStubGenerator) Filter(c *generator.Context, t *types.Type) bool {
    42  	// Only process the type for this generator.
    43  	return t == g.typeToGenerate
    44  }
    45  
    46  func (g *reconcilerReconcilerStubGenerator) Namers(c *generator.Context) namer.NameSystems {
    47  	return namer.NameSystems{
    48  		"raw": namer.NewRawNamer(g.outputPackage, g.imports),
    49  	}
    50  }
    51  
    52  func (g *reconcilerReconcilerStubGenerator) Imports(c *generator.Context) (imports []string) {
    53  	imports = append(imports, g.imports.ImportLines()...)
    54  	return imports
    55  }
    56  
    57  func (g *reconcilerReconcilerStubGenerator) GenerateType(c *generator.Context, t *types.Type, w io.Writer) error {
    58  	sw := generator.NewSnippetWriter(w, c, "{{", "}}")
    59  
    60  	klog.V(5).Info("processing type ", t)
    61  
    62  	m := map[string]interface{}{
    63  		"type": t,
    64  		"reconcilerEvent": c.Universe.Type(types.Name{
    65  			Package: "knative.dev/pkg/reconciler",
    66  			Name:    "Event",
    67  		}),
    68  		"reconcilerNewEvent": c.Universe.Function(types.Name{
    69  			Package: "knative.dev/pkg/reconciler",
    70  			Name:    "NewEvent",
    71  		}),
    72  		"reconcilerInterface": c.Universe.Type(types.Name{
    73  			Package: g.reconcilerPkg,
    74  			Name:    "Interface",
    75  		}),
    76  		"reconcilerFinalizer": c.Universe.Type(types.Name{
    77  			Package: g.reconcilerPkg,
    78  			Name:    "Finalizer",
    79  		}),
    80  		"reconcilerReadOnlyInterface": c.Universe.Type(types.Name{
    81  			Package: g.reconcilerPkg,
    82  			Name:    "ReadOnlyInterface",
    83  		}),
    84  		"corev1EventTypeNormal": c.Universe.Type(types.Name{
    85  			Package: "k8s.io/api/core/v1",
    86  			Name:    "EventTypeNormal",
    87  		}),
    88  		"contextContext": c.Universe.Type(types.Name{
    89  			Package: "context",
    90  			Name:    "Context",
    91  		}),
    92  	}
    93  
    94  	sw.Do(reconcilerReconcilerStub, m)
    95  
    96  	return sw.Error()
    97  }
    98  
    99  var reconcilerReconcilerStub = `
   100  // TODO: PLEASE COPY AND MODIFY THIS FILE AS A STARTING POINT
   101  
   102  // newReconciledNormal makes a new reconciler event with event type Normal, and
   103  // reason {{.type|public}}Reconciled.
   104  func newReconciledNormal(namespace, name string) reconciler.Event {
   105  	return {{.reconcilerNewEvent|raw}}({{.corev1EventTypeNormal|raw}}, "{{.type|public}}Reconciled", "{{.type|public}} reconciled: \"%s/%s\"", namespace, name)
   106  }
   107  
   108  // Reconciler implements controller.Reconciler for {{.type|public}} resources.
   109  type Reconciler struct {
   110  	// TODO: add additional requirements here.
   111  }
   112  
   113  // Check that our Reconciler implements Interface
   114  var _ {{.reconcilerInterface|raw}} = (*Reconciler)(nil)
   115  
   116  // Optionally check that our Reconciler implements Finalizer
   117  //var _ {{.reconcilerFinalizer|raw}} = (*Reconciler)(nil)
   118  
   119  // Optionally check that our Reconciler implements ReadOnlyInterface
   120  // Implement this to observe resources even when we are not the leader.
   121  //var _ {{.reconcilerReadOnlyInterface|raw}} = (*Reconciler)(nil)
   122  
   123  // ReconcileKind implements Interface.ReconcileKind.
   124  func (r *Reconciler) ReconcileKind(ctx {{.contextContext|raw}}, o *{{.type|raw}}) {{.reconcilerEvent|raw}} {
   125  	{{if not .isKRShaped}}// TODO: use this if the resource implements InitializeConditions.
   126  	// o.Status.InitializeConditions()
   127  	{{end}}
   128  
   129  	// TODO: add custom reconciliation logic here.
   130  
   131  	{{if not .isKRShaped}}
   132  	// TODO: use this if the object has .status.ObservedGeneration.
   133  	// o.Status.ObservedGeneration = o.Generation
   134  	{{end}}
   135  	return nil
   136  }
   137  
   138  // Optionally, use FinalizeKind to add finalizers. FinalizeKind will be called
   139  // when the resource is deleted.
   140  //func (r *Reconciler) FinalizeKind(ctx {{.contextContext|raw}}, o *{{.type|raw}}) {{.reconcilerEvent|raw}} {
   141  //	// TODO: add custom finalization logic here.
   142  //	return nil
   143  //}
   144  
   145  // Optionally, use ObserveKind to observe the resource when we are not the leader.
   146  // func (r *Reconciler) ObserveKind(ctx {{.contextContext|raw}}, o *{{.type|raw}}) {{.reconcilerEvent|raw}} {
   147  // 	// TODO: add custom observation logic here.
   148  // 	return nil
   149  // }
   150  `