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