Skip to content

Commit

Permalink
Refactor deployment.go code
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bshephar committed Sep 19, 2024
1 parent 9c3e4ba commit 574ee2a
Showing 1 changed file with 110 additions and 52 deletions.
162 changes: 110 additions & 52 deletions pkg/horizon/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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{
Expand Down Expand Up @@ -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
}

0 comments on commit 574ee2a

Please sign in to comment.