Skip to content

Commit e6c9a07

Browse files
committed
Update changes in resource.go file and tests
Signed-off-by: Rizwana777 <[email protected]>
1 parent 4359722 commit e6c9a07

File tree

3 files changed

+34
-51
lines changed

3 files changed

+34
-51
lines changed

controllers/argorollouts_controller.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
apierrors "k8s.io/apimachinery/pkg/api/errors"
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
"k8s.io/apimachinery/pkg/runtime"
30+
"k8s.io/client-go/discovery"
31+
"k8s.io/client-go/rest"
3032
ctrl "sigs.k8s.io/controller-runtime"
3133
"sigs.k8s.io/controller-runtime/pkg/client"
3234
logr "sigs.k8s.io/controller-runtime/pkg/log"
@@ -162,8 +164,28 @@ func (r *RolloutManagerReconciler) SetupWithManager(mgr ctrl.Manager) error {
162164

163165
// Watch for changes to ClusterRoleBinding sub-resources owned by RolloutManager.
164166
bld.Owns(&rbacv1.ClusterRoleBinding{})
165-
// Watch for changes to ServiceMonitor sub-resources owned by RolloutManager.
166-
bld.Owns(&monitoringv1.ServiceMonitor{})
167+
168+
if r.isCRDExist(mgr.GetConfig(), "servicemonitors.monitoring.coreos.com") {
169+
bld.Owns(&monitoringv1.ServiceMonitor{})
170+
}
167171

168172
return bld.Complete(r)
169173
}
174+
175+
// isCRDExist checks if a CRD is present in the cluster.
176+
func (r *RolloutManagerReconciler) isCRDExist(cfg *rest.Config, crdName string) bool {
177+
discoveryClient, err := discovery.NewDiscoveryClientForConfig(cfg)
178+
if err != nil {
179+
return false
180+
}
181+
apiResources, err := discoveryClient.ServerResourcesForGroupVersion("monitoring.coreos.com/v1")
182+
if err != nil {
183+
return false
184+
}
185+
for _, resource := range apiResources.APIResources {
186+
if resource.Name == crdName {
187+
return true
188+
}
189+
}
190+
return false
191+
}

controllers/resources.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -376,23 +376,19 @@ func (r *RolloutManagerReconciler) reconcileRolloutsMetricsService(ctx context.C
376376
if err := controllerutil.SetControllerReference(&cr, expectedSvc, r.Scheme); err != nil {
377377
return err
378378
}
379-
379+
fmt.Println("kuuu")
380380
log.Info(fmt.Sprintf("Creating Service %s", expectedSvc.Name))
381381
if err := r.Client.Create(ctx, expectedSvc); err != nil {
382382
log.Error(err, "Error creating Service", "Name", expectedSvc.Name)
383383
return err
384384
}
385-
return nil
386-
}
387-
388-
if !reflect.DeepEqual(actualSvc.Spec.Ports, expectedSvc.Spec.Ports) {
385+
} else if !reflect.DeepEqual(actualSvc.Spec.Ports, expectedSvc.Spec.Ports) {
389386
log.Info(fmt.Sprintf("Ports of Service %s do not match the expected state, hence updating it", actualSvc.Name))
390387
actualSvc.Spec.Ports = expectedSvc.Spec.Ports
391388
if err := r.Client.Update(ctx, actualSvc); err != nil {
392389
log.Error(err, "Error updating Ports of Service", "Name", actualSvc.Name)
393390
return err
394391
}
395-
return nil
396392
}
397393

