Skip to content

Commit 000da2a

Browse files
committed
Replace watch with builder.ControllerManagedBy
Signed-off-by: Manuel de Brito Fontes <[email protected]>
1 parent 72a58dd commit 000da2a

File tree

3 files changed

+45
-68
lines changed

3 files changed

+45
-68
lines changed

components/node-labeler/cmd/run.go

Lines changed: 34 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ import (
1818
"github.com/spf13/cobra"
1919
corev1 "k8s.io/api/core/v1"
2020
"k8s.io/apimachinery/pkg/api/errors"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2122
"k8s.io/apimachinery/pkg/runtime"
2223
"k8s.io/apimachinery/pkg/types"
2324
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2425
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
2526
_ "k8s.io/client-go/plugin/pkg/client/auth"
2627
"k8s.io/client-go/util/retry"
28+
"k8s.io/utils/pointer"
2729
ctrl "sigs.k8s.io/controller-runtime"
30+
"sigs.k8s.io/controller-runtime/pkg/builder"
2831
"sigs.k8s.io/controller-runtime/pkg/client"
2932
"sigs.k8s.io/controller-runtime/pkg/controller"
30-
"sigs.k8s.io/controller-runtime/pkg/event"
31-
"sigs.k8s.io/controller-runtime/pkg/handler"
3233
"sigs.k8s.io/controller-runtime/pkg/healthz"
3334
"sigs.k8s.io/controller-runtime/pkg/metrics"
3435
"sigs.k8s.io/controller-runtime/pkg/predicate"
3536
"sigs.k8s.io/controller-runtime/pkg/reconcile"
36-
"sigs.k8s.io/controller-runtime/pkg/source"
3737

3838
"github.com/gitpod-io/gitpod/common-go/log"
3939
)
@@ -60,6 +60,10 @@ var runCmd = &cobra.Command{
6060
LeaderElection: true,
6161
LeaderElectionID: "node-labeler.gitpod.io",
6262
Namespace: namespace,
63+
// default sync period is 10h.
64+
// in case node-labeler is restarted and not change happens, we could waste (at least) 20m in a node
65+
// that never will run workspaces and the additional nodes cluster-autoscaler adds to compensate
66+
SyncPeriod: pointer.Duration(1 * time.Minute),
6367
})
6468
if err != nil {
6569
log.WithError(err).Fatal("unable to start node-labeber")
@@ -74,35 +78,27 @@ var runCmd = &cobra.Command{
7478
client,
7579
}
7680

77-
c, err := controller.New("pod-watcher", mgr, controller.Options{
78-
Reconciler: r,
79-
MaxConcurrentReconciles: 20,
81+
filterPredicate, err := predicate.LabelSelectorPredicate(metav1.LabelSelector{
82+
MatchLabels: map[string]string{
83+
"required-by-node-labeler": "true",
84+
},
8085
})
86+
if err != nil {
87+
log.WithError(err).Fatal("unable to create predicate")
88+
}
89+
90+
err = ctrl.NewControllerManagedBy(mgr).
91+
Named("pod-watcher").
92+
For(&corev1.Pod{}, builder.WithPredicates(filterPredicate)).
93+
WithOptions(controller.Options{MaxConcurrentReconciles: 1}).
94+
Complete(r)
8195
if err != nil {
8296
log.WithError(err).Fatal("unable to bind controller watch event handler")
8397
}
8498

8599
metrics.Registry.MustRegister(NodeLabelerCounterVec)
86100
metrics.Registry.MustRegister(NodeLabelerTimeHistVec)
87101

88-
err = c.Watch(&source.Kind{Type: &corev1.Pod{}}, &handler.EnqueueRequestForObject{}, predicate.Funcs{
89-
CreateFunc: func(ce event.CreateEvent) bool {
90-
return processPodEvent(ce.Object)
91-
},
92-
UpdateFunc: func(ue event.UpdateEvent) bool {
93-
return processPodEvent(ue.ObjectNew)
94-
},
95-
DeleteFunc: func(deleteEvent event.DeleteEvent) bool {
96-
return processPodEvent(deleteEvent.Object)
97-
},
98-
GenericFunc: func(genericEvent event.GenericEvent) bool {
99-
return false
100-
},
101-
})
102-
if err != nil {
103-
log.WithError(err).Fatal("unable to create controller")
104-
}
105-
106102
err = mgr.AddHealthzCheck("healthz", healthz.Ping)
107103
if err != nil {
108104
log.WithError(err).Fatal("unable to set up health check")
@@ -132,14 +128,6 @@ var (
132128
scheme = runtime.NewScheme()
133129
)
134130

135-
func processPodEvent(pod client.Object) bool {
136-
if strings.HasPrefix(pod.GetName(), registryFacade) || strings.HasPrefix(pod.GetName(), wsDaemon) {
137-
return true
138-
}
139-
140-
return false
141-
}
142-
143131
type PodReconciler struct {
144132
client.Client
145133
}
@@ -165,8 +153,6 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req reconcile.Request) (r
165153
port string
166154
component string
167155
labelToUpdate string
168-
169-
waitTimeout time.Duration = 5 * time.Second
170156
)
171157

172158
switch {
@@ -181,7 +167,7 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req reconcile.Request) (r
181167
ipAddress = pod.Status.PodIP
182168
port = strconv.Itoa(wsdaemonPort)
183169
default:
184-
log.WithField("pod", pod.Name).Info("Invalid pod. Skipping...")
170+
// nothing to do
185171
return reconcile.Result{}, nil
186172
}
187173

@@ -215,26 +201,25 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req reconcile.Request) (r
215201
return reconcile.Result{}, fmt.Errorf("obtaining node %s: %w", nodeName, err)
216202
}
217203

218-
if node.Labels[labelToUpdate] == "true" {
219-
// Label already exists.
204+
if labelValue, exists := node.Labels[labelToUpdate]; exists && labelValue == "true" {
205+
// nothing to do, the label already exists.
220206
return reconcile.Result{}, nil
221207
}
222208

223-
err = waitForTCPPortToBeReachable(ipAddress, port, 30*time.Second)
209+
err = checkTCPPortIsReachable(ipAddress, port)
224210
if err != nil {
225-
return reconcile.Result{}, fmt.Errorf("waiting for TCP port: %v", err)
211+
log.WithField("host", ipAddress).WithField("port", port).WithField("pod", pod.Name).WithError(err).Error("checking if TCP port is open")
212+
return reconcile.Result{RequeueAfter: time.Second * 5}, nil
226213
}
227214

228215
if component == registryFacade {
229216
err = checkRegistryFacade(ipAddress, port)
230217
if err != nil {
231218
log.WithError(err).Error("checking registry-facade")
232-
return reconcile.Result{RequeueAfter: time.Second * 10}, nil
219+
return reconcile.Result{RequeueAfter: time.Second * 5}, nil
233220
}
234221
}
235222

236-
time.Sleep(waitTimeout)
237-
238223
err = updateLabel(labelToUpdate, true, nodeName, r)
239224
if err != nil {
240225
return reconcile.Result{}, fmt.Errorf("trying to add the label: %v", err)
@@ -280,31 +265,14 @@ func updateLabel(label string, add bool, nodeName string, client client.Client)
280265
})
281266
}
282267

283-
func waitForTCPPortToBeReachable(host string, port string, timeout time.Duration) error {
284-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
285-
defer cancel()
286-
287-
ticker := time.NewTicker(1 * time.Second)
288-
defer ticker.Stop()
289-
290-
for {
291-
select {
292-
case <-ctx.Done():
293-
return fmt.Errorf("port %v on host %v never reachable", port, host)
294-
case <-ticker.C:
295-
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), 500*time.Millisecond)
296-
if err != nil {
297-
continue
298-
}
299-
300-
if conn != nil {
301-
conn.Close()
302-
return nil
303-
}
304-
305-
continue
306-
}
268+
func checkTCPPortIsReachable(host string, port string) error {
269+
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), 1*time.Second)
270+
if err != nil {
271+
return err
307272
}
273+
defer conn.Close()
274+
275+
return nil
308276
}
309277

