Skip to content

Commit

Permalink
clean: refactor twemproxy duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
raelga committed Sep 13, 2023
1 parent 58a8fd7 commit b93526a
Showing 1 changed file with 58 additions and 82 deletions.
140 changes: 58 additions & 82 deletions pkg/resource_builders/twemproxy/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,52 @@ const (
healthCommand = "health"
)

func TwemproxyContainer(twemproxySpec *saasv1alpha1.TwemproxySpec) corev1.Container {

return corev1.Container{
Env: pod.BuildEnvironment(NewTwemproxyOptions(*twemproxySpec)),
Name: twemproxy,
Image: pod.Image(*twemproxySpec.Image),
Ports: pod.ContainerPorts(
pod.ContainerPortTCP(twemproxy, 22121),
pod.ContainerPortTCP("twem-metrics", int32(*twemproxySpec.Options.MetricsPort)),
),
Resources: corev1.ResourceRequirements(*twemproxySpec.Resources),
ImagePullPolicy: *twemproxySpec.Image.PullPolicy,
LivenessProbe: pod.ExecProbe(healthCommand, *twemproxySpec.LivenessProbe),
ReadinessProbe: pod.ExecProbe(healthCommand, *twemproxySpec.ReadinessProbe),
TerminationMessagePath: corev1.TerminationMessagePathDefault,
TerminationMessagePolicy: corev1.TerminationMessageReadFile,
Lifecycle: &corev1.Lifecycle{
PreStop: &corev1.LifecycleHandler{
Exec: &corev1.ExecAction{
Command: []string{"pre-stop", TwemproxyConfigFile},
},
},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: twemproxy + "-config",
MountPath: filepath.Dir(TwemproxyConfigFile),
},
},
}
}

func TwemproxyContainerVolume(twemproxySpec *saasv1alpha1.TwemproxySpec) corev1.Volume {
return corev1.Volume{
Name: twemproxy + "-config",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: twemproxySpec.ConfigMapName(),
},
DefaultMode: pointer.Int32(420),
},
},
}
}

func AddTwemproxySidecar(podTemplateSpec corev1.PodTemplateSpec, twemproxySpec *saasv1alpha1.TwemproxySpec) corev1.PodTemplateSpec {

// Labels to subscribe to the TwemproxyConfig sync events
Expand All @@ -28,109 +74,39 @@ func AddTwemproxySidecar(podTemplateSpec corev1.PodTemplateSpec, twemproxySpec *
)

// Twemproxy container
podTemplateSpec.Spec.Containers = append(podTemplateSpec.Spec.Containers,
corev1.Container{
Env: pod.BuildEnvironment(NewTwemproxyOptions(*twemproxySpec)),
Name: twemproxy,
Image: pod.Image(*twemproxySpec.Image),
Ports: pod.ContainerPorts(
pod.ContainerPortTCP(twemproxy, 22121),
pod.ContainerPortTCP("twem-metrics", int32(*twemproxySpec.Options.MetricsPort)),
),
Resources: corev1.ResourceRequirements(*twemproxySpec.Resources),
ImagePullPolicy: *twemproxySpec.Image.PullPolicy,
LivenessProbe: pod.ExecProbe(healthCommand, *twemproxySpec.LivenessProbe),
ReadinessProbe: pod.ExecProbe(healthCommand, *twemproxySpec.ReadinessProbe),
TerminationMessagePath: corev1.TerminationMessagePathDefault,
TerminationMessagePolicy: corev1.TerminationMessageReadFile,
Lifecycle: &corev1.Lifecycle{
PreStop: &corev1.LifecycleHandler{
Exec: &corev1.ExecAction{
Command: []string{"pre-stop", TwemproxyConfigFile},
},
},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: twemproxy + "-config",
MountPath: filepath.Dir(TwemproxyConfigFile),
},
},
})
podTemplateSpec.Spec.Containers = append(
podTemplateSpec.Spec.Containers,
TwemproxyContainer(twemproxySpec),
)

if podTemplateSpec.Spec.Volumes == nil {
podTemplateSpec.Spec.Volumes = []corev1.Volume{}
}

// Mount the TwemproxyConfig ConfigMap in the Pod
podTemplateSpec.Spec.Volumes = append(
podTemplateSpec.Spec.Volumes,
corev1.Volume{
Name: twemproxy + "-config",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: twemproxySpec.ConfigMapName(),
},
DefaultMode: pointer.Int32(420),
},
},
})
podTemplateSpec.Spec.Volumes, TwemproxyContainerVolume(twemproxySpec),
)

return podTemplateSpec
}

func AddTwemproxyTaskSidecar(taskSpec pipelinev1beta1.TaskSpec, twemproxySpec *saasv1alpha1.TwemproxySpec) pipelinev1beta1.TaskSpec {

twemproxySidecar := pipelinev1beta1.Sidecar{}
twemproxySidecar.SetContainerFields(TwemproxyContainer(twemproxySpec))

This comment has been minimized.

Copy link
@roivaz

roivaz Sep 13, 2023

Member

nice that they provide that function! well spotted! :)


// Twemproxy container
taskSpec.Sidecars = append(taskSpec.Sidecars,
pipelinev1beta1.Sidecar{
Name: twemproxy,
Env: pod.BuildEnvironment(NewTwemproxyOptions(*twemproxySpec)),
Image: pod.Image(*twemproxySpec.Image),
Ports: pod.ContainerPorts(
pod.ContainerPortTCP(twemproxy, 22121),
pod.ContainerPortTCP("twem-metrics", int32(*twemproxySpec.Options.MetricsPort)),
),
Resources: corev1.ResourceRequirements(*twemproxySpec.Resources),
ImagePullPolicy: *twemproxySpec.Image.PullPolicy,
LivenessProbe: pod.ExecProbe(healthCommand, *twemproxySpec.LivenessProbe),
ReadinessProbe: pod.ExecProbe(healthCommand, *twemproxySpec.ReadinessProbe),
TerminationMessagePath: corev1.TerminationMessagePathDefault,
TerminationMessagePolicy: corev1.TerminationMessageReadFile,
Lifecycle: &corev1.Lifecycle{
PreStop: &corev1.LifecycleHandler{
Exec: &corev1.ExecAction{
Command: []string{"pre-stop", TwemproxyConfigFile},
},
},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: twemproxy + "-config",
MountPath: filepath.Dir(TwemproxyConfigFile),
},
},
})
taskSpec.Sidecars = append(taskSpec.Sidecars, twemproxySidecar)

if taskSpec.Volumes == nil {
taskSpec.Volumes = []corev1.Volume{}
}

// Mount the TwemproxyConfig ConfigMap in the Pod
taskSpec.Volumes = append(
taskSpec.Volumes,
corev1.Volume{
Name: twemproxy + "-config",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: twemproxySpec.ConfigMapName(),
},
DefaultMode: pointer.Int32(420),
},
},
})
taskSpec.Volumes, TwemproxyContainerVolume(twemproxySpec),
)

return taskSpec
}

0 comments on commit b93526a

Please sign in to comment.