Skip to content

Commit

Permalink
Improve IgnoreSameContentPredicate to handle Deployment obj correctly
Browse files Browse the repository at this point in the history
Predicate now will ignore following fields for Deployment obj:
- metadata.generation
- status.observedGeneration
- metadata.annotations["deployment.kubernetes.io/revision"]

Signed-off-by: Yury Kulazhenkov <ykulazhenkov@nvidia.com>
  • Loading branch information
ykulazhenkov committed Jun 6, 2023
1 parent e1267e8 commit b7ab849
Showing 1 changed file with 23 additions and 0 deletions.
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)
}

0 comments on commit b7ab849

Please sign in to comment.