mirror of https://github.com/knative/pkg.git
Generate common logic for krshaped resources (#1311)
* generate krshaped logic * add logic to the tag * Update injection/README.md Co-authored-by: Matt Moore <mattmoor@vmware.com> * move var to reconciler Co-authored-by: Matt Moore <mattmoor@vmware.com>
This commit is contained in:
parent
ae0b7dda87
commit
b559da3646
|
@ -27,7 +27,7 @@ import (
|
|||
)
|
||||
|
||||
// +genclient
|
||||
// +genreconciler
|
||||
// +genreconciler:krshapedlogic=true
|
||||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
|
||||
|
||||
// Foo is for testing.
|
||||
|
|
|
@ -27,7 +27,7 @@ import (
|
|||
)
|
||||
|
||||
// +genclient
|
||||
// +genreconciler:class=example.com/filter.class
|
||||
// +genreconciler:class=example.com/filter.class,krshapedlogic=true
|
||||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
|
||||
|
||||
// Bar is for testing.
|
||||
|
|
|
@ -211,6 +211,15 @@ func isNonNamespaced(tags map[string]map[string]string) bool {
|
|||
return has
|
||||
}
|
||||
|
||||
func isKRShaped(tags map[string]map[string]string) bool {
|
||||
vals, has := tags["genclient"]
|
||||
if !has {
|
||||
return false
|
||||
}
|
||||
shaped, _ := vals["krshapedlogic"]
|
||||
return shaped == "true"
|
||||
}
|
||||
|
||||
func vendorless(p string) string {
|
||||
if pos := strings.LastIndex(p, "/vendor/"); pos != -1 {
|
||||
return p[pos+len("/vendor/"):]
|
||||
|
@ -424,6 +433,7 @@ func reconcilerPackages(basePackage string, groupPkgName string, gv clientgentyp
|
|||
extracted := extractCommentTags(t)
|
||||
reconcilerClass, hasReconcilerClass := extractReconcilerClassTag(extracted)
|
||||
nonNamespaced := isNonNamespaced(extracted)
|
||||
isKRShaped := isKRShaped(extracted)
|
||||
|
||||
packagePath := filepath.Join(packagePath, strings.ToLower(t.Name.Name))
|
||||
|
||||
|
@ -512,6 +522,7 @@ func reconcilerPackages(basePackage string, groupPkgName string, gv clientgentyp
|
|||
reconcilerClass: reconcilerClass,
|
||||
hasReconcilerClass: hasReconcilerClass,
|
||||
nonNamespaced: nonNamespaced,
|
||||
isKRShaped: isKRShaped,
|
||||
})
|
||||
|
||||
return generators
|
||||
|
|
|
@ -40,6 +40,7 @@ type reconcilerReconcilerGenerator struct {
|
|||
reconcilerClass string
|
||||
hasReconcilerClass bool
|
||||
nonNamespaced bool
|
||||
isKRShaped bool
|
||||
|
||||
groupGoName string
|
||||
groupVersion clientgentypes.GroupVersion
|
||||
|
@ -74,6 +75,7 @@ func (g *reconcilerReconcilerGenerator) GenerateType(c *generator.Context, t *ty
|
|||
"version": namer.IC(g.groupVersion.Version.String()),
|
||||
"class": g.reconcilerClass,
|
||||
"hasClass": g.hasReconcilerClass,
|
||||
"isKRShaped": g.isKRShaped,
|
||||
"nonNamespaced": g.nonNamespaced,
|
||||
"controllerImpl": c.Universe.Type(types.Name{
|
||||
Package: "knative.dev/pkg/controller",
|
||||
|
@ -309,9 +311,17 @@ func (r *reconcilerImpl) Reconcile(ctx {{.contextContext|raw}}, key string) erro
|
|||
logger.Warnw("Failed to set finalizers", zap.Error(err))
|
||||
}
|
||||
|
||||
{{if .isKRShaped}}
|
||||
reconciler.PreProcessReconcile(ctx, resource)
|
||||
{{end}}
|
||||
|
||||
// Reconcile this copy of the resource and then write back any status
|
||||
// updates regardless of whether the reconciliation errored out.
|
||||
reconcileEvent = r.reconciler.ReconcileKind(ctx, resource)
|
||||
|
||||
{{if .isKRShaped}}
|
||||
reconciler.PostProcessReconcile(ctx, resource)
|
||||
{{end}}
|
||||
} else if fin, ok := r.reconciler.(Finalizer); ok {
|
||||
// Append the target method to the logger.
|
||||
logger = logger.With(zap.String("targetMethod", "FinalizeKind"))
|
||||
|
|
|
@ -452,6 +452,28 @@ value of the `<key>` annotation on a resource must match the value provided to
|
|||
`NewImpl` (or `NewReconcile`) for `ReconcileKind` or `FinalizeKind` to be called
|
||||
for that resource.
|
||||
|
||||
#### Annotation based common logic
|
||||
|
||||
**krshaped=true may become the default if omitted in the future**
|
||||
|
||||
Reconcilers can handle common logic for resources that conform to the KRShaped
|
||||
interface. This allows the generated code to automatically increment
|
||||
ObservedGeneration.
|
||||
|
||||
```go
|
||||
// +genreconciler:krshapedlogic=true
|
||||
```
|
||||
|
||||
Setting this annotation will emit the following in the generated reconciler.
|
||||
|
||||
```go
|
||||
reconciler.PreProcessReconcile(ctx, resource)
|
||||
|
||||
reconcileEvent = r.reconciler.ReconcileKind(ctx, resource)
|
||||
|
||||
reconciler.PostProcessReconcile(ctx, resource)
|
||||
```
|
||||
|
||||
#### Stubs
|
||||
|
||||
To get started, or to use as reference. It is intended to be copied out of the
|
||||
|
|
Loading…
Reference in New Issue