mirror of
https://github.com/YuzuZensai/netbird-kubernetes-operator.git
synced 2026-09-13 10:49:15 +00:00
e752d1587f
Bumps [github.com/netbirdio/netbird](https://github.com/netbirdio/netbird) from 0.72.4 to 0.74.7. <details> <summary>Release notes</summary> <p><em>Sourced from <a href="https://github.com/netbirdio/netbird/releases">github.com/netbirdio/netbird's releases</a>.</em></p> <blockquote> <h2>v0.74.7</h2> <h2>What's Changed</h2> <ul> <li>[relay] Handle QUIC connections concurrently to prevent handshake head-of-line blocking by <a href="https://github.com/lixmal"><code>@lixmal</code></a> in <a href="https://redirect.github.com/netbirdio/netbird/pull/6784">netbirdio/netbird#6784</a></li> <li>[client] Reject leading hyphen in getent input to prevent flag injection by <a href="https://github.com/lixmal"><code>@lixmal</code></a> in <a href="https://redirect.github.com/netbirdio/netbird/pull/6787">netbirdio/netbird#6787</a></li> <li>[client] Sanitize peer FQDN/hostname in generated SSH config by <a href="https://github.com/riccardomanfrin"><code>@riccardomanfrin</code></a> in <a href="https://redirect.github.com/netbirdio/netbird/pull/6805">netbirdio/netbird#6805</a></li> <li>[client] Disable gVisor TCP RACK loss detection on Windows by <a href="https://github.com/lixmal"><code>@lixmal</code></a> in <a href="https://redirect.github.com/netbirdio/netbird/pull/6808">netbirdio/netbird#6808</a></li> <li>[client] Rename isValidAccessToken to reflect audience-only check by <a href="https://github.com/riccardomanfrin"><code>@riccardomanfrin</code></a> in <a href="https://redirect.github.com/netbirdio/netbird/pull/6806">netbirdio/netbird#6806</a></li> <li>[client] Bind netstack SOCKS5 proxy to 127.0.0.1 by default by <a href="https://github.com/riccardomanfrin"><code>@riccardomanfrin</code></a> in <a href="https://redirect.github.com/netbirdio/netbird/pull/6812">netbirdio/netbird#6812</a></li> <li>[client] Evaluate IP fragments against firewall ACLs by <a href="https://github.com/lixmal"><code>@lixmal</code></a> in <a href="https://redirect.github.com/netbirdio/netbird/pull/6781">netbirdio/netbird#6781</a></li> </ul> <p><strong>Full Changelog</strong>: <a href="https://github.com/netbirdio/netbird/compare/v0.74.6...v0.74.7">https://github.com/netbirdio/netbird/compare/v0.74.6...v0.74.7</a></p> <h2>v0.74.6</h2> <h2>What's Changed</h2> <ul> <li>[client] ios: preserve WireGuard key on interactive re-login (<a href="https://redirect.github.com/netbirdio/netbird/issues/6777">#6777</a>)</li> </ul> <p><strong>Full Changelog</strong>: <a href="https://github.com/netbirdio/netbird/compare/v0.74.5...v0.74.6">https://github.com/netbirdio/netbird/compare/v0.74.5...v0.74.6</a></p> <h2>v0.74.5</h2> <h2>What's Changed</h2> <ul> <li>[proxy] enforce model allowlist for URL-routed providers (Bedrock/Vertex) by <a href="https://github.com/mlsmaycon"><code>@mlsmaycon</code></a> in <a href="https://redirect.github.com/netbirdio/netbird/pull/6764">netbirdio/netbird#6764</a></li> <li>[management] Remove proxy peer stale deduplication logic by <a href="https://github.com/mlsmaycon"><code>@mlsmaycon</code></a> in <a href="https://redirect.github.com/netbirdio/netbird/pull/6768">netbirdio/netbird#6768</a></li> </ul> <p><strong>Full Changelog</strong>: <a href="https://github.com/netbirdio/netbird/compare/v0.74.4...v0.74.5">https://github.com/netbirdio/netbird/compare/v0.74.4...v0.74.5</a></p> <h2>v0.74.4</h2> <h2>What's Changed</h2> <ul> <li>[management] fix: prevent reverse proxy domain from being pushed as DNS search domain by <a href="https://github.com/blaugrau90"><code>@blaugrau90</code></a> in <a href="https://redirect.github.com/netbirdio/netbird/pull/6498">netbirdio/netbird#6498</a></li> <li>[client] Recover from rosenpass key desync by <a href="https://github.com/lixmal"><code>@lixmal</code></a> in <a href="https://redirect.github.com/netbirdio/netbird/pull/6714">netbirdio/netbird#6714</a></li> <li>[client] Bump golang.org/x/crypto to v0.54.0 by <a href="https://github.com/lixmal"><code>@lixmal</code></a> in <a href="https://redirect.github.com/netbirdio/netbird/pull/6709">netbirdio/netbird#6709</a></li> </ul> <h2>New Contributors</h2> <ul> <li><a href="https://github.com/blaugrau90"><code>@blaugrau90</code></a> made their first contribution in <a href="https://redirect.github.com/netbirdio/netbird/pull/6498">netbirdio/netbird#6498</a></li> </ul> <p><strong>Full Changelog</strong>: <a href="https://github.com/netbirdio/netbird/compare/v0.74.3...v0.74.4">https://github.com/netbirdio/netbird/compare/v0.74.3...v0.74.4</a></p> <h2>v0.74.3</h2> <h2>What's Changed</h2> <ul> <li>[client] fix MDM managementURL conflict on default-port URL echo by <a href="https://github.com/riccardomanfrin"><code>@riccardomanfrin</code></a> in <a href="https://redirect.github.com/netbirdio/netbird/pull/6672">netbirdio/netbird#6672</a></li> <li>[client] Update gopsutil to v4 by <a href="https://github.com/mlsmaycon"><code>@mlsmaycon</code></a> in <a href="https://redirect.github.com/netbirdio/netbird/pull/6688">netbirdio/netbird#6688</a></li> <li>[client] Fix hanging status command during relay dial by <a href="https://github.com/theodorsm"><code>@theodorsm</code></a> in <a href="https://redirect.github.com/netbirdio/netbird/pull/6694">netbirdio/netbird#6694</a></li> </ul> <p><strong>Full Changelog</strong>: <a href="https://github.com/netbirdio/netbird/compare/v0.74.2...v0.74.3">https://github.com/netbirdio/netbird/compare/v0.74.2...v0.74.3</a></p> <h2>v0.74.2</h2> <h2>What's Changed</h2> <ul> <li>[management] Add vLLM e2e test by <a href="https://github.com/braginini"><code>@braginini</code></a> in <a href="https://redirect.github.com/netbirdio/netbird/pull/6649">netbirdio/netbird#6649</a></li> </ul> <!-- raw HTML omitted --> </blockquote> <p>... (truncated)</p> </details> <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/netbirdio/netbird/commit/a1c9427d8004576e2cbb9e546d409847fa9df318"><code>a1c9427</code></a> [client] Evaluate IP fragments against firewall ACLs (<a href="https://redirect.github.com/netbirdio/netbird/issues/6781">#6781</a>)</li> <li><a href="https://github.com/netbirdio/netbird/commit/b7b0d5796e988ac5f369d51d3a16b162d5fb9522"><code>b7b0d57</code></a> [client] Bind netstack SOCKS5 proxy to 127.0.0.1 by default (<a href="https://redirect.github.com/netbirdio/netbird/issues/6812">#6812</a>)</li> <li><a href="https://github.com/netbirdio/netbird/commit/3f8c4473783424e1642d2991b896add973249d97"><code>3f8c447</code></a> [client] Rename isValidAccessToken to reflect audience-only check (<a href="https://redirect.github.com/netbirdio/netbird/issues/6806">#6806</a>)</li> <li><a href="https://github.com/netbirdio/netbird/commit/6e3f4d8722d1c3f4482c44aec725aaba80c4512c"><code>6e3f4d8</code></a> [client] Disable gVisor TCP RACK loss detection on Windows (<a href="https://redirect.github.com/netbirdio/netbird/issues/6808">#6808</a>)</li> <li><a href="https://github.com/netbirdio/netbird/commit/099ae4bc6cc8ab95ef16343acb87c33b8197711c"><code>099ae4b</code></a> [client] Sanitize peer FQDN/hostname in generated SSH config (<a href="https://redirect.github.com/netbirdio/netbird/issues/6805">#6805</a>)</li> <li><a href="https://github.com/netbirdio/netbird/commit/63d60ba490794eebd0ad5ce77e4d31269e9c793b"><code>63d60ba</code></a> [client] Reject leading hyphen in getent input to prevent flag injection (<a href="https://redirect.github.com/netbirdio/netbird/issues/6787">#6787</a>)</li> <li><a href="https://github.com/netbirdio/netbird/commit/62fc8d254e636c3053ae8a21c4ea075558a8273f"><code>62fc8d2</code></a> [relay] Handle QUIC connections concurrently to prevent handshake head-of-lin...</li> <li><a href="https://github.com/netbirdio/netbird/commit/3a2f773d655d88d16ed953fc2a114a4e690a1b08"><code>3a2f773</code></a> [client] preserve WireGuard key on interactive re-login (<a href="https://redirect.github.com/netbirdio/netbird/issues/6777">#6777</a>)</li> <li><a href="https://github.com/netbirdio/netbird/commit/f0eed7564f3a9138962da1408986e4666d7137b5"><code>f0eed75</code></a> [management] Remove proxy peer stale deduplication logic (<a href="https://redirect.github.com/netbirdio/netbird/issues/6768">#6768</a>)</li> <li><a href="https://github.com/netbirdio/netbird/commit/277d8e4c5352950e1ec4fbd21a3266f0412b09fe"><code>277d8e4</code></a> [proxy] enforce model allowlist for URL-routed providers (Bedrock/Vertex) (<a href="https://redirect.github.com/netbirdio/netbird/issues/6">#6</a>...</li> <li>Additional commits viewable in <a href="https://github.com/netbirdio/netbird/compare/v0.72.4...v0.74.7">compare view</a></li> </ul> </details> <br /> [](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) </details> <!-- codesmith:footer --> --- <a href="https://app.blacksmith.sh/netbirdio/codesmith/kubernetes-operator/pr/376"><picture><source media="(prefers-color-scheme: dark)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/view-with-codesmith-dark-v2.svg"><source media="(prefers-color-scheme: light)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/view-with-codesmith-light-v2.svg"><img alt="View with Codesmith" src="https://pr-comments-assets.blacksmith.sh/codesmith/view-with-codesmith-dark-v2.svg"></picture></a> <a href="https://backend.blacksmith.sh/track/enable-autofix?expires=1787085769&installation_id=146802194&pr_number=376&repository=netbirdio%2Fkubernetes-operator&return_to=https%3A%2F%2Fgithub.com%2Fnetbirdio%2Fkubernetes-operator%2Fpull%2F376&signature=2f50a4b5245bbba3f14f2249365f03c700d30db07e15709056766b6a716394dd"><picture><source media="(prefers-color-scheme: dark)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-light.svg"><img alt="Autofix with Codesmith" src="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark.svg"></picture></a> <sup>Need help on this PR? Tag <code>/codesmith</code> with what you need. Autofix is disabled.</sup> <!-- codesmith:autofix:disabled --> <!-- /codesmith:footer --> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
358 lines
9.5 KiB
Go
358 lines
9.5 KiB
Go
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package v1
|
|
|
|
import (
|
|
"cmp"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
|
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/admission"
|
|
|
|
nbv1 "github.com/netbirdio/kubernetes-operator/api/v1"
|
|
nbv1alpha1 "github.com/netbirdio/kubernetes-operator/api/v1alpha1"
|
|
"github.com/netbirdio/kubernetes-operator/internal/controller"
|
|
)
|
|
|
|
const (
|
|
SidecarProfileAnnotation = "netbird.io/sidecar-profile"
|
|
|
|
setupKeyAnnotation = "netbird.io/setup-key"
|
|
sidecarAnnotation = "netbird.io/init-sidecar"
|
|
)
|
|
|
|
// 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, &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 _ admission.Defaulter[*corev1.Pod] = &PodNetbirdInjector{}
|
|
|
|
func (d *PodNetbirdInjector) Default(ctx context.Context, pod *corev1.Pod) error {
|
|
// If setup key annotations are set we do the legacy injection.
|
|
if pod.Annotations != nil && pod.Annotations[setupKeyAnnotation] != "" {
|
|
return d.legacyInjector(ctx, pod)
|
|
}
|
|
|
|
// Find sidecar profiles matching pods labels.
|
|
sidecarProfileList := &nbv1alpha1.SidecarProfileList{}
|
|
err := d.client.List(ctx, sidecarProfileList, client.InNamespace(pod.Namespace))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sidecarProfiles := []nbv1alpha1.SidecarProfile{}
|
|
for _, sidecarProfile := range sidecarProfileList.Items {
|
|
if sidecarProfile.Spec.PodSelector == nil || sidecarProfile.Spec.PodSelector.Size() == 0 {
|
|
sidecarProfiles = append(sidecarProfiles, sidecarProfile)
|
|
continue
|
|
}
|
|
selector, err := metav1.LabelSelectorAsSelector(sidecarProfile.Spec.PodSelector)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if selector.Matches(labels.Set(pod.Labels)) {
|
|
sidecarProfiles = append(sidecarProfiles, sidecarProfile)
|
|
}
|
|
}
|
|
// Do nothing if no profile matches.
|
|
if len(sidecarProfiles) == 0 {
|
|
return nil
|
|
}
|
|
// If two match we chose the first in alphabetical order.
|
|
if len(sidecarProfiles) > 1 {
|
|
slices.SortFunc(sidecarProfiles, func(a, b nbv1alpha1.SidecarProfile) int {
|
|
return cmp.Compare(a.Name, b.Name)
|
|
})
|
|
}
|
|
sidecarProfile := sidecarProfiles[0]
|
|
|
|
// Get setup key referenced by sidecar profile.
|
|
setupKey := &nbv1alpha1.SetupKey{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: sidecarProfile.Spec.SetupKeyRef.Name,
|
|
Namespace: pod.Namespace,
|
|
},
|
|
}
|
|
err = d.client.Get(ctx, client.ObjectKeyFromObject(setupKey), setupKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Add sidecar container.
|
|
envVars := []corev1.EnvVar{
|
|
{
|
|
Name: "NB_SETUP_KEY",
|
|
ValueFrom: &corev1.EnvVarSource{
|
|
SecretKeyRef: &corev1.SecretKeySelector{
|
|
LocalObjectReference: corev1.LocalObjectReference{
|
|
Name: setupKey.SecretName(),
|
|
},
|
|
Key: controller.SetupKeySecretKey,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Name: "NB_MANAGEMENT_URL",
|
|
Value: d.managementURL,
|
|
},
|
|
{
|
|
Name: "NB_LOG_FILE",
|
|
Value: "console",
|
|
},
|
|
{
|
|
Name: "NB_DISABLE_PROFILES",
|
|
Value: "true",
|
|
},
|
|
{
|
|
Name: "NB_DAEMON_ADDR",
|
|
Value: "unix:///var/run/netbird/netbird.sock",
|
|
},
|
|
{
|
|
Name: "NB_ENTRYPOINT_SERVICE_TIMEOUT",
|
|
Value: "0",
|
|
},
|
|
}
|
|
if len(sidecarProfile.Spec.ExtraDNSLabels) > 0 {
|
|
envVars = append(envVars, corev1.EnvVar{
|
|
Name: "NB_EXTRA_DNS_LABELS",
|
|
Value: strings.Join(sidecarProfile.Spec.ExtraDNSLabels, ","),
|
|
})
|
|
}
|
|
|
|
container := corev1.Container{
|
|
Name: "netbird",
|
|
Image: d.clientImage,
|
|
Env: envVars,
|
|
SecurityContext: &corev1.SecurityContext{
|
|
ReadOnlyRootFilesystem: new(true),
|
|
Capabilities: &corev1.Capabilities{
|
|
Add: []corev1.Capability{
|
|
"NET_ADMIN",
|
|
"SYS_RESOURCE",
|
|
"SYS_ADMIN",
|
|
},
|
|
},
|
|
Privileged: new(true),
|
|
},
|
|
VolumeMounts: []corev1.VolumeMount{
|
|
{
|
|
Name: "netbird-run",
|
|
MountPath: "/var/run/netbird",
|
|
},
|
|
{
|
|
Name: "netbird-lib",
|
|
MountPath: "/var/lib/netbird",
|
|
},
|
|
{
|
|
Name: "ssh-etc",
|
|
MountPath: "/etc/ssh",
|
|
},
|
|
{
|
|
Name: "resolv-conf",
|
|
MountPath: "/etc/resolv.conf",
|
|
SubPath: "resolv.conf",
|
|
},
|
|
{
|
|
Name: "resolv-conf",
|
|
MountPath: "/etc/resolv.conf.original.netbird",
|
|
SubPath: "resolv.conf.original.netbird",
|
|
},
|
|
},
|
|
Resources: corev1.ResourceRequirements{
|
|
Requests: corev1.ResourceList{
|
|
corev1.ResourceCPU: resource.MustParse("100m"),
|
|
corev1.ResourceMemory: resource.MustParse("128Mi"),
|
|
},
|
|
},
|
|
}
|
|
if sidecarProfile.Spec.ContainerOverride != nil {
|
|
baseJSON, err := json.Marshal(&container)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
overrideJSON, err := json.Marshal(sidecarProfile.Spec.ContainerOverride)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
mergedJSON, err := strategicpatch.StrategicMergePatch(baseJSON, overrideJSON, corev1.Container{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = json.Unmarshal(mergedJSON, &container)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
volumes := []corev1.Volume{
|
|
{
|
|
Name: "netbird-run",
|
|
VolumeSource: corev1.VolumeSource{
|
|
EmptyDir: &corev1.EmptyDirVolumeSource{},
|
|
},
|
|
},
|
|
{
|
|
Name: "netbird-lib",
|
|
VolumeSource: corev1.VolumeSource{
|
|
EmptyDir: &corev1.EmptyDirVolumeSource{},
|
|
},
|
|
},
|
|
{
|
|
Name: "ssh-etc",
|
|
VolumeSource: corev1.VolumeSource{
|
|
EmptyDir: &corev1.EmptyDirVolumeSource{},
|
|
},
|
|
},
|
|
{
|
|
Name: "resolv-conf",
|
|
VolumeSource: corev1.VolumeSource{
|
|
EmptyDir: &corev1.EmptyDirVolumeSource{},
|
|
},
|
|
},
|
|
}
|
|
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,
|
|
Command: []string{"sh", "-c", "cp /etc/resolv.conf /tmp/resolv.conf && cp /etc/resolv.conf /tmp/resolv.conf.original.netbird"},
|
|
SecurityContext: &corev1.SecurityContext{
|
|
ReadOnlyRootFilesystem: new(true),
|
|
Capabilities: &corev1.Capabilities{
|
|
Drop: []corev1.Capability{"ALL"},
|
|
},
|
|
},
|
|
VolumeMounts: []corev1.VolumeMount{
|
|
{
|
|
Name: "resolv-conf",
|
|
MountPath: "/tmp",
|
|
},
|
|
},
|
|
}
|
|
pod.Spec.InitContainers = slices.Insert(pod.Spec.InitContainers, 0, resolvInitContainer)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d *PodNetbirdInjector) legacyInjector(ctx context.Context, pod *corev1.Pod) error {
|
|
podlog.Info("Defaulting for Pod", "name", pod.GetName())
|
|
|
|
// retrieve the NBSetupKey resource
|
|
var nbSetupKey nbv1.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 == nbv1.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,
|
|
})
|
|
}
|
|
}
|
|
|
|
// Build the netbird container spec.
|
|
nbContainer := corev1.Container{
|
|
Name: "netbird",
|
|
Image: d.clientImage,
|
|
Env: envVars,
|
|
SecurityContext: &corev1.SecurityContext{
|
|
Capabilities: &corev1.Capabilities{
|
|
Add: []corev1.Capability{"NET_ADMIN"},
|
|
},
|
|
},
|
|
VolumeMounts: nbSetupKey.Spec.VolumeMounts,
|
|
}
|
|
|
|
// If sidecar mode is requested, inject as a sidecar (init container with restartPolicy: Always).
|
|
if pod.Annotations[sidecarAnnotation] == "true" {
|
|
restartPolicy := corev1.ContainerRestartPolicyAlways
|
|
nbContainer.RestartPolicy = &restartPolicy
|
|
pod.Spec.InitContainers = append(pod.Spec.InitContainers, nbContainer)
|
|
} else {
|
|
pod.Spec.Containers = append(pod.Spec.Containers, nbContainer)
|
|
}
|
|
|
|
pod.Spec.Volumes = append(pod.Spec.Volumes, nbSetupKey.Spec.Volumes...)
|
|
return nil
|
|
}
|