mirror of
https://github.com/YuzuZensai/netbird-kubernetes-operator.git
synced 2026-09-13 18:59:09 +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>
487 lines
16 KiB
Go
487 lines
16 KiB
Go
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package controller
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/json"
|
|
"fmt"
|
|
"maps"
|
|
"time"
|
|
|
|
"github.com/fluxcd/pkg/runtime/conditions"
|
|
"github.com/fluxcd/pkg/runtime/patch"
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
corev1 "k8s.io/api/core/v1"
|
|
policyv1 "k8s.io/api/policy/v1"
|
|
rbacv1 "k8s.io/api/rbac/v1"
|
|
kerrors "k8s.io/apimachinery/pkg/api/errors"
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
|
appsv1ac "k8s.io/client-go/applyconfigurations/apps/v1"
|
|
corev1ac "k8s.io/client-go/applyconfigurations/core/v1"
|
|
metav1ac "k8s.io/client-go/applyconfigurations/meta/v1"
|
|
policyv1ac "k8s.io/client-go/applyconfigurations/policy/v1"
|
|
rbacv1ac "k8s.io/client-go/applyconfigurations/rbac/v1"
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
|
|
|
netbird "github.com/netbirdio/netbird/shared/management/client/rest"
|
|
"github.com/netbirdio/netbird/shared/management/http/api"
|
|
|
|
nbv1alpha1 "github.com/netbirdio/kubernetes-operator/api/v1alpha1"
|
|
"github.com/netbirdio/kubernetes-operator/internal/k8sutil"
|
|
"github.com/netbirdio/kubernetes-operator/internal/netbirdutil"
|
|
nbv1alpha1ac "github.com/netbirdio/kubernetes-operator/pkg/applyconfigurations/api/v1alpha1"
|
|
"github.com/netbirdio/kubernetes-operator/pkg/version"
|
|
)
|
|
|
|
type NetworkRouterReconciler struct {
|
|
client.Client
|
|
|
|
Netbird *netbird.Client
|
|
ManagementURL string
|
|
ClientImage string
|
|
}
|
|
|
|
// +kubebuilder:rbac:groups=netbird.io,resources=networkrouters,verbs=get;list;watch;create;update;patch;delete
|
|
// +kubebuilder:rbac:groups=netbird.io,resources=networkrouters/status,verbs=get;update;patch
|
|
// +kubebuilder:rbac:groups=netbird.io,resources=networkrouters/finalizers,verbs=update
|
|
|
|
// nolint:gocyclo
|
|
func (r *NetworkRouterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
|
netRouter := &nbv1alpha1.NetworkRouter{}
|
|
err := r.Get(ctx, req.NamespacedName, netRouter)
|
|
if err != nil {
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
}
|
|
sp := patch.NewSerialPatcher(netRouter, r.Client)
|
|
|
|
if !netRouter.DeletionTimestamp.IsZero() {
|
|
return r.reconcileDelete(ctx, sp, netRouter)
|
|
}
|
|
|
|
ownerRef, err := k8sutil.ControllerReference(netRouter, r.Scheme())
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
|
|
// Ensure the DNS Zone exists.
|
|
_, err = netbirdutil.GetDNSZoneByName(ctx, r.Netbird, netRouter.Spec.DNSZoneRef.Name)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
|
|
controllerutil.AddFinalizer(netRouter, k8sutil.Finalizer("networkrouter"))
|
|
|
|
networkID, err := func() (string, error) {
|
|
networkReq := api.NetworkRequest{
|
|
Name: netRouter.Name,
|
|
}
|
|
if netRouter.Status.NetworkID != "" {
|
|
networkResp, err := r.Netbird.Networks.Update(ctx, netRouter.Status.NetworkID, networkReq)
|
|
if err != nil && !netbird.IsNotFound(err) {
|
|
return "", err
|
|
}
|
|
if err == nil {
|
|
return networkResp.Id, nil
|
|
}
|
|
}
|
|
networkResp, err := r.Netbird.Networks.Create(ctx, networkReq)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return networkResp.Id, nil
|
|
}()
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
netRouter.Status.NetworkID = networkID
|
|
err = sp.Patch(ctx, netRouter)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
|
|
// Calculate unique suffix used for Netbird resources.
|
|
sum := sha256.Sum256([]byte(netRouter.UID))
|
|
uniqueSuffix := networkID + "-" + fmt.Sprintf("%x", sum[:4])[:8]
|
|
|
|
// Create the group used by the router to discover peers.
|
|
groupAC := nbv1alpha1ac.Group(fmt.Sprintf("networkrouter-%s", netRouter.Name), req.Namespace).
|
|
WithOwnerReferences(ownerRef).
|
|
WithSpec(
|
|
nbv1alpha1ac.GroupSpec().
|
|
WithName(fmt.Sprintf("networkrouter-%s", uniqueSuffix)),
|
|
)
|
|
err = r.Client.Apply(ctx, groupAC, client.ForceOwnership)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
group := &nbv1alpha1.Group{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: *groupAC.Name,
|
|
Namespace: *groupAC.Namespace,
|
|
},
|
|
}
|
|
err = r.Client.Get(ctx, client.ObjectKeyFromObject(group), group)
|
|
if err != nil {
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
}
|
|
if group.Status.GroupID == "" {
|
|
return ctrl.Result{}, nil
|
|
}
|
|
|
|
// Create the setup key used by routing peers.
|
|
setupKeyAC := nbv1alpha1ac.SetupKey(fmt.Sprintf("networkrouter-%s", netRouter.Name), req.Namespace).
|
|
WithOwnerReferences(ownerRef).
|
|
WithSpec(
|
|
nbv1alpha1ac.SetupKeySpec().
|
|
WithName(fmt.Sprintf("networkrouter-%s", uniqueSuffix)).
|
|
WithEphemeral(true).
|
|
WithAutoGroups(nbv1alpha1ac.GroupReference().WithID(group.Status.GroupID)),
|
|
)
|
|
err = r.Client.Apply(ctx, setupKeyAC, client.ForceOwnership)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
setupKey := nbv1alpha1.SetupKey{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: *setupKeyAC.Name,
|
|
Namespace: *setupKeyAC.Namespace,
|
|
},
|
|
}
|
|
err = r.Get(ctx, client.ObjectKeyFromObject(&setupKey), &setupKey)
|
|
if err != nil {
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
}
|
|
if setupKey.Status.SetupKeyID == "" {
|
|
return ctrl.Result{}, nil
|
|
}
|
|
|
|
// Create the routing peer in netbird.
|
|
routingPeerID, err := func() (string, error) {
|
|
routerReq := api.NetworkRouterRequest{
|
|
Enabled: true,
|
|
Masquerade: true,
|
|
Metric: 9999,
|
|
PeerGroups: new([]string{group.Status.GroupID}),
|
|
}
|
|
if netRouter.Status.RoutingPeerID != "" {
|
|
resp, err := r.Netbird.Networks.Routers(networkID).Update(ctx, netRouter.Status.RoutingPeerID, routerReq)
|
|
if err != nil && !netbird.IsNotFound(err) {
|
|
return "", err
|
|
}
|
|
if err == nil {
|
|
return resp.Id, nil
|
|
}
|
|
}
|
|
resp, err := r.Netbird.Networks.Routers(networkID).Create(ctx, routerReq)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return resp.Id, nil
|
|
}()
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
netRouter.Status.RoutingPeerID = routingPeerID
|
|
err = sp.Patch(ctx, netRouter, patch.WithStatusObservedGeneration{})
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
|
|
// Setup router configuration.
|
|
selectorLabels := map[string]string{
|
|
"app.kubernetes.io/name": "networkrouter",
|
|
"app.kubernetes.io/instance": req.Name,
|
|
}
|
|
|
|
logLevel := "info"
|
|
if netRouter.Spec.LogLevel != "" {
|
|
logLevel = netRouter.Spec.LogLevel
|
|
}
|
|
|
|
clientImage := r.ClientImage
|
|
if netRouter.Spec.Image != "" {
|
|
clientImage = netRouter.Spec.Image
|
|
}
|
|
|
|
// Create forwarder resources.
|
|
forwarderName := fmt.Sprintf("networkrouter-%s-forwarder", req.Name)
|
|
forwarderSvcAC := corev1ac.Service(forwarderName, req.Namespace).
|
|
WithLabels(map[string]string{ForwarderRouterNameLabel: netRouter.Name}).
|
|
WithOwnerReferences(ownerRef).
|
|
WithSpec(
|
|
corev1ac.ServiceSpec().
|
|
WithClusterIP("None").
|
|
WithSelector(selectorLabels),
|
|
)
|
|
err = r.Client.Apply(ctx, forwarderSvcAC, client.ForceOwnership)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
|
|
saAC := corev1ac.ServiceAccount(forwarderName, req.Namespace).
|
|
WithOwnerReferences(ownerRef)
|
|
err = r.Client.Apply(ctx, saAC, client.ForceOwnership)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
roleAC := rbacv1ac.Role(forwarderName, req.Namespace).
|
|
WithOwnerReferences(ownerRef).
|
|
WithRules(rbacv1ac.PolicyRule().WithAPIGroups("").WithResources("configmaps").WithVerbs("get", "watch"))
|
|
err = r.Client.Apply(ctx, roleAC, client.ForceOwnership)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
roleBindAC := rbacv1ac.RoleBinding(forwarderName, req.Namespace).
|
|
WithOwnerReferences(ownerRef).
|
|
WithSubjects(rbacv1ac.Subject().WithKind(*saAC.Kind).WithName(*saAC.Name)).
|
|
WithRoleRef(rbacv1ac.RoleRef().WithKind(*roleAC.Kind).WithName(*roleAC.Name))
|
|
err = r.Client.Apply(ctx, roleBindAC, client.ForceOwnership)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
|
|
// Create the deployment.
|
|
podTemplateSpecAC := corev1ac.PodTemplateSpec().
|
|
WithLabels(selectorLabels).
|
|
WithSpec(corev1ac.PodSpec().
|
|
WithTopologySpreadConstraints(
|
|
corev1ac.TopologySpreadConstraint().
|
|
WithMaxSkew(1).
|
|
WithTopologyKey(corev1.LabelHostname).
|
|
WithWhenUnsatisfiable(corev1.ScheduleAnyway).
|
|
WithLabelSelector(metav1ac.LabelSelector().
|
|
WithMatchLabels(selectorLabels),
|
|
),
|
|
).
|
|
WithInitContainers(corev1ac.Container().
|
|
WithName("resolv-conf").
|
|
WithImage(clientImage).
|
|
WithCommand("sh", "-c", "cp /etc/resolv.conf /tmp/resolv.conf && cp /etc/resolv.conf /tmp/resolv.conf.original.netbird").
|
|
WithVolumeMounts(corev1ac.VolumeMount().
|
|
WithName("resolv-conf").
|
|
WithMountPath("/tmp"),
|
|
).
|
|
WithSecurityContext(corev1ac.SecurityContext().
|
|
WithCapabilities(corev1ac.Capabilities().WithDrop("ALL")).
|
|
WithReadOnlyRootFilesystem(true),
|
|
),
|
|
).
|
|
WithContainers(
|
|
corev1ac.Container().
|
|
WithName("netbird").
|
|
WithImage(clientImage).
|
|
WithEnv(
|
|
corev1ac.EnvVar().
|
|
WithName("NB_SETUP_KEY").
|
|
WithValueFrom(corev1ac.EnvVarSource().
|
|
WithSecretKeyRef(corev1ac.SecretKeySelector().
|
|
WithName(setupKey.SecretName()).
|
|
WithKey(SetupKeySecretKey),
|
|
),
|
|
),
|
|
corev1ac.EnvVar().
|
|
WithName("NB_MANAGEMENT_URL").
|
|
WithValue(r.ManagementURL),
|
|
corev1ac.EnvVar().
|
|
WithName("NB_LOG_LEVEL").
|
|
WithValue(logLevel),
|
|
corev1ac.EnvVar().
|
|
WithName("NB_LOG_FILE").
|
|
WithValue("console"),
|
|
corev1ac.EnvVar().
|
|
WithName("NB_DISABLE_PROFILES").
|
|
WithValue("true"),
|
|
corev1ac.EnvVar().
|
|
WithName("NB_DAEMON_ADDR").
|
|
WithValue("unix:///var/run/netbird/netbird.sock"),
|
|
corev1ac.EnvVar().
|
|
WithName("NB_ENTRYPOINT_SERVICE_TIMEOUT").
|
|
WithValue("0"),
|
|
).
|
|
WithStartupProbe(corev1ac.Probe().WithExec(corev1ac.ExecAction().WithCommand("netbird", "status", "--check", "startup"))).
|
|
WithReadinessProbe(corev1ac.Probe().WithExec(corev1ac.ExecAction().WithCommand("netbird", "status", "--check", "ready"))).
|
|
WithVolumeMounts(
|
|
corev1ac.VolumeMount().WithName("netbird-run").WithMountPath("/var/run/netbird"),
|
|
corev1ac.VolumeMount().WithName("netbird-lib").WithMountPath("/var/lib/netbird"),
|
|
corev1ac.VolumeMount().WithName("ssh-etc").WithMountPath("/etc/ssh"),
|
|
corev1ac.VolumeMount().WithName("resolv-conf").WithMountPath("/etc/resolv.conf").WithSubPath("resolv.conf"),
|
|
corev1ac.VolumeMount().WithName("resolv-conf").WithMountPath("/etc/resolv.conf.original.netbird").WithSubPath("resolv.conf.original.netbird"),
|
|
).
|
|
WithSecurityContext(corev1ac.SecurityContext().
|
|
WithReadOnlyRootFilesystem(true).
|
|
WithCapabilities(corev1ac.Capabilities().
|
|
WithAdd("NET_ADMIN").
|
|
WithAdd("SYS_RESOURCE").
|
|
WithAdd("SYS_ADMIN"),
|
|
).
|
|
WithPrivileged(true),
|
|
).
|
|
WithResources(corev1ac.ResourceRequirements().
|
|
WithRequests(corev1.ResourceList{
|
|
corev1.ResourceCPU: resource.MustParse("100m"),
|
|
corev1.ResourceMemory: resource.MustParse("128Mi"),
|
|
}),
|
|
),
|
|
corev1ac.Container().
|
|
WithName("kube-egress-forwarder").
|
|
WithImage(version.KubeEgressForwarderImage).
|
|
WithArgs("--configmap-name", forwarderName, "--configmap-namespace", req.Namespace).
|
|
WithVolumeMounts(
|
|
corev1ac.VolumeMount().WithName("resolv-conf").WithMountPath("/etc/resolv.conf").WithSubPath("resolv.conf"),
|
|
).
|
|
WithSecurityContext(corev1ac.SecurityContext().
|
|
WithCapabilities(corev1ac.Capabilities().WithDrop("ALL")).
|
|
WithReadOnlyRootFilesystem(true),
|
|
).
|
|
WithResources(corev1ac.ResourceRequirements().
|
|
WithRequests(corev1.ResourceList{
|
|
corev1.ResourceCPU: resource.MustParse("100m"),
|
|
corev1.ResourceMemory: resource.MustParse("128Mi"),
|
|
}),
|
|
),
|
|
).
|
|
WithServiceAccountName(*saAC.Name).
|
|
WithVolumes(
|
|
corev1ac.Volume().WithName("netbird-run").WithEmptyDir(corev1ac.EmptyDirVolumeSource()),
|
|
corev1ac.Volume().WithName("netbird-lib").WithEmptyDir(corev1ac.EmptyDirVolumeSource()),
|
|
corev1ac.Volume().WithName("ssh-etc").WithEmptyDir(corev1ac.EmptyDirVolumeSource()),
|
|
corev1ac.Volume().WithName("resolv-conf").WithEmptyDir(corev1ac.EmptyDirVolumeSource()),
|
|
),
|
|
)
|
|
|
|
workloadLabels := map[string]string{}
|
|
workloadAnnotations := map[string]string{}
|
|
replicas := int32(3)
|
|
if netRouter.Spec.WorkloadOverride != nil {
|
|
if netRouter.Spec.WorkloadOverride.Labels != nil {
|
|
workloadLabels = netRouter.Spec.WorkloadOverride.Labels
|
|
}
|
|
if netRouter.Spec.WorkloadOverride.Annotations != nil {
|
|
workloadAnnotations = netRouter.Spec.WorkloadOverride.Annotations
|
|
}
|
|
if netRouter.Spec.WorkloadOverride.Replicas != nil {
|
|
replicas = *netRouter.Spec.WorkloadOverride.Replicas
|
|
}
|
|
if netRouter.Spec.WorkloadOverride.PodTemplate != nil {
|
|
baseJSON, err := json.Marshal(&podTemplateSpecAC)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
overrideJSON, err := json.Marshal(netRouter.Spec.WorkloadOverride.PodTemplate)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
mergedJSON, err := strategicpatch.StrategicMergePatch(baseJSON, overrideJSON, corev1.PodTemplateSpec{})
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
err = json.Unmarshal(mergedJSON, &podTemplateSpecAC)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
}
|
|
}
|
|
maps.Copy(workloadLabels, selectorLabels)
|
|
|
|
depAC := appsv1ac.Deployment(fmt.Sprintf("networkrouter-%s", req.Name), req.Namespace).
|
|
WithOwnerReferences(ownerRef).
|
|
WithLabels(workloadLabels).
|
|
WithAnnotations(workloadAnnotations).
|
|
WithSpec(appsv1ac.DeploymentSpec().WithReplicas(replicas).WithSelector(metav1ac.LabelSelector().WithMatchLabels(selectorLabels)).WithTemplate(podTemplateSpecAC))
|
|
err = r.Client.Apply(ctx, depAC, client.ForceOwnership)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
|
|
if replicas > 1 {
|
|
pdbAC := policyv1ac.PodDisruptionBudget(fmt.Sprintf("networkrouter-%s", req.Name), req.Namespace).
|
|
WithOwnerReferences(ownerRef).
|
|
WithLabels(workloadLabels).
|
|
WithAnnotations(workloadAnnotations).
|
|
WithSpec(policyv1ac.PodDisruptionBudgetSpec().
|
|
WithMaxUnavailable(intstr.FromInt(1)).
|
|
WithSelector(metav1ac.LabelSelector().
|
|
WithMatchLabels(selectorLabels),
|
|
),
|
|
)
|
|
err = r.Client.Apply(ctx, pdbAC, client.ForceOwnership)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
} else {
|
|
pdb := policyv1.PodDisruptionBudget{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: fmt.Sprintf("networkrouter-%s", req.Name),
|
|
Namespace: req.Namespace,
|
|
},
|
|
}
|
|
err = r.Client.Delete(ctx, &pdb)
|
|
if err != nil && !kerrors.IsNotFound(err) {
|
|
return ctrl.Result{}, err
|
|
}
|
|
}
|
|
|
|
dep := &appsv1.Deployment{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: *depAC.Name,
|
|
Namespace: *depAC.Namespace,
|
|
},
|
|
}
|
|
err = r.Client.Get(ctx, client.ObjectKeyFromObject(dep), dep)
|
|
if err != nil {
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
}
|
|
if dep.Status.ReadyReplicas != dep.Status.Replicas {
|
|
return ctrl.Result{}, nil
|
|
}
|
|
|
|
conditions.MarkTrue(netRouter, nbv1alpha1.ReadyCondition, nbv1alpha1.ReconciledReason, "")
|
|
err = sp.Patch(ctx, netRouter, patch.WithStatusObservedGeneration{})
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
return ctrl.Result{RequeueAfter: 15 * time.Minute}, nil
|
|
}
|
|
|
|
func (r *NetworkRouterReconciler) reconcileDelete(ctx context.Context, sp *patch.SerialPatcher, netRouter *nbv1alpha1.NetworkRouter) (ctrl.Result, error) {
|
|
if netRouter.Status.RoutingPeerID != "" {
|
|
err := r.Netbird.Networks.Routers(netRouter.Status.NetworkID).Delete(ctx, netRouter.Status.RoutingPeerID)
|
|
if err != nil && !netbird.IsNotFound(err) {
|
|
return ctrl.Result{}, err
|
|
}
|
|
}
|
|
if netRouter.Status.NetworkID != "" {
|
|
err := r.Netbird.Networks.Delete(ctx, netRouter.Status.NetworkID)
|
|
if err != nil && !netbird.IsNotFound(err) {
|
|
return ctrl.Result{}, err
|
|
}
|
|
}
|
|
|
|
controllerutil.RemoveFinalizer(netRouter, k8sutil.Finalizer("networkrouter"))
|
|
err := sp.Patch(ctx, netRouter)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
return ctrl.Result{}, nil
|
|
}
|
|
|
|
func (r *NetworkRouterReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
|
return ctrl.NewControllerManagedBy(mgr).
|
|
For(&nbv1alpha1.NetworkRouter{}).
|
|
Owns(&nbv1alpha1.Group{}).
|
|
Owns(&nbv1alpha1.SetupKey{}).
|
|
Owns(&appsv1.Deployment{}).
|
|
Owns(&corev1.Service{}).
|
|
Owns(&corev1.ServiceAccount{}).
|
|
Owns(&rbacv1.Role{}).
|
|
Owns(&rbacv1.RoleBinding{}).
|
|
Complete(r)
|
|
}
|