Implement group resource (#181)

This change implements a new group resource. 

It also sets the standard for a resource reference will be done through
out the controller. A resource reference can either be done by ID or as
a local named reference to the actual resource. This allows end users to
chose if they want to manage things completely in the cluster or not.

Part of #172

Signed-off-by: Philip Laine <philip.laine@gmail.com>
This commit is contained in:
Philip Laine
2026-04-15 10:16:10 +02:00
committed by GitHub
parent 26479a19c0
commit 9c4ca73712
19 changed files with 1026 additions and 11 deletions
+103
View File
@@ -0,0 +1,103 @@
package controller
import (
"context"
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"
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
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)
}
if !group.DeletionTimestamp.IsZero() {
return r.reconcileDelete(ctx, group)
}
groupAC := nbv1alpha1ac.Group(req.Name, req.Namespace).WithFinalizers(SetupKeyFinalizer)
err = r.Client.Apply(ctx, groupAC)
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)
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
}
groupAC = nbv1alpha1ac.Group(req.Name, req.Namespace).WithStatus(nbv1alpha1ac.GroupStatus().WithGroupID(groupID))
err = r.Client.Status().Apply(ctx, groupAC)
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)
if err != nil && !netbird.IsNotFound(err) {
return ctrl.Result{}, err
}
}
groupAC := nbv1alpha1ac.Group(group.Name, group.Namespace).WithFinalizers()
err := r.Client.Apply(ctx, groupAC)
if err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *GroupReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&nbv1alpha1.Group{}).
Complete(r)
}
@@ -0,0 +1,139 @@
package controller
import (
"context"
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"net/http/httptest"
netbird "github.com/netbirdio/netbird/shared/management/client/rest"
"github.com/netbirdio/netbird/shared/management/http/api"
"github.com/netbirdio/netbird/shared/management/http/util"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
nbv1alpha1 "github.com/netbirdio/kubernetes-operator/api/v1alpha1"
)
var _ = Describe("Group Controller", func() {
Context("When reconciling a resource", func() {
ctx := context.Background()
r := rand.New(rand.NewSource(GinkgoRandomSeed()))
groupStore := map[string]*api.Group{}
mux := &http.ServeMux{}
mux.Handle("POST /api/groups", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
b, err := io.ReadAll(req.Body)
Expect(err).NotTo(HaveOccurred())
groupReq := api.GroupRequest{}
err = json.Unmarshal(b, &groupReq)
Expect(err).NotTo(HaveOccurred())
groupResp := &api.Group{
Id: fmt.Sprintf("id-%d", r.Int63()),
Name: groupReq.Name,
}
groupStore[groupResp.Id] = groupResp
b, err = json.Marshal(groupResp)
Expect(err).NotTo(HaveOccurred())
_, err = rw.Write(b)
Expect(err).NotTo(HaveOccurred())
}))
mux.Handle("PUT /api/groups/{id}", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
id := req.PathValue("id")
groupResp, ok := groupStore[id]
if !ok {
util.WriteErrorResponse("Not Found", http.StatusNotFound, rw)
return
}
b, err := io.ReadAll(req.Body)
Expect(err).NotTo(HaveOccurred())
groupReq := api.GroupRequest{}
err = json.Unmarshal(b, &groupReq)
Expect(err).NotTo(HaveOccurred())
groupResp.Name = groupReq.Name
b, err = json.Marshal(groupResp)
Expect(err).NotTo(HaveOccurred())
_, err = rw.Write(b)
Expect(err).NotTo(HaveOccurred())
}))
mux.Handle("DELETE /api/groups/{id}", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
id := req.PathValue("id")
_, ok := groupStore[id]
if !ok {
util.WriteErrorResponse("Not Found", http.StatusNotFound, rw)
return
}
delete(groupStore, id)
}))
server := httptest.NewServer(mux)
nbClient := netbird.New(server.URL, "ABC")
var controllerReconciler *GroupReconciler
nn := client.ObjectKey{
Name: "test-resource",
Namespace: "default",
}
BeforeEach(func() {
controllerReconciler = &GroupReconciler{
Client: k8sClient,
Netbird: nbClient,
}
})
AfterEach(func() {
groupStore = map[string]*api.Group{}
group := &nbv1alpha1.Group{}
err := k8sClient.Get(ctx, nn, group)
if kerrors.IsNotFound(err) {
return
}
Expect(err).ToNot(HaveOccurred())
Expect(k8sClient.Delete(ctx, group)).To(Succeed())
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: nn})
Expect(err).ToNot(HaveOccurred())
})
It("ensures a Netbird group exists on reconcile", func() {
group := &nbv1alpha1.Group{
ObjectMeta: metav1.ObjectMeta{
Name: nn.Name,
Namespace: nn.Namespace,
},
Spec: nbv1alpha1.GroupSpec{
Name: "foobar",
},
}
Expect(k8sClient.Create(ctx, group)).To(Succeed())
By("creating a group on initial creation")
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: nn})
Expect(err).NotTo(HaveOccurred())
err = k8sClient.Get(ctx, nn, group)
Expect(err).NotTo(HaveOccurred())
Expect(*group.Status.GroupID).NotTo(BeEmpty())
By("crerating a new group when deleted from API")
delete(groupStore, *group.Status.GroupID)
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: nn})
Expect(err).NotTo(HaveOccurred())
newGroup := &nbv1alpha1.Group{}
err = k8sClient.Get(ctx, nn, newGroup)
Expect(err).NotTo(HaveOccurred())
Expect(*newGroup.Status.GroupID).NotTo(BeEmpty())
Expect(*newGroup.Status.GroupID).NotTo(Equal(*group.Status.GroupID))
})
})
})
+38 -10
View File
@@ -3,12 +3,14 @@ package controller
import (
"context"
"errors"
"fmt"
"time"
netbird "github.com/netbirdio/netbird/shared/management/client/rest"
"github.com/netbirdio/netbird/shared/management/http/api"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1ac "k8s.io/client-go/applyconfigurations/core/v1"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
@@ -44,6 +46,34 @@ func (r *SetupKeyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return r.reconcileDelete(ctx, setupKey)
}
// Get ids for auto groups.
autoGroupIDs := []string{}
for _, ref := range setupKey.Spec.AutoGroups {
switch {
case ref.ID != nil:
_, err := r.Netbird.Groups.Get(ctx, *ref.ID)
if err != nil {
return ctrl.Result{}, err
}
autoGroupIDs = append(autoGroupIDs, *ref.ID)
case ref.LocalRef != nil:
group := nbv1alpha1.Group{
ObjectMeta: metav1.ObjectMeta{
Name: ref.LocalRef.Name,
Namespace: setupKey.Namespace,
},
}
err = r.Client.Get(ctx, client.ObjectKeyFromObject(&group), &group)
if err != nil {
return ctrl.Result{}, err
}
if group.Status.GroupID == nil {
return ctrl.Result{}, fmt.Errorf("group %s in auto groups list is not ready", group.Name)
}
autoGroupIDs = append(autoGroupIDs, *group.Status.GroupID)
}
}
// Set finalizer on the setup key.
setupKeyAC := nbv1alpha1ac.SetupKey(req.Name, req.Namespace).WithFinalizers(SetupKeyFinalizer)
err = r.Client.Apply(ctx, setupKeyAC)
@@ -89,7 +119,7 @@ func (r *SetupKeyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
// Auto groups have not been changed.
setupKeyReq := api.PutApiSetupKeysKeyIdJSONRequestBody{
AutoGroups: []string{},
AutoGroups: autoGroupIDs,
}
_, err = r.Netbird.SetupKeys.Update(ctx, *setupKey.Status.SetupKeyID, setupKeyReq)
if err != nil {
@@ -113,7 +143,7 @@ func (r *SetupKeyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
}
setupKeyReq := api.PostApiSetupKeysJSONRequestBody{
AllowExtraDnsLabels: ptr.To(false),
AutoGroups: []string{},
AutoGroups: autoGroupIDs,
Ephemeral: ptr.To(setupKey.Spec.Ephemeral),
ExpiresIn: expiresIn,
Name: req.Name,
@@ -160,17 +190,15 @@ func (r *SetupKeyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
}
func (r *SetupKeyReconciler) reconcileDelete(ctx context.Context, setupKey nbv1alpha1.SetupKey) (ctrl.Result, error) {
if setupKey.Status.SetupKeyID == nil {
return ctrl.Result{}, nil
}
err := r.Netbird.SetupKeys.Delete(ctx, *setupKey.Status.SetupKeyID)
if err != nil && !netbird.IsNotFound(err) {
return ctrl.Result{}, err
if setupKey.Status.SetupKeyID != nil {
err := r.Netbird.SetupKeys.Delete(ctx, *setupKey.Status.SetupKeyID)
if err != nil && !netbird.IsNotFound(err) {
return ctrl.Result{}, err
}
}
setupKeyAC := nbv1alpha1ac.SetupKey(setupKey.Name, setupKey.Namespace).WithFinalizers()
err = r.Client.Apply(ctx, setupKeyAC)
err := r.Client.Apply(ctx, setupKeyAC)
if err != nil {
return ctrl.Result{}, err
}
@@ -27,7 +27,6 @@ var _ = Describe("SetupKey Controller", func() {
ctx := context.Background()
r := rand.New(rand.NewSource(GinkgoRandomSeed()))
setupKeyStore := map[string]*api.SetupKey{}
mux := &http.ServeMux{}
mux.HandleFunc("/api/setup-keys", func(rw http.ResponseWriter, req *http.Request) {