Skip to content

Commit

Permalink
Add support for multiple gRPC client management
Browse files Browse the repository at this point in the history
  • Loading branch information
levikobi committed Aug 14, 2023
1 parent 468d0a0 commit c3e13af
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 158 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM golang:1.19 as builder
FROM golang:1.20 as builder

LABEL org.opencontainers.image.source=https://github.com/kong/blixt
LABEL org.opencontainers.image.description="An experimental layer 4 load-balancer built using eBPF/XDP with ebpf-go \
Expand Down
51 changes: 27 additions & 24 deletions controllers/tcproute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -15,6 +14,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

Expand All @@ -33,7 +33,8 @@ import (
// TCPRouteReconciler reconciles a TCPRoute object
type TCPRouteReconciler struct {
client.Client
Scheme *runtime.Scheme
Scheme *runtime.Scheme
BackendsClientManager *dataplane.BackendsClientManager

log logr.Logger
}
Expand All @@ -44,17 +45,33 @@ func (r *TCPRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {

return ctrl.NewControllerManagedBy(mgr).
For(&gatewayv1alpha2.TCPRoute{}).
Watches(
&appsv1.DaemonSet{},
handler.EnqueueRequestsFromMapFunc(r.mapDataPlaneDaemonsetToTCPRoutes),
).
Watches(
&gatewayv1beta1.Gateway{},
handler.EnqueueRequestsFromMapFunc(r.mapGatewayToTCPRoutes),
).
Complete(r)
}

func (r *TCPRouteReconciler) SetupReconciliation(ctx context.Context) {
tcproutes := &gatewayv1alpha2.TCPRouteList{}
if err := r.Client.List(ctx, tcproutes); err != nil {
// TODO: https://github.com/kubernetes-sigs/controller-runtime/issues/1996
r.log.Error(err, "could not enqueue TCPRoutes for DaemonSet update")
return
}

for _, tcproute := range tcproutes.Items {
req := reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: tcproute.Namespace,
Name: tcproute.Name,
},
}

r.Reconcile(ctx, req)
}
}

