From 01ec08fd7915bf0d08eebc87f2899afe560f4333 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 22 Jan 2024 06:40:04 -0500 Subject: [PATCH] refactor: simplify port string --- plugins/scheduler-k3s/functions.go | 4 ++++ plugins/scheduler-k3s/template.go | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/plugins/scheduler-k3s/functions.go b/plugins/scheduler-k3s/functions.go index 0eb850257..3338548bf 100644 --- a/plugins/scheduler-k3s/functions.go +++ b/plugins/scheduler-k3s/functions.go @@ -261,6 +261,10 @@ type PortMap struct { Scheme string `json:"scheme"` } +func (p PortMap) String() string { + return fmt.Sprintf("%s-%d-%d", p.Scheme, p.HostPort, p.ContainerPort) +} + func (p PortMap) IsAllowedHttp() bool { return p.Scheme == "http" || p.ContainerPort == 80 } diff --git a/plugins/scheduler-k3s/template.go b/plugins/scheduler-k3s/template.go index 9830163d1..edb4e3d8e 100644 --- a/plugins/scheduler-k3s/template.go +++ b/plugins/scheduler-k3s/template.go @@ -460,7 +460,7 @@ func templateKubernetesDeployment(input Deployment) (appsv1.Deployment, error) { protocol = "UDP" } deployment.Spec.Template.Spec.Containers[0].Ports = append(deployment.Spec.Template.Spec.Containers[0].Ports, corev1.ContainerPort{ - Name: fmt.Sprintf("%s-%d-%d", portMap.Scheme, portMap.HostPort, portMap.ContainerPort), + Name: portMap.String(), ContainerPort: portMap.ContainerPort, Protocol: corev1.Protocol(protocol), }) @@ -557,7 +557,7 @@ func templateKubernetesIngressRoute(input IngressRoute) traefikv1alpha1.IngressR "dokku.com/managed": "true", } - port := fmt.Sprintf("%s-%d-%d", input.PortMap.Scheme, input.PortMap.HostPort, input.PortMap.ContainerPort) + port := input.PortMap.String() ingressRoute := traefikv1alpha1.IngressRoute{ ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf("%s-%s", input.ServiceName, port), @@ -811,9 +811,9 @@ func templateKubernetesService(input Service) corev1.Service { protocol = "UDP" } service.Spec.Ports = append(service.Spec.Ports, corev1.ServicePort{ - Name: fmt.Sprintf("%s-%d-%d", portMap.Scheme, portMap.HostPort, portMap.ContainerPort), + Name: portMap.String(), Port: portMap.HostPort, - TargetPort: intstr.FromString(fmt.Sprintf("%s-%d-%d", portMap.Scheme, portMap.HostPort, portMap.ContainerPort)), + TargetPort: intstr.FromString(portMap.String()), Protocol: corev1.Protocol(protocol), }) }