Skip to content

Commit e222edf

Browse files
committed
Allow configuring the kubernetes rest client timeout for draining nodes
1 parent ae9464d commit e222edf

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

controllers/alias.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package controllers
1818

1919
import (
2020
"context"
21+
"time"
2122

2223
ctrl "sigs.k8s.io/controller-runtime"
2324
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -67,6 +68,9 @@ type MachineReconciler struct {
6768

6869
// WatchFilterValue is the label value used to filter events prior to reconciliation.
6970
WatchFilterValue string
71+
72+
// NodeDrainClientTimeout timeout of the client used for draining nodes.
73+
NodeDrainClientTimeout time.Duration
7074
}
7175

7276
func (r *MachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
@@ -76,6 +80,7 @@ func (r *MachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manag
7680
APIReader: r.APIReader,
7781
Tracker: r.Tracker,
7882
WatchFilterValue: r.WatchFilterValue,
83+
NodeDrainClientTimeout: r.NodeDrainClientTimeout,
7984
}).SetupWithManager(ctx, mgr, options)
8085
}
8186

internal/controllers/machine/machine_controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
kerrors "k8s.io/apimachinery/pkg/util/errors"
3131
"k8s.io/apimachinery/pkg/util/wait"
3232
"k8s.io/client-go/kubernetes"
33+
"k8s.io/client-go/rest"
3334
"k8s.io/client-go/tools/record"
3435
"k8s.io/klog/v2"
3536
kubedrain "k8s.io/kubectl/pkg/drain"
@@ -81,6 +82,9 @@ type Reconciler struct {
8182
// WatchFilterValue is the label value used to filter events prior to reconciliation.
8283
WatchFilterValue string
8384

85+
// NodeDrainClientTimeout timeout of the client used for draining nodes.
86+
NodeDrainClientTimeout time.Duration
87+
8488
controller controller.Controller
8589
recorder record.EventRecorder
8690
externalTracker external.ObjectTracker
@@ -602,6 +606,8 @@ func (r *Reconciler) drainNode(ctx context.Context, cluster *clusterv1.Cluster,
602606
log.Error(err, "Error creating a remote client while deleting Machine, won't retry")
603607
return ctrl.Result{}, nil
604608
}
609+
restConfig = rest.CopyConfig(restConfig)
610+
restConfig.Timeout = r.NodeDrainClientTimeout
605611
kubeClient, err := kubernetes.NewForConfig(restConfig)
606612
if err != nil {
607613
log.Error(err, "Error creating a remote client while deleting Machine, won't retry")

main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919

2020
import (
2121
"context"
22+
"errors"
2223
"flag"
2324
"fmt"
2425
"os"
@@ -103,6 +104,7 @@ var (
103104
syncPeriod time.Duration
104105
restConfigQPS float32
105106
restConfigBurst int
107+
nodeDrainClientTimeout time.Duration
106108
webhookPort int
107109
webhookCertDir string
108110
healthAddr string
@@ -205,6 +207,9 @@ func InitFlags(fs *pflag.FlagSet) {
205207
fs.IntVar(&restConfigBurst, "kube-api-burst", 30,
206208
"Maximum number of queries that should be allowed in one burst from the controller client to the Kubernetes API server. Default 30")
207209

210+
fs.DurationVar(&nodeDrainClientTimeout, "node-drain-client-timeout-duration", time.Second*10,
211+
"The timeout of the client used for draining nodes. Defaults to 10s")
212+
208213
fs.IntVar(&webhookPort, "webhook-port", 9443,
209214
"Webhook Server port")
210215

@@ -238,6 +243,11 @@ func main() {
238243
restConfig.Burst = restConfigBurst
239244
restConfig.UserAgent = remote.DefaultClusterAPIUserAgent(controllerName)
240245

246+
if nodeDrainClientTimeout <= 0 {
247+
setupLog.Error(errors.New("node drain client timeout must be greater than zero"), "unable to start manager")
248+
os.Exit(1)
249+
}
250+
241251
minVer := version.MinimumKubernetesVersion
242252
if feature.Gates.Enabled(feature.ClusterTopology) {
243253
minVer = version.MinimumKubernetesVersionClusterTopology
@@ -475,6 +485,7 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
475485
APIReader: mgr.GetAPIReader(),
476486
Tracker: tracker,
477487
WatchFilterValue: watchFilterValue,
488+
NodeDrainClientTimeout: nodeDrainClientTimeout,
478489
}).SetupWithManager(ctx, mgr, concurrency(machineConcurrency)); err != nil {
479490
setupLog.Error(err, "unable to create controller", "controller", "Machine")
480491
os.Exit(1)

0 commit comments

Comments
 (0)