// Reconcile reconciles TCPRoute object
func (r *TCPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
tcproute := new(gatewayv1alpha2.TCPRoute)
Expand Down Expand Up @@ -185,18 +202,11 @@ func (r *TCPRouteReconciler) ensureTCPRouteConfiguredInDataPlane(ctx context.Con
return err
}

// TODO: add multiple endpoint support https://github.com/Kong/blixt/issues/46
dataplaneClient, err := dataplane.NewDataPlaneClient(context.Background(), r.Client)
if err != nil {
if _, err = r.BackendsClientManager.Update(ctx, targets); err != nil {
return err
}

confirmation, err := dataplaneClient.Update(context.Background(), targets)
if err != nil {
return err
}

r.log.Info(fmt.Sprintf("successful data-plane UPDATE, confirmation: %s", confirmation.String()))
r.log.Info("successful data-plane UPDATE")

return nil
}
Expand All @@ -208,19 +218,12 @@ func (r *TCPRouteReconciler) ensureTCPRouteDeletedInDataPlane(ctx context.Contex
return err
}

// TODO: add multiple endpoint support https://github.com/Kong/blixt/issues/46
dataplaneClient, err := dataplane.NewDataPlaneClient(context.Background(), r.Client)
if err != nil {
return err
}

// delete the target from the dataplane
confirmation, err := dataplaneClient.Delete(context.Background(), targets.Vip)
if err != nil {
if _, err = r.BackendsClientManager.Delete(ctx, targets.Vip); err != nil {
return err
}

r.log.Info(fmt.Sprintf("successful data-plane DELETE, confirmation: %s", confirmation.String()))
r.log.Info("successful data-plane DELETE")

oldFinalizers := tcproute.GetFinalizers()
newFinalizers := make([]string, 0, len(oldFinalizers)-1)
Expand Down
51 changes: 27 additions & 24 deletions controllers/udproute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -15,6 +14,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

Expand All @@ -33,7 +33,8 @@ import (
// UDPRouteReconciler reconciles a UDPRoute object
type UDPRouteReconciler struct {
client.Client
Scheme *runtime.Scheme
Scheme *runtime.Scheme
BackendsClientManager *dataplane.BackendsClientManager

log logr.Logger
}
Expand All @@ -44,17 +45,33 @@ func (r *UDPRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {

return ctrl.NewControllerManagedBy(mgr).
For(&gatewayv1alpha2.UDPRoute{}).
Watches(
&appsv1.DaemonSet{},
handler.EnqueueRequestsFromMapFunc(r.mapDataPlaneDaemonsetToUDPRoutes),
).
Watches(
&gatewayv1beta1.Gateway{},
handler.EnqueueRequestsFromMapFunc(r.mapGatewayToUDPRoutes),
).
Complete(r)
}

func (r *UDPRouteReconciler) SetupReconciliation(ctx context.Context) {
udproutes := &gatewayv1alpha2.UDPRouteList{}
if err := r.Client.List(ctx, udproutes); err != nil {
// TODO: https://github.com/kubernetes-sigs/controller-runtime/issues/1996
r.log.Error(err, "could not enqueue TCPRoutes for DaemonSet update")
return
}

for _, tcproute := range udproutes.Items {
req := reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: tcproute.Namespace,
Name: tcproute.Name,
},
}

r.Reconcile(ctx, req)
}
}

// Reconcile reconciles UDPRoute object
func (r *UDPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
udproute := new(gatewayv1alpha2.UDPRoute)
Expand Down Expand Up @@ -185,18 +202,11 @@ func (r *UDPRouteReconciler) ensureUDPRouteConfiguredInDataPlane(ctx context.Con
return err
}

// TODO: add multiple endpoint support https://github.com/Kong/blixt/issues/46
dataplaneClient, err := dataplane.NewDataPlaneClient(context.Background(), r.Client)
if err != nil {
if _, err = r.BackendsClientManager.Update(ctx, targets); err != nil {
return err
}

confirmation, err := dataplaneClient.Update(context.Background(), targets)
if err != nil {
return err
}

r.log.Info(fmt.Sprintf("successful data-plane UPDATE, confirmation: %s", confirmation.String()))
r.log.Info("successful data-plane UPDATE")

return nil
}
Expand All @@ -208,19 +218,12 @@ func (r *UDPRouteReconciler) ensureUDPRouteDeletedInDataPlane(ctx context.Contex
return err
}

// TODO: add multiple endpoint support https://github.com/Kong/blixt/issues/46
dataplaneClient, err := dataplane.NewDataPlaneClient(context.Background(), r.Client)
if err != nil {
return err
}

// delete the target from the dataplane
confirmation, err := dataplaneClient.Delete(context.Background(), targets.Vip)
if err != nil {
if _, err = r.BackendsClientManager.Delete(ctx, targets.Vip); err != nil {
return err
}

r.log.Info(fmt.Sprintf("successful data-plane DELETE, confirmation: %s", confirmation.String()))
r.log.Info("successful data-plane DELETE")

oldFinalizers := udproute.GetFinalizers()
newFinalizers := make([]string, 0, len(oldFinalizers)-1)
Expand Down
45 changes: 22 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
module github.com/kong/blixt

go 1.19
go 1.20

require (
github.com/go-logr/logr v1.2.4
github.com/go-logr/stdr v1.2.2
github.com/google/uuid v1.3.0
github.com/kong/kubernetes-testing-framework v0.31.0
github.com/kong/kubernetes-testing-framework v0.30.0
github.com/onsi/ginkgo/v2 v2.9.5
github.com/onsi/gomega v1.27.7
github.com/stretchr/testify v1.8.3
google.golang.org/grpc v1.55.0
google.golang.org/protobuf v1.30.0
k8s.io/api v0.27.2
k8s.io/apiextensions-apiserver v0.27.2
k8s.io/apimachinery v0.27.2
k8s.io/client-go v0.27.2
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2
github.com/stretchr/testify v1.8.4
google.golang.org/grpc v1.56.1
google.golang.org/protobuf v1.31.0
k8s.io/api v0.27.3
k8s.io/apiextensions-apiserver v0.27.3
k8s.io/apimachinery v0.27.3
k8s.io/client-go v0.27.3
k8s.io/utils v0.0.0-20230505201702-9f6742963106
sigs.k8s.io/controller-runtime v0.15.0
sigs.k8s.io/gateway-api v0.7.1
)

require (
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/avast/retry-go/v4 v4.3.4 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
Expand All @@ -50,7 +49,7 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand All @@ -68,33 +67,33 @@ require (
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/sirupsen/logrus v1.9.2 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xlab/treeprint v1.1.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.9.1 // indirect
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230629202037-9506855d4529 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/component-base v0.27.2 // indirect
k8s.io/component-base v0.27.3 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
k8s.io/kube-openapi v0.0.0-20230515203736-54b630e78af5 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kind v0.19.0 // indirect
sigs.k8s.io/kind v0.20.0 // indirect
sigs.k8s.io/kustomize/api v0.13.4 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
Expand Down
Loading

0 comments on commit c3e13af

Please sign in to comment.