-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dataplane controller and multi gRPC clients manager
- Loading branch information
Showing
8 changed files
with
442 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/builder" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
|
||
dataplane "github.com/kong/blixt/internal/dataplane/client" | ||
"github.com/kong/blixt/pkg/vars" | ||
) | ||
|
||
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways,verbs=get;list;watch;create;update;patch;delete | ||
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways/status,verbs=get;update;patch | ||
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways/finalizers,verbs=update | ||
|
||
//+kubebuilder:rbac:groups=core,resources=services,verbs=get;list;watch;create;update;patch;delete | ||
//+kubebuilder:rbac:groups=core,resources=services/status,verbs=get | ||
|
||
//+kubebuilder:rbac:groups=core,resources=endpoints,verbs=get;list;watch;create;update;patch;delete | ||
//+kubebuilder:rbac:groups=core,resources=endpoints/status,verbs=get | ||
|
||
// DataplaneReconciler reconciles the dataplane pods. | ||
type DataplaneReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
|
||
BackendsClientManager *dataplane.BackendsClientManager | ||
|
||
updates chan event.GenericEvent | ||
} | ||
|
||
func NewDataplaneReconciler(client client.Client, schema *runtime.Scheme, manager *dataplane.BackendsClientManager) *DataplaneReconciler { | ||
return &DataplaneReconciler{ | ||
Client: client, | ||
Scheme: schema, | ||
BackendsClientManager: manager, | ||
updates: make(chan event.GenericEvent, 1000), | ||
} | ||
} | ||
|
||
var ( | ||
podOwnerKey = ".metadata.controller" | ||
apiGVStr = appsv1.SchemeGroupVersion.String() | ||
) | ||
|
||
// SetupWithManager loads the controller into the provided controller manager. | ||
func (r *DataplaneReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
|
||
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &corev1.Pod{}, podOwnerKey, func(rawObj client.Object) []string { | ||
// grab the pod object, extract the owner... | ||
pod := rawObj.(*corev1.Pod) | ||
owner := metav1.GetControllerOf(pod) | ||
if owner == nil { | ||
return nil | ||
} | ||
// ...make sure it's a DaemonSet... | ||
if owner.APIVersion != apiGVStr || owner.Kind != "DaemonSet" { | ||
return nil | ||
} | ||
|
||
// ...and if so, return it | ||
return []string{owner.Name} | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&appsv1.DaemonSet{}, | ||
builder.WithPredicates(predicate.NewPredicateFuncs(r.daemonsetHasMatchingAnnotations)), | ||
). | ||
Complete(r) | ||
} | ||
|
||
func (r *DataplaneReconciler) daemonsetHasMatchingAnnotations(obj client.Object) bool { | ||
daemonset, ok := obj.(*appsv1.DaemonSet) | ||
if !ok { | ||
return false | ||
} | ||
|
||
// determine if this is a blixt daemonset | ||
matchLabels := daemonset.Spec.Selector.MatchLabels | ||
app, ok := matchLabels["app"] | ||
if !ok || app != vars.DefaultDataPlaneAppLabel { | ||
return false | ||
} | ||
|
||
// verify that it's the dataplane daemonset | ||
component, ok := matchLabels["component"] | ||
if !ok || component != vars.DefaultDataPlaneComponentLabel { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
// Reconcile provisions (and de-provisions) resources relevant to this controller. | ||
func (r *DataplaneReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
logger := log.FromContext(ctx) | ||
|
||
ds := new(appsv1.DaemonSet) | ||
if err := r.Client.Get(ctx, req.NamespacedName, ds); err != nil { | ||
if errors.IsNotFound(err) { | ||
logger.Info("DataplaneReconciler", "reconcile status", "object enqueued no longer exists, skipping") | ||
return ctrl.Result{}, nil | ||
} | ||
return ctrl.Result{}, err | ||
} | ||
|
||
var childPods corev1.PodList | ||
if err := r.List(ctx, &childPods, client.InNamespace(req.Namespace), client.MatchingFields{podOwnerKey: req.Name}); err != nil { | ||
logger.Error(err, "DataplaneReconciler", "reconcile status", "unable to list child pods") | ||
return ctrl.Result{}, err | ||
} | ||
|
||
readyPodByNN := make(map[types.NamespacedName]corev1.Pod) | ||
for _, pod := range childPods.Items { | ||
for _, container := range pod.Status.ContainerStatuses { | ||
if container.Name == vars.DefaultDataPlaneComponentLabel && container.Ready { | ||
key := types.NamespacedName{Namespace: pod.Namespace, Name: pod.Name} | ||
readyPodByNN[key] = pod | ||
} | ||
} | ||
} | ||
|
||
logger.Info("DataplaneReconciler", "reconcile status", "setting updated backends client list", "num ready pods", len(readyPodByNN)) | ||
updated, err := r.BackendsClientManager.SetClientsList(ctx, readyPodByNN) | ||
if updated { | ||
logger.Info("DataplaneReconciler", "reconcile status", "backends client list updated, sending generic event") | ||
r.updates <- event.GenericEvent{Object: ds} | ||
logger.Info("DataplaneReconciler", "reconcile status", "generic event sent") | ||
} | ||
if err != nil { | ||
logger.Error(err, "DataplaneReconciler", "reconcile status", "partial failure for backends client list update") | ||
return ctrl.Result{Requeue: true}, err | ||
} | ||
|
||
logger.Info("DataplaneReconciler", "reconcile status", "done") | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
func (r *DataplaneReconciler) GetUpdates() <-chan event.GenericEvent { | ||
return r.updates | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/kong/blixt | ||
|
||
go 1.19 | ||
go 1.20 | ||
|
||
require ( | ||
github.com/go-logr/logr v1.2.4 | ||
|
Oops, something went wrong.