Skip to content

Commit d2560e2

Browse files
authored
Improve node-labeler (#17006)
* Improve node-labeler Signed-off-by: Manuel de Brito Fontes <[email protected]> * Requeue instead of waiting 0.blocking Signed-off-by: Manuel de Brito Fontes <[email protected]> * Requeue Signed-off-by: Manuel de Brito Fontes <[email protected]> * Cleanup error messages Signed-off-by: Manuel de Brito Fontes <[email protected]> --------- Signed-off-by: Manuel de Brito Fontes <[email protected]>
1 parent 2cfe264 commit d2560e2

File tree

1 file changed

+60
-8
lines changed
  • components/node-labeler/cmd

1 file changed

+60
-8
lines changed

components/node-labeler/cmd/run.go

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ package cmd
66

77
import (
88
"context"
9+
"crypto/tls"
910
"fmt"
1011
"net"
12+
"net/http"
1113
"strconv"
1214
"strings"
1315
"time"
@@ -164,7 +166,7 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req reconcile.Request) (r
164166
component string
165167
labelToUpdate string
166168

167-
waitTimeout time.Duration = 1 * time.Second
169+
waitTimeout time.Duration = 5 * time.Second
168170
)
169171

170172
switch {
@@ -173,8 +175,6 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req reconcile.Request) (r
173175
labelToUpdate = fmt.Sprintf(registryFacadeLabel, namespace)
174176
ipAddress = pod.Status.HostIP
175177
port = strconv.Itoa(registryFacadePort)
176-
// wait for kube-proxy sync to avoid connection refused due to missing nodeport rules
177-
waitTimeout = 15 * time.Second
178178
case strings.HasPrefix(pod.Name, wsDaemon):
179179
component = wsDaemon
180180
labelToUpdate = fmt.Sprintf(wsdaemonLabel, namespace)
@@ -197,7 +197,7 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req reconcile.Request) (r
197197
return reconcile.Result{}, nil
198198
}
199199

200-
log.WithError(err).Error("unexpected error removing node label")
200+
log.WithError(err).Error("removing node label")
201201
return reconcile.Result{RequeueAfter: time.Second * 10}, err
202202
}
203203

@@ -206,14 +206,13 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req reconcile.Request) (r
206206

207207
if !IsPodReady(&pod) {
208208
// not ready. Wait until the next update.
209-
log.WithField("pod", pod.Name).Info("pod is not yet ready")
210209
return reconcile.Result{}, nil
211210
}
212211

213212
var node corev1.Node
214213
err = r.Get(ctx, types.NamespacedName{Name: nodeName}, &node)
215214
if err != nil {
216-
return reconcile.Result{}, fmt.Errorf("failed to get node %s: %w", nodeName, err)
215+
return reconcile.Result{}, fmt.Errorf("obtaining node %s: %w", nodeName, err)
217216
}
218217

219218
if node.Labels[labelToUpdate] == "true" {
@@ -223,14 +222,22 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req reconcile.Request) (r
223222

224223
err = waitForTCPPortToBeReachable(ipAddress, port, 30*time.Second)
225224
if err != nil {
226-
return reconcile.Result{}, fmt.Errorf("Unexpected error waiting for probe URL: %v", err)
225+
return reconcile.Result{}, fmt.Errorf("waiting for TCP port: %v", err)
226+
}
227+
228+
if component == registryFacade {
229+
err = checkRegistryFacade(ipAddress, port)
230+
if err != nil {
231+
log.WithError(err).Error("checking registry-facade")
232+
return reconcile.Result{RequeueAfter: time.Second * 10}, nil
233+
}
227234
}
228235

229236
time.Sleep(waitTimeout)
230237

231238
err = updateLabel(labelToUpdate, true, nodeName, r)
232239
if err != nil {
233-
return reconcile.Result{}, fmt.Errorf("Unexpected error while trying to add the label: %v", err)
240+
return reconcile.Result{}, fmt.Errorf("trying to add the label: %v", err)
234241
}
235242

236243
readyIn := time.Since(pod.Status.StartTime.Time)
@@ -299,3 +306,48 @@ func waitForTCPPortToBeReachable(host string, port string, timeout time.Duration
299306
}
300307
}
301308
}
309+
310+
func checkRegistryFacade(host, port string) error {
311+
transport := newDefaultTransport()
312+
transport.TLSClientConfig = &tls.Config{
313+
InsecureSkipVerify: true,
314+
}
315+
316+
client := &http.Client{
317+
Transport: transport,
318+
}
319+
320+
dummyURL := fmt.Sprintf("https://%v:%v/v2/remote/not-a-valid-image/manifests/latest", host, port)
321+
req, err := http.NewRequest(http.MethodGet, dummyURL, nil)
322+
if err != nil {
323+
return fmt.Errorf("building HTTP request: %v", err)
324+
}
325+
326+
req.Header.Set("Accept", "application/vnd.oci.image.manifest.v1+json, application/vnd.oci.image.index.v1+json")
327+
resp, err := client.Do(req)
328+
if err != nil {
329+
return fmt.Errorf("unexpected error during HTTP request: %v", err)
330+
}
331+
resp.Body.Close()
332+
333+
if resp.StatusCode == http.StatusNotFound {
334+
return nil
335+
}
336+
337+
return fmt.Errorf("registry-facade is not ready yet")
338+
}
339+
340+
func newDefaultTransport() *http.Transport {
341+
return &http.Transport{
342+
DialContext: (&net.Dialer{
343+
Timeout: 1 * time.Second,
344+
KeepAlive: 1 * time.Second,
345+
DualStack: false,
346+
}).DialContext,
347+
MaxIdleConns: 0,
348+
MaxIdleConnsPerHost: 1,
349+
IdleConnTimeout: 5 * time.Second,
350+
ExpectContinueTimeout: 5 * time.Second,
351+
DisableKeepAlives: true,
352+
}
353+
}

0 commit comments

Comments
 (0)