Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Reduce cyclomatic complexity #54

Merged
merged 1 commit into from
Apr 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
♻️ Reduce cyclo
  • Loading branch information
adyanth committed Apr 14, 2022
commit ee2d8bf4dbc010a2ae654ffce31999cc6d4b13b1
54 changes: 31 additions & 23 deletions controllers/service_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ func (r *ServiceReconciler) creationLogic() error {
r.Recorder.Event(r.service, corev1.EventTypeNormal, "MetaSet", "Service Finalizer and Labels added")

// Create DNS entry
return r.createDNSLogic()
}

func (r *ServiceReconciler) createDNSLogic() error {
txtId, dnsTxtResponse, canUseDns, err := r.cfAPI.GetManagedDnsTxt(r.config.Hostname)
if err != nil {
// We should not use this entry
Expand Down Expand Up @@ -418,12 +422,35 @@ func (r ServiceReconciler) getConfigForService(tunnelDomain string, service *cor
}

servicePort := service.Spec.Ports[0]

// Logic to get serviceProto
var serviceProto string
tunnelProto := service.Annotations[tunnelProtoAnnotation]
validProto := tunnelValidProtoMap[tunnelProto]

serviceProto := r.getServiceProto(tunnelProto, validProto, servicePort)

r.log.Info("Selected protocol", "protocol", serviceProto)

cfService := fmt.Sprintf("%s://%s.%s.svc:%d", serviceProto, service.Name, service.Namespace, servicePort.Port)

cfHostname := service.Annotations[fqdnAnnotation]

// Generate cfHostname string from Service Spec if not provided
if cfHostname == "" {
if tunnelDomain == "" {
r.log.Info("Using current tunnel's domain for generating config")
tunnelDomain = r.cfAPI.Domain
}
cfHostname = fmt.Sprintf("%s.%s", service.Name, tunnelDomain)
r.log.Info("using default domain value", "domain", tunnelDomain)
}

r.log.Info("generated cloudflare config", "cfHostname", cfHostname, "cfService", cfService)

return UnvalidatedIngressRule{Hostname: cfHostname, Service: cfService}, nil
}

// getServiceProto returns the service protocol to be used
func (r *ServiceReconciler) getServiceProto(tunnelProto string, validProto bool, servicePort corev1.ServicePort) string {
var serviceProto string
if tunnelProto != "" && !validProto {
r.log.Info("Invalid Protocol provided, following default protocol logic")
}
Expand All @@ -450,26 +477,7 @@ func (r ServiceReconciler) getConfigForService(tunnelDomain string, service *cor
err := fmt.Errorf("unsupported protocol")
r.log.Error(err, "could not select protocol", "portProtocol", servicePort.Protocol, "annotationProtocol", tunnelProto)
}

r.log.Info("Selected protocol", "protocol", serviceProto)

cfService := fmt.Sprintf("%s://%s.%s.svc:%d", serviceProto, service.Name, service.Namespace, servicePort.Port)

cfHostname := service.Annotations[fqdnAnnotation]

// Generate cfHostname string from Service Spec if not provided
if cfHostname == "" {
if tunnelDomain == "" {
r.log.Info("Using current tunnel's domain for generating config")
tunnelDomain = r.cfAPI.Domain
}
cfHostname = fmt.Sprintf("%s.%s", service.Name, tunnelDomain)
r.log.Info("using default domain value", "domain", tunnelDomain)
}

r.log.Info("generated cloudflare config", "cfHostname", cfHostname, "cfService", cfService)

return UnvalidatedIngressRule{Hostname: cfHostname, Service: cfService}, nil
return serviceProto
}

func (r *ServiceReconciler) getConfigMapConfiguration() (*Configuration, error) {
Expand Down