@@ -14,7 +14,7 @@ import (
14
14
"strings"
15
15
"time"
16
16
17
- "github.com/bombsimon/logrusr/v4 "
17
+ "github.com/bombsimon/logrusr/v2 "
18
18
"github.com/spf13/cobra"
19
19
corev1 "k8s.io/api/core/v1"
20
20
"k8s.io/apimachinery/pkg/api/errors"
@@ -32,13 +32,13 @@ import (
32
32
"sigs.k8s.io/controller-runtime/pkg/client"
33
33
"sigs.k8s.io/controller-runtime/pkg/controller"
34
34
"sigs.k8s.io/controller-runtime/pkg/healthz"
35
+ "sigs.k8s.io/controller-runtime/pkg/metrics"
35
36
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
36
37
"sigs.k8s.io/controller-runtime/pkg/predicate"
37
38
"sigs.k8s.io/controller-runtime/pkg/reconcile"
38
39
"sigs.k8s.io/controller-runtime/pkg/webhook"
39
40
40
41
"github.com/gitpod-io/gitpod/common-go/log"
41
- "github.com/gitpod-io/gitpod/components/scrubber"
42
42
)
43
43
44
44
const (
@@ -56,9 +56,7 @@ var runCmd = &cobra.Command{
56
56
Use : "run" ,
57
57
Short : "Starts the node labeler" ,
58
58
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 ))
62
60
63
61
mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
64
62
Scheme : scheme ,
@@ -112,6 +110,9 @@ var runCmd = &cobra.Command{
112
110
log .WithError (err ).Fatal ("unable to bind controller watch event handler" )
113
111
}
114
112
113
+ metrics .Registry .MustRegister (NodeLabelerCounterVec )
114
+ metrics .Registry .MustRegister (NodeLabelerTimeHistVec )
115
+
115
116
err = mgr .AddHealthzCheck ("healthz" , healthz .Ping )
116
117
if err != nil {
117
118
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
203
204
return reconcile.Result {}, err
204
205
}
205
206
207
+ if ! IsPodReady (pod ) {
208
+ // not ready. Wait until the next update.
209
+ return reconcile.Result {}, nil
210
+ }
211
+
206
212
var node corev1.Node
207
213
err = r .Get (ctx , types.NamespacedName {Name : nodeName }, & node )
208
214
if err != nil {
209
215
return reconcile.Result {}, fmt .Errorf ("obtaining node %s: %w" , nodeName , err )
210
216
}
211
217
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
+ }
221
222
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
+ }
228
228
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
230
234
}
231
- return true , false
232
- }()
233
-
234
- _ , nodeLabelExists := node .Labels [labelToUpdate ]
235
235
236
- if isReady && nodeLabelExists || ! isReady && ! nodeLabelExists {
237
- return reconcile.Result {}, nil
236
+ time .Sleep (1 * time .Second )
238
237
}
239
238
240
- err = updateLabel (labelToUpdate , isReady , nodeName , r )
239
+ err = updateLabel (labelToUpdate , true , nodeName , r )
241
240
if err != nil {
242
241
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 )
244
243
}
245
244
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
+
249
249
return reconcile.Result {}, nil
250
250
}
251
251
0 commit comments