Skip to content

Commit

Permalink
Allow configuring the kubernetes rest client timeout for draining nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
cnmcavoy committed Jun 26, 2023
1 parent e8eb540 commit fad64b3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions controllers/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllers

import (
"context"
"time"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -64,6 +65,9 @@ type MachineReconciler struct {

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string

// DrainKubeTimeout is the maximum length of time to wait for a response before giving up on kubernetes to drain a node.
DrainKubeTimeout time.Duration
}

func (r *MachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
Expand All @@ -72,6 +76,7 @@ func (r *MachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manag
APIReader: r.APIReader,
Tracker: r.Tracker,
WatchFilterValue: r.WatchFilterValue,
DrainKubeTimeout: r.DrainKubeTimeout,
}).SetupWithManager(ctx, mgr, options)
}

Expand Down
6 changes: 6 additions & 0 deletions internal/controllers/machine/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
kubedrain "k8s.io/kubectl/pkg/drain"
Expand Down Expand Up @@ -80,6 +81,9 @@ type Reconciler struct {
// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string

// DrainKubeTimeout is the maximum length of time to wait for a response before giving up on kubernetes to drain a node.
DrainKubeTimeout time.Duration

controller controller.Controller
recorder record.EventRecorder
externalTracker external.ObjectTracker
Expand Down Expand Up @@ -601,6 +605,8 @@ func (r *Reconciler) drainNode(ctx context.Context, cluster *clusterv1.Cluster,
log.Error(err, "Error creating a remote client while deleting Machine, won't retry")
return ctrl.Result{}, nil
}
restConfig = rest.CopyConfig(restConfig)
restConfig.Timeout = r.DrainKubeTimeout
kubeClient, err := kubernetes.NewForConfig(restConfig)
if err != nil {
log.Error(err, "Error creating a remote client while deleting Machine, won't retry")
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ var (
syncPeriod time.Duration
restConfigQPS float32
restConfigBurst int
drainKubeTimeout time.Duration
webhookPort int
webhookCertDir string
healthAddr string
Expand Down Expand Up @@ -198,6 +199,9 @@ func InitFlags(fs *pflag.FlagSet) {
fs.IntVar(&restConfigBurst, "kube-api-burst", 30,
"Maximum number of queries that should be allowed in one burst from the controller client to the Kubernetes API server. Default 30")

fs.DurationVar(&drainKubeTimeout, "drain-api-timeout", time.Second*10,
"The maximum length of time to wait for a response before giving up on kubernetes to drain a node. A value of zero means no timeout.")

fs.IntVar(&webhookPort, "webhook-port", 9443,
"Webhook Server port")

Expand Down Expand Up @@ -229,6 +233,7 @@ func main() {
restConfig := ctrl.GetConfigOrDie()
restConfig.QPS = restConfigQPS
restConfig.Burst = restConfigBurst
restConfig.Timeout = drainKubeTimeout
restConfig.UserAgent = remote.DefaultClusterAPIUserAgent(controllerName)

minVer := version.MinimumKubernetesVersion
Expand Down

0 comments on commit fad64b3

Please sign in to comment.