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 12, 2024
1 parent d479a9c commit 4a594fc
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 61 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("Test ServiceMonitor creation", 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
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 4a594fc

Please sign in to comment.