Skip to content

Commit

Permalink
add a helper func to check if a managed cluster condition changes (#230)
Browse files Browse the repository at this point in the history
Signed-off-by: zhujian <jiazhu@redhat.com>
  • Loading branch information
zhujian7 authored Jan 19, 2024
1 parent 44852ea commit d2afcef
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 1 deletion.
30 changes: 29 additions & 1 deletion pkg/utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,36 @@ func MapValueChanged(old, new map[string]string, key string) bool {
// ClusterImageRegistriesAnnotationChanged returns true if the value of the ClusterImageRegistriesAnnotationKey
// in the new managed cluster annotation is different from the old managed cluster annotation
func ClusterImageRegistriesAnnotationChanged(old, new *clusterv1.ManagedCluster) bool {
return ClusterAnnotationChanged(old, new, clusterv1.ClusterImageRegistriesAnnotationKey)
}

// ClusterAnnotationChanged returns true if the value of the specified annotation in the new managed cluster annotation
// is different from the old managed cluster annotation
func ClusterAnnotationChanged(old, new *clusterv1.ManagedCluster, annotation string) bool {
if new == nil || old == nil {
return false
}
return MapValueChanged(old.Annotations, new.Annotations, clusterv1.ClusterImageRegistriesAnnotationKey)
return MapValueChanged(old.Annotations, new.Annotations, annotation)
}

// ClusterAvailableConditionChanged returns true if the value of the Available condition in the new managed cluster
// is different from the old managed cluster
func ClusterAvailableConditionChanged(old, new *clusterv1.ManagedCluster) bool {
return ClusterConditionChanged(old, new, clusterv1.ManagedClusterConditionAvailable)
}

// ClusterAvailableConditionChanged returns true if the value of the specified conditionType in the new managed cluster
// is different from the old managed cluster
func ClusterConditionChanged(old, new *clusterv1.ManagedCluster, conditionType string) bool {
if new == nil || old == nil {
return false
}

oldAvailableCondition := meta.FindStatusCondition(old.Status.Conditions, conditionType)
newAvailableCondition := meta.FindStatusCondition(new.Status.Conditions, conditionType)

return (oldAvailableCondition == nil && newAvailableCondition != nil) ||
(oldAvailableCondition != nil && newAvailableCondition == nil) ||
(oldAvailableCondition != nil && newAvailableCondition != nil &&
oldAvailableCondition.Status != newAvailableCondition.Status)
}
129 changes: 129 additions & 0 deletions pkg/utils/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,132 @@ func TestClusterImageRegistriesAnnotationChanged(t *testing.T) {
})
}
}

func TestClusterAvailableConditionChanged(t *testing.T) {
cases := []struct {
name string
old *clusterv1.ManagedCluster
new *clusterv1.ManagedCluster
expected bool
}{
{
name: "condition not changed",
old: &clusterv1.ManagedCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Status: clusterv1.ManagedClusterStatus{
Conditions: []metav1.Condition{
{
Type: clusterv1.ManagedClusterConditionAvailable,
Status: metav1.ConditionTrue,
Message: "test",
},
},
},
},
new: &clusterv1.ManagedCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Status: clusterv1.ManagedClusterStatus{
Conditions: []metav1.Condition{
{
Type: clusterv1.ManagedClusterConditionAvailable,
Status: metav1.ConditionTrue,
Message: "message changed",
},
},
},
},
expected: false,
},
{
name: "old condition not present",
old: &clusterv1.ManagedCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
},
new: &clusterv1.ManagedCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Status: clusterv1.ManagedClusterStatus{
Conditions: []metav1.Condition{
{
Type: clusterv1.ManagedClusterConditionAvailable,
Status: metav1.ConditionTrue,
Message: "test",
},
},
},
},
expected: true,
},
{
name: "new condition not present",
old: &clusterv1.ManagedCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Status: clusterv1.ManagedClusterStatus{
Conditions: []metav1.Condition{
{
Type: clusterv1.ManagedClusterConditionAvailable,
Status: metav1.ConditionUnknown,
Message: "test",
},
},
},
},
new: &clusterv1.ManagedCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
},
expected: true,
},
{
name: "condition changed",
old: &clusterv1.ManagedCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Status: clusterv1.ManagedClusterStatus{
Conditions: []metav1.Condition{
{
Type: clusterv1.ManagedClusterConditionAvailable,
Status: metav1.ConditionUnknown,
Message: "test",
},
},
},
},
new: &clusterv1.ManagedCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Status: clusterv1.ManagedClusterStatus{
Conditions: []metav1.Condition{
{
Type: clusterv1.ManagedClusterConditionAvailable,
Status: metav1.ConditionTrue,
Message: "test",
},
},
},
},
expected: true,
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := ClusterAvailableConditionChanged(c.old, c.new)
if actual != c.expected {
t.Errorf("name %s: expected %v, but got %v", c.name, c.expected, actual)
}
})
}
}

0 comments on commit d2afcef

Please sign in to comment.