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

Avoid Concurrent writes to ManagedClusterList #1516

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,6 @@ func (r *PlacementRuleReconciler) Reconcile(ctx context.Context, req ctrl.Reques
reqLogger := log.WithValues("Request.Namespace", req.Namespace, "Request.Name", req.Name)
reqLogger.Info("Reconciling PlacementRule")

// ACM 8509: Special case for hub/local cluster metrics collection
// We want to ensure that the local-cluster is always in the managedClusterList
// In the case when hubSelfManagement is enabled, we will delete it from the list and modify the object
// to cater to the use case of deploying in open-cluster-management-observability namespace
delete(managedClusterList, "local-cluster")
if _, ok := managedClusterList["local-cluster"]; !ok {
obj := &clusterv1.ManagedCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "local-cluster",
Namespace: config.GetDefaultNamespace(),
Labels: map[string]string{
"openshiftVersion": "mimical",
},
},
}
installMetricsWithoutAddon = true
updateManagedClusterList(obj)
}

if config.GetMonitoringCRName() == "" {
reqLogger.Info("multicluster observability resource is not available")
return ctrl.Result{}, nil
Expand Down Expand Up @@ -139,6 +120,27 @@ func (r *PlacementRuleReconciler) Reconcile(ctx context.Context, req ctrl.Reques
return ctrl.Result{}, nil
}

// ACM 8509: Special case for hub/local cluster metrics collection
// We want to ensure that the local-cluster is always in the managedClusterList
// In the case when hubSelfManagement is enabled, we will delete it from the list and modify the object
// to cater to the use case of deploying in open-cluster-management-observability namespace
if req.Name == "local-cluster" {
//if _, ok := managedClusterList["local-cluster"]; !ok {
// obj := &clusterv1.ManagedCluster{
// ObjectMeta: metav1.ObjectMeta{
// Name: "local-cluster",
// Namespace: config.GetDefaultNamespace(),
// Labels: map[string]string{
// "openshiftVersion": "mimical",
// },
// },
// }
//
// updateManagedClusterList(obj)
//}
reqLogger.Info("Coleen request for local cluster managed cluster")
installMetricsWithoutAddon = true
}
if !deleteAll && !mco.Spec.ObservabilityAddonSpec.EnableMetrics {
reqLogger.Info("EnableMetrics is set to false. Delete Observability addons")
deleteAll = true
Expand Down Expand Up @@ -179,24 +181,24 @@ func (r *PlacementRuleReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}

if !deleteAll && installMetricsWithoutAddon {
obsAddonList.Items = append(obsAddonList.Items, mcov1beta1.ObservabilityAddon{
ObjectMeta: metav1.ObjectMeta{
Name: obsAddonName,
Namespace: config.GetDefaultNamespace(),
Labels: map[string]string{
ownerLabelKey: ownerLabelValue,
},
},
})
//obsAddonList.Items = append(obsAddonList.Items, mcov1beta1.ObservabilityAddon{
// ObjectMeta: metav1.ObjectMeta{
// Name: obsAddonName,
// Namespace: config.GetDefaultNamespace(),
// Labels: map[string]string{
// ownerLabelKey: ownerLabelValue,
// },
// },
//})
err = deleteObsAddon(r.Client, localClusterName)
if err != nil {
log.Error(err, "Failed to delete observabilityaddon")
return ctrl.Result{}, err
}
}
if operatorconfig.IsMCOTerminating {
delete(managedClusterList, "local-cluster")
}
//if operatorconfig.IsMCOTerminating {
// delete(managedClusterList, "local-cluster")
//}

if !deleteAll {
if err := createAllRelatedRes(
Expand Down Expand Up @@ -238,6 +240,7 @@ func (r *PlacementRuleReconciler) Reconcile(ctx context.Context, req ctrl.Reques
latestClusters = append(latestClusters, addon.Namespace)
staleAddons = append(staleAddons, addon.Namespace)
}

for _, work := range workList.Items {
if work.Name != work.Namespace+workNameSuffix {
// ACM 8509: Special case for hub metrics collector
Expand Down Expand Up @@ -293,7 +296,7 @@ func (r *PlacementRuleReconciler) Reconcile(ctx context.Context, req ctrl.Reques

if deleteAll {
// delete managedclusteraddon for local-cluster
err = deleteManagedClusterRes(r.Client, localClusterName)
err = deleteManagedClusterRes(r.Client, config.GetDefaultNamespace())
if err != nil {
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -450,12 +453,10 @@ func createAllRelatedRes(

failedDeleteOba := false
for _, cluster := range clustersToCleanup {
if cluster != config.GetDefaultNamespace() {
err = deleteObsAddon(c, cluster)
if err != nil {
failedDeleteOba = true
log.Error(err, "Failed to delete observabilityaddon", "namespace", cluster)
}
err = deleteObsAddon(c, cluster)
if err != nil {
failedDeleteOba = true
log.Error(err, "Failed to delete observabilityaddon", "namespace", cluster)
}
}

Expand Down Expand Up @@ -614,6 +615,10 @@ func areManagedClusterLabelsReady(obj client.Object) bool {
func updateManagedClusterList(obj client.Object) {
managedClusterListMutex.Lock()
defer managedClusterListMutex.Unlock()
if obj.GetName() == localClusterName {
managedClusterList[obj.GetName()] = "mimical"
return
}
if version, ok := obj.GetLabels()["openshiftVersion"]; ok {
managedClusterList[obj.GetName()] = version
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ func getClusterPreds() predicate.Funcs {
//ACM 8509: Special case for local-cluster, we do not react changes to
//local-cluster as it is expected to be always present in the managed cluster list
//whether hubSelfManagement is enabled or not
if e.Object.GetName() == "local-cluster" {
return false
}
//if e.Object.GetName() == "local-cluster" {
// return true
//}

if isAutomaticAddonInstallationDisabled(e.Object) {
return false
Expand All @@ -46,9 +46,9 @@ func getClusterPreds() predicate.Funcs {
updateFunc := func(e event.UpdateEvent) bool {
log.Info("UpdateFunc", "managedCluster", e.ObjectNew.GetName())

if e.ObjectNew.GetName() == "local-cluster" {
return false
}
//if e.ObjectNew.GetName() == "local-cluster" {
// return true
//}

if e.ObjectNew.GetResourceVersion() == e.ObjectOld.GetResourceVersion() {
return false
Expand Down Expand Up @@ -79,10 +79,6 @@ func getClusterPreds() predicate.Funcs {
deleteFunc := func(e event.DeleteEvent) bool {
log.Info("DeleteFunc", "managedCluster", e.Object.GetName())

if e.Object.GetName() == "local-cluster" {
return false
}

if isAutomaticAddonInstallationDisabled(e.Object) {
return false
}
Expand Down