310278
func checkRegistryFacade(host, port string) error {

install/installer/pkg/components/registry-facade/daemonset.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ import (
2626
const wsManagerMk2ClientTlsVolume = "ws-manager-mk2-client-tls-certs"
2727

2828
func daemonset(ctx *common.RenderContext) ([]runtime.Object, error) {
29-
labels := common.CustomizeLabel(ctx, Component, common.TypeMetaDaemonset)
29+
labels := common.CustomizeLabel(ctx, Component, common.TypeMetaDaemonset, func() map[string]string {
30+
return map[string]string{
31+
"required-by-node-labeler": "true",
32+
}
33+
})
3034

3135
var hashObj []runtime.Object
3236
if objs, err := configmap(ctx); err != nil {

install/installer/pkg/components/ws-daemon/daemonset.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ import (
2222

2323
func daemonset(ctx *common.RenderContext) ([]runtime.Object, error) {
2424
cfg := ctx.Config
25-
labels := common.CustomizeLabel(ctx, Component, common.TypeMetaDaemonset)
25+
26+
labels := common.CustomizeLabel(ctx, Component, common.TypeMetaDaemonset, func() map[string]string {
27+
return map[string]string{
28+
"required-by-node-labeler": "true",
29+
}
30+
})
2631

2732
configHash, err := common.ObjectHash(configmap(ctx))
2833
if err != nil {

0 commit comments

Comments
 (0)