Skip to content

Commit

Permalink
add cluster monitoring label to mco namespace
Browse files Browse the repository at this point in the history
Signed-off-by: Coleen Iona Quadros <[email protected]>
  • Loading branch information
coleenquadros committed Oct 12, 2023
1 parent 73f92a9 commit dcff34c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ const (
)

const (
infoAddingBackupLabel = "adding backup label"
errorAddingBackupLabel = "failed to add backup label"
infoAddingBackupLabel = "adding backup label"
errorAddingBackupLabel = "failed to add backup label"
openShiftClusterMonitoringLabel = "openshift.io/cluster-monitoring"
)

var (
Expand Down Expand Up @@ -249,6 +250,18 @@ func (r *MultiClusterObservabilityReconciler) Reconcile(ctx context.Context, req
}
}

/*
In ACM 2.9, we need to ensure that the openshift.io/cluster-monitoring is added to the same namespace as the
Multi-cluster Observability Operator to avoid conflicts with the openshift-* namespace when deploying PrometheusRules and
ServiceMonitors in ACM.
*/
_, err = r.ensureOpenShiftNamespaceLabel(ctx, instance)
if err != nil {
r.Log.Error(err, "Failed to add to %s label to namespace: %s", openShiftClusterMonitoringLabel,
instance.GetNamespace())
return ctrl.Result{}, err
}

// the route resource won't be created in testing env, for instance, KinD
// in the testing env, the service can be accessed via service name, we assume that
// in testing env, the local-cluster is the only allowed managedcluster
Expand Down Expand Up @@ -940,3 +953,38 @@ func cleanUpClusterScopedResources(

return nil
}

func (r *MultiClusterObservabilityReconciler) ensureOpenShiftNamespaceLabel(ctx context.Context,
m *mcov1beta2.MultiClusterObservability) (reconcile.Result, error) {

log := logf.FromContext(ctx)
existingNs := &corev1.Namespace{}
resNS := m.GetNamespace()
if resNS == "" {
resNS = config.GetDefaultNamespace()
}

err := r.Client.Get(ctx, types.NamespacedName{Name: resNS}, existingNs)
if err != nil || errors.IsNotFound(err) {
log.Error(err, fmt.Sprintf("Failed to find namespace for Multicluster Operator: %s", m.GetNamespace()))
return reconcile.Result{Requeue: true}, err
}

if existingNs.ObjectMeta.Labels == nil || len(existingNs.ObjectMeta.Labels) == 0 {
existingNs.ObjectMeta.Labels = make(map[string]string)
}

if _, ok := existingNs.ObjectMeta.Labels[config.OpenShiftClusterMonitoringlabel]; !ok {
log.Info(fmt.Sprintf("Adding label: %s to namespace: %s", config.OpenShiftClusterMonitoringlabel, resNS))
existingNs.ObjectMeta.Labels[openShiftClusterMonitoringLabel] = "true"

err = r.Client.Update(ctx, existingNs)
if err != nil {
log.Error(err, fmt.Sprintf("Failed to update namespace for MultiClusterHub: %s with the label: %s",
m.GetNamespace(), config.OpenShiftClusterMonitoringlabel))
return reconcile.Result{Requeue: true}, err
}
}

return reconcile.Result{}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,18 @@ func TestMultiClusterMonitoringCRUpdate(t *testing.T) {
//wait for update status
time.Sleep(1 * time.Second)

//verify openshiftcluster monitoring label is set to true in namespace
updatedNS := &corev1.Namespace{}
err = cl.Get(context.TODO(), types.NamespacedName{
Name: namespace,
}, updatedNS)
if err != nil {
t.Fatalf("Failed to get namespace: (%v)", err)
}
if _, ok := updatedNS.Labels[config.OpenShiftClusterMonitoringlabel]; !ok {
t.Fatalf("Failed to get correct namespace label, expect true")
}

updatedMCO := &mcov1beta2.MultiClusterObservability{}
err = cl.Get(context.TODO(), req.NamespacedName, updatedMCO)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ func updateReadyStatus(

// setStatusCondition sets the corresponding condition in conditions to newCondition.
// conditions must be non-nil.
// 1. if the condition of the specified type already exists (all fields of the existing condition are updated to
// newCondition, LastTransitionTime is set to now if the new status differs from the old status)
// 2. if a condition of the specified type does not exist (LastTransitionTime is set to now() if unset,
// and newCondition is appended)
// 1. if the condition of the specified type already exists (all fields of the existing condition are updated to
// newCondition, LastTransitionTime is set to now if the new status differs from the old status)
// 2. if a condition of the specified type does not exist (LastTransitionTime is set to now() if unset,
// and newCondition is appended)
func setStatusCondition(conditions *[]mcoshared.Condition, newCondition mcoshared.Condition) {
if conditions == nil {
return
Expand Down
1 change: 1 addition & 0 deletions operators/multiclusterobservability/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const (
WebhookServiceName = "multicluster-observability-webhook-service"
BackupLabelName = "cluster.open-cluster-management.io/backup"
BackupLabelValue = ""
OpenShiftClusterMonitoringlabel = "openshift.io/cluster-monitoring"
)

const (
Expand Down

0 comments on commit dcff34c

Please sign in to comment.