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

[v23.5.x] Improve IgnoreSameContentPredicate to handle Deployment obj correctly #553

Merged
merged 1 commit into from
Jun 7, 2023
Merged
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
23 changes: 23 additions & 0 deletions controllers/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"reflect"

appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -60,9 +61,20 @@ func (p IgnoreSameContentPredicate) Update(e event.UpdateEvent) bool {
// ignore resource version
oldObj.SetResourceVersion("")
newObj.SetResourceVersion("")
// ignore generation
oldObj.SetGeneration(0)
newObj.SetGeneration(0)
// ignore managed fields
oldObj.SetManagedFields([]metav1.ManagedFieldsEntry{})
newObj.SetManagedFields([]metav1.ManagedFieldsEntry{})

// logic specific to resource type
switch v := oldObj.(type) {
case *appsv1.Deployment:
p.handleDeployment(v, newObj.(*appsv1.Deployment))
default:
}

oldUnstr, err := runtime.DefaultUnstructuredConverter.ToUnstructured(oldObj)
if err != nil {
return false
Expand All @@ -73,3 +85,14 @@ func (p IgnoreSameContentPredicate) Update(e event.UpdateEvent) bool {
}
return !reflect.DeepEqual(oldUnstr, newUnstr)
}

func (p IgnoreSameContentPredicate) handleDeployment(oldObj, newObj *appsv1.Deployment) {
// ignore object if only Observed generation changed
oldObj.Status.ObservedGeneration = 0
newObj.Status.ObservedGeneration = 0

// ignore annotation which is auto set by Kubernetes
revAnnotation := "deployment.kubernetes.io/revision"
delete(oldObj.Annotations, revAnnotation)
delete(newObj.Annotations, revAnnotation)
}