Skip to content

Commit

Permalink
[FIX] reduce sonar reported complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
skrishnan-sap committed Sep 25, 2024
1 parent de12fe7 commit d5ba324
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions internal/controller/reconcile-capapplicationversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,46 +466,47 @@ func (c *Controller) updateServiceMonitors(ctx context.Context, ca *v1alpha1.CAP
return nil
}

isWorkloadPort := func(wlPorts []corev1.ServicePort, scrapePort string) bool {
for j := range wlPorts {
if wlPorts[j].Name == scrapePort {
return true
}
}
return false
}

for i := range cav.Spec.Workloads {
wl := cav.Spec.Workloads[i]
if wl.DeploymentDefinition == nil || wl.DeploymentDefinition.Monitoring == nil || wl.DeploymentDefinition.Monitoring.ScrapeConfig == nil {
continue // do not reconcile service monitors
if err := c.reconcileWorkloadServiceMonitor(ctx, &wl, cav, workloadServicePortInfos, ca); err != nil {
return err
}
}

wlPortInfos := getServicePortInfoByWorkloadName(workloadServicePortInfos, cav.Name, wl.Name)
if wlPortInfos == nil {
return fmt.Errorf("could not identify workload port information for workload %s in version %s", wl.Name, cav.Name)
}
return nil
}

if portVerified := isWorkloadPort(wlPortInfos.Ports, wl.DeploymentDefinition.Monitoring.ScrapeConfig.WorkloadPort); !portVerified {
return fmt.Errorf("invalid port reference in workload %s monitoring config of version %s", wl.Name, cav.Name)
}
func (c *Controller) reconcileWorkloadServiceMonitor(ctx context.Context, wl *v1alpha1.WorkloadDetails, cav *v1alpha1.CAPApplicationVersion, workloadServicePortInfos []servicePortInfo, ca *v1alpha1.CAPApplication) error {
if wl.DeploymentDefinition == nil || wl.DeploymentDefinition.Monitoring == nil || wl.DeploymentDefinition.Monitoring.ScrapeConfig == nil {
return nil // do not reconcile service monitors
}

sm, err := c.promClient.MonitoringV1().ServiceMonitors(cav.Namespace).Get(ctx, wlPortInfos.WorkloadName+ServiceSuffix, metav1.GetOptions{})
if err != nil {
if k8sErrors.IsNotFound(err) {
sm, err = c.promClient.MonitoringV1().ServiceMonitors(cav.Namespace).Create(ctx, newServiceMonitor(ca, cav, &wl, wlPortInfos), metav1.CreateOptions{})
if err == nil {
util.LogInfo("Service monitor created successfully", string(Processing), cav, sm, "version", cav.Spec.Version)
}
}
}
err = doChecks(err, sm, cav, wlPortInfos.WorkloadName+ServiceSuffix)
if err != nil {
return err
wlPortInfos := getServicePortInfoByWorkloadName(workloadServicePortInfos, cav.Name, wl.Name)
if wlPortInfos == nil {
return fmt.Errorf("could not identify workload port information for workload %s in version %s", wl.Name, cav.Name)
}

if portVerified := isWorkloadPort(wlPortInfos.Ports, wl.DeploymentDefinition.Monitoring.ScrapeConfig.WorkloadPort); !portVerified {
return fmt.Errorf("invalid port reference in workload %s monitoring config of version %s", wl.Name, cav.Name)
}

sm, err := c.promClient.MonitoringV1().ServiceMonitors(cav.Namespace).Get(ctx, wlPortInfos.WorkloadName+ServiceSuffix, metav1.GetOptions{})
if err != nil && k8sErrors.IsNotFound(err) {
sm, err = c.promClient.MonitoringV1().ServiceMonitors(cav.Namespace).Create(ctx, newServiceMonitor(ca, cav, wl, wlPortInfos), metav1.CreateOptions{})
if err == nil {
util.LogInfo("Service monitor created successfully", string(Processing), cav, sm, "version", cav.Spec.Version)
}
}
return doChecks(err, sm, cav, wlPortInfos.WorkloadName+ServiceSuffix)
}

return nil
func isWorkloadPort(wlPorts []corev1.ServicePort, scrapePort string) bool {
for j := range wlPorts {
if wlPorts[j].Name == scrapePort {
return true
}
}
return false
}

func newServiceMonitor(ca *v1alpha1.CAPApplication, cav *v1alpha1.CAPApplicationVersion, wl *v1alpha1.WorkloadDetails, wlPortInfos *servicePortInfo) *monv1.ServiceMonitor {
Expand Down

0 comments on commit d5ba324

Please sign in to comment.