|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + appsv1 "k8s.io/api/apps/v1" |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + "k8s.io/apimachinery/pkg/api/errors" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/runtime" |
| 12 | + "k8s.io/apimachinery/pkg/types" |
| 13 | + ctrl "sigs.k8s.io/controller-runtime" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 19 | + |
| 20 | + dataplane "github.com/kubernetes-sigs/blixt/internal/dataplane/client" |
| 21 | + "github.com/kubernetes-sigs/blixt/pkg/vars" |
| 22 | +) |
| 23 | + |
| 24 | +//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways,verbs=get;list;watch;create;update;patch;delete |
| 25 | +//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways/status,verbs=get;update;patch |
| 26 | +//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways/finalizers,verbs=update |
| 27 | + |
| 28 | +//+kubebuilder:rbac:groups=core,resources=services,verbs=get;list;watch;create;update;patch;delete |
| 29 | +//+kubebuilder:rbac:groups=core,resources=services/status,verbs=get |
| 30 | + |
| 31 | +//+kubebuilder:rbac:groups=core,resources=endpoints,verbs=get;list;watch;create;update;patch;delete |
| 32 | +//+kubebuilder:rbac:groups=core,resources=endpoints/status,verbs=get |
| 33 | + |
| 34 | +// DataplaneReconciler reconciles the dataplane pods. |
| 35 | +type DataplaneReconciler struct { |
| 36 | + client.Client |
| 37 | + scheme *runtime.Scheme |
| 38 | + |
| 39 | + backendsClientManager *dataplane.BackendsClientManager |
| 40 | + |
| 41 | + updates chan event.GenericEvent |
| 42 | +} |
| 43 | + |
| 44 | +func NewDataplaneReconciler(client client.Client, schema *runtime.Scheme, manager *dataplane.BackendsClientManager) *DataplaneReconciler { |
| 45 | + return &DataplaneReconciler{ |
| 46 | + Client: client, |
| 47 | + scheme: schema, |
| 48 | + backendsClientManager: manager, |
| 49 | + updates: make(chan event.GenericEvent, 1), |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +var ( |
| 54 | + podOwnerKey = ".metadata.controller" |
| 55 | + apiGVStr = appsv1.SchemeGroupVersion.String() |
| 56 | +) |
| 57 | + |
| 58 | +// SetupWithManager loads the controller into the provided controller manager. |
| 59 | +func (r *DataplaneReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 60 | + |
| 61 | + // In order to allow our reconciler to quickly look up Pods by their owner, we’ll |
| 62 | + // need an index. We declare an index key that we can later use with the client |
| 63 | + // as a pseudo-field name, and then describe how to extract the indexed value from |
| 64 | + // the Pod object. The indexer will automatically take care of namespaces for us, |
| 65 | + // so we just have to extract the owner name if the Pod has a DaemonSet owner. |
| 66 | + if err := mgr.GetFieldIndexer().IndexField(context.Background(), &corev1.Pod{}, podOwnerKey, func(rawObj client.Object) []string { |
| 67 | + // grab the pod object, extract the owner... |
| 68 | + pod := rawObj.(*corev1.Pod) |
| 69 | + owner := metav1.GetControllerOf(pod) |
| 70 | + if owner == nil { |
| 71 | + return nil |
| 72 | + } |
| 73 | + // ...make sure it's a DaemonSet... |
| 74 | + if owner.APIVersion != apiGVStr || owner.Kind != "DaemonSet" { |
| 75 | + return nil |
| 76 | + } |
| 77 | + |
| 78 | + // ...and if so, return it |
| 79 | + return []string{owner.Name} |
| 80 | + }); err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + return ctrl.NewControllerManagedBy(mgr). |
| 85 | + For(&appsv1.DaemonSet{}, |
| 86 | + builder.WithPredicates(predicate.NewPredicateFuncs(r.daemonsetHasMatchingAnnotations)), |
| 87 | + ). |
| 88 | + WithEventFilter(predicate.Funcs{ |
| 89 | + UpdateFunc: func(e event.UpdateEvent) bool { |
| 90 | + return true |
| 91 | + }, |
| 92 | + }). |
| 93 | + Complete(r) |
| 94 | +} |
| 95 | + |
| 96 | +func (r *DataplaneReconciler) daemonsetHasMatchingAnnotations(obj client.Object) bool { |
| 97 | + log := log.FromContext(context.Background()) |
| 98 | + |
| 99 | + daemonset, ok := obj.(*appsv1.DaemonSet) |
| 100 | + if !ok { |
| 101 | + log.Error(fmt.Errorf("received unexpected type in daemonset watch predicates: %T", obj), "THIS SHOULD NEVER HAPPEN!") |
| 102 | + return false |
| 103 | + } |
| 104 | + |
| 105 | + // determine if this is a blixt daemonset |
| 106 | + matchLabels := daemonset.Spec.Selector.MatchLabels |
| 107 | + app, ok := matchLabels["app"] |
| 108 | + if !ok || app != vars.DefaultDataPlaneAppLabel { |
| 109 | + return false |
| 110 | + } |
| 111 | + |
| 112 | + // verify that it's the dataplane daemonset |
| 113 | + component, ok := matchLabels["component"] |
| 114 | + if !ok || component != vars.DefaultDataPlaneComponentLabel { |
| 115 | + return false |
| 116 | + } |
| 117 | + |
| 118 | + return true |
| 119 | +} |
| 120 | + |
| 121 | +// Reconcile provisions (and de-provisions) resources relevant to this controller. |
| 122 | +func (r *DataplaneReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 123 | + logger := log.FromContext(ctx) |
| 124 | + |
| 125 | + ds := new(appsv1.DaemonSet) |
| 126 | + if err := r.Client.Get(ctx, req.NamespacedName, ds); err != nil { |
| 127 | + if errors.IsNotFound(err) { |
| 128 | + logger.Info("DataplaneReconciler", "reconcile status", "object enqueued no longer exists, skipping") |
| 129 | + return ctrl.Result{}, nil |
| 130 | + } |
| 131 | + return ctrl.Result{}, err |
| 132 | + } |
| 133 | + |
| 134 | + var childPods corev1.PodList |
| 135 | + if err := r.List(ctx, &childPods, client.InNamespace(req.Namespace), client.MatchingFields{podOwnerKey: req.Name}); err != nil { |
| 136 | + logger.Error(err, "DataplaneReconciler", "reconcile status", "unable to list child pods") |
| 137 | + return ctrl.Result{}, err |
| 138 | + } |
| 139 | + |
| 140 | + readyPodByNN := make(map[types.NamespacedName]corev1.Pod) |
| 141 | + for _, pod := range childPods.Items { |
| 142 | + for _, container := range pod.Status.ContainerStatuses { |
| 143 | + if container.Name == vars.DefaultDataPlaneComponentLabel && container.Ready { |
| 144 | + key := types.NamespacedName{Namespace: pod.Namespace, Name: pod.Name} |
| 145 | + readyPodByNN[key] = pod |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + logger.Info("DataplaneReconciler", "reconcile status", "setting updated backends client list", "num ready pods", len(readyPodByNN)) |
| 151 | + updated, err := r.backendsClientManager.SetClientsList(readyPodByNN) |
| 152 | + if updated { |
| 153 | + logger.Info("DataplaneReconciler", "reconcile status", "backends client list updated, sending generic event") |
| 154 | + select { |
| 155 | + case r.updates <- event.GenericEvent{Object: ds}: |
| 156 | + logger.Info("DataplaneReconciler", "reconcile status", "generic event sent") |
| 157 | + default: |
| 158 | + logger.Info("DataplaneReconciler", "reconcile status", "generic event skipped - channel is full") |
| 159 | + } |
| 160 | + } |
| 161 | + if err != nil { |
| 162 | + logger.Error(err, "DataplaneReconciler", "reconcile status", "partial failure for backends client list update") |
| 163 | + return ctrl.Result{Requeue: true}, err |
| 164 | + } |
| 165 | + |
| 166 | + logger.Info("DataplaneReconciler", "reconcile status", "done") |
| 167 | + return ctrl.Result{}, nil |
| 168 | +} |
| 169 | + |
| 170 | +func (r *DataplaneReconciler) GetUpdates() <-chan event.GenericEvent { |
| 171 | + return r.updates |
| 172 | +} |
0 commit comments