From 76e8a58a8bd9e5b8095dd702134efdc2efc50ccc Mon Sep 17 00:00:00 2001 From: hime Date: Mon, 14 Oct 2024 22:26:45 +0000 Subject: [PATCH] Simplify injection logic. --- cmd/metadata_prefetch/Dockerfile | 2 +- pkg/webhook/injection.go | 48 ++++++++++++++------------------ 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/cmd/metadata_prefetch/Dockerfile b/cmd/metadata_prefetch/Dockerfile index b94ff436..f3ccb8a4 100644 --- a/cmd/metadata_prefetch/Dockerfile +++ b/cmd/metadata_prefetch/Dockerfile @@ -41,4 +41,4 @@ COPY --from=debian /lib64/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2 # Copy the built binaries COPY --from=metadata-prefetch-builder /bin/gcs-fuse-csi-driver-metadata-prefetch /gcs-fuse-csi-driver-metadata-prefetch -ENTRYPOINT ["/gcs-fuse-csi-driver-metadata-prefetch"] \ No newline at end of file +ENTRYPOINT ["/gcs-fuse-csi-driver-metadata-prefetch"] diff --git a/pkg/webhook/injection.go b/pkg/webhook/injection.go index 2d7bee1d..1021b8aa 100644 --- a/pkg/webhook/injection.go +++ b/pkg/webhook/injection.go @@ -72,39 +72,33 @@ func injectSidecarContainer(pod *corev1.Pod, config *Config, supportsNativeSidec } func injectMetadataPrefetchSidecarContainer(pod *corev1.Pod, config *Config, supportsNativeSidecar bool) { - // TODO(hime): Simplify this logic, this is very repetitive at first glance. - if supportsNativeSidecar { - spec := GetNativeMetadataPrefetchSidecarContainerSpec(pod, config) - index := getInjectIndexAfterContainer(pod.Spec.InitContainers, SidecarContainerName) - if len(spec.VolumeMounts) == 0 { - klog.Info("no volumes are requesting metadata prefetch, skipping metadata prefetch sidecar injection...") - - return - } + var containerSpec corev1.Container + var index int - if index == 0 { - klog.Warning("gke-gcsfuse-sidecar not found when attempting to inject metadata prefetch sidecar... skipping injection") + if supportsNativeSidecar { + containerSpec = GetNativeMetadataPrefetchSidecarContainerSpec(pod, config) + index = getInjectIndexAfterContainer(pod.Spec.InitContainers, SidecarContainerName) + } else { + containerSpec = GetMetadataPrefetchSidecarContainerSpec(pod, config) + index = getInjectIndexAfterContainer(pod.Spec.Containers, SidecarContainerName) + } - return - } + if len(containerSpec.VolumeMounts) == 0 { + klog.Info("no volumes are requesting metadata prefetch, skipping metadata prefetch sidecar injection...") - pod.Spec.InitContainers = insert(pod.Spec.InitContainers, spec, index) - } else { - spec := GetMetadataPrefetchSidecarContainerSpec(pod, config) - index := getInjectIndexAfterContainer(pod.Spec.Containers, SidecarContainerName) - if len(spec.VolumeMounts) == 0 { - klog.Info("no volumes are requesting metadata prefetch, skipping metadata prefetch sidecar injection...") + return + } - return - } + if index == 0 { + klog.Warning("gke-gcsfuse-sidecar not found when attempting to inject metadata prefetch sidecar... skipping injection") - if index == 0 { - klog.Warning("gke-gcsfuse-sidecar not found when attempting to inject metadata prefetch sidecar... skipping injection") + return + } - return - } - // Inject sidecar. - pod.Spec.Containers = insert(pod.Spec.Containers, spec, index) + if supportsNativeSidecar { + pod.Spec.InitContainers = insert(pod.Spec.InitContainers, containerSpec, index) + } else { + pod.Spec.Containers = insert(pod.Spec.Containers, containerSpec, index) } }