398394
// Checks if user is using the Prometheus operator by checking CustomResourceDefinition for ServiceMonitor
@@ -413,7 +409,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsMetricsService(ctx context.C
413409
existingServiceMonitor := &monitoringv1.ServiceMonitor{}
414410
if err := fetchObject(ctx, r.Client, cr.Namespace, actualSvc.Name, existingServiceMonitor); err != nil {
415411
if apierrors.IsNotFound(err) {
416-
err = r.createServiceMonitorIfAbsent(ctx, cr.Namespace, cr, actualSvc.Name, actualSvc.Name)
412+
err = r.createServiceMonitorIfAbsent(ctx, cr.Namespace, cr, expectedSvc.Name, expectedSvc.Name)
417413
if err != nil {
418414
return err
419415
}
@@ -1050,7 +1046,7 @@ func serviceMonitorMatches(sm *monitoringv1.ServiceMonitor, matchLabel string) b
10501046
}
10511047

10521048
// Check if endpoints match
1053-
if sm.Spec.Endpoints[0].Port != "metrics" {
1049+
if len(sm.Spec.Endpoints) == 0 || sm.Spec.Endpoints[0].Port != "metrics" {
10541050
return false
10551051
}
10561052

controllers/resources_test.go

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1414
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1515
"k8s.io/apimachinery/pkg/types"
16-
"k8s.io/apimachinery/pkg/util/intstr"
1716
"sigs.k8s.io/controller-runtime/pkg/client"
1817
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1918
)
@@ -278,7 +277,7 @@ var _ = Describe("Resource creation and cleanup tests", func() {
278277
}
279278
})
280279

281-
Context("Rollouts Metics ServiceMonitor test", func() {
280+
Context("Rollouts Metrics ServiceMonitor test", func() {
282281
var (
283282
ctx context.Context
284283
a *v1alpha1.RolloutManager
@@ -301,9 +300,8 @@ var _ = Describe("Resource creation and cleanup tests", func() {
301300
})
302301

303302
It("Verify whether RolloutManager creating ServiceMonitor", func() {
304-
smCRD, existingSvc := serviceAndServiceMonitorCRD(req.Namespace)
303+
smCRD := serviceAndServiceMonitorCRD(req.Namespace)
305304
Expect(r.Client.Create(ctx, smCRD)).To(Succeed())
306-
Expect(r.Client.Create(ctx, existingSvc)).To(Succeed())
307305

308306
res, err := r.Reconcile(ctx, req)
309307
Expect(err).ToNot(HaveOccurred())
@@ -323,10 +321,6 @@ var _ = Describe("Resource creation and cleanup tests", func() {
323321
})
324322

325323
It("Verify if ServiceMonitor exists, but has different content than we expect then it should update ServiceMonitor", func() {
326-
smCRD, existingSvc := serviceAndServiceMonitorCRD(req.Namespace)
327-
Expect(r.Client.Create(ctx, smCRD)).To(Succeed())
328-
Expect(r.Client.Create(ctx, existingSvc)).To(Succeed())
329-
330324
existingServiceMonitor := &monitoringv1.ServiceMonitor{
331325
ObjectMeta: metav1.ObjectMeta{
332326
Name: DefaultArgoRolloutsMetricsServiceName,
@@ -335,12 +329,12 @@ var _ = Describe("Resource creation and cleanup tests", func() {
335329
Spec: monitoringv1.ServiceMonitorSpec{
336330
Selector: metav1.LabelSelector{
337331
MatchLabels: map[string]string{
338-
"app.kubernetes.io/name": "test-label",
332+
"app.kubernetes.io/name": "argo-rollouts-metrics",
339333
},
340334
},
341335
Endpoints: []monitoringv1.Endpoint{
342336
{
343-
Port: "metrics-test",
337+
Port: "metrics",
344338
},
345339
},
346340
},
@@ -366,9 +360,6 @@ var _ = Describe("Resource creation and cleanup tests", func() {
366360
})
367361

368362
It("Verify ServiceMonitor is not created if the CRD does not exist.", func() {
369-
_, existingSvc := serviceAndServiceMonitorCRD(req.Namespace)
370-
Expect(r.Client.Create(ctx, existingSvc)).To(Succeed())
371-
372363
res, err := r.Reconcile(ctx, req)
373364
Expect(err).ToNot(HaveOccurred())
374365
Expect(res.Requeue).Should(BeFalse(), "reconcile should not requeue request")
@@ -405,37 +396,11 @@ func serviceMonitor() *monitoringv1.ServiceMonitor {
405396
return sm
406397
}
407398

408-
func serviceAndServiceMonitorCRD(namespace string) (*crdv1.CustomResourceDefinition, *corev1.Service) {
399+
func serviceAndServiceMonitorCRD(namespace string) *crdv1.CustomResourceDefinition {
409400
smCRD := &crdv1.CustomResourceDefinition{
410401
ObjectMeta: metav1.ObjectMeta{
411402
Name: "servicemonitors.monitoring.coreos.com",
412403
},
413404
}
414-
415-
existingSvc := &corev1.Service{
416-
ObjectMeta: metav1.ObjectMeta{
417-
Name: DefaultArgoRolloutsMetricsServiceName,
418-
Namespace: namespace,
419-
Labels: map[string]string{
420-
"app.kubernetes.io/name": DefaultArgoRolloutsResourceName,
421-
"app.kubernetes.io/component": "server",
422-
},
423-
},
424-
Spec: corev1.ServiceSpec{
425-
Ports: []corev1.ServicePort{
426-
{
427-
Name: "metrics",
428-
Port: 8090,
429-
Protocol: corev1.ProtocolTCP,
430-
TargetPort: intstr.FromInt(8090),
431-
},
432-
},
433-
Selector: map[string]string{
434-
DefaultRolloutsSelectorKey: DefaultArgoRolloutsResourceName,
435-
},
436-
},
437-
}
438-
439-
return smCRD, existingSvc
440-
405+
return smCRD
441406
}

0 commit comments

Comments
 (0)