Skip to content

Commit 6ad173f

Browse files
committed
Allow using kube-apiserver cache for pod list requests
1 parent 45c378d commit 6ad173f

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

pkg/config/config.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ const (
8888
uptimeFromFileDefault = ""
8989
workersConfigKey = "WORKERS"
9090
workersDefault = 10
91+
useAPIServerCache = "USE_APISERVER_CACHE"
9192
// prometheus
9293
enablePrometheusDefault = false
9394
enablePrometheusConfigKey = "ENABLE_PROMETHEUS_SERVER"
@@ -161,6 +162,7 @@ type Config struct {
161162
UseProviderId bool
162163
CompleteLifecycleActionDelaySeconds int
163164
DeleteSqsMsgIfNodeNotFound bool
165+
UseAPIServerCacheToListPods bool
164166
}
165167

166168
// ParseCliArgs parses cli arguments and uses environment variables as fallback values
@@ -223,6 +225,7 @@ func ParseCliArgs() (config Config, err error) {
223225
flag.BoolVar(&config.UseProviderId, "use-provider-id", getBoolEnv(useProviderIdConfigKey, useProviderIdDefault), "If true, fetch node name through Kubernetes node spec ProviderID instead of AWS event PrivateDnsHostname.")
224226
flag.IntVar(&config.CompleteLifecycleActionDelaySeconds, "complete-lifecycle-action-delay-seconds", getIntEnv(completeLifecycleActionDelaySecondsKey, -1), "Delay completing the Autoscaling lifecycle action after a node has been drained.")
225227
flag.BoolVar(&config.DeleteSqsMsgIfNodeNotFound, "delete-sqs-msg-if-node-not-found", getBoolEnv(deleteSqsMsgIfNodeNotFoundKey, false), "If true, delete SQS Messages from the SQS Queue if the targeted node(s) are not found.")
228+
flag.BoolVar(&config.UseAPIServerCacheToListPods, "use-apiserver-cache", getBoolEnv(useAPIServerCache, false), "If true, leverage the k8s apiserver's index on pod's spec.nodeName to list pods on a node, instead of doing an etcd quorum read.")
226229
flag.Parse()
227230

228231
if isConfigProvided("pod-termination-grace-period", podTerminationGracePeriodConfigKey) && isConfigProvided("grace-period", gracePeriodConfigKey) {
@@ -324,6 +327,7 @@ func (c Config) PrintJsonConfigArgs() {
324327
Bool("check_tag_before_draining", c.CheckTagBeforeDraining).
325328
Str("ManagedTag", c.ManagedTag).
326329
Bool("use_provider_id", c.UseProviderId).
330+
Bool("use_apiserver_cache", c.UseAPIServerCacheToListPods).
327331
Msg("aws-node-termination-handler arguments")
328332
}
329333

@@ -374,7 +378,8 @@ func (c Config) PrintHumanConfigArgs() {
374378
"\tcheck-tag-before-draining: %t,\n"+
375379
"\tmanaged-tag: %s,\n"+
376380
"\tuse-provider-id: %t,\n"+
377-
"\taws-endpoint: %s,\n",
381+
"\taws-endpoint: %s,\n"+
382+
"\tuse-apiserver-cache: %t,\n",
378383
c.DryRun,
379384
c.NodeName,
380385
c.PodName,
@@ -414,6 +419,7 @@ func (c Config) PrintHumanConfigArgs() {
414419
c.ManagedTag,
415420
c.UseProviderId,
416421
c.AWSEndpoint,
422+
c.UseAPIServerCacheToListPods,
417423
)
418424
}
419425

pkg/node/node.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,13 @@ func (n Node) fetchAllPods(nodeName string) (*corev1.PodList, error) {
628628
log.Info().Msgf("Would have retrieved running pod list on node %s, but dry-run flag was set", nodeName)
629629
return &corev1.PodList{}, nil
630630
}
631-
return n.drainHelper.Client.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{
631+
listOptions := metav1.ListOptions{
632632
FieldSelector: "spec.nodeName=" + nodeName,
633-
})
633+
}
634+
if n.nthConfig.UseAPIServerCacheToListPods {
635+
listOptions.ResourceVersion = "0"
636+
}
637+
return n.drainHelper.Client.CoreV1().Pods("").List(context.TODO(), listOptions)
634638
}
635639

636640
func getDrainHelper(nthConfig config.Config, clientset *kubernetes.Clientset) (*drain.Helper, error) {

0 commit comments

Comments
 (0)