mirror of
https://github.com/YuzuZensai/netbird-kubernetes-operator.git
synced 2026-09-13 18:59:09 +00:00
Gateway API support (#117)
This change adds support for the new proxy service to the operator through Gateway API. This change attempts to standardize concepts around the Gateway API to allow for compatibility with other projects. Fixes #111 Fixes #44 Signed-off-by: Philip Laine <philip.laine@gmail.com>
This commit is contained in:
@@ -0,0 +1,212 @@
|
||||
/*
|
||||
Copyright 2025.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
netbird "github.com/netbirdio/netbird/shared/management/client/rest"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
|
||||
|
||||
netbirdiov1 "github.com/netbirdio/kubernetes-operator/api/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
GatewayFinalizer = "gateway.netbird.io/gateway"
|
||||
)
|
||||
|
||||
type GatewayReconciler struct {
|
||||
client.Client
|
||||
}
|
||||
|
||||
func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
||||
gw := gatewayv1.Gateway{}
|
||||
err := r.Get(ctx, req.NamespacedName, &gw)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, client.IgnoreNotFound(err)
|
||||
}
|
||||
|
||||
// Check if referenced class belongs to this controller.
|
||||
gwc := &gatewayv1.GatewayClass{}
|
||||
nn := types.NamespacedName{
|
||||
Name: string(gw.Spec.GatewayClassName),
|
||||
}
|
||||
err = r.Get(ctx, nn, gwc)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
if string(gwc.Spec.ControllerName) != GatewayControllerName {
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
if !meta.IsStatusConditionTrue(gwc.Status.Conditions, string(gatewayv1.GatewayClassConditionStatusAccepted)) {
|
||||
return ctrl.Result{RequeueAfter: 5 * time.Second}, nil
|
||||
}
|
||||
|
||||
// Handle resource deletion.
|
||||
if !gw.DeletionTimestamp.IsZero() {
|
||||
return r.reconcileDelete(ctx, gw)
|
||||
}
|
||||
|
||||
// Verify Gateway configuration.
|
||||
if gw.Spec.Infrastructure == nil || gw.Spec.Infrastructure.ParametersRef == nil {
|
||||
cond := metav1.Condition{
|
||||
Type: string(gatewayv1.GatewayConditionAccepted),
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: string(gatewayv1.GatewayReasonInvalidParameters),
|
||||
Message: "Gateway expected to reference a NBRoutingPeer",
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
cond := metav1.Condition{
|
||||
Type: string(gatewayv1.GatewayConditionAccepted),
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: string(gatewayv1.GatewayReasonAccepted),
|
||||
}
|
||||
if meta.SetStatusCondition(&gw.Status.Conditions, cond) {
|
||||
err = r.Status().Update(ctx, &gw)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
if controllerutil.AddFinalizer(&gw, GatewayFinalizer) {
|
||||
err = r.Client.Update(ctx, &gw)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure routing peer is ready.
|
||||
nbrp := &netbirdiov1.NBRoutingPeer{}
|
||||
err = r.Get(ctx, types.NamespacedName{Namespace: req.Namespace, Name: parametersRef.Name}, nbrp)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
idx := slices.IndexFunc(nbrp.Status.Conditions, func(cond netbirdiov1.NBCondition) bool {
|
||||
return cond.Type == netbirdiov1.NBSetupKeyReady
|
||||
})
|
||||
if idx == -1 || nbrp.Status.Conditions[idx].Status != corev1.ConditionStatus(metav1.ConditionTrue) {
|
||||
cond := metav1.Condition{
|
||||
Type: string(gatewayv1.GatewayConditionProgrammed),
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: string(gatewayv1.GatewayReasonProgrammed),
|
||||
Message: fmt.Sprintf("NBRoutingPeer %s is not ready", parametersRef.Name),
|
||||
}
|
||||
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{RequeueAfter: 5 * time.Second}, nil
|
||||
}
|
||||
|
||||
// Signal Gateway is programmed.
|
||||
cond = metav1.Condition{
|
||||
Type: string(gatewayv1.GatewayConditionProgrammed),
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: string(gatewayv1.GatewayReasonProgrammed),
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
func (r *GatewayReconciler) reconcileDelete(ctx context.Context, gw gatewayv1.Gateway) (ctrl.Result, error) {
|
||||
var httpRouteList gatewayv1.HTTPRouteList
|
||||
err := r.Client.List(ctx, &httpRouteList)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
gvk := gw.GroupVersionKind()
|
||||
for _, route := range httpRouteList.Items {
|
||||
for _, ref := range route.Spec.ParentRefs {
|
||||
group := gvk.Group
|
||||
if ref.Group != nil {
|
||||
group = string(*ref.Group)
|
||||
}
|
||||
kind := gvk.Kind
|
||||
if ref.Kind != nil {
|
||||
kind = string(*ref.Kind)
|
||||
}
|
||||
namespace := route.Namespace
|
||||
if ref.Namespace != nil {
|
||||
namespace = string(*ref.Namespace)
|
||||
}
|
||||
if group == gvk.Group && kind == gvk.Kind && namespace == gw.Namespace && string(ref.Name) == gw.Name {
|
||||
return ctrl.Result{RequeueAfter: 5 * time.Second}, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if controllerutil.RemoveFinalizer(&gw, GatewayFinalizer) {
|
||||
err := r.Client.Update(ctx, &gw)
|
||||
if err != nil && !netbird.IsNotFound(err) {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
// SetupWithManager sets up the controller with the Manager.
|
||||
func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
For(&gatewayv1.Gateway{}).
|
||||
Complete(r)
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
GatewayClassFinalizer = "gateway.netbird.io/gatewayclass"
|
||||
GatewayControllerName = "gateway.netbird.io/controller"
|
||||
)
|
||||
|
||||
type GatewayClassReconciler struct {
|
||||
client.Client
|
||||
}
|
||||
|
||||
func (r *GatewayClassReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
||||
gwc := gatewayv1.GatewayClass{}
|
||||
err := r.Client.Get(ctx, req.NamespacedName, &gwc)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, client.IgnoreNotFound(err)
|
||||
}
|
||||
|
||||
// Controller name does not match.
|
||||
if gwc.Spec.ControllerName != GatewayControllerName {
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
// Gateway class is being deleted.
|
||||
if !gwc.GetDeletionTimestamp().IsZero() {
|
||||
return r.reconcileDelete(ctx, gwc)
|
||||
}
|
||||
|
||||
// Validate configuration.
|
||||
if gwc.Spec.ParametersRef != nil {
|
||||
cond := metav1.Condition{
|
||||
Type: string(gatewayv1.GatewayClassConditionStatusAccepted),
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: string(gatewayv1.GatewayClassReasonInvalidParameters),
|
||||
Message: "Parameters references is not supported.",
|
||||
}
|
||||
if meta.SetStatusCondition(&gwc.Status.Conditions, cond) {
|
||||
err = r.Client.Status().Update(ctx, &gwc)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Add finalizer to validate deletion.
|
||||
if controllerutil.AddFinalizer(&gwc, GatewayClassFinalizer) {
|
||||
err = r.Client.Update(ctx, &gwc)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
}
|
||||
|
||||
// Set condition to accepted.
|
||||
cond := metav1.Condition{
|
||||
Type: string(gatewayv1.GatewayClassConditionStatusAccepted),
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: string(gatewayv1.GatewayClassReasonAccepted),
|
||||
Message: "Reconciled by Netbird Operator.",
|
||||
}
|
||||
meta.SetStatusCondition(&gwc.Status.Conditions, cond)
|
||||
err = r.Client.Status().Update(ctx, &gwc)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
func (r *GatewayClassReconciler) reconcileDelete(ctx context.Context, gwc gatewayv1.GatewayClass) (ctrl.Result, error) {
|
||||
var gatewayList gatewayv1.GatewayList
|
||||
err := r.Client.List(ctx, &gatewayList)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
for _, gw := range gatewayList.Items {
|
||||
if string(gw.Spec.GatewayClassName) == gwc.ObjectMeta.Name {
|
||||
return ctrl.Result{RequeueAfter: 5 * time.Second}, nil
|
||||
}
|
||||
}
|
||||
if controllerutil.RemoveFinalizer(&gwc, GatewayClassFinalizer) {
|
||||
err = r.Client.Update(ctx, &gwc)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
// SetupWithManager sets up the controller with the Manager.
|
||||
func (r *GatewayClassReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
For(&gatewayv1.GatewayClass{}).
|
||||
Complete(r)
|
||||
}
|
||||
@@ -0,0 +1,286 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
netbird "github.com/netbirdio/netbird/shared/management/client/rest"
|
||||
"github.com/netbirdio/netbird/shared/management/http/api"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
|
||||
|
||||
netbirdiov1 "github.com/netbirdio/kubernetes-operator/api/v1"
|
||||
"github.com/netbirdio/kubernetes-operator/internal/util"
|
||||
)
|
||||
|
||||
const (
|
||||
HTTPRouteFinalizer = "gateway.netbird.io/httproute"
|
||||
ResourceIDAnnotationKey = "gateway.netbird.io/resource-ids"
|
||||
ProxyIDAnnotationKey = "gateway.netbird.io/proxy-ids"
|
||||
)
|
||||
|
||||
type HTTPRouteReconciler struct {
|
||||
client.Client
|
||||
|
||||
Netbird *netbird.Client
|
||||
ClusterDNS string
|
||||
}
|
||||
|
||||
// nolint:gocyclo
|
||||
func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
||||
logger := ctrl.Log.WithName("HTTPRoute").WithValues("namespace", req.Namespace, "name", req.Name)
|
||||
|
||||
hr := gatewayv1.HTTPRoute{}
|
||||
err := r.Get(ctx, req.NamespacedName, &hr)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, client.IgnoreNotFound(err)
|
||||
}
|
||||
|
||||
if !hr.DeletionTimestamp.IsZero() {
|
||||
return r.reconcileDelete(ctx, hr)
|
||||
}
|
||||
|
||||
for _, parent := range hr.Spec.ParentRefs {
|
||||
// Check if controller is responsible for route.
|
||||
parentNamespace := hr.Namespace
|
||||
if parent.Namespace != nil {
|
||||
parentNamespace = string(*parent.Namespace)
|
||||
}
|
||||
gw := &gatewayv1.Gateway{}
|
||||
err = r.Client.Get(ctx, types.NamespacedName{Namespace: parentNamespace, Name: string(parent.Name)}, gw)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
gwc := &gatewayv1.GatewayClass{}
|
||||
err := r.Get(ctx, client.ObjectKey{Name: string(gw.Spec.GatewayClassName)}, gwc)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
if gwc.Spec.ControllerName != GatewayControllerName {
|
||||
continue
|
||||
}
|
||||
|
||||
if !meta.IsStatusConditionTrue(gw.Status.Conditions, string(gatewayv1.GatewayConditionProgrammed)) {
|
||||
logger.Info("gateway is not ready", "name", gw.ObjectMeta.Name)
|
||||
return ctrl.Result{Requeue: true}, nil
|
||||
}
|
||||
|
||||
nbrp := &netbirdiov1.NBRoutingPeer{}
|
||||
err = r.Get(ctx, types.NamespacedName{Namespace: gw.Namespace, Name: gw.Spec.Infrastructure.ParametersRef.Name}, nbrp)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
if controllerutil.AddFinalizer(&hr, HTTPRouteFinalizer) {
|
||||
err = r.Client.Update(ctx, &hr)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
}
|
||||
|
||||
// Create network resources.
|
||||
oldResourceIDs := map[string]string{}
|
||||
if s, ok := hr.Annotations[ResourceIDAnnotationKey]; ok {
|
||||
err := json.Unmarshal([]byte(s), &oldResourceIDs)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
}
|
||||
resourceIDs := map[string]string{}
|
||||
targets := []api.ServiceTarget{}
|
||||
for _, rule := range hr.Spec.Rules {
|
||||
for _, ref := range rule.BackendRefs {
|
||||
// TODO (phillebaba): Support reference grants.
|
||||
refNamespace := hr.Namespace
|
||||
|
||||
key := strings.Join([]string{string(ref.Name), refNamespace}, "/")
|
||||
networkResourceReq := api.NetworkResourceRequest{
|
||||
Name: fmt.Sprintf("%s/%s/%s/%s", refNamespace, gw.Name, hr.Name, ref.Name),
|
||||
Enabled: true,
|
||||
Address: fmt.Sprintf("%s.%s.%s", ref.Name, refNamespace, r.ClusterDNS),
|
||||
Groups: []string{},
|
||||
}
|
||||
|
||||
id, err := func() (string, error) {
|
||||
if id, ok := oldResourceIDs[key]; ok {
|
||||
_, err := r.Netbird.Networks.Resources(*nbrp.Status.NetworkID).Get(ctx, id)
|
||||
if err != nil && !netbird.IsNotFound(err) {
|
||||
return "", err
|
||||
}
|
||||
if err == nil {
|
||||
_, err = r.Netbird.Networks.Resources(*nbrp.Status.NetworkID).Update(ctx, id, networkResourceReq)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
delete(oldResourceIDs, key)
|
||||
return id, nil
|
||||
}
|
||||
}
|
||||
resource, err := r.Netbird.Networks.Resources(*nbrp.Status.NetworkID).Create(ctx, networkResourceReq)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return resource.Id, nil
|
||||
}()
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
resourceIDs[key] = id
|
||||
target := api.ServiceTarget{
|
||||
Enabled: true,
|
||||
Path: nil,
|
||||
TargetId: id,
|
||||
Protocol: "http",
|
||||
TargetType: "domain",
|
||||
}
|
||||
targets = append(targets, target)
|
||||
}
|
||||
}
|
||||
|
||||
// Create proxy service.
|
||||
oldProxyIDs := map[string]string{}
|
||||
if s, ok := hr.Annotations[ProxyIDAnnotationKey]; ok {
|
||||
err := json.Unmarshal([]byte(s), &oldProxyIDs)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
}
|
||||
proxyIDs := map[string]string{}
|
||||
for _, hostname := range hr.Spec.Hostnames {
|
||||
proxyCreate := api.PostApiReverseProxiesServicesJSONRequestBody{
|
||||
Auth: api.ServiceAuthConfig{},
|
||||
Domain: string(hostname),
|
||||
Enabled: true,
|
||||
Name: string(hostname),
|
||||
PassHostHeader: util.Ptr(false),
|
||||
RewriteRedirects: util.Ptr(false),
|
||||
Targets: targets,
|
||||
}
|
||||
|
||||
id, err := func() (string, error) {
|
||||
if id, ok := oldProxyIDs[string(hostname)]; ok {
|
||||
_, err := r.Netbird.ReverseProxyServices.Get(ctx, id)
|
||||
if err != nil && !netbird.IsNotFound(err) {
|
||||
return "", err
|
||||
}
|
||||
if err == nil {
|
||||
_, err := r.Netbird.ReverseProxyServices.Update(ctx, id, proxyCreate)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
delete(oldProxyIDs, string(hostname))
|
||||
return id, nil
|
||||
}
|
||||
}
|
||||
proxy, err := r.Netbird.ReverseProxyServices.Create(ctx, proxyCreate)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return proxy.Id, nil
|
||||
}()
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
proxyIDs[string(hostname)] = id
|
||||
}
|
||||
|
||||
for _, id := range oldResourceIDs {
|
||||
err = r.Netbird.Networks.Resources(*nbrp.Status.NetworkID).Delete(ctx, id)
|
||||
if err != nil && !netbird.IsNotFound(err) {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
}
|
||||
for _, id := range oldProxyIDs {
|
||||
err = r.Netbird.ReverseProxyServices.Delete(ctx, id)
|
||||
if err != nil && !netbird.IsNotFound(err) {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
}
|
||||
|
||||
b, err := json.Marshal(resourceIDs)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
hr.Annotations[ResourceIDAnnotationKey] = string(b)
|
||||
b, err = json.Marshal(proxyIDs)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
hr.Annotations[ProxyIDAnnotationKey] = string(b)
|
||||
err = r.Client.Update(ctx, &hr)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
}
|
||||
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
func (r *HTTPRouteReconciler) reconcileDelete(ctx context.Context, hr gatewayv1.HTTPRoute) (ctrl.Result, error) {
|
||||
for _, parent := range hr.Spec.ParentRefs {
|
||||
parentNamespace := hr.Namespace
|
||||
if parent.Namespace != nil {
|
||||
parentNamespace = string(*parent.Namespace)
|
||||
}
|
||||
gw := &gatewayv1.Gateway{}
|
||||
err := r.Client.Get(ctx, types.NamespacedName{Namespace: parentNamespace, Name: string(parent.Name)}, gw)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
nbrp := &netbirdiov1.NBRoutingPeer{}
|
||||
err = r.Get(ctx, types.NamespacedName{Namespace: gw.Namespace, Name: gw.Spec.Infrastructure.ParametersRef.Name}, nbrp)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
proxyIDs := map[string]string{}
|
||||
if s, ok := hr.Annotations[ProxyIDAnnotationKey]; ok {
|
||||
err := json.Unmarshal([]byte(s), &proxyIDs)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
}
|
||||
for _, id := range proxyIDs {
|
||||
err = r.Netbird.ReverseProxyServices.Delete(ctx, id)
|
||||
if err != nil && !netbird.IsNotFound(err) {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
}
|
||||
|
||||
resourceIDs := map[string]string{}
|
||||
if s, ok := hr.Annotations[ResourceIDAnnotationKey]; ok {
|
||||
err := json.Unmarshal([]byte(s), &resourceIDs)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
}
|
||||
for _, id := range resourceIDs {
|
||||
err = r.Netbird.Networks.Resources(*nbrp.Status.NetworkID).Delete(ctx, id)
|
||||
if err != nil && !netbird.IsNotFound(err) {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
}
|
||||
|
||||
if controllerutil.RemoveFinalizer(&hr, HTTPRouteFinalizer) {
|
||||
err := r.Client.Update(ctx, &hr)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
// SetupWithManager sets up the controller with the Manager.
|
||||
func (r *HTTPRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
For(&gatewayv1.HTTPRoute{}).
|
||||
Complete(r)
|
||||
}
|
||||
@@ -23,8 +23,7 @@ import (
|
||||
type NBPolicyReconciler struct {
|
||||
client.Client
|
||||
|
||||
Netbird *netbird.Client
|
||||
ClusterName string
|
||||
Netbird *netbird.Client
|
||||
}
|
||||
|
||||
var (
|
||||
|
||||
@@ -89,9 +89,8 @@ var _ = Describe("NBPolicy Controller", func() {
|
||||
When("Not enough information to create policy", func() {
|
||||
It("should not create any policy", func() {
|
||||
controllerReconciler := &NBPolicyReconciler{
|
||||
Client: k8sClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "Kubernetes",
|
||||
Client: k8sClient,
|
||||
Netbird: netbirdClient,
|
||||
}
|
||||
|
||||
mux.HandleFunc("/api/groups", func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -117,9 +116,8 @@ var _ = Describe("NBPolicy Controller", func() {
|
||||
When("Enough information to create TCP policy", func() {
|
||||
It("should create 1 policy", func() {
|
||||
controllerReconciler := &NBPolicyReconciler{
|
||||
Client: k8sClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "Kubernetes",
|
||||
Client: k8sClient,
|
||||
Netbird: netbirdClient,
|
||||
}
|
||||
|
||||
nbResource := &netbirdiov1.NBResource{
|
||||
@@ -215,9 +213,8 @@ var _ = Describe("NBPolicy Controller", func() {
|
||||
When("TCP information no longer sufficient", func() {
|
||||
It("should delete tcp policy", func() {
|
||||
controllerReconciler := &NBPolicyReconciler{
|
||||
Client: k8sClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "Kubernetes",
|
||||
Client: k8sClient,
|
||||
Netbird: netbirdClient,
|
||||
}
|
||||
|
||||
nbpolicy.Status.ManagedServiceList = append(nbpolicy.Status.ManagedServiceList, "default/noexist")
|
||||
@@ -257,9 +254,8 @@ var _ = Describe("NBPolicy Controller", func() {
|
||||
When("Enough information to create UDP policy", func() {
|
||||
It("should create 1 policy", func() {
|
||||
controllerReconciler := &NBPolicyReconciler{
|
||||
Client: k8sClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "Kubernetes",
|
||||
Client: k8sClient,
|
||||
Netbird: netbirdClient,
|
||||
}
|
||||
|
||||
nbResource := &netbirdiov1.NBResource{
|
||||
@@ -355,9 +351,8 @@ var _ = Describe("NBPolicy Controller", func() {
|
||||
When("UDP information no longer sufficient", func() {
|
||||
It("should delete udp policy", func() {
|
||||
controllerReconciler := &NBPolicyReconciler{
|
||||
Client: k8sClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "Kubernetes",
|
||||
Client: k8sClient,
|
||||
Netbird: netbirdClient,
|
||||
}
|
||||
|
||||
nbpolicy.Status.ManagedServiceList = append(nbpolicy.Status.ManagedServiceList, "default/noexist")
|
||||
@@ -397,9 +392,8 @@ var _ = Describe("NBPolicy Controller", func() {
|
||||
When("Existing protocol gets restricted", func() {
|
||||
It("Should delete protocol policy", func() {
|
||||
controllerReconciler := &NBPolicyReconciler{
|
||||
Client: k8sClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "Kubernetes",
|
||||
Client: k8sClient,
|
||||
Netbird: netbirdClient,
|
||||
}
|
||||
|
||||
nbResource := &netbirdiov1.NBResource{
|
||||
@@ -477,9 +471,8 @@ var _ = Describe("NBPolicy Controller", func() {
|
||||
|
||||
It("Should give all information to Update method", func() {
|
||||
controllerReconciler := &NBPolicyReconciler{
|
||||
Client: k8sClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "Kubernetes",
|
||||
Client: k8sClient,
|
||||
Netbird: netbirdClient,
|
||||
}
|
||||
|
||||
nbResource := &netbirdiov1.NBResource{
|
||||
@@ -595,9 +588,8 @@ var _ = Describe("NBPolicy Controller", func() {
|
||||
When("NBPolicy is set for deletion", func() {
|
||||
It("should delete Policies", func() {
|
||||
controllerReconciler := &NBPolicyReconciler{
|
||||
Client: k8sClient,
|
||||
Netbird: netbirdClient,
|
||||
ClusterName: "Kubernetes",
|
||||
Client: k8sClient,
|
||||
Netbird: netbirdClient,
|
||||
}
|
||||
|
||||
nbpolicy.Status.TCPPolicyID = util.Ptr("policyidtcp")
|
||||
|
||||
Reference in New Issue
Block a user