Skip to content

Commit 07182a1

Browse files
authored
Revert "refactory node-label (#19909)" (#19977)
This reverts commit 0c37525.
1 parent b06f861 commit 07182a1

File tree

4 files changed

+71
-36
lines changed

4 files changed

+71
-36
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License.AGPL.txt in the project root for license information.
4+
5+
package cmd
6+
7+
import "github.com/prometheus/client_golang/prometheus"
8+
9+
const (
10+
metricsNamespace = "gitpod"
11+
metricsWorkspaceSubsystem = "node_labeler"
12+
)
13+
14+
var (
15+
NodeLabelerCounterVec = prometheus.NewCounterVec(prometheus.CounterOpts{
16+
Namespace: metricsNamespace,
17+
Subsystem: metricsWorkspaceSubsystem,
18+
Name: "reconcile_total",
19+
Help: "Total number of reconciliations per component",
20+
}, []string{"component"})
21+
22+
NodeLabelerTimeHistVec = prometheus.NewHistogramVec(prometheus.HistogramOpts{
23+
Namespace: metricsNamespace,
24+
Subsystem: metricsWorkspaceSubsystem,
25+
Name: "ready_seconds",
26+
Help: "time it took for a pods to reach the running phase and the ready label was applied to the node",
27+
Buckets: []float64{5, 10, 15, 20, 25, 30, 45, 60, 75},
28+
}, []string{"component"})
29+
)

components/node-labeler/cmd/run.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"strings"
1515
"time"
1616

17-
"github.com/bombsimon/logrusr/v4"
17+
"github.com/bombsimon/logrusr/v2"
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"
3536
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3637
"sigs.k8s.io/controller-runtime/pkg/predicate"
3738
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3839
"sigs.k8s.io/controller-runtime/pkg/webhook"
3940

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

4444
const (
@@ -56,9 +56,7 @@ 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, logrusr.WithFormatter(func(i interface{}) interface{} {
60-
return &log.TrustedValueWrap{Value: scrubber.Default.DeepCopyStruct(i)}
61-
})))
59+
ctrl.SetLogger(logrusr.New(log.Log))
6260

6361
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
6462
Scheme: scheme,
@@ -112,6 +110,9 @@ var runCmd = &cobra.Command{
112110
log.WithError(err).Fatal("unable to bind controller watch event handler")
113111
}
114112

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

207+
if !IsPodReady(pod) {
208+
// not ready. Wait until the next update.
209+
return reconcile.Result{}, nil
210+
}
211+
206212
var node corev1.Node
207213
err = r.Get(ctx, types.NamespacedName{Name: nodeName}, &node)
208214
if err != nil {
209215
return reconcile.Result{}, fmt.Errorf("obtaining node %s: %w", nodeName, err)
210216
}
211217

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-
}
218+
if labelValue, exists := node.Labels[labelToUpdate]; exists && labelValue == "true" {
219+
// nothing to do, the label already exists.
220+
return reconcile.Result{}, nil
221+
}
221222

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-
}
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+
}
228228

229-
time.Sleep(1 * time.Second)
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
230234
}
231-
return true, false
232-
}()
233-
234-
_, nodeLabelExists := node.Labels[labelToUpdate]
235235

236-
if isReady && nodeLabelExists || !isReady && !nodeLabelExists {
237-
return reconcile.Result{}, nil
236+
time.Sleep(1 * time.Second)
238237
}
239238

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

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

components/node-labeler/go.mod

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

55
require (
6-
github.com/bombsimon/logrusr/v4 v4.1.0
6+
github.com/bombsimon/logrusr/v2 v2.0.1
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
98
github.com/prometheus/client_golang v1.19.0
109
github.com/spf13/cobra v1.7.0
1110
k8s.io/api v0.29.3
@@ -22,6 +21,7 @@ require (
2221
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
2322
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
2423
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

components/node-labeler/go.sum

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)