mirror of
https://github.com/YuzuZensai/netbird-kubernetes-operator.git
synced 2026-09-13 18:59:09 +00:00
Ensure netbird sidecar container starts first (#339)
This changes the webhook from appending the container to the end to the beginning. Fixes #323 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Fixed pod injection ordering: injected components are now placed at the start of their respective lists to ensure correct initialization. * Updated injected sidecar behavior, including setting the restart policy to keep it running consistently. * Adjusted init-container ordering to ensure DNS-related initialization runs first. * **Tests** * Improved coverage by validating both container and sidecar injection modes, including restart policy and injected annotation behavior. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -246,6 +246,21 @@ func (d *PodNetbirdInjector) Default(ctx context.Context, pod *corev1.Pod) error
|
||||
}
|
||||
pod.Spec.Volumes = append(pod.Spec.Volumes, volumes...)
|
||||
|
||||
switch sidecarProfile.Spec.InjectionMode {
|
||||
case nbv1alpha1.InjectionModeSidecar:
|
||||
container.RestartPolicy = new(corev1.ContainerRestartPolicyAlways)
|
||||
pod.Spec.InitContainers = slices.Insert(pod.Spec.InitContainers, 0, container)
|
||||
case nbv1alpha1.InjectionModeContainer:
|
||||
pod.Spec.Containers = slices.Insert(pod.Spec.Containers, 0, container)
|
||||
default:
|
||||
return fmt.Errorf("unknown injection mode %s", sidecarProfile.Spec.InjectionMode)
|
||||
}
|
||||
|
||||
if pod.Annotations == nil {
|
||||
pod.Annotations = map[string]string{}
|
||||
}
|
||||
pod.Annotations[SidecarProfileAnnotation] = sidecarProfile.Name
|
||||
|
||||
resolvInitContainer := corev1.Container{
|
||||
Name: "resolv-conf",
|
||||
Image: d.clientImage,
|
||||
@@ -263,23 +278,7 @@ func (d *PodNetbirdInjector) Default(ctx context.Context, pod *corev1.Pod) error
|
||||
},
|
||||
},
|
||||
}
|
||||
pod.Spec.InitContainers = append(pod.Spec.InitContainers, resolvInitContainer)
|
||||
|
||||
switch sidecarProfile.Spec.InjectionMode {
|
||||
case nbv1alpha1.InjectionModeSidecar:
|
||||
restartPolicy := corev1.ContainerRestartPolicyAlways
|
||||
container.RestartPolicy = &restartPolicy
|
||||
pod.Spec.InitContainers = append(pod.Spec.InitContainers, container)
|
||||
case nbv1alpha1.InjectionModeContainer:
|
||||
pod.Spec.Containers = append(pod.Spec.Containers, container)
|
||||
default:
|
||||
return fmt.Errorf("unknown injection mode %s", sidecarProfile.Spec.InjectionMode)
|
||||
}
|
||||
|
||||
if pod.Annotations == nil {
|
||||
pod.Annotations = map[string]string{}
|
||||
}
|
||||
pod.Annotations[SidecarProfileAnnotation] = sidecarProfile.Name
|
||||
pod.Spec.InitContainers = slices.Insert(pod.Spec.InitContainers, 0, resolvInitContainer)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user