Skip to content

Commit

Permalink
Add a logic for RolloutManager to create ServiceMonitor
Browse files Browse the repository at this point in the history
Signed-off-by: Rizwana777 <rizwananaaz177@gmail.com>
  • Loading branch information
Rizwana777 committed Apr 14, 2024
1 parent d479a9c commit 904ae66
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 62 deletions.
142 changes: 83 additions & 59 deletions controllers/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,85 +280,109 @@ var _ = Describe("Resource creation and cleanup tests", func() {
}
})

It("Verify whether RolloutManager creating ServiceMonitor", func() {
smCRD, existingSvc := serviceAndServiceMonitorCRD(req.Namespace)
Expect(r.Client.Create(ctx, smCRD)).To(Succeed())
Expect(r.Client.Create(ctx, existingSvc)).To(Succeed())

res, err := r.Reconcile(ctx, req)
Expect(err).ToNot(HaveOccurred())
Expect(res.Requeue).Should(BeFalse(), "reconcile should not requeue request")
Context("ServiceMonitor Creation test", func() {
var (
ctx context.Context
a *v1alpha1.RolloutManager
r *RolloutManagerReconciler
req reconcile.Request
)

expectedServiceMonitor := serviceMonitor()
BeforeEach(func() {
ctx = context.Background()
a = makeTestRolloutManager()
r = makeTestReconciler(a)
err := createNamespace(r, a.Namespace)
Expect(err).ToNot(HaveOccurred())
req = reconcile.Request{
NamespacedName: types.NamespacedName{
Name: a.Name,
Namespace: a.Namespace,
},
}
})

sm := &monitoringv1.ServiceMonitor{}
Expect(r.Client.Get(ctx, types.NamespacedName{
Name: DefaultArgoRolloutsMetricsServiceName,
Namespace: testNamespace,
}, sm)).To(Succeed())
It("Verify whether RolloutManager creating ServiceMonitor", func() {
smCRD, existingSvc := serviceAndServiceMonitorCRD(req.Namespace)
Expect(r.Client.Create(ctx, smCRD)).To(Succeed())
Expect(r.Client.Create(ctx, existingSvc)).To(Succeed())

Expect(sm.Name).To(Equal(expectedServiceMonitor.Name))
Expect(sm.Namespace).To(Equal(expectedServiceMonitor.Namespace))
Expect(sm.Spec).To(Equal(expectedServiceMonitor.Spec))
})
res, err := r.Reconcile(ctx, req)
Expect(err).ToNot(HaveOccurred())
Expect(res.Requeue).Should(BeFalse(), "reconcile should not requeue request")

It("Verify if ServiceMonitor exists, but has different content than we expect then it should update ServiceMonitor", func() {
smCRD, existingSvc := serviceAndServiceMonitorCRD(req.Namespace)
Expect(r.Client.Create(ctx, smCRD)).To(Succeed())
Expect(r.Client.Create(ctx, existingSvc)).To(Succeed())
expectedServiceMonitor := serviceMonitor()

existingServiceMonitor := &monitoringv1.ServiceMonitor{
ObjectMeta: metav1.ObjectMeta{
sm := &monitoringv1.ServiceMonitor{}
Expect(r.Client.Get(ctx, types.NamespacedName{
Name: DefaultArgoRolloutsMetricsServiceName,
Namespace: testNamespace,
},
Spec: monitoringv1.ServiceMonitorSpec{
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"app.kubernetes.io/name": "test-label",
},
}, sm)).To(Succeed())

Expect(sm.Name).To(Equal(expectedServiceMonitor.Name))
Expect(sm.Namespace).To(Equal(expectedServiceMonitor.Namespace))
Expect(sm.Spec).To(Equal(expectedServiceMonitor.Spec))
})

It("Verify if ServiceMonitor exists, but has different content than we expect then it should update ServiceMonitor", func() {
smCRD, existingSvc := serviceAndServiceMonitorCRD(req.Namespace)
Expect(r.Client.Create(ctx, smCRD)).To(Succeed())
Expect(r.Client.Create(ctx, existingSvc)).To(Succeed())

existingServiceMonitor := &monitoringv1.ServiceMonitor{
ObjectMeta: metav1.ObjectMeta{
Name: DefaultArgoRolloutsMetricsServiceName,
Namespace: testNamespace,
},
Endpoints: []monitoringv1.Endpoint{
{
Port: "metrics-test",
Spec: monitoringv1.ServiceMonitorSpec{
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"app.kubernetes.io/name": "test-label",
},
},
Endpoints: []monitoringv1.Endpoint{
{
Port: "metrics-test",
},
},
},
},
}
}

Expect(r.Client.Create(ctx, existingServiceMonitor)).To(Succeed())
Expect(r.Client.Create(ctx, existingServiceMonitor)).To(Succeed())

res, err := r.Reconcile(ctx, req)
Expect(err).ToNot(HaveOccurred())
Expect(res.Requeue).Should(BeFalse(), "reconcile should not requeue request")
res, err := r.Reconcile(ctx, req)
Expect(err).ToNot(HaveOccurred())
Expect(res.Requeue).Should(BeFalse(), "reconcile should not requeue request")

expectedSM := serviceMonitor()
expectedSM := serviceMonitor()

Expect(r.Client.Get(ctx, types.NamespacedName{
Name: DefaultArgoRolloutsMetricsServiceName,
Namespace: testNamespace,
}, existingServiceMonitor)).To(Succeed())
Expect(r.Client.Get(ctx, types.NamespacedName{
Name: DefaultArgoRolloutsMetricsServiceName,
Namespace: testNamespace,
}, existingServiceMonitor)).To(Succeed())

Expect(existingServiceMonitor.Name).To(Equal(expectedSM.Name))
Expect(existingServiceMonitor.Namespace).To(Equal(expectedSM.Namespace))
Expect(existingServiceMonitor.Spec).To(Equal(expectedSM.Spec))
Expect(existingServiceMonitor.Name).To(Equal(expectedSM.Name))
Expect(existingServiceMonitor.Namespace).To(Equal(expectedSM.Namespace))
Expect(existingServiceMonitor.Spec).To(Equal(expectedSM.Spec))

})
})

It("Verify ServiceMonitor is not created if the CRD does not exist.", func() {
_, existingSvc := serviceAndServiceMonitorCRD(req.Namespace)
Expect(r.Client.Create(ctx, existingSvc)).To(Succeed())
It("Verify ServiceMonitor is not created if the CRD does not exist.", func() {
_, existingSvc := serviceAndServiceMonitorCRD(req.Namespace)
Expect(r.Client.Create(ctx, existingSvc)).To(Succeed())

res, err := r.Reconcile(ctx, req)
Expect(err).To(HaveOccurred())
Expect(res.Requeue).Should(BeFalse(), "reconcile should not requeue request")
res, err := r.Reconcile(ctx, req)
Expect(err).To(HaveOccurred())
Expect(res.Requeue).Should(BeFalse(), "reconcile should not requeue request")

sm := &monitoringv1.ServiceMonitor{}
Expect(r.Client.Get(ctx, types.NamespacedName{
Name: DefaultArgoRolloutsMetricsServiceName,
Namespace: testNamespace,
}, sm)).ToNot(Succeed())
sm := &monitoringv1.ServiceMonitor{}
Expect(r.Client.Get(ctx, types.NamespacedName{
Name: DefaultArgoRolloutsMetricsServiceName,
Namespace: testNamespace,
}, sm)).ToNot(Succeed())
})
})

})

