Merge pull request #113787 from gjkim42/update-daemonset-status-despite-error
Update daemonSet status even if syncDaemonSet fails
This commit is contained in:
@@ -39,10 +39,12 @@ import (
|
||||
"k8s.io/client-go/tools/events"
|
||||
"k8s.io/client-go/util/flowcontrol"
|
||||
"k8s.io/client-go/util/retry"
|
||||
"k8s.io/kubernetes/cmd/kube-apiserver/app/options"
|
||||
kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
|
||||
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
|
||||
"k8s.io/kubernetes/pkg/controller"
|
||||
"k8s.io/kubernetes/pkg/controller/daemon"
|
||||
"k8s.io/kubernetes/pkg/controlplane"
|
||||
"k8s.io/kubernetes/pkg/scheduler"
|
||||
"k8s.io/kubernetes/pkg/scheduler/profile"
|
||||
labelsutil "k8s.io/kubernetes/pkg/util/labels"
|
||||
@@ -52,14 +54,26 @@ import (
|
||||
var zero = int64(0)
|
||||
|
||||
func setup(t *testing.T) (context.Context, kubeapiservertesting.TearDownFunc, *daemon.DaemonSetsController, informers.SharedInformerFactory, clientset.Interface) {
|
||||
// Disable ServiceAccount admission plugin as we don't have serviceaccount controller running.
|
||||
server := kubeapiservertesting.StartTestServerOrDie(t, nil, []string{"--disable-admission-plugins=ServiceAccount,TaintNodesByCondition"}, framework.SharedEtcd())
|
||||
return setupWithServerSetup(t, framework.TestServerSetup{})
|
||||
}
|
||||
|
||||
config := restclient.CopyConfig(server.ClientConfig)
|
||||
clientSet, err := clientset.NewForConfig(config)
|
||||
if err != nil {
|
||||
t.Fatalf("Error in creating clientset: %v", err)
|
||||
func setupWithServerSetup(t *testing.T, serverSetup framework.TestServerSetup) (context.Context, kubeapiservertesting.TearDownFunc, *daemon.DaemonSetsController, informers.SharedInformerFactory, clientset.Interface) {
|
||||
modifyServerRunOptions := serverSetup.ModifyServerRunOptions
|
||||
serverSetup.ModifyServerRunOptions = func(opts *options.ServerRunOptions) {
|
||||
if modifyServerRunOptions != nil {
|
||||
modifyServerRunOptions(opts)
|
||||
}
|
||||
|
||||
opts.Admission.GenericAdmission.DisablePlugins = append(opts.Admission.GenericAdmission.DisablePlugins,
|
||||
// Disable ServiceAccount admission plugin as we don't have
|
||||
// serviceaccount controller running.
|
||||
"ServiceAccount",
|
||||
"TaintNodesByCondition",
|
||||
)
|
||||
}
|
||||
|
||||
clientSet, config, closeFn := framework.StartTestServer(t, serverSetup)
|
||||
|
||||
resyncPeriod := 12 * time.Hour
|
||||
informers := informers.NewSharedInformerFactory(clientset.NewForConfigOrDie(restclient.AddUserAgent(config, "daemonset-informers")), resyncPeriod)
|
||||
dc, err := daemon.NewDaemonSetsController(
|
||||
@@ -96,7 +110,7 @@ func setup(t *testing.T) (context.Context, kubeapiservertesting.TearDownFunc, *d
|
||||
|
||||
tearDownFn := func() {
|
||||
cancel()
|
||||
server.TearDownFn()
|
||||
closeFn()
|
||||
eventBroadcaster.Shutdown()
|
||||
}
|
||||
|
||||
@@ -999,3 +1013,40 @@ func TestUnschedulableNodeDaemonDoesLaunchPod(t *testing.T) {
|
||||
validateDaemonSetStatus(dsClient, ds.Name, 2, t)
|
||||
})
|
||||
}
|
||||
|
||||
func TestUpdateStatusDespitePodCreationFailure(t *testing.T) {
|
||||
forEachStrategy(t, func(t *testing.T, strategy *apps.DaemonSetUpdateStrategy) {
|
||||
limitedPodNumber := 2
|
||||
ctx, closeFn, dc, informers, clientset := setupWithServerSetup(t, framework.TestServerSetup{
|
||||
ModifyServerConfig: func(config *controlplane.Config) {
|
||||
config.GenericConfig.AdmissionControl = &fakePodFailAdmission{
|
||||
limitedPodNumber: limitedPodNumber,
|
||||
}
|
||||
},
|
||||
})
|
||||
defer closeFn()
|
||||
ns := framework.CreateNamespaceOrDie(clientset, "update-status-despite-pod-failure", t)
|
||||
defer framework.DeleteNamespaceOrDie(clientset, ns, t)
|
||||
|
||||
dsClient := clientset.AppsV1().DaemonSets(ns.Name)
|
||||
podClient := clientset.CoreV1().Pods(ns.Name)
|
||||
nodeClient := clientset.CoreV1().Nodes()
|
||||
podInformer := informers.Core().V1().Pods().Informer()
|
||||
|
||||
informers.Start(ctx.Done())
|
||||
go dc.Run(ctx, 2)
|
||||
|
||||
ds := newDaemonSet("foo", ns.Name)
|
||||
ds.Spec.UpdateStrategy = *strategy
|
||||
_, err := dsClient.Create(context.TODO(), ds, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create DaemonSet: %v", err)
|
||||
}
|
||||
defer cleanupDaemonSets(t, clientset, ds)
|
||||
|
||||
addNodes(nodeClient, 0, 5, nil, t)
|
||||
|
||||
validateDaemonSetPodsAndMarkReady(podClient, podInformer, limitedPodNumber, t)
|
||||
validateDaemonSetStatus(dsClient, ds.Name, int32(limitedPodNumber), t)
|
||||
})
|
||||
}
|
||||
|
53
test/integration/daemonset/util.go
Normal file
53
test/integration/daemonset/util.go
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
Copyright 2022 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package daemonset
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"k8s.io/apiserver/pkg/admission"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
)
|
||||
|
||||
var _ admission.ValidationInterface = &fakePodFailAdmission{}
|
||||
|
||||
type fakePodFailAdmission struct {
|
||||
lock sync.Mutex
|
||||
limitedPodNumber int
|
||||
succeedPodsCount int
|
||||
}
|
||||
|
||||
func (f *fakePodFailAdmission) Handles(operation admission.Operation) bool {
|
||||
return operation == admission.Create
|
||||
}
|
||||
|
||||
func (f *fakePodFailAdmission) Validate(ctx context.Context, attr admission.Attributes, o admission.ObjectInterfaces) (err error) {
|
||||
if attr.GetKind().GroupKind() != api.Kind("Pod") {
|
||||
return nil
|
||||
}
|
||||
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
if f.succeedPodsCount >= f.limitedPodNumber {
|
||||
return fmt.Errorf("fakePodFailAdmission error")
|
||||
}
|
||||
f.succeedPodsCount++
|
||||
return nil
|
||||
}
|
@@ -19,6 +19,7 @@ package statefulset
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -344,6 +345,7 @@ func scaleSTS(t *testing.T, c clientset.Interface, sts *appsv1.StatefulSet, repl
|
||||
var _ admission.ValidationInterface = &fakePodFailAdmission{}
|
||||
|
||||
type fakePodFailAdmission struct {
|
||||
lock sync.Mutex
|
||||
limitedPodNumber int
|
||||
succeedPodsCount int
|
||||
}
|
||||
@@ -357,6 +359,9 @@ func (f *fakePodFailAdmission) Validate(ctx context.Context, attr admission.Attr
|
||||
return nil
|
||||
}
|
||||
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
if f.succeedPodsCount >= f.limitedPodNumber {
|
||||
return fmt.Errorf("fakePodFailAdmission error")
|
||||
}
|
||||
|
Reference in New Issue
Block a user