Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit b2130fb

Browse files
committed
chore: add our previous go control-plane
Adding this back before the repository is archived. Signed-off-by: Shane Utt <[email protected]>
1 parent 95ed451 commit b2130fb

20 files changed

+3814
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ bin/** linguist-detectable=false
33
config/** linguist-detectable=false
44
tools/** linguist-detectable=false
55
Makefile linguist-detectable=false
6+
old-golang-control-plane/** linguist-detectable=false
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package controllers contains the controllers which manage the lifecycle of
18+
// the resources relevant to the operator, such as DataPlanes and ControlPlane
19+
// resources.
20+
package controllers

0 commit comments

Comments
 (0)