Skip to content

Commit 0c37525

Browse files
authoredJun 17, 2024
refactory node-label (#19909)
1 parent 518115c commit 0c37525

File tree

4 files changed

+36
-71
lines changed

4 files changed

+36
-71
lines changed
 

Diff for: ‎components/node-labeler/cmd/metrics.go

-29
This file was deleted.

Diff for: ‎components/node-labeler/cmd/run.go

+32-32
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"strings"
1515
"time"
1616

17-
"github.com/bombsimon/logrusr/v2"
17+
"github.com/bombsimon/logrusr/v4"
1818
"github.com/spf13/cobra"
1919
corev1 "k8s.io/api/core/v1"
2020
"k8s.io/apimachinery/pkg/api/errors"
@@ -32,13 +32,13 @@ import (
3232
"sigs.k8s.io/controller-runtime/pkg/client"
3333
"sigs.k8s.io/controller-runtime/pkg/controller"
3434
"sigs.k8s.io/controller-runtime/pkg/healthz"
35-
"sigs.k8s.io/controller-runtime/pkg/metrics"
3635
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3736
"sigs.k8s.io/controller-runtime/pkg/predicate"
3837
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3938
"sigs.k8s.io/controller-runtime/pkg/webhook"
4039

4140
"github.com/gitpod-io/gitpod/common-go/log"
41+
"github.com/gitpod-io/gitpod/components/scrubber"
4242
)
4343

4444
const (
@@ -56,7 +56,9 @@ var runCmd = &cobra.Command{
5656
Use: "run",
5757
Short: "Starts the node labeler",
5858
Run: func(cmd *cobra.Command, args []string) {
59-
ctrl.SetLogger(logrusr.New(log.Log))
59+
ctrl.SetLogger(logrusr.New(log.Log, logrusr.WithFormatter(func(i interface{}) interface{} {
60+
return &log.TrustedValueWrap{Value: scrubber.Default.DeepCopyStruct(i)}
61+
})))
6062

6163
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
6264
Scheme: scheme,
@@ -110,9 +112,6 @@ var runCmd = &cobra.Command{
110112
log.WithError(err).Fatal("unable to bind controller watch event handler")
111113
}
112114

113-
metrics.Registry.MustRegister(NodeLabelerCounterVec)
114-
metrics.Registry.MustRegister(NodeLabelerTimeHistVec)
115-
116115
err = mgr.AddHealthzCheck("healthz", healthz.Ping)
117116
if err != nil {
118117
log.WithError(err).Fatal("unable to set up health check")
@@ -204,48 +203,49 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req reconcile.Request) (r
204203
return reconcile.Result{}, err
205204
}
206205

207-
if !IsPodReady(pod) {
208-
// not ready. Wait until the next update.
209-
return reconcile.Result{}, nil
210-
}
211-
212206
var node corev1.Node
213207
err = r.Get(ctx, types.NamespacedName{Name: nodeName}, &node)
214208
if err != nil {
215209
return reconcile.Result{}, fmt.Errorf("obtaining node %s: %w", nodeName, err)
216210
}
217211

218-
if labelValue, exists := node.Labels[labelToUpdate]; exists && labelValue == "true" {
219-
// nothing to do, the label already exists.
220-
return reconcile.Result{}, nil
221-
}
212+
isReady, needRequeue := func() (bool, bool) {
213+
if !IsPodReady(pod) {
214+
return false, false
215+
}
216+
err = checkTCPPortIsReachable(ipAddress, port)
217+
if err != nil {
218+
log.WithField("host", ipAddress).WithField("port", port).WithField("pod", pod.Name).WithError(err).Error("checking if TCP port is open")
219+
return false, true
220+
}
222221

223-
err = checkTCPPortIsReachable(ipAddress, port)
224-
if err != nil {
225-
log.WithField("host", ipAddress).WithField("port", port).WithField("pod", pod.Name).WithError(err).Error("checking if TCP port is open")
226-
return reconcile.Result{RequeueAfter: defaultRequeueTime}, nil
227-
}
222+
if component == registryFacade {
223+
err = checkRegistryFacade(ipAddress, port)
224+
if err != nil {
225+
log.WithError(err).Error("checking registry-facade")
226+
return false, true
227+
}
228228

229-
if component == registryFacade {
230-
err = checkRegistryFacade(ipAddress, port)
231-
if err != nil {
232-
log.WithError(err).Error("checking registry-facade")
233-
return reconcile.Result{RequeueAfter: defaultRequeueTime}, nil
229+
time.Sleep(1 * time.Second)
234230
}
231+
return true, false
232+
}()
235233

236-
time.Sleep(1 * time.Second)
234+
_, nodeLabelExists := node.Labels[labelToUpdate]
235+
236+
if isReady && nodeLabelExists || !isReady && !nodeLabelExists {
237+
return reconcile.Result{}, nil
237238
}
238239

239-
err = updateLabel(labelToUpdate, true, nodeName, r)
240+
err = updateLabel(labelToUpdate, isReady, nodeName, r)
240241
if err != nil {
241242
log.WithError(err).Error("updating node label")
242-
return reconcile.Result{}, fmt.Errorf("trying to add the label: %v", err)
243+
return reconcile.Result{}, fmt.Errorf("trying to modify the label: %v", err)
243244
}
244245

245-
readyIn := time.Since(pod.Status.StartTime.Time)
246-
NodeLabelerTimeHistVec.WithLabelValues(component).Observe(readyIn.Seconds())
247-
NodeLabelerCounterVec.WithLabelValues(component).Inc()
248-
246+
if needRequeue {
247+
return reconcile.Result{RequeueAfter: defaultRequeueTime}, nil
248+
}
249249
return reconcile.Result{}, nil
250250
}
251251

Diff for: ‎components/node-labeler/go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ module github.com/gitpod-io/gitpod/node-labeler
33
go 1.22
44

55
require (
6-
github.com/bombsimon/logrusr/v2 v2.0.1
6+
github.com/bombsimon/logrusr/v4 v4.1.0
77
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
8+
github.com/gitpod-io/gitpod/components/scrubber v0.0.0-00010101000000-000000000000
89
github.com/prometheus/client_golang v1.19.0
910
github.com/spf13/cobra v1.7.0
1011
k8s.io/api v0.29.3
@@ -21,7 +22,6 @@ require (
2122
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
2223
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
2324
github.com/fsnotify/fsnotify v1.7.0 // indirect
24-
github.com/gitpod-io/gitpod/components/scrubber v0.0.0-00010101000000-000000000000 // indirect
2525
github.com/go-logr/logr v1.4.1 // indirect
2626
github.com/go-openapi/jsonpointer v0.19.6 // indirect
2727
github.com/go-openapi/jsonreference v0.20.2 // indirect

Diff for: ‎components/node-labeler/go.sum

+2-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.