Skip to content

Commit

Permalink
Simplify injection logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
hime committed Oct 16, 2024
1 parent 2f70ec3 commit 76e8a58
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cmd/metadata_prefetch/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
ENTRYPOINT ["/gcs-fuse-csi-driver-metadata-prefetch"]
48 changes: 21 additions & 27 deletions pkg/webhook/injection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 76e8a58

Please sign in to comment.