Skip to content

Commit 7c187ca

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request #39825 from foxish/automated-cherry-pick-of-#39454-#39544-upstream-release-1.5
Automatic merge from submit-queue Automated cherry pick of #39454 #39544 Cherry pick of #39454 #39544 on release-1.5. #39454: Make PDBs represent percentage in StatefulSet #39544: Allow disruption controller to read statefulsets /cc @davidopp @maisem @kubernetes/sig-apps-misc **Release note**: ``` Fixed bug which prevents PDBs which have minAvailable specified as a percentage working with StatefulSet pods ```
2 parents b71d9b9 + a52537e commit 7c187ca

3 files changed

Lines changed: 110 additions & 3 deletions

File tree

‎pkg/controller/disruption/BUILD‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ go_library(
1919
"//pkg/api/unversioned:go_default_library",
2020
"//pkg/apis/extensions:go_default_library",
2121
"//pkg/apis/policy:go_default_library",
22+
"//pkg/apis/apps:go_default_library",
2223
"//pkg/client/cache:go_default_library",
2324
"//pkg/client/clientset_generated/internalclientset:go_default_library",
2425
"//pkg/client/clientset_generated/internalclientset/typed/core/internalversion:go_default_library",
@@ -47,6 +48,7 @@ go_test(
4748
"//pkg/apimachinery/registered:go_default_library",
4849
"//pkg/apis/extensions:go_default_library",
4950
"//pkg/apis/policy:go_default_library",
51+
"//pkg/apis/apps:go_default_library",
5052
"//pkg/client/cache:go_default_library",
5153
"//pkg/client/record:go_default_library",
5254
"//pkg/controller:go_default_library",

‎pkg/controller/disruption/disruption.go‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"k8s.io/kubernetes/pkg/api"
2525
"k8s.io/kubernetes/pkg/api/unversioned"
26+
"k8s.io/kubernetes/pkg/apis/apps"
2627
"k8s.io/kubernetes/pkg/apis/extensions"
2728
"k8s.io/kubernetes/pkg/apis/policy"
2829
"k8s.io/kubernetes/pkg/client/cache"
@@ -50,7 +51,7 @@ const statusUpdateRetries = 2
5051
// all and the corresponding entry can be removed from pdb.Status.DisruptedPods. It is assumed that
5152
// pod/pdb apiserver to controller latency is relatively small (like 1-2sec) so the below value should
5253
// be more than enough.
53-
// If the cotroller is running on a different node it is important that the two nodes have synced
54+
// If the controller is running on a different node it is important that the two nodes have synced
5455
// clock (via ntp for example). Otherwise PodDisruptionBudget controller may not provide enough
5556
// protection against unwanted pod disruptions.
5657
const DeletionTimeout = 2 * 60 * time.Second
@@ -79,6 +80,10 @@ type DisruptionController struct {
7980
dController *cache.Controller
8081
dLister cache.StoreToDeploymentLister
8182

83+
ssStore cache.Store
84+
ssController *cache.Controller
85+
ssLister cache.StoreToStatefulSetLister
86+
8287
// PodDisruptionBudget keys that need to be synced.
8388
queue workqueue.RateLimitingInterface
8489
recheckQueue workqueue.DelayingInterface
@@ -186,9 +191,23 @@ func NewDisruptionController(podInformer cache.SharedIndexInformer, kubeClient i
186191
cache.ResourceEventHandlerFuncs{},
187192
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
188193
)
189-
190194
dc.dLister.Indexer = dc.dIndexer
191195

196+
dc.ssStore, dc.ssController = cache.NewInformer(
197+
&cache.ListWatch{
198+
ListFunc: func(options api.ListOptions) (runtime.Object, error) {
199+
return dc.kubeClient.Apps().StatefulSets(api.NamespaceAll).List(options)
200+
},
201+
WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
202+
return dc.kubeClient.Apps().StatefulSets(api.NamespaceAll).Watch(options)
203+
},
204+
},
205+
&apps.StatefulSet{},
206+
30*time.Second,
207+
cache.ResourceEventHandlerFuncs{},
208+
)
209+
dc.ssLister.Store = dc.ssStore
210+
192211
return dc
193212
}
194213

@@ -200,7 +219,8 @@ func NewDisruptionController(podInformer cache.SharedIndexInformer, kubeClient i
200219
// and we may well need further tweaks just to be able to access scale
201220
// subresources.
202221
func (dc *DisruptionController) finders() []podControllerFinder {
203-
return []podControllerFinder{dc.getPodReplicationControllers, dc.getPodDeployments, dc.getPodReplicaSets}
222+
return []podControllerFinder{dc.getPodReplicationControllers, dc.getPodDeployments, dc.getPodReplicaSets,
223+
dc.getPodStatefulSets}
204224
}
205225

206226
// getPodReplicaSets finds replicasets which have no matching deployments.
@@ -230,6 +250,29 @@ func (dc *DisruptionController) getPodReplicaSets(pod *api.Pod) ([]controllerAnd
230250
return cas, nil
231251
}
232252

253+
// getPodStatefulSet returns the statefulset managing the given pod.
254+
func (dc *DisruptionController) getPodStatefulSets(pod *api.Pod) ([]controllerAndScale, error) {
255+
cas := []controllerAndScale{}
256+
ss, err := dc.ssLister.GetPodStatefulSets(pod)
257+
258+
// GetPodStatefulSets returns an error only if no StatefulSets are found. We
259+
// don't return that as an error to the caller.
260+
if err != nil {
261+
return cas, nil
262+
}
263+
264+
controllerScale := map[types.UID]int32{}
265+
for _, s := range ss {
266+
controllerScale[s.UID] = s.Spec.Replicas
267+
}
268+
269+
for uid, scale := range controllerScale {
270+
cas = append(cas, controllerAndScale{UID: uid, scale: scale})
271+
}
272+
273+
return cas, nil
274+
}
275+
233276
// getPodDeployments finds deployments for any replicasets which are being managed by deployments.
234277
func (dc *DisruptionController) getPodDeployments(pod *api.Pod) ([]controllerAndScale, error) {
235278
cas := []controllerAndScale{}
@@ -283,6 +326,7 @@ func (dc *DisruptionController) Run(stopCh <-chan struct{}) {
283326
go dc.rcController.Run(stopCh)
284327
go dc.rsController.Run(stopCh)
285328
go dc.dController.Run(stopCh)
329+
go dc.ssController.Run(stopCh)
286330
go wait.Until(dc.worker, time.Second, stopCh)
287331
go wait.Until(dc.recheckWorker, time.Second, stopCh)
288332

‎pkg/controller/disruption/disruption_test.go‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"k8s.io/kubernetes/pkg/api"
2727
"k8s.io/kubernetes/pkg/api/unversioned"
2828
"k8s.io/kubernetes/pkg/apimachinery/registered"
29+
"k8s.io/kubernetes/pkg/apis/apps"
2930
"k8s.io/kubernetes/pkg/apis/extensions"
3031
"k8s.io/kubernetes/pkg/apis/policy"
3132
"k8s.io/kubernetes/pkg/client/cache"
@@ -91,6 +92,7 @@ func newFakeDisruptionController() (*DisruptionController, *pdbStates) {
9192
rcLister: cache.StoreToReplicationControllerLister{Indexer: cache.NewIndexer(controller.KeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})},
9293
rsLister: cache.StoreToReplicaSetLister{Indexer: cache.NewIndexer(controller.KeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})},
9394
dLister: cache.StoreToDeploymentLister{Indexer: cache.NewIndexer(controller.KeyFunc, cache.Indexers{})},
95+
ssLister: cache.StoreToStatefulSetLister{Store: cache.NewStore(controller.KeyFunc)},
9496
getUpdater: func() updater { return ps.Set },
9597
broadcaster: record.NewBroadcaster(),
9698
}
@@ -235,6 +237,30 @@ func newReplicaSet(t *testing.T, size int32) (*extensions.ReplicaSet, string) {
235237
return rs, rsName
236238
}
237239

240+
func newStatefulSet(t *testing.T, size int32) (*apps.StatefulSet, string) {
241+
ss := &apps.StatefulSet{
242+
TypeMeta: unversioned.TypeMeta{APIVersion: registered.GroupOrDie(api.GroupName).GroupVersion.String()},
243+
ObjectMeta: api.ObjectMeta{
244+
UID: uuid.NewUUID(),
245+
Name: "foobar",
246+
Namespace: api.NamespaceDefault,
247+
ResourceVersion: "18",
248+
Labels: fooBar(),
249+
},
250+
Spec: apps.StatefulSetSpec{
251+
Replicas: size,
252+
Selector: newSelFooBar(),
253+
},
254+
}
255+
256+
ssName, err := controller.KeyFunc(ss)
257+
if err != nil {
258+
t.Fatalf("Unexpected error naming StatefulSet %q: %v", ss.Name, err)
259+
}
260+
261+
return ss, ssName
262+
}
263+
238264
func update(t *testing.T, store cache.Store, obj interface{}) {
239265
if err := store.Update(obj); err != nil {
240266
t.Fatalf("Could not add %+v to %+v: %v", obj, store, err)
@@ -408,6 +434,41 @@ func TestReplicationController(t *testing.T) {
408434
ps.VerifyDisruptionAllowed(t, pdbName, 0)
409435
}
410436

437+
func TestStatefulSetController(t *testing.T) {
438+
labels := map[string]string{
439+
"foo": "bar",
440+
"baz": "quux",
441+
}
442+
443+
dc, ps := newFakeDisruptionController()
444+
445+
// 34% should round up to 2
446+
pdb, pdbName := newPodDisruptionBudget(t, intstr.FromString("34%"))
447+
add(t, dc.pdbLister.Store, pdb)
448+
ss, _ := newStatefulSet(t, 3)
449+
add(t, dc.ssLister.Store, ss)
450+
dc.sync(pdbName)
451+
452+
// It starts out at 0 expected because, with no pods, the PDB doesn't know
453+
// about the SS. This is a known bug. TODO(mml): file issue
454+
ps.VerifyPdbStatus(t, pdbName, 0, 0, 0, 0, map[string]unversioned.Time{})
455+
456+
pods := []*api.Pod{}
457+
458+
for i := int32(0); i < 3; i++ {
459+
pod, _ := newPod(t, fmt.Sprintf("foobar %d", i))
460+
pods = append(pods, pod)
461+
pod.Labels = labels
462+
add(t, dc.podLister.Indexer, pod)
463+
dc.sync(pdbName)
464+
if i < 2 {
465+
ps.VerifyPdbStatus(t, pdbName, 0, i+1, 2, 3, map[string]unversioned.Time{})
466+
} else {
467+
ps.VerifyPdbStatus(t, pdbName, 1, 3, 2, 3, map[string]unversioned.Time{})
468+
}
469+
}
470+
}
471+
411472
func TestTwoControllers(t *testing.T) {
412473
// Most of this test is in verifying intermediate cases as we define the
413474
// three controllers and create the pods.

0 commit comments

Comments
 (0)