From 574ee2a297bd6ea14a0cd7433b5ad73f9c2c6835 Mon Sep 17 00:00:00 2001 From: Brendan Shephard Date: Thu, 19 Sep 2024 10:26:18 +1000 Subject: [PATCH] Refactor deployment.go code This change does some refactoring of the deployment.go code to avoid overuse of string literals and replace with consts. Along with moving excessive code from the main Deployment function into dedicated functions that are easier to test and read. Signed-off-by: Brendan Shephard --- pkg/horizon/deployment.go | 162 ++++++++++++++++++++++++++------------ 1 file changed, 110 insertions(+), 52 deletions(-) diff --git a/pkg/horizon/deployment.go b/pkg/horizon/deployment.go index 519399c..461bf00 100644 --- a/pkg/horizon/deployment.go +++ b/pkg/horizon/deployment.go @@ -20,6 +20,7 @@ import ( common "github.com/openstack-k8s-operators/lib-common/modules/common" "github.com/openstack-k8s-operators/lib-common/modules/common/affinity" env "github.com/openstack-k8s-operators/lib-common/modules/common/env" + "github.com/openstack-k8s-operators/lib-common/modules/common/tls" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -29,9 +30,20 @@ import ( const ( // ServiceCommand is the command used to run Kolla and launch the initial Apache process - ServiceCommand = "/usr/local/bin/kolla_start" + ServiceCommand = "/usr/local/bin/kolla_start" + horizonContainerPortName string = "horizon" + horizonDashboardURL string = "/dashboard/auth/login/?next=/dashboard/" ) +type TLSRequiredOptions struct { + containerPort *corev1.ContainerPort + livenessProbe *corev1.Probe + readinessProbe *corev1.Probe + startupProbe *corev1.Probe + volumes []corev1.Volume + volumeMounts []corev1.VolumeMount +} + // Deployment creates the k8s deployment structure required to run Horizon func Deployment( instance *horizonv1.Horizon, @@ -44,55 +56,16 @@ func Deployment( args := []string{"-c", ServiceCommand} containerPort := corev1.ContainerPort{ - Name: "horizon", + Name: horizonContainerPortName, Protocol: corev1.ProtocolTCP, ContainerPort: HorizonPort, } - livenessProbe := &corev1.Probe{ - TimeoutSeconds: 5, - PeriodSeconds: 10, - InitialDelaySeconds: 10, - ProbeHandler: corev1.ProbeHandler{ - HTTPGet: &corev1.HTTPGetAction{ - Path: "/dashboard/auth/login/?next=/dashboard/", - Port: intstr.FromString("horizon"), - }, - }, - } - readinessProbe := &corev1.Probe{ - TimeoutSeconds: 5, - PeriodSeconds: 10, - InitialDelaySeconds: 10, - ProbeHandler: corev1.ProbeHandler{ - HTTPGet: &corev1.HTTPGetAction{ - Path: "/dashboard/auth/login/?next=/dashboard/", - Port: intstr.FromString("horizon"), - }, - }, - } - - startupProbe := &corev1.Probe{ - TimeoutSeconds: 5, - PeriodSeconds: 10, - FailureThreshold: 30, - InitialDelaySeconds: 10, - ProbeHandler: corev1.ProbeHandler{ - HTTPGet: &corev1.HTTPGetAction{ - Path: "/dashboard/auth/login/?next=/dashboard/", - Port: intstr.FromString("horizon"), - }, - }, - } + livenessProbe := formatLivenessProbe() + readinessProbe := formatReadinesProbe() + startupProbe := formatStartupProbe() - envVars := map[string]env.Setter{} - envVars["KOLLA_CONFIG_STRATEGY"] = env.SetValue("COPY_ALWAYS") - envVars["ENABLE_DESIGNATE"] = env.SetValue("yes") - envVars["ENABLE_HEAT"] = env.SetValue("yes") - envVars["ENABLE_IRONIC"] = env.SetValue("yes") - envVars["ENABLE_MANILA"] = env.SetValue("yes") - envVars["ENABLE_OCTAVIA"] = env.SetValue("yes") - envVars["CONFIG_HASH"] = env.SetValue(configHash) + envVars := getEnvVars(configHash) // create Volumes and VolumeMounts volumes := getVolumes(instance.Name, instance.Spec.ExtraMounts, HorizonPropagation) @@ -105,16 +78,20 @@ func Deployment( } if instance.Spec.TLS.Enabled() { - svc, err := instance.Spec.TLS.GenericService.ToService() + tlsRequireOptions := TLSRequiredOptions{ + &containerPort, + livenessProbe, + readinessProbe, + startupProbe, + volumes, + volumeMounts, + } + + var err error + volumes, volumeMounts, err = tlsRequireOptions.formatTLSOptions(instance) if err != nil { return nil, err } - containerPort.ContainerPort = HorizonPortTLS - livenessProbe.HTTPGet.Scheme = corev1.URISchemeHTTPS - readinessProbe.HTTPGet.Scheme = corev1.URISchemeHTTPS - startupProbe.HTTPGet.Scheme = corev1.URISchemeHTTPS - volumes = append(volumes, svc.CreateVolume(ServiceName)) - volumeMounts = append(volumeMounts, svc.CreateVolumeMounts(ServiceName)...) } deployment := &appsv1.Deployment{ @@ -171,3 +148,84 @@ func Deployment( return deployment, nil } + +func getEnvVars(configHash string) map[string]env.Setter { + + envVars := map[string]env.Setter{} + + envVars["KOLLA_CONFIG_STRATEGY"] = env.SetValue("COPY_ALWAYS") + envVars["ENABLE_DESIGNATE"] = env.SetValue("yes") + envVars["ENABLE_HEAT"] = env.SetValue("yes") + envVars["ENABLE_IRONIC"] = env.SetValue("yes") + envVars["ENABLE_MANILA"] = env.SetValue("yes") + envVars["ENABLE_OCTAVIA"] = env.SetValue("yes") + envVars["CONFIG_HASH"] = env.SetValue(configHash) + + return envVars +} + +func formatLivenessProbe() *corev1.Probe { + + return &corev1.Probe{ + TimeoutSeconds: 5, + PeriodSeconds: 10, + InitialDelaySeconds: 10, + ProbeHandler: corev1.ProbeHandler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: horizonDashboardURL, + Port: intstr.FromString(horizonContainerPortName), + }, + }, + } +} + +func formatReadinesProbe() *corev1.Probe { + + return &corev1.Probe{ + TimeoutSeconds: 5, + PeriodSeconds: 10, + InitialDelaySeconds: 10, + ProbeHandler: corev1.ProbeHandler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: horizonContainerPortName, + Port: intstr.FromString(horizonContainerPortName), + }, + }, + } +} + +func formatStartupProbe() *corev1.Probe { + + return &corev1.Probe{ + TimeoutSeconds: 5, + PeriodSeconds: 10, + FailureThreshold: 30, + InitialDelaySeconds: 10, + ProbeHandler: corev1.ProbeHandler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: horizonDashboardURL, + Port: intstr.FromString(horizonContainerPortName), + }, + }, + } +} + +func (t *TLSRequiredOptions) formatTLSOptions(instance *horizonv1.Horizon) ([]corev1.Volume, []corev1.VolumeMount, error) { + + var err error + var svc *tls.Service + + svc, err = instance.Spec.TLS.GenericService.ToService() + if err != nil { + return t.volumes, t.volumeMounts, err + } + + t.containerPort.ContainerPort = HorizonPortTLS + t.livenessProbe.HTTPGet.Scheme = corev1.URISchemeHTTPS + t.readinessProbe.HTTPGet.Scheme = corev1.URISchemeHTTPS + t.startupProbe.HTTPGet.Scheme = corev1.URISchemeHTTPS + t.volumes = append(t.volumes, svc.CreateVolume(ServiceName)) + t.volumeMounts = append(t.volumeMounts, svc.CreateVolumeMounts(ServiceName)...) + + return t.volumes, t.volumeMounts, nil +}