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

[ACM-7674] Updated MCE operand namespace to support openshift.io/cluster-monitor… #517

Merged
merged 6 commits into from
Sep 29, 2023
Merged
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
45 changes: 45 additions & 0 deletions controllers/backplaneconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,18 @@ func (r *MultiClusterEngineReconciler) Reconcile(ctx context.Context, req ctrl.R
return ctrl.Result{Requeue: true}, err
}

/*
In MCE 2.4, we need to ensure that the openshift.io/cluster-monitoring is added to the same namespace as the
MultiClusterEngine to avoid conflicts with the openshift-* namespace when deploying PrometheusRules and
ServiceMonitors in ACM and MCE.
*/
result, err = r.ensureOpenShiftNamespaceLabel(ctx, backplaneConfig)
if err != nil {
log.Error(err, "Failed to add to %s label to namespace: %s", utils.OpenShiftClusterMonitoringLabel,
backplaneConfig.Spec.TargetNamespace)
return ctrl.Result{}, err
}

if !utils.ShouldIgnoreOCPVersion(backplaneConfig) {
currentOCPVersion, err := r.getClusterVersion(ctx)
if err != nil {
Expand Down Expand Up @@ -821,6 +833,39 @@ func (r *MultiClusterEngineReconciler) ensureCustomResources(ctx context.Context
return ctrl.Result{}, nil
}

func (r *MultiClusterEngineReconciler) ensureOpenShiftNamespaceLabel(ctx context.Context,
backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) {

log := log.FromContext(ctx)
existingNs := &corev1.Namespace{}

err := r.Client.Get(ctx, types.NamespacedName{Name: backplaneConfig.Spec.TargetNamespace}, existingNs)
if err != nil || apierrors.IsNotFound(err) {
log.Error(err, fmt.Sprintf("Failed to find namespace for MultiClusterEngine: %s",
backplaneConfig.Spec.TargetNamespace))
return ctrl.Result{Requeue: true}, err
}

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

if _, ok := existingNs.Labels[utils.OpenShiftClusterMonitoringLabel]; !ok {
log.Info(fmt.Sprintf("Adding label: %s to namespace: %s", utils.OpenShiftClusterMonitoringLabel,
backplaneConfig.Spec.TargetNamespace))
existingNs.Labels[utils.OpenShiftClusterMonitoringLabel] = "true"

err = r.Client.Update(ctx, existingNs)
if err != nil {
log.Error(err, fmt.Sprintf("Failed to update namespace for MultiClusterEngine: %s with the label: %s",
backplaneConfig.Spec.TargetNamespace, utils.OpenShiftClusterMonitoringLabel))
return ctrl.Result{Requeue: true}, err
}
}

return ctrl.Result{}, nil
}

func (r *MultiClusterEngineReconciler) finalizeBackplaneConfig(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine) error {
log := log.FromContext(ctx)

Expand Down
23 changes: 20 additions & 3 deletions controllers/backplaneconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -379,7 +380,6 @@ var _ = Describe("BackplaneConfig controller", func() {
Context("and no image pull policy is specified", func() {
It("should deploy sub components", func() {
createCtx := context.Background()
By("creating the backplane config")
backplaneConfig := &v1.MultiClusterEngine{
TypeMeta: metav1.TypeMeta{
APIVersion: "multicluster.openshift.io/v1",
Expand All @@ -393,8 +393,27 @@ var _ = Describe("BackplaneConfig controller", func() {
ImagePullSecret: "testsecret",
},
}

By("creating the backplane config")
Expect(k8sClient.Create(createCtx, backplaneConfig)).Should(Succeed())

By("ensuring that no openshift.io/cluster-monitoring label is enabled if MCE does not exist")
backplaneConfig2 := &v1.MultiClusterEngine{
TypeMeta: metav1.TypeMeta{
APIVersion: "multicluster.openshift.io/v1",
Kind: "MultiClusterEngine",
},
ObjectMeta: metav1.ObjectMeta{
Name: BackplaneConfigName,
},
Spec: v1.MultiClusterEngineSpec{
TargetNamespace: "test-n2",
},
}

res, _ := reconciler.ensureOpenShiftNamespaceLabel(createCtx, backplaneConfig2)
Expect(res).To(Equal(ctrl.Result{Requeue: true}))

By("ensuring each deployment and config is created")
for _, test := range tests {
By(fmt.Sprintf("ensuring %s is created", test.Name))
Expand Down Expand Up @@ -978,7 +997,6 @@ var _ = Describe("BackplaneConfig controller", func() {

g.Expect(existingMCE.Status.Phase).To(Not(Equal(v1.MultiClusterEnginePhaseError)))
}, timeout, interval).Should(Succeed())

})
})

Expand Down Expand Up @@ -1033,6 +1051,5 @@ var _ = Describe("BackplaneConfig controller", func() {

})
})

})
})
3 changes: 3 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (

const (
UnitTestEnvVar = "UNIT_TEST"

// OpenShiftClusterMonitoringLabel is the label for OpenShift cluster monitoring.
OpenShiftClusterMonitoringLabel = "openshift.io/cluster-monitoring"
)

var onComponents = []string{
Expand Down