Skip to content

Commit 8f1cd1a

Browse files
committed
Restructure NBGroup controller
1 parent 821891a commit 8f1cd1a

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/netbirdio/netbird v0.36.7
1010
github.com/onsi/ginkgo/v2 v2.21.0
1111
github.com/onsi/gomega v1.35.1
12+
gorm.io/gorm v1.25.12
1213
k8s.io/api v0.32.0
1314
k8s.io/apimachinery v0.32.0
1415
k8s.io/client-go v0.32.0

internal/controller/nbgroup_controller.go

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"k8s.io/apimachinery/pkg/runtime"
1111
ctrl "sigs.k8s.io/controller-runtime"
1212
"sigs.k8s.io/controller-runtime/pkg/client"
13-
"sigs.k8s.io/controller-runtime/pkg/log"
1413

14+
"github.com/go-logr/logr"
1515
netbirdiov1 "github.com/netbirdio/kubernetes-operator/api/v1"
1616
"github.com/netbirdio/kubernetes-operator/internal/util"
1717
netbird "github.com/netbirdio/netbird/management/client/rest"
@@ -34,15 +34,14 @@ const (
3434
// Reconcile is part of the main kubernetes reconciliation loop which aims to
3535
// move the current state of the cluster closer to the desired state.
3636
func (r *NBGroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.Result, err error) {
37-
_ = log.FromContext(ctx)
38-
39-
ctrl.Log.Info("NBGroup: Reconciling", "namespace", req.Namespace, "name", req.Name)
37+
logger := ctrl.Log.WithName("NBGroup").WithValues("namespace", req.Namespace, "name", req.Name)
38+
logger.Info("Reconciling NBGroup")
4039

4140
nbGroup := netbirdiov1.NBGroup{}
4241
err = r.Client.Get(ctx, req.NamespacedName, &nbGroup)
4342
if err != nil {
4443
if !errors.IsNotFound(err) {
45-
ctrl.Log.Error(errKubernetesAPI, "error getting NBGroup", "err", err, "namespace", req.Namespace, "name", req.Name)
44+
logger.Error(errKubernetesAPI, "error getting NBGroup", "err", err)
4645
}
4746
return ctrl.Result{RequeueAfter: defaultRequeueAfter}, nil
4847
}
@@ -64,37 +63,44 @@ func (r *NBGroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
6463
if len(nbGroup.Finalizers) == 0 {
6564
return ctrl.Result{}, nil
6665
}
67-
return ctrl.Result{}, r.handleDelete(ctx, req, nbGroup)
66+
return ctrl.Result{}, r.handleDelete(ctx, nbGroup, logger)
6867
}
6968

69+
return r.syncNetBirdGroup(ctx, &nbGroup, logger)
70+
}
71+
72+
func (r *NBGroupReconciler) syncNetBirdGroup(ctx context.Context, nbGroup *netbirdiov1.NBGroup, logger logr.Logger) (ctrl.Result, error) {
7073
groups, err := r.netbird.Groups.List(ctx)
74+
if err != nil {
75+
logger.Error(errNetBirdAPI, "error listing groups", "err", err)
76+
return ctrl.Result{}, err
77+
}
7178
var group *api.Group
7279
for _, g := range groups {
7380
if g.Name == nbGroup.Spec.Name {
7481
group = &g
7582
}
7683
}
7784
if nbGroup.Status.GroupID == nil && group == nil {
78-
ctrl.Log.Info("NBGroup: Creating group on NetBird", "name", nbGroup.Spec.Name)
85+
logger.Info("NBGroup: Creating group on NetBird", "name", nbGroup.Spec.Name)
7986
group, err := r.netbird.Groups.Create(ctx, api.GroupRequest{
8087
Name: nbGroup.Spec.Name,
8188
})
82-
ctrl.Log.Info("NBGroup: Created group on NetBird", "name", nbGroup.Spec.Name, "id", group.Id)
83-
8489
if err != nil {
8590
nbGroup.Status.Conditions = netbirdiov1.NBConditionFalse("APIError", fmt.Sprintf("NetBird API Error: %v", err))
86-
ctrl.Log.Error(errNetBirdAPI, "error creating group", "err", err, "namespace", req.Namespace, "name", req.Name)
91+
logger.Error(errNetBirdAPI, "error creating group", "err", err)
8792
return ctrl.Result{}, err
8893
}
8994

95+
logger.Info("NBGroup: Created group on NetBird", "name", nbGroup.Spec.Name, "id", group.Id)
9096
nbGroup.Status.GroupID = &group.Id
9197
nbGroup.Status.Conditions = netbirdiov1.NBConditionTrue()
9298
} else if nbGroup.Status.GroupID == nil && group != nil {
93-
ctrl.Log.Info("NBGroup: Found group with same name on NetBird", "name", nbGroup.Spec.Name, "id", group.Id)
99+
logger.Info("NBGroup: Found group with same name on NetBird", "name", nbGroup.Spec.Name, "id", group.Id)
94100
nbGroup.Status.GroupID = &group.Id
95101
nbGroup.Status.Conditions = netbirdiov1.NBConditionTrue()
96102
} else if group == nil {
97-
ctrl.Log.Info("NBGroup: Group was deleted", "name", nbGroup.Spec.Name, "id", *nbGroup.Status.GroupID)
103+
logger.Info("NBGroup: Group was deleted", "name", nbGroup.Spec.Name, "id", *nbGroup.Status.GroupID)
98104
nbGroup.Status.GroupID = nil
99105
nbGroup.Status.Conditions = netbirdiov1.NBConditionFalse("GroupGone", "Group was deleted from NetBird API")
100106
return ctrl.Result{Requeue: true}, nil
@@ -108,16 +114,15 @@ func (r *NBGroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
108114
nbGroup.Status.GroupID = &group.Id
109115
nbGroup.Status.Conditions = netbirdiov1.NBConditionTrue()
110116
}
111-
112117
return ctrl.Result{}, nil
113118
}
114119

115-
func (r *NBGroupReconciler) handleDelete(ctx context.Context, req ctrl.Request, nbGroup netbirdiov1.NBGroup) error {
120+
func (r *NBGroupReconciler) handleDelete(ctx context.Context, nbGroup netbirdiov1.NBGroup, logger logr.Logger) error {
116121
if nbGroup.Status.GroupID == nil {
117122
nbGroup.Finalizers = util.Without(nbGroup.Finalizers, "netbird.io/group-cleanup")
118123
err := r.Client.Update(ctx, &nbGroup)
119124
if err != nil {
120-
ctrl.Log.Error(errKubernetesAPI, "error updating NBGroup", "err", err, "namespace", req.Namespace, "name", req.Name)
125+
logger.Error(errKubernetesAPI, "error updating NBGroup", "err", err)
121126
return err
122127
}
123128

@@ -126,17 +131,17 @@ func (r *NBGroupReconciler) handleDelete(ctx context.Context, req ctrl.Request,
126131

127132
err := r.netbird.Groups.Delete(ctx, *nbGroup.Status.GroupID)
128133
if err != nil && !strings.Contains(err.Error(), "not found") && !strings.Contains(err.Error(), "linked") {
129-
ctrl.Log.Error(errNetBirdAPI, "error deleting group", "err", err, "namespace", req.Namespace, "name", req.Name)
134+
logger.Error(errNetBirdAPI, "error deleting group", "err", err)
130135
return err
131136
}
132137

133138
if err != nil && strings.Contains(err.Error(), "linked") {
134-
ctrl.Log.Info("group still linked to resources on netbird", "err", err, "namespace", req.Namespace, "name", req.Name)
139+
logger.Info("group still linked to resources on netbird", "err", err)
135140
// Check if group is defined elsewhere in the cluster
136141
var groups netbirdiov1.NBGroupList
137142
listErr := r.Client.List(ctx, &groups)
138143
if listErr != nil {
139-
ctrl.Log.Error(errKubernetesAPI, "error listing NBGroups", "err", listErr)
144+
logger.Error(errKubernetesAPI, "error listing NBGroups", "err", listErr)
140145
return listErr
141146
}
142147
for _, v := range groups.Items {
@@ -145,11 +150,11 @@ func (r *NBGroupReconciler) handleDelete(ctx context.Context, req ctrl.Request,
145150
}
146151
if v.Status.GroupID != nil && nbGroup.Status.GroupID != nil && *v.Status.GroupID == *nbGroup.Status.GroupID {
147152
// Same group, multiple resources
148-
ctrl.Log.Info("group exists in another namespace", "namespace", v.Namespace, "name", v.Name)
153+
logger.Info("group exists in another namespace", "namespace", v.Namespace, "name", v.Name)
149154
nbGroup.Finalizers = util.Without(nbGroup.Finalizers, "netbird.io/group-cleanup")
150155
err = r.Client.Update(ctx, &nbGroup)
151156
if err != nil {
152-
ctrl.Log.Error(errKubernetesAPI, "error updating NBGroup", "err", err, "namespace", req.Namespace, "name", req.Name)
157+
logger.Error(errKubernetesAPI, "error updating NBGroup", "err", err)
153158
return err
154159
}
155160
return nil
@@ -161,7 +166,7 @@ func (r *NBGroupReconciler) handleDelete(ctx context.Context, req ctrl.Request,
161166
nbGroup.Finalizers = util.Without(nbGroup.Finalizers, "netbird.io/group-cleanup")
162167
err = r.Client.Update(ctx, &nbGroup)
163168
if err != nil {
164-
ctrl.Log.Error(errKubernetesAPI, "error updating NBGroup", "err", err, "namespace", req.Namespace, "name", req.Name)
169+
logger.Error(errKubernetesAPI, "error updating NBGroup", "err", err)
165170
return err
166171
}
167172

0 commit comments

Comments
 (0)