func serviceMonitor() *monitoringv1.ServiceMonitor {
Expand Down
2 changes: 1 addition & 1 deletion hack/run-rollouts-manager-e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ if [ -f "/tmp/e2e-operator-run.log" ]; then

UNEXPECTED_ERRORS_FOUND_TEXT=`cat /tmp/e2e-operator-run.log | grep "ERROR" | grep -v "because it is being terminated" | grep -v "the object has been modified; please apply your changes to the latest version and try again" | grep -v "unable to fetch" | grep -v "StorageError"`
UNEXPECTED_ERRORS_COUNT=`echo $UNEXPECTED_ERRORS_FOUND_TEXT | grep "ERROR" | wc -l`

if [ "$UNEXPECTED_ERRORS_COUNT" != "0" ]; then
echo "Unexpected errors found: $UNEXPECTED_ERRORS_FOUND_TEXT"
exit 1
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/fixture/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ func getKubeClient(config *rest.Config) (client.Client, *runtime.Scheme, error)
}

if err := monitoringv1.AddToScheme(scheme); err != nil {
return nil, err
return nil, nil, err
}

if err := crdv1.AddToScheme(scheme); err != nil {
return nil, err
return nil, nil, err
}

k8sClient, err := client.New(config, client.Options{Scheme: scheme})
Expand Down

0 comments on commit 904ae66

Please sign in to comment.