Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-2.1] Allow GeneratingHandlers to skip Apply if resource version didn't change #356

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
submodules: recursive

Expand All @@ -32,7 +32,7 @@ jobs:
export PATH=$PATH:/home/runner/go/bin/

- name: golangci-lint
uses: golangci/golangci-lint-action@v3.4.0
uses: golangci/golangci-lint-action@v3.7.0
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.51
version: v1.55
5 changes: 3 additions & 2 deletions pkg/apply/fake/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ var _ apply.Apply = (*FakeApply)(nil)

type FakeApply struct {
Objects []*objectset.ObjectSet
Count int
}

func (f *FakeApply) Apply(set *objectset.ObjectSet) error {
f.Objects = append(f.Objects, set)
f.Count++
return nil
}

func (f *FakeApply) ApplyObjects(objs ...runtime.Object) error {
os := objectset.NewObjectSet()
os.Add(objs...)
f.Objects = append(f.Objects, os)
return nil
return f.Apply(os)
}

func (f *FakeApply) WithCacheTypes(igs ...apply.InformerGetter) apply.Apply {
Expand Down
56 changes: 51 additions & 5 deletions pkg/controller-gen/generators/type_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,14 @@ type {{.type}}Cache interface {
}

{{ if .hasStatus -}}
// {{.type}}StatusHandler is executed for every added or modified {{.type}}. Should return the new status to be updated
type {{.type}}StatusHandler func(obj *{{.version}}.{{.type}}, status {{.version}}.{{.statusType}}) ({{.version}}.{{.statusType}}, error)

// {{.type}}GeneratingHandler is the top-level handler that is executed for every {{.type}} event. It extends {{.type}}StatusHandler by a returning a slice of child objects to be passed to apply.Apply
type {{.type}}GeneratingHandler func(obj *{{.version}}.{{.type}}, status {{.version}}.{{.statusType}}) ([]runtime.Object, {{.version}}.{{.statusType}}, error)

// Register{{.type}}StatusHandler configures a {{.type}}Controller to execute a {{.type}}StatusHandler for every events observed.
// If a non-empty condition is provided, it will be updated in the status conditions for every handler execution
func Register{{.type}}StatusHandler(ctx context.Context, controller {{.type}}Controller, condition condition.Cond, name string, handler {{.type}}StatusHandler) {
statusHandler := &{{.lowerName}}StatusHandler{
client: controller,
Expand All @@ -111,6 +115,8 @@ func Register{{.type}}StatusHandler(ctx context.Context, controller {{.type}}Con
controller.AddGenericHandler(ctx, name, generic.FromObjectHandlerToHandler(statusHandler.sync))
}

// Register{{.type}}GeneratingHandler configures a {{.type}}Controller to execute a {{.type}}GeneratingHandler for every events observed, passing the returned objects to the provided apply.Apply.
// If a non-empty condition is provided, it will be updated in the status conditions for every handler execution
func Register{{.type}}GeneratingHandler(ctx context.Context, controller {{.type}}Controller, apply apply.Apply,
condition condition.Cond, name string, handler {{.type}}GeneratingHandler, opts *generic.GeneratingHandlerOptions) {
statusHandler := &{{.lowerName}}GeneratingHandler{
Expand All @@ -132,6 +138,7 @@ type {{.lowerName}}StatusHandler struct {
handler {{.type}}StatusHandler
}

// sync is executed on every resource addition or modification. Executes the configured handlers and sends the updated status to the Kubernetes API
func (a *{{.lowerName}}StatusHandler) sync(key string, obj *{{.version}}.{{.type}}) (*{{.version}}.{{.type}}, error) {
if obj == nil {
return obj, nil
Expand Down Expand Up @@ -173,12 +180,14 @@ func (a *{{.lowerName}}StatusHandler) sync(key string, obj *{{.version}}.{{.type

type {{.lowerName}}GeneratingHandler struct {
{{.type}}GeneratingHandler
apply apply.Apply
opts generic.GeneratingHandlerOptions
gvk schema.GroupVersionKind
name string
apply apply.Apply
opts generic.GeneratingHandlerOptions
gvk schema.GroupVersionKind
name string
seen sync.Map
}

// Remove handles the observed deletion of a resource, cascade deleting every associated resource previously applied
func (a *{{.lowerName}}GeneratingHandler) Remove(key string, obj *{{.version}}.{{.type}}) (*{{.version}}.{{.type}}, error) {
if obj != nil {
return obj, nil
Expand All @@ -188,12 +197,17 @@ func (a *{{.lowerName}}GeneratingHandler) Remove(key string, obj *{{.version}}.{
obj.Namespace, obj.Name = kv.RSplit(key, "/")
obj.SetGroupVersionKind(a.gvk)

if a.opts.UniqueApplyForResourceVersion {
a.seen.Delete(key)
}

return nil, generic.ConfigureApplyForObject(a.apply, obj, &a.opts).
WithOwner(obj).
WithSetID(a.name).
ApplyObjects()
}

// Handle executes the configured {{.type}}GeneratingHandler and pass the resulting objects to apply.Apply, finally returning the new status of the resource
func (a *{{.lowerName}}GeneratingHandler) Handle(obj *{{.version}}.{{.type}}, status {{.version}}.{{.statusType}}) ({{.version}}.{{.statusType}}, error) {
if !obj.DeletionTimestamp.IsZero() {
return status, nil
Expand All @@ -203,11 +217,43 @@ func (a *{{.lowerName}}GeneratingHandler) Handle(obj *{{.version}}.{{.type}}, st
if err != nil {
return newStatus, err
}
if !a.isNewResourceVersion(obj) {
return newStatus, nil
}

return newStatus, generic.ConfigureApplyForObject(a.apply, obj, &a.opts).
err = generic.ConfigureApplyForObject(a.apply, obj, &a.opts).
WithOwner(obj).
WithSetID(a.name).
ApplyObjects(objs...)
if err != nil {
return newStatus, err
}
a.storeResourceVersion(obj)
return newStatus, nil
}

// isNewResourceVersion detects if a specific resource version was already successfully processed.
// Only used if UniqueApplyForResourceVersion is set in generic.GeneratingHandlerOptions
func (a *{{.lowerName}}GeneratingHandler) isNewResourceVersion(obj *{{.version}}.{{.type}}) bool {
if !a.opts.UniqueApplyForResourceVersion {
return true
}

// Apply once per resource version
key := obj.Namespace + "/" + obj.Name
previous, ok := a.seen.Load(key)
return !ok || previous != obj.ResourceVersion
}

// storeResourceVersion keeps track of the latest resource version of an object for which Apply was executed
// Only used if UniqueApplyForResourceVersion is set in generic.GeneratingHandlerOptions
func (a *{{.lowerName}}GeneratingHandler) storeResourceVersion(obj *{{.version}}.{{.type}}) {
if !a.opts.UniqueApplyForResourceVersion {
return
}

key := obj.Namespace + "/" + obj.Name
a.seen.Store(key, obj.ResourceVersion)
}
{{- end }}
`
1 change: 1 addition & 0 deletions pkg/controller-gen/generators/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
var (
Imports = []string{
"context",
"sync",
"time",
"k8s.io/client-go/rest",
"github.com/rancher/wrangler/v2/pkg/apply",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading