mirror of
https://github.com/YuzuZensai/netbird-kubernetes-operator.git
synced 2026-09-13 10:49:15 +00:00
Add ready conditon and cleanup finalizer and status patching (#186)
This change adds a ready condition. It also sets a standard for status fields and documentation. It makes use of helper functions from FluxCD to better manage patching of finalizers and status. Signed-off-by: Philip Laine <philip.laine@gmail.com>
This commit is contained in:
@@ -3,20 +3,17 @@ package controller
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/fluxcd/pkg/runtime/conditions"
|
||||
"github.com/fluxcd/pkg/runtime/patch"
|
||||
netbird "github.com/netbirdio/netbird/shared/management/client/rest"
|
||||
"github.com/netbirdio/netbird/shared/management/http/api"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
|
||||
nbv1alpha1 "github.com/netbirdio/kubernetes-operator/api/v1alpha1"
|
||||
nbv1alpha1ac "github.com/netbirdio/kubernetes-operator/pkg/applyconfigurations/api/v1alpha1"
|
||||
)
|
||||
|
||||
const (
|
||||
GroupFinalizer = "netbird.io/group"
|
||||
)
|
||||
|
||||
// GroupReconciler reconciles a Group object
|
||||
type GroupReconciler struct {
|
||||
client.Client
|
||||
|
||||
@@ -27,28 +24,29 @@ type GroupReconciler struct {
|
||||
// +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)
|
||||
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, group)
|
||||
return r.reconcileDelete(ctx, sp, group)
|
||||
}
|
||||
|
||||
groupAC := nbv1alpha1ac.Group(req.Name, req.Namespace).WithFinalizers(SetupKeyFinalizer)
|
||||
err = r.Client.Apply(ctx, groupAC)
|
||||
controllerutil.AddFinalizer(group, nbv1alpha1.NetbirdFinalizer)
|
||||
err = sp.Patch(ctx, group)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
groupID, err := func() (string, error) {
|
||||
if group.Status.GroupID != nil {
|
||||
groupReq := api.GroupRequest{
|
||||
Name: group.Spec.Name,
|
||||
}
|
||||
resp, err := r.Netbird.Groups.Update(ctx, *group.Status.GroupID, groupReq)
|
||||
groupReq := api.GroupRequest{
|
||||
Name: group.Spec.Name,
|
||||
}
|
||||
if group.Status.GroupID != "" {
|
||||
resp, err := r.Netbird.Groups.Update(ctx, group.Status.GroupID, groupReq)
|
||||
if err != nil && !netbird.IsNotFound(err) {
|
||||
return "", err
|
||||
}
|
||||
@@ -56,10 +54,6 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
|
||||
return resp.Id, nil
|
||||
}
|
||||
}
|
||||
|
||||
groupReq := api.GroupRequest{
|
||||
Name: group.Spec.Name,
|
||||
}
|
||||
resp, err := r.Netbird.Groups.Create(ctx, groupReq)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -69,26 +63,26 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
group.Status.GroupID = groupID
|
||||
|
||||
groupAC = nbv1alpha1ac.Group(req.Name, req.Namespace).WithStatus(nbv1alpha1ac.GroupStatus().WithGroupID(groupID))
|
||||
err = r.Client.Status().Apply(ctx, groupAC)
|
||||
conditions.MarkTrue(group, nbv1alpha1.ReadyCondition, nbv1alpha1.ReconciledReason, "")
|
||||
err = sp.Patch(ctx, group, patch.WithStatusObservedGeneration{})
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
func (r *GroupReconciler) reconcileDelete(ctx context.Context, group nbv1alpha1.Group) (ctrl.Result, error) {
|
||||
if group.Status.GroupID != nil {
|
||||
err := r.Netbird.Groups.Delete(ctx, *group.Status.GroupID)
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
groupAC := nbv1alpha1ac.Group(group.Name, group.Namespace).WithFinalizers()
|
||||
err := r.Client.Apply(ctx, groupAC)
|
||||
controllerutil.RemoveFinalizer(group, nbv1alpha1.NetbirdFinalizer)
|
||||
err := sp.Patch(ctx, group)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user