Files
netbird-kubernetes-operator/internal/webhook/v1/pod_webhook.go
T
Christian De LeonandGitHub 6c855c5d4e 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.
2025-11-24 19:00:58 +02:00

141 lines
3.9 KiB
Go

/*
Copyright 2025.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
netbirdiov1 "github.com/netbirdio/kubernetes-operator/api/v1"
)
const (
setupKeyAnnotation = "netbird.io/setup-key"
)
// nolint:unused
// log is for logging in this package.
var podlog = logf.Log.WithName("pod-resource")
// SetupPodWebhookWithManager registers the webhook for Pod in the manager.
func SetupPodWebhookWithManager(mgr ctrl.Manager, managementURL, clientImage string) error {
return ctrl.NewWebhookManagedBy(mgr).For(&corev1.Pod{}).
WithDefaulter(&PodNetbirdInjector{
client: mgr.GetClient(),
managementURL: managementURL,
clientImage: clientImage,
}).
Complete()
}
// PodNetbirdInjector struct is responsible for setting default values on the custom resource of the
// Kind Pod when those are created or updated.
type PodNetbirdInjector struct {
client client.Client
managementURL string
clientImage string
}
var _ webhook.CustomDefaulter = &PodNetbirdInjector{}
// Default implements webhook.CustomDefaulter so a webhook will be registered for the Kind Pod.
func (d *PodNetbirdInjector) Default(ctx context.Context, obj runtime.Object) error {
pod, ok := obj.(*corev1.Pod)
if !ok {
return fmt.Errorf("expected a Pod object but got %T", obj)
}
podlog.Info("Defaulting for Pod", "name", pod.GetName())
// if the setup key annotation is missing, do nothing.
if pod.Annotations == nil || pod.Annotations[setupKeyAnnotation] == "" {
return nil
}
// retrieve the NBSetupKey resource
var nbSetupKey netbirdiov1.NBSetupKey
err := d.client.Get(ctx, types.NamespacedName{Namespace: pod.Namespace, Name: pod.Annotations[setupKeyAnnotation]}, &nbSetupKey)
if err != nil {
return err
}
// ensure the NBSetupKey is ready.
ready := false
for _, c := range nbSetupKey.Status.Conditions {
if c.Type == netbirdiov1.NBSetupKeyReady {
ready = c.Status == corev1.ConditionTrue
}
}
if !ready {
return fmt.Errorf("NBSetupKey is not ready")
}
managementURL := d.managementURL
if nbSetupKey.Spec.ManagementURL != "" {
managementURL = nbSetupKey.Spec.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 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)
envVars = append(envVars, corev1.EnvVar{
Name: "NB_EXTRA_DNS_LABELS",
Value: extra,
})
}
}
// Append the netbird container with the constructed env vars.
pod.Spec.Containers = append(pod.Spec.Containers, corev1.Container{
Name: "netbird",
Image: d.clientImage,
Env: envVars,
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Add: []corev1.Capability{"NET_ADMIN"},
},
},
VolumeMounts: nbSetupKey.Spec.VolumeMounts,
})
pod.Spec.Volumes = append(pod.Spec.Volumes, nbSetupKey.Spec.Volumes...)
return nil
}