mirror of
https://github.com/YuzuZensai/netbird-kubernetes-operator.git
synced 2026-09-13 10:49:15 +00:00
Use Gateway listener to reference routing peer (#150)
This just changes how a routing peer is referenced. In my head this model makes more sense, especially as we need at least one listener right now. Signed-off-by: Philip Laine <philip.laine@gmail.com>
This commit is contained in:
@@ -13,14 +13,9 @@ metadata:
|
|||||||
spec:
|
spec:
|
||||||
gatewayClassName: public
|
gatewayClassName: public
|
||||||
listeners:
|
listeners:
|
||||||
- protocol: HTTP
|
- protocol: gateway.netbird.io/NBRoutingPeer
|
||||||
port: 80
|
|
||||||
name: dummy
|
|
||||||
infrastructure:
|
|
||||||
parametersRef:
|
|
||||||
group: netbird.io
|
|
||||||
kind: NBRoutingPeer
|
|
||||||
name: netbird
|
name: netbird
|
||||||
|
port: 80
|
||||||
---
|
---
|
||||||
apiVersion: netbird.io/v1
|
apiVersion: netbird.io/v1
|
||||||
kind: NBRoutingPeer
|
kind: NBRoutingPeer
|
||||||
|
|||||||
@@ -18,8 +18,10 @@ package controller
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"slices"
|
"slices"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
netbird "github.com/netbirdio/netbird/shared/management/client/rest"
|
netbird "github.com/netbirdio/netbird/shared/management/client/rest"
|
||||||
@@ -72,36 +74,19 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Verify Gateway configuration.
|
// Verify Gateway configuration.
|
||||||
if gw.Spec.Infrastructure == nil || gw.Spec.Infrastructure.ParametersRef == nil {
|
routingPeerName, err := getRoutingPeerName(gw.Spec.Listeners)
|
||||||
|
if err != nil {
|
||||||
cond := metav1.Condition{
|
cond := metav1.Condition{
|
||||||
Type: string(gatewayv1.GatewayConditionAccepted),
|
Type: string(gatewayv1.GatewayConditionAccepted),
|
||||||
Status: metav1.ConditionFalse,
|
Status: metav1.ConditionFalse,
|
||||||
Reason: string(gatewayv1.GatewayReasonInvalidParameters),
|
Reason: string(gatewayv1.GatewayReasonInvalidParameters),
|
||||||
Message: "Gateway expected to reference a NBRoutingPeer",
|
Message: err.Error(),
|
||||||
}
|
}
|
||||||
if meta.SetStatusCondition(&gw.Status.Conditions, cond) {
|
if meta.SetStatusCondition(&gw.Status.Conditions, cond) {
|
||||||
err = r.Status().Update(ctx, &gw)
|
err = r.Status().Update(ctx, &gw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ctrl.Result{}, err
|
return ctrl.Result{}, err
|
||||||
}
|
}
|
||||||
return ctrl.Result{}, nil
|
|
||||||
}
|
|
||||||
return ctrl.Result{}, nil
|
|
||||||
}
|
|
||||||
parametersRef := gw.Spec.Infrastructure.ParametersRef
|
|
||||||
if parametersRef.Group != "netbird.io" && parametersRef.Kind != "NBRoutingPeer" {
|
|
||||||
cond := metav1.Condition{
|
|
||||||
Type: string(gatewayv1.GatewayConditionAccepted),
|
|
||||||
Status: metav1.ConditionFalse,
|
|
||||||
Reason: string(gatewayv1.GatewayReasonInvalidParameters),
|
|
||||||
Message: fmt.Sprintf("unsupported parameter group and kind %s.%s", parametersRef.Group, parametersRef.Kind),
|
|
||||||
}
|
|
||||||
if meta.SetStatusCondition(&gw.Status.Conditions, cond) {
|
|
||||||
err = r.Status().Update(ctx, &gw)
|
|
||||||
if err != nil {
|
|
||||||
return ctrl.Result{}, err
|
|
||||||
}
|
|
||||||
return ctrl.Result{}, nil
|
|
||||||
}
|
}
|
||||||
return ctrl.Result{}, nil
|
return ctrl.Result{}, nil
|
||||||
}
|
}
|
||||||
@@ -127,7 +112,7 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
|
|||||||
|
|
||||||
// Ensure routing peer is ready.
|
// Ensure routing peer is ready.
|
||||||
nbrp := &netbirdiov1.NBRoutingPeer{}
|
nbrp := &netbirdiov1.NBRoutingPeer{}
|
||||||
err = r.Get(ctx, types.NamespacedName{Namespace: req.Namespace, Name: parametersRef.Name}, nbrp)
|
err = r.Get(ctx, types.NamespacedName{Namespace: req.Namespace, Name: routingPeerName}, nbrp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ctrl.Result{}, err
|
return ctrl.Result{}, err
|
||||||
}
|
}
|
||||||
@@ -139,7 +124,7 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
|
|||||||
Type: string(gatewayv1.GatewayConditionProgrammed),
|
Type: string(gatewayv1.GatewayConditionProgrammed),
|
||||||
Status: metav1.ConditionFalse,
|
Status: metav1.ConditionFalse,
|
||||||
Reason: string(gatewayv1.GatewayReasonProgrammed),
|
Reason: string(gatewayv1.GatewayReasonProgrammed),
|
||||||
Message: fmt.Sprintf("NBRoutingPeer %s is not ready", parametersRef.Name),
|
Message: fmt.Sprintf("NBRoutingPeer %s is not ready", routingPeerName),
|
||||||
}
|
}
|
||||||
if meta.SetStatusCondition(&gw.Status.Conditions, cond) {
|
if meta.SetStatusCondition(&gw.Status.Conditions, cond) {
|
||||||
err = r.Status().Update(ctx, &gw)
|
err = r.Status().Update(ctx, &gw)
|
||||||
@@ -210,3 +195,17 @@ func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
|||||||
For(&gatewayv1.Gateway{}).
|
For(&gatewayv1.Gateway{}).
|
||||||
Complete(r)
|
Complete(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getRoutingPeerName(listeners []gatewayv1.Listener) (string, error) {
|
||||||
|
if len(listeners) > 1 {
|
||||||
|
return "", errors.New("netbird Gateway only supports a single listener")
|
||||||
|
}
|
||||||
|
group, kind, ok := strings.Cut(string(listeners[0].Protocol), "/")
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("invalid protocol %s, expected gateway.netbird.io/NBRoutingPeer", listeners[0].Protocol)
|
||||||
|
}
|
||||||
|
if group != "gateway.netbird.io" || kind != "NBRoutingPeer" {
|
||||||
|
return "", fmt.Errorf("invalid group %s and kind %s, expected gateway.netbird.io/NBRoutingPeer", group, kind)
|
||||||
|
}
|
||||||
|
return string(listeners[0].Name), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
netbird "github.com/netbirdio/netbird/shared/management/client/rest"
|
netbird "github.com/netbirdio/netbird/shared/management/client/rest"
|
||||||
"github.com/netbirdio/netbird/shared/management/http/api"
|
"github.com/netbirdio/netbird/shared/management/http/api"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
kerrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
@@ -69,8 +70,13 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
|
|||||||
logger.Info("gateway is not ready", "name", gw.ObjectMeta.Name)
|
logger.Info("gateway is not ready", "name", gw.ObjectMeta.Name)
|
||||||
return ctrl.Result{RequeueAfter: 1 * time.Second}, nil
|
return ctrl.Result{RequeueAfter: 1 * time.Second}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
routingPeerName, err := getRoutingPeerName(gw.Spec.Listeners)
|
||||||
|
if err != nil {
|
||||||
|
return ctrl.Result{}, err
|
||||||
|
}
|
||||||
nbrp := &netbirdiov1.NBRoutingPeer{}
|
nbrp := &netbirdiov1.NBRoutingPeer{}
|
||||||
err = r.Get(ctx, types.NamespacedName{Namespace: gw.Namespace, Name: gw.Spec.Infrastructure.ParametersRef.Name}, nbrp)
|
err = r.Get(ctx, types.NamespacedName{Namespace: gw.Namespace, Name: routingPeerName}, nbrp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ctrl.Result{}, err
|
return ctrl.Result{}, err
|
||||||
}
|
}
|
||||||
@@ -234,6 +240,9 @@ func (r *HTTPRouteReconciler) reconcileDelete(ctx context.Context, hr gatewayv1.
|
|||||||
key := client.ObjectKey{Namespace: hr.Namespace, Name: string(ref.Name)}
|
key := client.ObjectKey{Namespace: hr.Namespace, Name: string(ref.Name)}
|
||||||
var svc corev1.Service
|
var svc corev1.Service
|
||||||
err := r.Client.Get(ctx, key, &svc)
|
err := r.Client.Get(ctx, key, &svc)
|
||||||
|
if kerrors.IsNotFound(err) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ctrl.Result{}, err
|
return ctrl.Result{}, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user