From 2e8ace82ebd4d088bb9004173b24b0ace5530982 Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Sun, 20 Oct 2019 16:14:49 -0400 Subject: [PATCH] replicaset: Ignore namespace termination errors when creating pods Instead of reporting an event or displaying an error, simply exit when the namespace is being terminated. This reduces the amount of controller churn on namespace shutdown. While we could technically exit the entire processing loop early for very large replica sets, we should wait for more evidence that is an issue before changing that logic substantially. --- pkg/controller/controller_utils.go | 5 ++++- pkg/controller/replicaset/replica_set.go | 25 +++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/pkg/controller/controller_utils.go b/pkg/controller/controller_utils.go index 17fcf98f9a7..9e57264875a 100644 --- a/pkg/controller/controller_utils.go +++ b/pkg/controller/controller_utils.go @@ -577,7 +577,10 @@ func (r RealPodControl) createPods(nodeName, namespace string, template *v1.PodT } newPod, err := r.KubeClient.CoreV1().Pods(namespace).Create(pod) if err != nil { - r.Recorder.Eventf(object, v1.EventTypeWarning, FailedCreatePodReason, "Error creating: %v", err) + // only send an event if the namespace isn't terminating + if !apierrors.HasStatusCause(err, v1.NamespaceTerminatingCause) { + r.Recorder.Eventf(object, v1.EventTypeWarning, FailedCreatePodReason, "Error creating: %v", err) + } return err } accessor, err := meta.Accessor(object) diff --git a/pkg/controller/replicaset/replica_set.go b/pkg/controller/replicaset/replica_set.go index 6727df358bb..37b51df9158 100644 --- a/pkg/controller/replicaset/replica_set.go +++ b/pkg/controller/replicaset/replica_set.go @@ -523,15 +523,22 @@ func (rsc *ReplicaSetController) manageReplicas(filteredPods []*v1.Pod, rs *apps // event spam that those failures would generate. successfulCreations, err := slowStartBatch(diff, controller.SlowStartInitialBatchSize, func() error { err := rsc.podControl.CreatePodsWithControllerRef(rs.Namespace, &rs.Spec.Template, rs, metav1.NewControllerRef(rs, rsc.GroupVersionKind)) - if err != nil && errors.IsTimeout(err) { - // Pod is created but its initialization has timed out. - // If the initialization is successful eventually, the - // controller will observe the creation via the informer. - // If the initialization fails, or if the pod keeps - // uninitialized for a long time, the informer will not - // receive any update, and the controller will create a new - // pod when the expectation expires. - return nil + if err != nil { + if errors.HasStatusCause(err, v1.NamespaceTerminatingCause) { + // if the namespace is being terminated, we don't have to do + // anything because any creation will fail + return nil + } + if errors.IsTimeout(err) { + // Pod is created but its initialization has timed out. + // If the initialization is successful eventually, the + // controller will observe the creation via the informer. + // If the initialization fails, or if the pod keeps + // uninitialized for a long time, the informer will not + // receive any update, and the controller will create a new + // pod when the expectation expires. + return nil + } } return err })