Skip to content

Commit

Permalink
Wait for vendor label on managedcluster object (stolostron#1476)
Browse files Browse the repository at this point in the history
* wait for vendor label on managedcluster object

Signed-off-by: Coleen Iona Quadros <[email protected]>

* wait for vendor label on managedcluster object

Signed-off-by: Coleen Iona Quadros <[email protected]>

* fix test

Signed-off-by: Coleen Iona Quadros <[email protected]>

* do not create managedclusteraddon for local cluster

Signed-off-by: Coleen Iona Quadros <[email protected]>

* refactor

Signed-off-by: Coleen Iona Quadros <[email protected]>

* refactor

Signed-off-by: Coleen Iona Quadros <[email protected]>

* refactor

Signed-off-by: Coleen Iona Quadros <[email protected]>

* logs

Signed-off-by: Coleen Iona Quadros <[email protected]>

* refactor

Signed-off-by: Coleen Iona Quadros <[email protected]>

* refactor

Signed-off-by: Coleen Iona Quadros <[email protected]>

* refactor

Signed-off-by: Coleen Iona Quadros <[email protected]>

* refactor

Signed-off-by: Coleen Iona Quadros <[email protected]>

* logs

Signed-off-by: Coleen Iona Quadros <[email protected]>

* logs

Signed-off-by: Coleen Iona Quadros <[email protected]>

* refactor

Signed-off-by: Coleen Iona Quadros <[email protected]>

* refactor

Signed-off-by: Coleen Iona Quadros <[email protected]>

* refactor

Signed-off-by: Coleen Iona Quadros <[email protected]>

---------

Signed-off-by: Coleen Iona Quadros <[email protected]>
  • Loading branch information
coleenquadros authored Jun 17, 2024
1 parent 78d5118 commit c8d3cd4
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func (r *PlacementRuleReconciler) Reconcile(ctx context.Context, req ctrl.Reques
if operatorconfig.IsMCOTerminating {
delete(managedClusterList, "local-cluster")
}

if !deleteAll {
if err := createAllRelatedRes(
r.Client,
Expand Down Expand Up @@ -583,6 +584,21 @@ func deleteManagedClusterRes(c client.Client, namespace string) error {
return nil
}

func areManagedClusterLabelsReady(obj client.Object) bool {
vendor, vendorOk := obj.GetLabels()["vendor"]
openshiftVendor := vendor == "OpenShift"

if _, openshiftVersionPresent := obj.GetLabels()["openshiftVersion"]; openshiftVersionPresent && openshiftVendor {
return true
}
if vendorOk && vendor != "auto-detect" {
return true
}

log.Info("ManagedCluster labels are not ready", "cluster", obj.GetName())
return false

}
func updateManagedClusterList(obj client.Object) {
managedClusterListMutex.Lock()
defer managedClusterListMutex.Unlock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ func getClusterPreds() predicate.Funcs {
if isAutomaticAddonInstallationDisabled(e.Object) {
return false
}
if e.Object.GetName() != "local-cluster" {
updateManagedClusterList(e.Object)
}
updateManagedClusterImageRegistry(e.Object)
if !areManagedClusterLabelsReady(e.Object) {
return false
}
updateManagedClusterList(e.Object)

return true
}
Expand Down Expand Up @@ -65,8 +66,11 @@ func getClusterPreds() predicate.Funcs {
delete(managedClusterImageRegistry, e.ObjectNew.GetName())
managedClusterImageRegistryMutex.Unlock()
} else {
updateManagedClusterList(e.ObjectNew)
updateManagedClusterImageRegistry(e.ObjectNew)
if !areManagedClusterLabelsReady(e.ObjectNew) {
return false
}
updateManagedClusterList(e.ObjectNew)
}

return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func TestClusterPred(t *testing.T) {
Name: name,
Namespace: c.namespace,
Annotations: c.annotations,
Labels: map[string]string{"vendor": "OpenShift", "openshiftVersion": "4.6.0"},
},
Spec: appsv1.DeploymentSpec{
Replicas: int32Ptr(2),
Expand All @@ -96,6 +97,7 @@ func TestClusterPred(t *testing.T) {
ResourceVersion: "2",
DeletionTimestamp: c.deletionTimestamp,
Annotations: c.annotations,
Labels: map[string]string{"vendor": "OpenShift", "openshiftVersion": "4.6.0"},
},
},
ObjectOld: &appsv1.Deployment{
Expand Down Expand Up @@ -148,6 +150,85 @@ func TestClusterPred(t *testing.T) {
}
}

func TestManagedClusterLabelReady(t *testing.T) {

name := "test-obj"
caseList := []struct {
caseName string
namespace string
annotations map[string]string
deletionTimestamp *metav1.Time
expectedCreate bool
expectedUpdate bool
expectedDelete bool
}{
{
caseName: "ManagedCluster with vendor label",
namespace: testNamespace,
expectedCreate: false,
expectedUpdate: true,
},
}

for _, c := range caseList {
t.Run(c.caseName, func(t *testing.T) {
pred := getClusterPreds()
create_event := event.CreateEvent{
Object: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: c.namespace,
Annotations: c.annotations,
Labels: map[string]string{"vendor": "auto-detect"},
},
Spec: appsv1.DeploymentSpec{
Replicas: int32Ptr(2),
},
},
}

if c.expectedCreate {
if !pred.CreateFunc(create_event) {
t.Fatalf("pre func return false on applied createevent in case: (%v)", c.caseName)
}
} else {
if pred.CreateFunc(create_event) {
t.Fatalf("pre func return true on non-applied createevent in case: (%v)", c.caseName)
}
}
update_event := event.UpdateEvent{
ObjectNew: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: c.namespace,
ResourceVersion: "2",
DeletionTimestamp: c.deletionTimestamp,
Annotations: c.annotations,
Labels: map[string]string{"vendor": "OpenShift", "openshiftVersion": "4.6.0"},
},
},
ObjectOld: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: c.namespace,
ResourceVersion: "1",
},
},
}

if c.expectedUpdate {
if !pred.UpdateFunc(update_event) {
t.Fatalf("pre func return false on applied update event in case: (%v)", c.caseName)
}
} else {
if pred.UpdateFunc(update_event) {
t.Fatalf("pre func return true on non-applied updateevent in case: (%v)", c.caseName)
}
}
})
}
}

func TestAddOnDeploymentConfigPredicate(t *testing.T) {
name := "test-obj"
caseList := []struct {
Expand Down

0 comments on commit c8d3cd4

Please sign in to comment.