Fix group reconcile removing peers (#226)

This changes the group reconcile to get the existing group first and
then update it in place. If not done like this peers will be removed
from the group on the next reconcile.

Fixes #221
This commit is contained in:
Philip Laine
2026-05-05 11:12:50 +02:00
committed by GitHub
parent f8a383f533
commit 0c542db9d7
2 changed files with 28 additions and 27 deletions
+4 -16
View File
@@ -76,29 +76,17 @@ build-installer: generate
##@ Deployment ##@ Deployment
ifndef ignore-not-found
ignore-not-found = false
endif
## Install CRDs into the K8s cluster specified in ~/.kube/config.
.PHONY: install .PHONY: install
install: generate install: generate
kubectl apply --server-side -f helm/kubernetes-operator/crds kubectl apply --server-side -f helm/kubernetes-operator/crds
## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
.PHONY: uninstall .PHONY: uninstall
uninstall: generate uninstall:
kubectl delete -f helm/kubernetes-operator/crds kubectl delete -f helm/kubernetes-operator/crds
## Deploy controller to the K8s cluster specified in ~/.kube/config. run: install
.PHONY: deploy kubectl create namespace netbird --dry-run=client -o yaml | kubectl apply -f -
deploy: genereate go run cmd/main.go --enable-webhooks=false --netbird-api-key=$${NB_API_KEY} --runtime-namespace netbird
helm install -n netbird --create-namespace kubernetes-operator --set operator.image.tag=$(word 2,$(subst :, ,${IMG})) helm/kubernetes-operator
## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
.PHONY: undeploy
undeploy:
helm uninstall -n netbird kubernetes-operator
##@ Dependencies ##@ Dependencies
+16 -3
View File
@@ -2,6 +2,7 @@ package controller
import ( import (
"context" "context"
"time"
"github.com/fluxcd/pkg/runtime/conditions" "github.com/fluxcd/pkg/runtime/conditions"
"github.com/fluxcd/pkg/runtime/patch" "github.com/fluxcd/pkg/runtime/patch"
@@ -25,6 +26,7 @@ type GroupReconciler struct {
// +kubebuilder:rbac:groups=netbird.io,resources=groups,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=netbird.io,resources=groups,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=netbird.io,resources=groups/status,verbs=get;update;patch // +kubebuilder:rbac:groups=netbird.io,resources=groups/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=netbird.io,resources=groups/finalizers,verbs=update // +kubebuilder:rbac:groups=netbird.io,resources=groups/finalizers,verbs=update
func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
group := &nbv1alpha1.Group{} group := &nbv1alpha1.Group{}
err := r.Get(ctx, req.NamespacedName, group) err := r.Get(ctx, req.NamespacedName, group)
@@ -44,10 +46,18 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
} }
groupID, err := func() (string, error) { groupID, err := func() (string, error) {
if group.Status.GroupID != "" {
groupResp, err := r.Netbird.Groups.Get(ctx, group.Status.GroupID)
if err == nil {
peers := []string{}
for _, peer := range groupResp.Peers {
peers = append(peers, peer.Id)
}
groupReq := api.GroupRequest{ groupReq := api.GroupRequest{
Name: group.Spec.Name, Name: group.Spec.Name,
Peers: &peers,
Resources: &groupResp.Resources,
} }
if group.Status.GroupID != "" {
resp, err := r.Netbird.Groups.Update(ctx, group.Status.GroupID, groupReq) resp, err := r.Netbird.Groups.Update(ctx, group.Status.GroupID, groupReq)
if err != nil && !netbird.IsNotFound(err) { if err != nil && !netbird.IsNotFound(err) {
return "", err return "", err
@@ -56,6 +66,10 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
return resp.Id, nil return resp.Id, nil
} }
} }
}
groupReq := api.GroupRequest{
Name: group.Spec.Name,
}
resp, err := r.Netbird.Groups.Create(ctx, groupReq) resp, err := r.Netbird.Groups.Create(ctx, groupReq)
if err != nil { if err != nil {
return "", err return "", err
@@ -72,7 +86,7 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
if err != nil { if err != nil {
return ctrl.Result{}, err return ctrl.Result{}, err
} }
return ctrl.Result{}, nil return ctrl.Result{RequeueAfter: 15 * time.Minute}, nil
} }
func (r *GroupReconciler) reconcileDelete(ctx context.Context, sp *patch.SerialPatcher, group *nbv1alpha1.Group) (ctrl.Result, error) { func (r *GroupReconciler) reconcileDelete(ctx context.Context, sp *patch.SerialPatcher, group *nbv1alpha1.Group) (ctrl.Result, error) {
@@ -91,7 +105,6 @@ func (r *GroupReconciler) reconcileDelete(ctx context.Context, sp *patch.SerialP
return ctrl.Result{}, nil return ctrl.Result{}, nil
} }
// SetupWithManager sets up the controller with the Manager.
func (r *GroupReconciler) SetupWithManager(mgr ctrl.Manager) error { func (r *GroupReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr). return ctrl.NewControllerManagedBy(mgr).
For(&nbv1alpha1.Group{}). For(&nbv1alpha1.Group{}).