@@ -20,14 +20,15 @@ import (
2020 "context"
2121 "fmt"
2222
23- netbirdiov1 "github.com/netbirdio/kubernetes-operator/api/v1"
2423 corev1 "k8s.io/api/core/v1"
2524 "k8s.io/apimachinery/pkg/runtime"
2625 "k8s.io/apimachinery/pkg/types"
2726 ctrl "sigs.k8s.io/controller-runtime"
2827 "sigs.k8s.io/controller-runtime/pkg/client"
2928 logf "sigs.k8s.io/controller-runtime/pkg/log"
3029 "sigs.k8s.io/controller-runtime/pkg/webhook"
30+
31+ netbirdiov1 "github.com/netbirdio/kubernetes-operator/api/v1"
3132)
3233
3334const (
@@ -62,22 +63,24 @@ var _ webhook.CustomDefaulter = &PodNetbirdInjector{}
6263// Default implements webhook.CustomDefaulter so a webhook will be registered for the Kind Pod.
6364func (d * PodNetbirdInjector ) Default (ctx context.Context , obj runtime.Object ) error {
6465 pod , ok := obj .(* corev1.Pod )
65-
6666 if ! ok {
67- return fmt .Errorf ("expected an Pod object but got %T" , obj )
67+ return fmt .Errorf ("expected a Pod object but got %T" , obj )
6868 }
6969 podlog .Info ("Defaulting for Pod" , "name" , pod .GetName ())
7070
71+ // if the setup key annotation is missing, do nothing.
7172 if pod .Annotations == nil || pod .Annotations [setupKeyAnnotation ] == "" {
7273 return nil
7374 }
7475
76+ // retrieve the NBSetupKey resource
7577 var nbSetupKey netbirdiov1.NBSetupKey
7678 err := d .client .Get (ctx , types.NamespacedName {Namespace : pod .Namespace , Name : pod .Annotations [setupKeyAnnotation ]}, & nbSetupKey )
7779 if err != nil {
7880 return err
7981 }
8082
83+ // ensure the NBSetupKey is ready.
8184 ready := false
8285 for _ , c := range nbSetupKey .Status .Conditions {
8386 if c .Type == netbirdiov1 .Ready {
@@ -93,15 +96,26 @@ func (d *PodNetbirdInjector) Default(ctx context.Context, obj runtime.Object) er
9396 managementURL = nbSetupKey .Spec .ManagementURL
9497 }
9598
99+ // build the base arguments.
100+ args := []string {
101+ "--setup-key-file" , "/etc/nbkey" ,
102+ "-m" , managementURL ,
103+ }
104+
105+ // check for extra DNS labels in annotations.
106+ if pod .Annotations != nil {
107+ if extra , ok := pod .Annotations ["netbird.io/extra-dns-labels" ]; ok && extra != "" {
108+ podlog .Info ("Found extra DNS labels" , "extra" , extra )
109+ // append extra DNS labels to the CLI args.
110+ args = append (args , "--extra-dns-labels" , extra )
111+ }
112+ }
113+
114+ // Append the netbird container with the constructed args.
96115 pod .Spec .Containers = append (pod .Spec .Containers , corev1.Container {
97116 Name : "netbird" ,
98117 Image : d .clientImage ,
99- Args : []string {
100- "--setup-key-file" ,
101- "/etc/nbkey" ,
102- "-m" ,
103- managementURL ,
104- },
118+ Args : args ,
105119 Env : []corev1.EnvVar {
106120 {
107121 Name : "NB_SETUP_KEY" ,
@@ -116,9 +130,7 @@ func (d *PodNetbirdInjector) Default(ctx context.Context, obj runtime.Object) er
116130 },
117131 SecurityContext : & corev1.SecurityContext {
118132 Capabilities : & corev1.Capabilities {
119- Add : []corev1.Capability {
120- "NET_ADMIN" ,
121- },
133+ Add : []corev1.Capability {"NET_ADMIN" },
122134 },
123135 },
124136 })
0 commit comments