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:
Philip Laine
2026-06-25 10:44:53 +02:00
committed by GitHub
parent b82544250f
commit 447bbc76cd
2 changed files with 94 additions and 62 deletions
+16 -17
View File
@@ -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
}