Fix: extra-dns-labels not being applied to pods (#82)

# Fix: NetBird extra-dns-labels not being applied to pods

## Problem

The `netbird.io/extra-dns-labels` annotation was not working when
applied to pods. Despite the webhook detecting the annotation and adding
it to the NetBird container configuration, the extra DNS labels were not
appearing in the NetBird UI or being applied to registered peers.

## Root Cause

The pod webhook had two issues:

1. **Invalid setup key argument**: The webhook was passing
`--setup-key-file /etc/nbkey` to the NetBird client, but this file path
was never created. The setup key was already being passed via the
`NB_SETUP_KEY` environment variable, making the file-based approach
unnecessary and causing confusion in the client startup.

2. **NetBird CLI flag bug**: The webhook was using the
`--extra-dns-labels` command line flag, but NetBird has a known issue
([netbirdio/netbird#4282](https://github.com/netbirdio/netbird/issues/4282))
where this flag is not properly processed. The workaround is to use the
`NB_EXTRA_DNS_LABELS` environment variable instead.

## Solution

- Removed the `--setup-key-file` argument entirely since the setup key
is provided via environment variable
- Removed all command line arguments from the NetBird container
- Added `NB_EXTRA_DNS_LABELS` environment variable when the
`netbird.io/extra-dns-labels` annotation is present
- NetBird client now uses only environment variables for configuration,
which is more reliable and matches the pattern used by the NBRoutingPeer
controller

## Changes

**Before:**
```go
args := []string{
    "--setup-key-file", "/etc/nbkey",
    "-m", managementURL,
}
// ... add extra-dns-labels to args
```

**After:**
```go
envVars := []corev1.EnvVar{
    {Name: "NB_SETUP_KEY", ValueFrom: ...},
    {Name: "NB_MANAGEMENT_URL", Value: managementURL},
}
// ... conditionally add NB_EXTRA_DNS_LABELS to envVars
```

## Testing

1. Create a deployment with the `netbird.io/setup-key` and
`netbird.io/extra-dns-labels` annotations:
```yaml
annotations:
  netbird.io/setup-key: my-setup-key
  netbird.io/extra-dns-labels: "my-label,another-label"
```

2. Verify the environment variable is set:
```bash
kubectl get pod <pod-name> -o jsonpath='{.spec.containers[?(@.name=="netbird")].env[*]}' | jq .
```

3. Check the NetBird UI to confirm the extra DNS labels appear on the
registered peer

4. Verify the NetBird container logs show successful registration
without errors

## References

- NetBird issue: https://github.com/netbirdio/netbird/issues/4282
- Documentation: [Extra DNS
Labels](https://docs.netbird.io/how-to/routing-traffic-to-private-networks#extra-dns-labels)

---

This fix ensures that the `netbird.io/extra-dns-labels` annotation works
as documented and provides a more robust configuration method by using
environment variables consistently across all NetBird deployments in the
operator.
This commit is contained in:
Christian De Leon
2025-11-24 19:00:58 +02:00
committed by GitHub
parent d8eb7cb513
commit 6c855c5d4e
+19 -21
View File
@@ -96,38 +96,36 @@ func (d *PodNetbirdInjector) Default(ctx context.Context, obj runtime.Object) er
managementURL = nbSetupKey.Spec.ManagementURL
}
// build the base arguments.
args := []string{
"--setup-key-file", "/etc/nbkey",
"-m", managementURL,
// build environment variables
envVars := []corev1.EnvVar{
{
Name: "NB_SETUP_KEY",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &nbSetupKey.Spec.SecretKeyRef,
},
},
{
Name: "NB_MANAGEMENT_URL",
Value: managementURL,
},
}
// check for extra DNS labels in annotations.
// check for extra DNS labels in annotations and add as environment variable
if pod.Annotations != nil {
if extra, ok := pod.Annotations["netbird.io/extra-dns-labels"]; ok && extra != "" {
podlog.Info("Found extra DNS labels", "extra", extra)
// append extra DNS labels to the CLI args.
args = append(args, "--extra-dns-labels", extra)
envVars = append(envVars, corev1.EnvVar{
Name: "NB_EXTRA_DNS_LABELS",
Value: extra,
})
}
}
// Append the netbird container with the constructed args.
// Append the netbird container with the constructed env vars.
pod.Spec.Containers = append(pod.Spec.Containers, corev1.Container{
Name: "netbird",
Image: d.clientImage,
Args: args,
Env: []corev1.EnvVar{
{
Name: "NB_SETUP_KEY",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &nbSetupKey.Spec.SecretKeyRef,
},
},
{
Name: "NB_MANAGEMENT_URL",
Value: managementURL,
},
},
Env: envVars,
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Add: []corev1.Capability{"NET_ADMIN"},