Skip to content

Commit

Permalink
refactory node-label (#19909)
Browse files Browse the repository at this point in the history
  • Loading branch information
iQQBot authored Jun 17, 2024
1 parent 518115c commit 0c37525
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 71 deletions.
29 changes: 0 additions & 29 deletions components/node-labeler/cmd/metrics.go

This file was deleted.

64 changes: 32 additions & 32 deletions components/node-labeler/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"strings"
"time"

"github.com/bombsimon/logrusr/v2"
"github.com/bombsimon/logrusr/v4"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -32,13 +32,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/metrics"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/webhook"

"github.com/gitpod-io/gitpod/common-go/log"
"github.com/gitpod-io/gitpod/components/scrubber"
)

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

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

metrics.Registry.MustRegister(NodeLabelerCounterVec)
metrics.Registry.MustRegister(NodeLabelerTimeHistVec)

err = mgr.AddHealthzCheck("healthz", healthz.Ping)
if err != nil {
log.WithError(err).Fatal("unable to set up health check")
Expand Down Expand Up @@ -204,48 +203,49 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req reconcile.Request) (r
return reconcile.Result{}, err
}

if !IsPodReady(pod) {
// not ready. Wait until the next update.
return reconcile.Result{}, nil
}

var node corev1.Node
err = r.Get(ctx, types.NamespacedName{Name: nodeName}, &node)
if err != nil {
return reconcile.Result{}, fmt.Errorf("obtaining node %s: %w", nodeName, err)
}

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

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

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

time.Sleep(1 * time.Second)
_, nodeLabelExists := node.Labels[labelToUpdate]

if isReady && nodeLabelExists || !isReady && !nodeLabelExists {
return reconcile.Result{}, nil
}

err = updateLabel(labelToUpdate, true, nodeName, r)
err = updateLabel(labelToUpdate, isReady, nodeName, r)
if err != nil {
log.WithError(err).Error("updating node label")
return reconcile.Result{}, fmt.Errorf("trying to add the label: %v", err)
return reconcile.Result{}, fmt.Errorf("trying to modify the label: %v", err)
}

readyIn := time.Since(pod.Status.StartTime.Time)
NodeLabelerTimeHistVec.WithLabelValues(component).Observe(readyIn.Seconds())
NodeLabelerCounterVec.WithLabelValues(component).Inc()

if needRequeue {
return reconcile.Result{RequeueAfter: defaultRequeueTime}, nil
}
return reconcile.Result{}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions components/node-labeler/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ module github.com/gitpod-io/gitpod/node-labeler
go 1.22

require (
github.com/bombsimon/logrusr/v2 v2.0.1
github.com/bombsimon/logrusr/v4 v4.1.0
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
github.com/gitpod-io/gitpod/components/scrubber v0.0.0-00010101000000-000000000000
github.com/prometheus/client_golang v1.19.0
github.com/spf13/cobra v1.7.0
k8s.io/api v0.29.3
Expand All @@ -21,7 +22,6 @@ require (
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gitpod-io/gitpod/components/scrubber v0.0.0-00010101000000-000000000000 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand Down
10 changes: 2 additions & 8 deletions components/node-labeler/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0c37525

Please sign in to comment.