Skip to content
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

Add annotation to mount connect inject volume to other containers #1111

Merged
merged 4 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ BREAKING CHANGES:
IMPROVEMENTS:
* Control Plane
* Upgrade Docker image Alpine version from 3.14 to 3.15. [[GH-1058](https://github.com/hashicorp/consul-k8s/pull/1058)]
* Support new annotation for mounting connect-inject volume to other containers. [[GH-1111](https://github.com/hashicorp/consul-k8s/pull/1111)]
* Helm
* API Gateway: Allow controller to read Kubernetes namespaces in order to determine if route is allowed for gateway. [[GH-1092](https://github.com/hashicorp/consul-k8s/pull/1092)]

Expand Down
6 changes: 6 additions & 0 deletions control-plane/connect-inject/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const (
// be set to a truthy or falsy value, as parseable by strconv.ParseBool.
annotationInject = "consul.hashicorp.com/connect-inject"

// annotationInjectMountVolumes is the key of the annotation that controls whether
// the data volume that connect inject uses to store data including the Consul ACL token
// is mounted to other containers in the pod. It is a comma-separated list of container names
// to mount the volume on. It will be mounted at the path `/consul/connect-inject`.
annotationInjectMountVolumes = "consul.hashicorp.com/connect-inject-mount-volume"

// annotationService is the name of the service to proxy.
// This defaults to the name of the Kubernetes service associated with the pod.
annotationService = "consul.hashicorp.com/connect-service"
Expand Down
25 changes: 25 additions & 0 deletions control-plane/connect-inject/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ func (h *Handler) Handle(ctx context.Context, req admission.Request) admission.R
// the sidecar for passing data in the pod.
pod.Spec.Volumes = append(pod.Spec.Volumes, h.containerVolume())

// Optionally mount data volume to other containers
h.injectVolumeMount(pod)

// Add the upstream services as environment variables for easy
// service discovery.
containerEnvVars := h.containerEnvVars(pod)
Expand Down Expand Up @@ -441,6 +444,19 @@ func (h *Handler) overwriteProbes(ns corev1.Namespace, pod *corev1.Pod) error {
return nil
}

func (h *Handler) injectVolumeMount(pod corev1.Pod) {
containersToInject := splitCommaSeparatedItemsFromAnnotation(annotationInjectMountVolumes, pod)

for index, container := range pod.Spec.Containers {
if sliceContains(containersToInject, container.Name) {
pod.Spec.Containers[index].VolumeMounts = append(pod.Spec.Containers[index].VolumeMounts, corev1.VolumeMount{
Name: volumeName,
MountPath: "/consul/connect-inject",
})
}
}
}

func (h *Handler) shouldInject(pod corev1.Pod, namespace string) (bool, error) {
// Don't inject in the Kubernetes system namespaces
if kubeSystemNamespaces.Contains(namespace) {
Expand Down Expand Up @@ -625,3 +641,12 @@ func (h *Handler) InjectDecoder(d *admission.Decoder) error {
h.decoder = d
return nil
}

func sliceContains(slice []string, entry string) bool {
for _, s := range slice {
if entry == s {
return true
}
}
return false
}
121 changes: 121 additions & 0 deletions control-plane/connect-inject/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,127 @@ func TestHandlerHandle(t *testing.T) {
},
},

{
"pod with empty volume mount annotation",
Handler{
Log: logrtest.TestLogger{T: t},
AllowK8sNamespacesSet: mapset.NewSetWith("*"),
DenyK8sNamespacesSet: mapset.NewSet(),
decoder: decoder,
Clientset: defaultTestClientWithNamespace(),
},
admission.Request{
AdmissionRequest: admissionv1.AdmissionRequest{
Namespace: namespaces.DefaultNamespace,
Object: encodeRaw(t, &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
annotationInjectMountVolumes: "",
},
},
Spec: basicSpec,
}),
},
},
"",
[]jsonpatch.Operation{
{
Operation: "add",
Path: "/spec/volumes",
},
{
Operation: "add",
Path: "/spec/initContainers",
},
{
Operation: "add",
Path: "/spec/containers/1",
},
{
Operation: "add",
Path: "/metadata/annotations/" + escapeJSONPointer(keyInjectStatus),
},
{
Operation: "add",
Path: "/metadata/annotations/" + escapeJSONPointer(annotationOriginalPod),
},
{
Operation: "add",
Path: "/metadata/labels",
},
},
},
{
"pod with volume mount annotation",
Handler{
Log: logrtest.TestLogger{T: t},
AllowK8sNamespacesSet: mapset.NewSetWith("*"),
DenyK8sNamespacesSet: mapset.NewSet(),
decoder: decoder,
Clientset: defaultTestClientWithNamespace(),
},
admission.Request{
AdmissionRequest: admissionv1.AdmissionRequest{
Namespace: namespaces.DefaultNamespace,
Object: encodeRaw(t, &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
annotationInjectMountVolumes: "web,unknown,web_three_point_oh",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "web",
},
{
Name: "web_two_point_oh",
},
{
Name: "web_three_point_oh",
},
},
},
}),
},
},
"",
[]jsonpatch.Operation{
{
Operation: "add",
Path: "/spec/volumes",
},
{
Operation: "add",
Path: "/spec/containers/0/volumeMounts",
},
{
Operation: "add",
Path: "/spec/containers/2/volumeMounts",
},
{
Operation: "add",
Path: "/spec/initContainers",
},
{
Operation: "add",
Path: "/spec/containers/3",
},
{
Operation: "add",
Path: "/metadata/annotations/" + escapeJSONPointer(keyInjectStatus),
},
{
Operation: "add",
Path: "/metadata/annotations/" + escapeJSONPointer(annotationOriginalPod),
},
{
Operation: "add",
Path: "/metadata/labels",
},
},
},

{
"pod with service annotation",
Handler{
Expand Down