mirror of
https://github.com/netbirdio/kubernetes-operator.git
synced 2026-05-22 17:11:40 -07:00
Share Netbird client between all reconcilers (#122)
This changes the reconcilers to take a netbird client rather than creating their own on setup. Signed-off-by: Philip Laine <philip.laine@gmail.com>
This commit is contained in:
+10
-11
@@ -28,6 +28,8 @@ import (
|
||||
// to ensure that exec-entrypoint and run can make use of them.
|
||||
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
||||
|
||||
netbirdrest "github.com/netbirdio/netbird/shared/management/client/rest"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
|
||||
@@ -38,8 +40,6 @@ import (
|
||||
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
|
||||
"sigs.k8s.io/controller-runtime/pkg/webhook"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
|
||||
netbirdiov1 "github.com/netbirdio/kubernetes-operator/api/v1"
|
||||
"github.com/netbirdio/kubernetes-operator/internal/controller"
|
||||
webhooknetbirdiov1 "github.com/netbirdio/kubernetes-operator/internal/webhook/v1"
|
||||
@@ -225,11 +225,13 @@ func main() {
|
||||
}
|
||||
|
||||
if len(netbirdAPIKey) > 0 {
|
||||
netbird := netbirdrest.New(managementURL, netbirdAPIKey)
|
||||
|
||||
if err = (&controller.NBRoutingPeerReconciler{
|
||||
Client: mgr.GetClient(),
|
||||
Netbird: netbird,
|
||||
ClientImage: clientImage,
|
||||
ClusterName: clusterName,
|
||||
APIKey: netbirdAPIKey,
|
||||
ManagementURL: managementURL,
|
||||
NamespacedNetworks: namespacedNetworks,
|
||||
DefaultLabels: defaultLabelsMap,
|
||||
@@ -258,8 +260,7 @@ func main() {
|
||||
|
||||
if err = (&controller.NBResourceReconciler{
|
||||
Client: mgr.GetClient(),
|
||||
APIKey: netbirdAPIKey,
|
||||
ManagementURL: managementURL,
|
||||
Netbird: netbird,
|
||||
AllowAutomaticPolicyCreation: allowAutomaticPolicyCreation,
|
||||
ClusterName: clusterName,
|
||||
DefaultLabels: defaultLabelsMap,
|
||||
@@ -269,18 +270,16 @@ func main() {
|
||||
}
|
||||
|
||||
if err = (&controller.NBGroupReconciler{
|
||||
Client: mgr.GetClient(),
|
||||
APIKey: netbirdAPIKey,
|
||||
ManagementURL: managementURL,
|
||||
Client: mgr.GetClient(),
|
||||
Netbird: netbird,
|
||||
}).SetupWithManager(mgr); err != nil {
|
||||
setupLog.Error(err, "unable to create controller", "controller", "NBGroup")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err = (&controller.NBPolicyReconciler{
|
||||
Client: mgr.GetClient(),
|
||||
APIKey: netbirdAPIKey,
|
||||
ManagementURL: managementURL,
|
||||
Client: mgr.GetClient(),
|
||||
Netbird: netbird,
|
||||
}).SetupWithManager(mgr); err != nil {
|
||||
setupLog.Error(err, "unable to create controller", "controller", "NBPolicy")
|
||||
os.Exit(1)
|
||||
|
||||
@@ -21,9 +21,7 @@ import (
|
||||
type NBGroupReconciler struct {
|
||||
client.Client
|
||||
|
||||
APIKey string
|
||||
ManagementURL string
|
||||
netbird *netbird.Client
|
||||
Netbird *netbird.Client
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -82,7 +80,7 @@ func (r *NBGroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
|
||||
// syncNetBirdGroup reconciliation logic for non-deleted objects.
|
||||
func (r *NBGroupReconciler) syncNetBirdGroup(ctx context.Context, nbGroup *netbirdiov1.NBGroup, logger logr.Logger) (ctrl.Result, error) {
|
||||
// Get all NetBird groups to ensure no group duplication
|
||||
groups, err := r.netbird.Groups.List(ctx)
|
||||
groups, err := r.Netbird.Groups.List(ctx)
|
||||
if err != nil {
|
||||
logger.Error(errNetBirdAPI, "error listing groups", "err", err)
|
||||
return ctrl.Result{}, err
|
||||
@@ -97,7 +95,7 @@ func (r *NBGroupReconciler) syncNetBirdGroup(ctx context.Context, nbGroup *netbi
|
||||
// Create group if not exists, and update status.groupId
|
||||
if nbGroup.Status.GroupID == nil && group == nil {
|
||||
logger.Info("NBGroup: Creating group on NetBird", "name", nbGroup.Spec.Name)
|
||||
group, err := r.netbird.Groups.Create(ctx, api.GroupRequest{
|
||||
group, err := r.Netbird.Groups.Create(ctx, api.GroupRequest{
|
||||
Name: nbGroup.Spec.Name,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -144,7 +142,7 @@ func (r *NBGroupReconciler) handleDelete(ctx context.Context, nbGroup netbirdiov
|
||||
return nil
|
||||
}
|
||||
|
||||
err := r.netbird.Groups.Delete(ctx, *nbGroup.Status.GroupID)
|
||||
err := r.Netbird.Groups.Delete(ctx, *nbGroup.Status.GroupID)
|
||||
if err != nil && !strings.Contains(err.Error(), "not found") && !strings.Contains(err.Error(), "linked") {
|
||||
logger.Error(errNetBirdAPI, "error deleting group", "err", err)
|
||||
return err
|
||||
@@ -193,8 +191,6 @@ func (r *NBGroupReconciler) handleDelete(ctx context.Context, nbGroup netbirdiov
|
||||
|
||||
// SetupWithManager sets up the controller with the Manager.
|
||||
func (r *NBGroupReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
r.netbird = netbird.New(r.ManagementURL, r.APIKey)
|
||||
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
For(&netbirdiov1.NBGroup{}).
|
||||
Named("nbgroup").
|
||||
|
||||
@@ -85,7 +85,7 @@ var _ = Describe("NBGroup Controller", func() {
|
||||
By("Reconciling the created resource")
|
||||
controllerReconciler := &NBGroupReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
}
|
||||
|
||||
mux.HandleFunc("/api/groups", func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -123,7 +123,7 @@ var _ = Describe("NBGroup Controller", func() {
|
||||
By("Reconciling the created resource")
|
||||
controllerReconciler := &NBGroupReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
}
|
||||
|
||||
mux.HandleFunc("/api/groups", func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -172,7 +172,7 @@ var _ = Describe("NBGroup Controller", func() {
|
||||
By("Reconciling the deleting resource")
|
||||
controllerReconciler := &NBGroupReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
}
|
||||
|
||||
method := ""
|
||||
@@ -198,7 +198,7 @@ var _ = Describe("NBGroup Controller", func() {
|
||||
By("Reconciling the deleting resource")
|
||||
controllerReconciler := &NBGroupReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
}
|
||||
|
||||
method := ""
|
||||
@@ -239,7 +239,7 @@ var _ = Describe("NBGroup Controller", func() {
|
||||
By("Reconciling the deleting resource")
|
||||
controllerReconciler := &NBGroupReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
}
|
||||
|
||||
method := ""
|
||||
@@ -266,7 +266,7 @@ var _ = Describe("NBGroup Controller", func() {
|
||||
It("should re-use existing group ID", func() {
|
||||
controllerReconciler := &NBGroupReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
}
|
||||
|
||||
mux.HandleFunc("/api/groups", func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -303,7 +303,7 @@ var _ = Describe("NBGroup Controller", func() {
|
||||
It("Should requeue and create group on next run", func() {
|
||||
controllerReconciler := &NBGroupReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
}
|
||||
|
||||
mux.HandleFunc("/api/groups", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -23,10 +23,8 @@ import (
|
||||
type NBPolicyReconciler struct {
|
||||
client.Client
|
||||
|
||||
ClusterName string
|
||||
APIKey string
|
||||
ManagementURL string
|
||||
netbird *netbird.Client
|
||||
Netbird *netbird.Client
|
||||
ClusterName string
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -119,7 +117,7 @@ func (r *NBPolicyReconciler) mapResources(ctx context.Context, nbPolicy *netbird
|
||||
func (r *NBPolicyReconciler) createPolicy(ctx context.Context, nbPolicy *netbirdiov1.NBPolicy, protocol string, sourceGroupIDs, destinationGroupIDs, ports []string, logger logr.Logger) (*string, error) {
|
||||
policyName := fmt.Sprintf("%s %s", nbPolicy.Spec.Name, strings.ToUpper(protocol))
|
||||
logger.Info("Creating NetBird Policy", "name", policyName, "description", nbPolicy.Spec.Description, "protocol", protocol, "sources", sourceGroupIDs, "destinations", destinationGroupIDs, "ports", ports, "bidirectional", nbPolicy.Spec.Bidirectional)
|
||||
policy, err := r.netbird.Policies.Create(ctx, api.PostApiPoliciesJSONRequestBody{
|
||||
policy, err := r.Netbird.Policies.Create(ctx, api.PostApiPoliciesJSONRequestBody{
|
||||
Enabled: true,
|
||||
Name: policyName,
|
||||
Description: &nbPolicy.Spec.Description,
|
||||
@@ -151,7 +149,7 @@ func (r *NBPolicyReconciler) createPolicy(ctx context.Context, nbPolicy *netbird
|
||||
func (r *NBPolicyReconciler) updatePolicy(ctx context.Context, policyID *string, nbPolicy *netbirdiov1.NBPolicy, protocol string, sourceGroupIDs, destinationGroupIDs, ports []string, logger logr.Logger) (*string, bool, error) {
|
||||
policyName := fmt.Sprintf("%s %s", nbPolicy.Spec.Name, strings.ToUpper(protocol))
|
||||
logger.Info("Updating NetBird Policy", "name", policyName, "description", nbPolicy.Spec.Description, "protocol", protocol, "sources", sourceGroupIDs, "destinations", destinationGroupIDs, "ports", ports, "bidirectional", nbPolicy.Spec.Bidirectional)
|
||||
_, err := r.netbird.Policies.Update(ctx, *policyID, api.PutApiPoliciesPolicyIdJSONRequestBody{
|
||||
_, err := r.Netbird.Policies.Update(ctx, *policyID, api.PutApiPoliciesPolicyIdJSONRequestBody{
|
||||
Enabled: true,
|
||||
Name: policyName,
|
||||
Description: &nbPolicy.Spec.Description,
|
||||
@@ -285,7 +283,7 @@ func (r *NBPolicyReconciler) syncPolicy(ctx context.Context, nbPolicy *netbirdio
|
||||
if len(nbPolicy.Spec.Protocols) > 0 && !slices.Contains(nbPolicy.Spec.Protocols, protocol) {
|
||||
if policyID != nil {
|
||||
logger.Info("Deleting protocol policy as NBPolicy has restricted protocols", "protocol", protocol)
|
||||
err := r.netbird.Policies.Delete(ctx, *policyID)
|
||||
err := r.Netbird.Policies.Delete(ctx, *policyID)
|
||||
if err != nil && !strings.Contains(err.Error(), "not found") {
|
||||
nbPolicy.Status.Conditions = netbirdiov1.NBConditionFalse("APIError", fmt.Sprintf("Error deleting policy: %v", err))
|
||||
return requeue, err
|
||||
@@ -307,7 +305,7 @@ func (r *NBPolicyReconciler) syncPolicy(ctx context.Context, nbPolicy *netbirdio
|
||||
} else if len(ports) == 0 || len(destGroups) == 0 || len(sourceGroups) == 0 {
|
||||
// Delete policy
|
||||
logger.Info("Deleting policy", "protocol", protocol)
|
||||
err := r.netbird.Policies.Delete(ctx, *policyID)
|
||||
err := r.Netbird.Policies.Delete(ctx, *policyID)
|
||||
if err != nil && !strings.Contains(err.Error(), "not found") {
|
||||
nbPolicy.Status.Conditions = netbirdiov1.NBConditionFalse("APIError", fmt.Sprintf("Error deleting policy: %v", err))
|
||||
return requeue, err
|
||||
@@ -350,14 +348,14 @@ func (r *NBPolicyReconciler) syncPolicy(ctx context.Context, nbPolicy *netbirdio
|
||||
|
||||
func (r *NBPolicyReconciler) handleDelete(ctx context.Context, nbPolicy *netbirdiov1.NBPolicy, logger logr.Logger) error {
|
||||
if nbPolicy.Status.TCPPolicyID != nil {
|
||||
err := r.netbird.Policies.Delete(ctx, *nbPolicy.Status.TCPPolicyID)
|
||||
err := r.Netbird.Policies.Delete(ctx, *nbPolicy.Status.TCPPolicyID)
|
||||
if err != nil && !strings.Contains("not found", err.Error()) {
|
||||
return err
|
||||
}
|
||||
nbPolicy.Status.TCPPolicyID = nil
|
||||
}
|
||||
if nbPolicy.Status.UDPPolicyID != nil {
|
||||
err := r.netbird.Policies.Delete(ctx, *nbPolicy.Status.UDPPolicyID)
|
||||
err := r.Netbird.Policies.Delete(ctx, *nbPolicy.Status.UDPPolicyID)
|
||||
if err != nil && !strings.Contains("not found", err.Error()) {
|
||||
return err
|
||||
}
|
||||
@@ -376,7 +374,7 @@ func (r *NBPolicyReconciler) handleDelete(ctx context.Context, nbPolicy *netbird
|
||||
|
||||
// groupNamesToIDs map list of NetBird group names to group IDs
|
||||
func (r *NBPolicyReconciler) groupNamesToIDs(ctx context.Context, groupNames []string, logger logr.Logger) ([]string, error) {
|
||||
groups, err := r.netbird.Groups.List(ctx)
|
||||
groups, err := r.Netbird.Groups.List(ctx)
|
||||
if err != nil {
|
||||
logger.Error(errNetBirdAPI, "Error listing Groups", "err", err)
|
||||
return nil, err
|
||||
@@ -397,8 +395,6 @@ func (r *NBPolicyReconciler) groupNamesToIDs(ctx context.Context, groupNames []s
|
||||
|
||||
// SetupWithManager sets up the controller with the Manager.
|
||||
func (r *NBPolicyReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
r.netbird = netbird.New(r.ManagementURL, r.APIKey)
|
||||
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
For(&netbirdiov1.NBPolicy{}).
|
||||
Named("nbpolicy").
|
||||
|
||||
@@ -90,7 +90,7 @@ var _ = Describe("NBPolicy Controller", func() {
|
||||
It("should not create any policy", func() {
|
||||
controllerReconciler := &NBPolicyReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "Kubernetes",
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ var _ = Describe("NBPolicy Controller", func() {
|
||||
It("should create 1 policy", func() {
|
||||
controllerReconciler := &NBPolicyReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "Kubernetes",
|
||||
}
|
||||
|
||||
@@ -216,7 +216,7 @@ var _ = Describe("NBPolicy Controller", func() {
|
||||
It("should delete tcp policy", func() {
|
||||
controllerReconciler := &NBPolicyReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "Kubernetes",
|
||||
}
|
||||
|
||||
@@ -258,7 +258,7 @@ var _ = Describe("NBPolicy Controller", func() {
|
||||
It("should create 1 policy", func() {
|
||||
controllerReconciler := &NBPolicyReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "Kubernetes",
|
||||
}
|
||||
|
||||
@@ -356,7 +356,7 @@ var _ = Describe("NBPolicy Controller", func() {
|
||||
It("should delete udp policy", func() {
|
||||
controllerReconciler := &NBPolicyReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "Kubernetes",
|
||||
}
|
||||
|
||||
@@ -398,7 +398,7 @@ var _ = Describe("NBPolicy Controller", func() {
|
||||
It("Should delete protocol policy", func() {
|
||||
controllerReconciler := &NBPolicyReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "Kubernetes",
|
||||
}
|
||||
|
||||
@@ -478,7 +478,7 @@ var _ = Describe("NBPolicy Controller", func() {
|
||||
It("Should give all information to Update method", func() {
|
||||
controllerReconciler := &NBPolicyReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "Kubernetes",
|
||||
}
|
||||
|
||||
@@ -596,7 +596,7 @@ var _ = Describe("NBPolicy Controller", func() {
|
||||
It("should delete Policies", func() {
|
||||
controllerReconciler := &NBPolicyReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "Kubernetes",
|
||||
}
|
||||
|
||||
|
||||
@@ -33,12 +33,10 @@ var (
|
||||
type NBResourceReconciler struct {
|
||||
client.Client
|
||||
|
||||
APIKey string
|
||||
ManagementURL string
|
||||
Netbird *netbird.Client
|
||||
AllowAutomaticPolicyCreation bool
|
||||
ClusterName string
|
||||
DefaultLabels map[string]string
|
||||
netbird *netbird.Client
|
||||
}
|
||||
|
||||
// Reconcile is part of the main kubernetes reconciliation loop which aims to
|
||||
@@ -377,7 +375,7 @@ func (r *NBResourceReconciler) handleGroupUpdate(ctx context.Context, nbResource
|
||||
}
|
||||
|
||||
if diffFound {
|
||||
_, err := r.netbird.Networks.Resources(nbResource.Spec.NetworkID).Update(ctx, resource.Id, api.NetworkResourceRequest{
|
||||
_, err := r.Netbird.Networks.Resources(nbResource.Spec.NetworkID).Update(ctx, resource.Id, api.NetworkResourceRequest{
|
||||
Name: nbResource.Spec.Name,
|
||||
Description: &networkDescription,
|
||||
Address: nbResource.Spec.Address,
|
||||
@@ -399,7 +397,7 @@ func (r *NBResourceReconciler) handleNetBirdResource(ctx context.Context, nbReso
|
||||
var resource *api.NetworkResource
|
||||
var err error
|
||||
if nbResource.Status.NetworkResourceID != nil {
|
||||
resource, err = r.netbird.Networks.Resources(nbResource.Spec.NetworkID).Get(ctx, *nbResource.Status.NetworkResourceID)
|
||||
resource, err = r.Netbird.Networks.Resources(nbResource.Spec.NetworkID).Get(ctx, *nbResource.Status.NetworkResourceID)
|
||||
if err != nil && !strings.Contains(err.Error(), "not found") {
|
||||
logger.Error(errNetBirdAPI, "error getting network resource", "err", err)
|
||||
return nil, err
|
||||
@@ -408,7 +406,7 @@ func (r *NBResourceReconciler) handleNetBirdResource(ctx context.Context, nbReso
|
||||
|
||||
// Create/Update upstream network resource
|
||||
if nbResource.Status.NetworkResourceID == nil && resource == nil {
|
||||
resource, err := r.netbird.Networks.Resources(nbResource.Spec.NetworkID).Create(ctx, api.NetworkResourceRequest{
|
||||
resource, err := r.Netbird.Networks.Resources(nbResource.Spec.NetworkID).Create(ctx, api.NetworkResourceRequest{
|
||||
Address: nbResource.Spec.Address,
|
||||
Enabled: true,
|
||||
Groups: groupIDs,
|
||||
@@ -442,7 +440,7 @@ func (r *NBResourceReconciler) handleNetBirdResource(ctx context.Context, nbReso
|
||||
!util.Equivalent(resourceGroups, groupIDs) ||
|
||||
*resource.Description != networkDescription ||
|
||||
resource.Name != nbResource.Spec.Name {
|
||||
_, err = r.netbird.Networks.Resources(nbResource.Spec.NetworkID).Update(ctx, *nbResource.Status.NetworkResourceID, api.NetworkResourceRequest{
|
||||
_, err = r.Netbird.Networks.Resources(nbResource.Spec.NetworkID).Update(ctx, *nbResource.Status.NetworkResourceID, api.NetworkResourceRequest{
|
||||
Address: nbResource.Spec.Address,
|
||||
Enabled: true,
|
||||
Groups: groupIDs,
|
||||
@@ -615,7 +613,7 @@ func (r *NBResourceReconciler) handleDelete(ctx context.Context, req ctrl.Reques
|
||||
}
|
||||
|
||||
if nbResource.Status.NetworkResourceID != nil {
|
||||
err := r.netbird.Networks.Resources(nbResource.Spec.NetworkID).Delete(ctx, *nbResource.Status.NetworkResourceID)
|
||||
err := r.Netbird.Networks.Resources(nbResource.Spec.NetworkID).Delete(ctx, *nbResource.Status.NetworkResourceID)
|
||||
if err != nil && !strings.Contains(err.Error(), "not found") {
|
||||
logger.Error(errNetBirdAPI, "error deleting resource", "err", err)
|
||||
return err
|
||||
@@ -671,8 +669,6 @@ func (r *NBResourceReconciler) handleDelete(ctx context.Context, req ctrl.Reques
|
||||
|
||||
// SetupWithManager sets up the controller with the Manager.
|
||||
func (r *NBResourceReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
r.netbird = netbird.New(r.ManagementURL, r.APIKey)
|
||||
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
For(&netbirdiov1.NBResource{}).
|
||||
Named("nbresource").
|
||||
|
||||
@@ -47,7 +47,7 @@ var _ = Describe("NBResource Controller", func() {
|
||||
netbirdClient = netbird.New(server.URL, "ABC")
|
||||
controllerReconciler = &NBResourceReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "kubernetes",
|
||||
DefaultLabels: map[string]string{"dog": "bark"},
|
||||
}
|
||||
|
||||
@@ -27,13 +27,12 @@ import (
|
||||
type NBRoutingPeerReconciler struct {
|
||||
client.Client
|
||||
|
||||
Netbird *netbird.Client
|
||||
ClientImage string
|
||||
ClusterName string
|
||||
APIKey string
|
||||
ManagementURL string
|
||||
NamespacedNetworks bool
|
||||
DefaultLabels map[string]string
|
||||
netbird *netbird.Client
|
||||
}
|
||||
|
||||
// Reconcile is part of the main kubernetes reconciliation loop which aims to
|
||||
@@ -281,7 +280,7 @@ func (r *NBRoutingPeerReconciler) handleDeployment(ctx context.Context, req ctrl
|
||||
// handleRouter reconcile network routing peer in NetBird management API
|
||||
func (r *NBRoutingPeerReconciler) handleRouter(ctx context.Context, nbrp *netbirdiov1.NBRoutingPeer, nbGroup netbirdiov1.NBGroup, logger logr.Logger) error {
|
||||
// Check NetworkRouter exists
|
||||
routers, err := r.netbird.Networks.Routers(*nbrp.Status.NetworkID).List(ctx)
|
||||
routers, err := r.Netbird.Networks.Routers(*nbrp.Status.NetworkID).List(ctx)
|
||||
|
||||
if err != nil {
|
||||
logger.Error(errNetBirdAPI, "error listing network routers", "err", err)
|
||||
@@ -295,7 +294,7 @@ func (r *NBRoutingPeerReconciler) handleRouter(ctx context.Context, nbrp *netbir
|
||||
nbrp.Status.RouterID = &routers[0].Id
|
||||
} else {
|
||||
// Create network router
|
||||
router, err := r.netbird.Networks.Routers(*nbrp.Status.NetworkID).Create(ctx, api.NetworkRouterRequest{
|
||||
router, err := r.Netbird.Networks.Routers(*nbrp.Status.NetworkID).Create(ctx, api.NetworkRouterRequest{
|
||||
Enabled: true,
|
||||
Masquerade: true,
|
||||
Metric: 9999,
|
||||
@@ -313,7 +312,7 @@ func (r *NBRoutingPeerReconciler) handleRouter(ctx context.Context, nbrp *netbir
|
||||
} else {
|
||||
// Ensure network router settings are correct
|
||||
if !routers[0].Enabled || !routers[0].Masquerade || routers[0].Metric != 9999 || len(*routers[0].PeerGroups) != 1 || (*routers[0].PeerGroups)[0] != *nbGroup.Status.GroupID {
|
||||
_, err = r.netbird.Networks.Routers(*nbrp.Status.NetworkID).Update(ctx, routers[0].Id, api.NetworkRouterRequest{
|
||||
_, err = r.Netbird.Networks.Routers(*nbrp.Status.NetworkID).Update(ctx, routers[0].Id, api.NetworkRouterRequest{
|
||||
Enabled: true,
|
||||
Masquerade: true,
|
||||
Metric: 9999,
|
||||
@@ -341,7 +340,7 @@ func (r *NBRoutingPeerReconciler) handleSetupKey(ctx context.Context, req ctrl.R
|
||||
// Check if setup key exists
|
||||
if nbrp.Status.SetupKeyID == nil {
|
||||
// Create new setup key with group Status.GroupID
|
||||
setupKey, err := r.netbird.SetupKeys.Create(ctx, api.CreateSetupKeyRequest{
|
||||
setupKey, err := r.Netbird.SetupKeys.Create(ctx, api.CreateSetupKeyRequest{
|
||||
AutoGroups: []string{*nbGroup.Status.GroupID},
|
||||
Ephemeral: util.Ptr(true),
|
||||
Name: networkName,
|
||||
@@ -395,7 +394,7 @@ func (r *NBRoutingPeerReconciler) handleSetupKey(ctx context.Context, req ctrl.R
|
||||
}
|
||||
} else {
|
||||
// Check SetupKey is not revoked
|
||||
setupKey, err := r.netbird.SetupKeys.Get(ctx, *nbrp.Status.SetupKeyID)
|
||||
setupKey, err := r.Netbird.SetupKeys.Get(ctx, *nbrp.Status.SetupKeyID)
|
||||
if err != nil && !strings.Contains(err.Error(), "not found") {
|
||||
logger.Error(errNetBirdAPI, "error getting setup key", "err", err)
|
||||
nbrp.Status.Conditions = netbirdiov1.NBConditionFalse("APIError", fmt.Sprintf("error getting setup key: %v", err))
|
||||
@@ -404,7 +403,7 @@ func (r *NBRoutingPeerReconciler) handleSetupKey(ctx context.Context, req ctrl.R
|
||||
|
||||
if (err != nil && strings.Contains(err.Error(), "not found")) || setupKey == nil || setupKey.Revoked {
|
||||
if setupKey != nil && setupKey.Revoked {
|
||||
err = r.netbird.SetupKeys.Delete(ctx, *nbrp.Status.SetupKeyID)
|
||||
err = r.Netbird.SetupKeys.Delete(ctx, *nbrp.Status.SetupKeyID)
|
||||
|
||||
if err != nil {
|
||||
logger.Error(errNetBirdAPI, "error deleting setup key", "err", err)
|
||||
@@ -430,7 +429,7 @@ func (r *NBRoutingPeerReconciler) handleSetupKey(ctx context.Context, req ctrl.R
|
||||
if _, ok := skSecret.Data["setupKey"]; errors.IsNotFound(err) || !ok {
|
||||
// Someone deleted setup key secret
|
||||
// Revoke SK from NetBird and re-generate
|
||||
err = r.netbird.SetupKeys.Delete(ctx, *nbrp.Status.SetupKeyID)
|
||||
err = r.Netbird.SetupKeys.Delete(ctx, *nbrp.Status.SetupKeyID)
|
||||
|
||||
if err != nil {
|
||||
logger.Error(errNetBirdAPI, "error deleting setup key", "err", err)
|
||||
@@ -516,7 +515,7 @@ func (r *NBRoutingPeerReconciler) handleNetwork(ctx context.Context, req ctrl.Re
|
||||
|
||||
if nbrp.Status.NetworkID == nil {
|
||||
// Check if network exists
|
||||
networks, err := r.netbird.Networks.List(ctx)
|
||||
networks, err := r.Netbird.Networks.List(ctx)
|
||||
if err != nil {
|
||||
logger.Error(errNetBirdAPI, "error listing networks", "err", err)
|
||||
nbrp.Status.Conditions = netbirdiov1.NBConditionFalse("APIError", fmt.Sprintf("error listing networks: %v", err))
|
||||
@@ -534,7 +533,7 @@ func (r *NBRoutingPeerReconciler) handleNetwork(ctx context.Context, req ctrl.Re
|
||||
nbrp.Status.NetworkID = &network.Id
|
||||
} else {
|
||||
logger.Info("creating network", "name", networkName)
|
||||
network, err := r.netbird.Networks.Create(ctx, api.NetworkRequest{
|
||||
network, err := r.Netbird.Networks.Create(ctx, api.NetworkRequest{
|
||||
Name: networkName,
|
||||
Description: &networkDescription,
|
||||
})
|
||||
@@ -567,7 +566,7 @@ func (r *NBRoutingPeerReconciler) handleDelete(ctx context.Context, req ctrl.Req
|
||||
|
||||
if nbrp.Status.SetupKeyID != nil {
|
||||
logger.Info("Deleting setup key", "id", *nbrp.Status.SetupKeyID)
|
||||
err = r.netbird.SetupKeys.Delete(ctx, *nbrp.Status.SetupKeyID)
|
||||
err = r.Netbird.SetupKeys.Delete(ctx, *nbrp.Status.SetupKeyID)
|
||||
if err != nil && !strings.Contains(err.Error(), "not found") {
|
||||
logger.Error(errNetBirdAPI, "error deleting setupKey", "err", err)
|
||||
return ctrl.Result{}, err
|
||||
@@ -606,7 +605,7 @@ func (r *NBRoutingPeerReconciler) handleDelete(ctx context.Context, req ctrl.Req
|
||||
|
||||
if len(nbResourceList.Items) == 0 {
|
||||
logger.Info("Deleting NetBird Network", "id", *nbrp.Status.NetworkID)
|
||||
err = r.netbird.Networks.Delete(ctx, *nbrp.Status.NetworkID)
|
||||
err = r.Netbird.Networks.Delete(ctx, *nbrp.Status.NetworkID)
|
||||
if err != nil && !strings.Contains(err.Error(), "not found") {
|
||||
logger.Error(errNetBirdAPI, "error deleting Network", "err", err)
|
||||
return ctrl.Result{}, err
|
||||
@@ -664,8 +663,6 @@ func (r *NBRoutingPeerReconciler) buildSecurityContext(nbrp *netbirdiov1.NBRouti
|
||||
|
||||
// SetupWithManager sets up the controller with the Manager.
|
||||
func (r *NBRoutingPeerReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
r.netbird = netbird.New(r.ManagementURL, r.APIKey)
|
||||
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
For(&netbirdiov1.NBRoutingPeer{}).
|
||||
Named("nbroutingpeer").
|
||||
|
||||
@@ -48,7 +48,7 @@ var _ = Describe("NBRoutingPeer Controller", func() {
|
||||
netbirdClient = netbird.New(server.URL, "ABC")
|
||||
controllerReconciler = &NBRoutingPeerReconciler{
|
||||
Client: k8sClient,
|
||||
netbird: netbirdClient,
|
||||
Netbird: netbirdClient,
|
||||
ClientImage: "netbirdio/netbird:latest",
|
||||
ClusterName: "kubernetes",
|
||||
DefaultLabels: make(map[string]string),
|
||||
|
||||
Reference in New Issue
Block a user