-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add predicates for GitRepo and ImagePolicy watches
ImageUpdateAutomationReconciler watches GitRepository and ImagePolicy kinds for every event. This leads to unnecessary extra reconciliations at times. For example when the controller starts with existing resources, the same ImageUpdateAutomation object gets reconciled at least twice, once due to the watch on ImageUpdateAutomation startup and again due to the watches on GitRepository and ImagePolicy for create event, as they get registered in the cache. Add predicates to filter the ImagePolicy to only allow events for latest image update, and GitRepository to only allow events for change in the source configuration. Signed-off-by: Sunny <[email protected]>
- Loading branch information
Showing
4 changed files
with
295 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
Copyright 2024 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package controller | ||
|
||
import ( | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
|
||
imagev1_reflect "github.com/fluxcd/image-reflector-controller/api/v1beta2" | ||
) | ||
|
||
// latestImageChangePredicate implements a predicate for latest image change. | ||
// This can be used to filter events from ImagePolicies for change in the latest | ||
// image. | ||
type latestImageChangePredicate struct { | ||
predicate.Funcs | ||
} | ||
|
||
func (latestImageChangePredicate) Create(e event.CreateEvent) bool { | ||
return false | ||
} | ||
|
||
func (latestImageChangePredicate) Delete(e event.DeleteEvent) bool { | ||
return false | ||
} | ||
|
||
func (latestImageChangePredicate) Update(e event.UpdateEvent) bool { | ||
if e.ObjectOld == nil || e.ObjectNew == nil { | ||
return false | ||
} | ||
|
||
oldSource, ok := e.ObjectOld.(*imagev1_reflect.ImagePolicy) | ||
if !ok { | ||
return false | ||
} | ||
|
||
newSource, ok := e.ObjectNew.(*imagev1_reflect.ImagePolicy) | ||
if !ok { | ||
return false | ||
} | ||
|
||
if oldSource.Status.LatestImage != newSource.Status.LatestImage { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
// sourceConfigChangePredicate implements a predicate for source configuration | ||
// change. This can be used to filter events from source objects for change in | ||
// source configuration. | ||
type sourceConfigChangePredicate struct { | ||
predicate.Funcs | ||
} | ||
|
||
func (sourceConfigChangePredicate) Create(e event.CreateEvent) bool { | ||
return false | ||
} | ||
|
||
func (sourceConfigChangePredicate) Delete(e event.DeleteEvent) bool { | ||
return false | ||
} | ||
|
||
func (sourceConfigChangePredicate) Update(e event.UpdateEvent) bool { | ||
if e.ObjectOld == nil || e.ObjectNew == nil { | ||
return false | ||
} | ||
|
||
return e.ObjectOld.GetGeneration() != e.ObjectNew.GetGeneration() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
Copyright 2024 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package controller | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
|
||
imagev1_reflect "github.com/fluxcd/image-reflector-controller/api/v1beta2" | ||
sourcev1 "github.com/fluxcd/source-controller/api/v1" | ||
) | ||
|
||
func Test_latestImageChangePredicate_Update(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
beforeFunc func(oldObj, newObj *imagev1_reflect.ImagePolicy) | ||
want bool | ||
}{ | ||
{ | ||
name: "no latest image", | ||
beforeFunc: func(oldObj, newObj *imagev1_reflect.ImagePolicy) { | ||
oldObj.Status.LatestImage = "" | ||
newObj.Status.LatestImage = "" | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "new image, no old image", | ||
beforeFunc: func(oldObj, newObj *imagev1_reflect.ImagePolicy) { | ||
oldObj.Status.LatestImage = "" | ||
newObj.Status.LatestImage = "foo" | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "different old and new image", | ||
beforeFunc: func(oldObj, newObj *imagev1_reflect.ImagePolicy) { | ||
oldObj.Status.LatestImage = "bar" | ||
newObj.Status.LatestImage = "foo" | ||
}, | ||
want: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
oldObj := &imagev1_reflect.ImagePolicy{} | ||
newObj := oldObj.DeepCopy() | ||
if tt.beforeFunc != nil { | ||
tt.beforeFunc(oldObj, newObj) | ||
} | ||
e := event.UpdateEvent{ | ||
ObjectOld: oldObj, | ||
ObjectNew: newObj, | ||
} | ||
p := latestImageChangePredicate{} | ||
g.Expect(p.Update(e)).To(Equal(tt.want)) | ||
}) | ||
} | ||
} | ||
|
||
func Test_sourceConfigChangePredicate_Update(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
beforeFunc func(oldObj, newObj *sourcev1.GitRepository) | ||
want bool | ||
}{ | ||
{ | ||
name: "no generation change, same config", | ||
beforeFunc: func(oldObj, newObj *sourcev1.GitRepository) { | ||
oldObj.Generation = 0 | ||
newObj.Generation = 0 | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "new generation, config change", | ||
beforeFunc: func(oldObj, newObj *sourcev1.GitRepository) { | ||
oldObj.Generation = 1 | ||
newObj.Generation = 2 | ||
}, | ||
want: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
oldObj := &sourcev1.GitRepository{} | ||
newObj := oldObj.DeepCopy() | ||
if tt.beforeFunc != nil { | ||
tt.beforeFunc(oldObj, newObj) | ||
} | ||
e := event.UpdateEvent{ | ||
ObjectOld: oldObj, | ||
ObjectNew: newObj, | ||
} | ||
p := sourceConfigChangePredicate{} | ||
g.Expect(p.Update(e)).To(Equal(tt.want)) | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters