Skip to content

Commit

Permalink
Update changes in resource.go file and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Rizwana777 <rizwananaaz177@gmail.com>
  • Loading branch information
Rizwana777 committed May 21, 2024
1 parent 4359722 commit 6d1ac70
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
18 changes: 16 additions & 2 deletions controllers/argorollouts_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -162,8 +163,21 @@ func (r *RolloutManagerReconciler) SetupWithManager(mgr ctrl.Manager) error {

// Watch for changes to ClusterRoleBinding sub-resources owned by RolloutManager.
bld.Owns(&rbacv1.ClusterRoleBinding{})
// Watch for changes to ServiceMonitor sub-resources owned by RolloutManager.
bld.Owns(&monitoringv1.ServiceMonitor{})

// Check if ServiceMonitor CRD is installed
crdClient := mgr.GetClient()
var crdList crdv1.CustomResourceDefinitionList
if err := crdClient.List(context.TODO(), &crdList); err != nil {
return err
}

for _, crd := range crdList.Items {
if crd.Name == "servicemonitors.monitoring.coreos.com" {
// Watch for changes to ServiceMonitor sub-resources owned by RolloutManager.
bld.Owns(&monitoringv1.ServiceMonitor{})
break
}
}

return bld.Complete(r)
}
8 changes: 2 additions & 6 deletions controllers/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,17 +382,13 @@ func (r *RolloutManagerReconciler) reconcileRolloutsMetricsService(ctx context.C
log.Error(err, "Error creating Service", "Name", expectedSvc.Name)
return err
}
return nil
}

if !reflect.DeepEqual(actualSvc.Spec.Ports, expectedSvc.Spec.Ports) {
} else if !reflect.DeepEqual(actualSvc.Spec.Ports, expectedSvc.Spec.Ports) {
log.Info(fmt.Sprintf("Ports of Service %s do not match the expected state, hence updating it", actualSvc.Name))
actualSvc.Spec.Ports = expectedSvc.Spec.Ports
if err := r.Client.Update(ctx, actualSvc); err != nil {
log.Error(err, "Error updating Ports of Service", "Name", actualSvc.Name)
return err
}
return nil
}

// Checks if user is using the Prometheus operator by checking CustomResourceDefinition for ServiceMonitor
Expand Down Expand Up @@ -1050,7 +1046,7 @@ func serviceMonitorMatches(sm *monitoringv1.ServiceMonitor, matchLabel string) b
}

// Check if endpoints match
if sm.Spec.Endpoints[0].Port != "metrics" {
if len(sm.Spec.Endpoints) == 0 || sm.Spec.Endpoints[0].Port != "metrics" {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ var _ = Describe("Resource creation and cleanup tests", func() {
}
})

Context("Rollouts Metics ServiceMonitor test", func() {
Context("Rollouts Metrics ServiceMonitor test", func() {
var (
ctx context.Context
a *v1alpha1.RolloutManager
Expand Down

0 comments on commit 6d1ac70

Please sign in to comment.