-
Notifications
You must be signed in to change notification settings - Fork 229
NE-2664: add opt-in config to deploy haproxy as sidecar #1439
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
Open
jcmoraisjr
wants to merge
1
commit into
openshift:master
Choose a base branch
from
jcmoraisjr:NE-2664-haproxy-sidecar
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ import ( | |
| "net/url" | ||
| "os" | ||
| "path/filepath" | ||
| "slices" | ||
| "sort" | ||
| "strconv" | ||
| "strings" | ||
|
|
@@ -158,6 +159,12 @@ const ( | |
| StatsPortName = "metrics" | ||
|
|
||
| haproxyMaxTimeoutMilliseconds = 2147483647 * time.Millisecond | ||
|
|
||
| routerInitContainerName = "init-router" | ||
| routerHAProxyContainerName = "haproxy" | ||
| routerHAProxyConfigVolume = "haproxy-config" | ||
| routerHAProxyAdminSocket = "/var/lib/haproxy/run/admin.sock" | ||
| routerServiceAccountVolumeName = "kube-api-access" | ||
| ) | ||
|
|
||
| // ensureRouterDeployment ensures the router deployment exists for a given | ||
|
|
@@ -1310,6 +1317,70 @@ func desiredRouterDeployment(ci *operatorv1.IngressController, config *Config, i | |
| }) | ||
| } | ||
|
|
||
| // TODO move to a proper API - part of https://github.com/openshift/enhancements/pull/1965 | ||
| haproxySidecar := ci.Annotations != nil && ci.Annotations["haproxy-sidecar"] != "" | ||
|
|
||
| if haproxySidecar { | ||
| // initImage just needs bash, reusing router's image is fine since we already need to download it anyway. | ||
| initImage := deployment.Spec.Template.Spec.Containers[0].Image | ||
|
|
||
| // haproxyImage uses router's image for now, but it needs its own image. | ||
| // HAProxy images: https://redhat.atlassian.net/browse/NE-2218 | ||
| // API field: https://redhat.atlassian.net/browse/NE-2217 | ||
| haproxyImage := deployment.Spec.Template.Spec.Containers[0].Image | ||
|
|
||
| // Add the haproxy config volume, this volume is shared between router controller and haproxy sidecar | ||
| deployment.Spec.Template.Spec.Volumes = append(deployment.Spec.Template.Spec.Volumes, corev1.Volume{ | ||
| Name: routerHAProxyConfigVolume, | ||
| VolumeSource: corev1.VolumeSource{ | ||
| EmptyDir: &corev1.EmptyDirVolumeSource{}, | ||
| }, | ||
| }) | ||
|
|
||
| // The initContainer is responsible for copying static config data from router's container to the shared volume | ||
| deployment.Spec.Template.Spec.InitContainers = append(deployment.Spec.Template.Spec.InitContainers, corev1.Container{ | ||
| Name: routerInitContainerName, | ||
| Image: initImage, | ||
| ImagePullPolicy: deployment.Spec.Template.Spec.Containers[0].ImagePullPolicy, | ||
| Command: []string{"/bin/bash", "-c", "cp -R -p /var/lib/haproxy/* /mnt/config/"}, | ||
| VolumeMounts: []corev1.VolumeMount{{ | ||
| Name: routerHAProxyConfigVolume, | ||
| MountPath: "/mnt/config", | ||
| }}, | ||
| }) | ||
|
|
||
| // Adding the shared haproxy config volume to the router's container. It is mounted in the same expected /var/lib/haproxy directory, | ||
| // overriding the original content, which was already copied to the shared volume via the init container | ||
| deployment.Spec.Template.Spec.Containers[0].VolumeMounts = append(deployment.Spec.Template.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{ | ||
| Name: routerHAProxyConfigVolume, | ||
| MountPath: "/var/lib/haproxy", | ||
| }) | ||
|
|
||
| // HAProxy sidecar mount all the router volumes, except the service account token | ||
| haproxyVolumeMounts := slices.Clone(deployment.Spec.Template.Spec.Containers[0].VolumeMounts) | ||
| haproxyVolumeMounts = slices.DeleteFunc(haproxyVolumeMounts, func(mount corev1.VolumeMount) bool { | ||
| return mount.Name == routerServiceAccountVolumeName | ||
| }) | ||
|
|
||
| // Finally, adding haproxy as a sidecar container | ||
| deployment.Spec.Template.Spec.Containers = append(deployment.Spec.Template.Spec.Containers, corev1.Container{ | ||
| Name: routerHAProxyContainerName, | ||
| Image: haproxyImage, | ||
| ImagePullPolicy: deployment.Spec.Template.Spec.Containers[0].ImagePullPolicy, | ||
| Command: []string{"/var/lib/haproxy/start-haproxy"}, | ||
| Args: []string{"-W", "-db", "-S", routerHAProxyAdminSocket + ",mode,600", "-f", "/var/lib/haproxy/conf/haproxy.config"}, | ||
| Resources: deployment.Spec.Template.Spec.Containers[0].Resources, | ||
| VolumeMounts: haproxyVolumeMounts, | ||
| }) | ||
|
|
||
| // ROUTER_HAPROXY_ADMIN_UNIX_SOCKET envvar, if configured, instructs the router that | ||
| // haproxy is deployed as a sidecar container, otherwise router uses the embedded haproxy. | ||
| env = append(env, corev1.EnvVar{ | ||
| Name: "ROUTER_HAPROXY_ADMIN_UNIX_SOCKET", | ||
| Value: routerHAProxyAdminSocket, | ||
| }) | ||
| } | ||
|
|
||
| // TODO: The only connections from the router that may need the cluster-wide proxy are those for downloading CRLs, | ||
| // which, as of writing this, will always be http. If https becomes necessary, the router will need to mount the | ||
| // trusted CA bundle that cluster-network-operator generates. The process for adding that is described here: | ||
|
|
@@ -1591,6 +1662,7 @@ func hashableDeployment(deployment *appsv1.Deployment, onlyTemplate bool) *appsv | |
| return containers[i].Name < containers[j].Name | ||
| }) | ||
| hashableDeployment.Spec.Template.Spec.Containers = containers | ||
| hashableDeployment.Spec.Template.Spec.InitContainers = deployment.Spec.Template.Spec.InitContainers | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If k8s injects defaults into |
||
| hashableDeployment.Spec.Template.Spec.DNSPolicy = deployment.Spec.Template.Spec.DNSPolicy | ||
| hashableDeployment.Spec.Template.Spec.HostNetwork = deployment.Spec.Template.Spec.HostNetwork | ||
| volumes := make([]corev1.Volume, len(deployment.Spec.Template.Spec.Volumes)) | ||
|
|
@@ -1773,6 +1845,7 @@ func deploymentConfigChanged(current, expected *appsv1.Deployment) (bool, *appsv | |
| containers[i+1] = *container.DeepCopy() | ||
| } | ||
| updated.Spec.Template.Spec.Containers = containers | ||
| updated.Spec.Template.Spec.InitContainers = expected.Spec.Template.Spec.InitContainers | ||
| updated.Spec.Template.Spec.DNSPolicy = expected.Spec.Template.Spec.DNSPolicy | ||
| updated.Spec.Template.Labels = expected.Spec.Template.Labels | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great security improvement