mirror of
https://github.com/YuzuZensai/netbird-kubernetes-operator.git
synced 2026-09-13 10:49:15 +00:00
Certain errors are better ignored to instead rely on the child resource to trigger a new reconcile. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **Bug Fixes** * Enhanced error handling in group resolution to prevent silent failures and ensure proper error notification * Improved system resilience by gracefully handling missing or unavailable Kubernetes resources without triggering reconciliation failures * Optimized setup key processing workflow to enhance overall system robustness and reliability <!-- review_stack_entry_start --> [](https://app.coderabbit.ai/change-stack/netbirdio/kubernetes-operator/pull/275?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack) <!-- review_stack_entry_end --> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
118 lines
3.2 KiB
Go
118 lines
3.2 KiB
Go
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package controller
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/fluxcd/pkg/runtime/conditions"
|
|
"github.com/fluxcd/pkg/runtime/patch"
|
|
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"
|
|
)
|
|
|
|
type GroupReconciler struct {
|
|
client.Client
|
|
|
|
Netbird *netbird.Client
|
|
}
|
|
|
|
// +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/finalizers,verbs=update
|
|
|
|
func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
|
group := &nbv1alpha1.Group{}
|
|
err := r.Get(ctx, req.NamespacedName, group)
|
|
if err != nil {
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
}
|
|
sp := patch.NewSerialPatcher(group, r.Client)
|
|
|
|
if !group.DeletionTimestamp.IsZero() {
|
|
return r.reconcileDelete(ctx, sp, group)
|
|
}
|
|
|
|
controllerutil.AddFinalizer(group, k8sutil.Finalizer("group"))
|
|
err = sp.Patch(ctx, group)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
|
|
groupID, err := func() (string, error) {
|
|
if group.Status.GroupID != "" {
|
|
groupResp, err := r.Netbird.Groups.Get(ctx, group.Status.GroupID)
|
|
if err != nil && !netbird.IsNotFound(err) {
|
|
return "", err
|
|
}
|
|
if err == nil {
|
|
peers := []string{}
|
|
for _, peer := range groupResp.Peers {
|
|
peers = append(peers, peer.Id)
|
|
}
|
|
groupReq := api.GroupRequest{
|
|
Name: group.Spec.Name,
|
|
Peers: &peers,
|
|
Resources: &groupResp.Resources,
|
|
}
|
|
resp, err := r.Netbird.Groups.Update(ctx, group.Status.GroupID, groupReq)
|
|
if err != nil && !netbird.IsNotFound(err) {
|
|
return "", err
|
|
}
|
|
if err == nil {
|
|
return resp.Id, nil
|
|
}
|
|
}
|
|
}
|
|
groupReq := api.GroupRequest{
|
|
Name: group.Spec.Name,
|
|
}
|
|
resp, err := r.Netbird.Groups.Create(ctx, groupReq)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return resp.Id, nil
|
|
}()
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
group.Status.GroupID = groupID
|
|
|
|
conditions.MarkTrue(group, nbv1alpha1.ReadyCondition, nbv1alpha1.ReconciledReason, "")
|
|
err = sp.Patch(ctx, group, patch.WithStatusObservedGeneration{})
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
return ctrl.Result{RequeueAfter: 15 * time.Minute}, nil
|
|
}
|
|
|
|
func (r *GroupReconciler) reconcileDelete(ctx context.Context, sp *patch.SerialPatcher, group *nbv1alpha1.Group) (ctrl.Result, error) {
|
|
if group.Status.GroupID != "" {
|
|
err := r.Netbird.Groups.Delete(ctx, group.Status.GroupID)
|
|
if err != nil && !netbird.IsNotFound(err) {
|
|
return ctrl.Result{}, err
|
|
}
|
|
}
|
|
|
|
controllerutil.RemoveFinalizer(group, k8sutil.Finalizer("group"))
|
|
err := sp.Patch(ctx, group)
|
|
if err != nil {
|
|
return ctrl.Result{}, err
|
|
}
|
|
return ctrl.Result{}, nil
|
|
}
|
|
|
|
func (r *GroupReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
|
return ctrl.NewControllerManagedBy(mgr).
|
|
For(&nbv1alpha1.Group{}).
|
|
Complete(r)
|
|
}
|