Wire contexts to Disruption controllers

This commit is contained in:
ravisantoshgudimetla
2021-04-22 14:34:55 -04:00
parent adbda068c1
commit 65ff81757d
6 changed files with 152 additions and 141 deletions

View File

@@ -98,14 +98,12 @@ func setup(t *testing.T) (*kubeapiservertesting.TestServer, *disruption.Disrupti
func TestPDBWithScaleSubresource(t *testing.T) {
s, pdbc, informers, clientSet, apiExtensionClient, dynamicClient := setup(t)
defer s.TearDownFn()
ctx := context.TODO()
nsName := "pdb-scale-subresource"
createNs(t, nsName, clientSet)
createNs(ctx, t, nsName, clientSet)
stopCh := make(chan struct{})
informers.Start(stopCh)
go pdbc.Run(stopCh)
defer close(stopCh)
informers.Start(ctx.Done())
go pdbc.Run(ctx)
crdDefinition := newCustomResourceDefinition()
etcd.CreateTestCRDs(t, apiExtensionClient, true, crdDefinition)
@@ -128,7 +126,7 @@ func TestPDBWithScaleSubresource(t *testing.T) {
},
},
}
createdResource, err := resourceClient.Create(context.TODO(), resource, metav1.CreateOptions{})
createdResource, err := resourceClient.Create(ctx, resource, metav1.CreateOptions{})
if err != nil {
t.Error(err)
}
@@ -144,7 +142,7 @@ func TestPDBWithScaleSubresource(t *testing.T) {
},
}
for i := 0; i < replicas; i++ {
createPod(t, fmt.Sprintf("pod-%d", i), nsName, map[string]string{"app": "test-crd"}, clientSet, ownerRefs)
createPod(ctx, t, fmt.Sprintf("pod-%d", i), nsName, map[string]string{"app": "test-crd"}, clientSet, ownerRefs)
}
waitToObservePods(t, informers.Core().V1().Pods().Informer(), 4, v1.PodRunning)
@@ -163,13 +161,13 @@ func TestPDBWithScaleSubresource(t *testing.T) {
},
},
}
if _, err := clientSet.PolicyV1().PodDisruptionBudgets(nsName).Create(context.TODO(), pdb, metav1.CreateOptions{}); err != nil {
if _, err := clientSet.PolicyV1().PodDisruptionBudgets(nsName).Create(ctx, pdb, metav1.CreateOptions{}); err != nil {
t.Errorf("Error creating PodDisruptionBudget: %v", err)
}
waitPDBStable(t, clientSet, 4, nsName, pdb.Name)
waitPDBStable(ctx, t, clientSet, 4, nsName, pdb.Name)
newPdb, err := clientSet.PolicyV1().PodDisruptionBudgets(nsName).Get(context.TODO(), pdb.Name, metav1.GetOptions{})
newPdb, err := clientSet.PolicyV1().PodDisruptionBudgets(nsName).Get(ctx, pdb.Name, metav1.GetOptions{})
if err != nil {
t.Errorf("Error getting PodDisruptionBudget: %v", err)
}
@@ -186,6 +184,8 @@ func TestPDBWithScaleSubresource(t *testing.T) {
}
func TestEmptySelector(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
testcases := []struct {
name string
createPDBFunc func(clientSet clientset.Interface, name, nsName string, minAvailable intstr.IntOrString) error
@@ -203,7 +203,7 @@ func TestEmptySelector(t *testing.T) {
Selector: &metav1.LabelSelector{},
},
}
_, err := clientSet.PolicyV1beta1().PodDisruptionBudgets(nsName).Create(context.TODO(), pdb, metav1.CreateOptions{})
_, err := clientSet.PolicyV1beta1().PodDisruptionBudgets(nsName).Create(ctx, pdb, metav1.CreateOptions{})
return err
},
expectedCurrentHealthy: 0,
@@ -220,7 +220,7 @@ func TestEmptySelector(t *testing.T) {
Selector: &metav1.LabelSelector{},
},
}
_, err := clientSet.PolicyV1().PodDisruptionBudgets(nsName).Create(context.TODO(), pdb, metav1.CreateOptions{})
_, err := clientSet.PolicyV1().PodDisruptionBudgets(nsName).Create(ctx, pdb, metav1.CreateOptions{})
return err
},
expectedCurrentHealthy: 4,
@@ -233,18 +233,16 @@ func TestEmptySelector(t *testing.T) {
defer s.TearDownFn()
nsName := fmt.Sprintf("pdb-empty-selector-%d", i)
createNs(t, nsName, clientSet)
createNs(ctx, t, nsName, clientSet)
stopCh := make(chan struct{})
informers.Start(stopCh)
go pdbc.Run(stopCh)
defer close(stopCh)
informers.Start(ctx.Done())
go pdbc.Run(ctx)
replicas := 4
minAvailable := intstr.FromInt(2)
for j := 0; j < replicas; j++ {
createPod(t, fmt.Sprintf("pod-%d", j), nsName, map[string]string{"app": "test-crd"},
createPod(ctx, t, fmt.Sprintf("pod-%d", j), nsName, map[string]string{"app": "test-crd"},
clientSet, []metav1.OwnerReference{})
}
@@ -255,9 +253,9 @@ func TestEmptySelector(t *testing.T) {
t.Errorf("Error creating PodDisruptionBudget: %v", err)
}
waitPDBStable(t, clientSet, tc.expectedCurrentHealthy, nsName, pdbName)
waitPDBStable(ctx, t, clientSet, tc.expectedCurrentHealthy, nsName, pdbName)
newPdb, err := clientSet.PolicyV1().PodDisruptionBudgets(nsName).Get(context.TODO(), pdbName, metav1.GetOptions{})
newPdb, err := clientSet.PolicyV1().PodDisruptionBudgets(nsName).Get(ctx, pdbName, metav1.GetOptions{})
if err != nil {
t.Errorf("Error getting PodDisruptionBudget: %v", err)
}
@@ -270,6 +268,8 @@ func TestEmptySelector(t *testing.T) {
}
func TestSelectorsForPodsWithoutLabels(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
testcases := []struct {
name string
createPDBFunc func(clientSet clientset.Interface, name, nsName string, minAvailable intstr.IntOrString) error
@@ -311,7 +311,7 @@ func TestSelectorsForPodsWithoutLabels(t *testing.T) {
},
},
}
_, err := clientSet.PolicyV1().PodDisruptionBudgets(nsName).Create(context.TODO(), pdb, metav1.CreateOptions{})
_, err := clientSet.PolicyV1().PodDisruptionBudgets(nsName).Create(ctx, pdb, metav1.CreateOptions{})
return err
},
expectedCurrentHealthy: 1,
@@ -335,7 +335,7 @@ func TestSelectorsForPodsWithoutLabels(t *testing.T) {
},
},
}
_, err := clientSet.PolicyV1beta1().PodDisruptionBudgets(nsName).Create(context.TODO(), pdb, metav1.CreateOptions{})
_, err := clientSet.PolicyV1beta1().PodDisruptionBudgets(nsName).Create(ctx, pdb, metav1.CreateOptions{})
return err
},
expectedCurrentHealthy: 1,
@@ -348,12 +348,10 @@ func TestSelectorsForPodsWithoutLabels(t *testing.T) {
defer s.TearDownFn()
nsName := fmt.Sprintf("pdb-selectors-%d", i)
createNs(t, nsName, clientSet)
createNs(ctx, t, nsName, clientSet)
stopCh := make(chan struct{})
informers.Start(stopCh)
go pdbc.Run(stopCh)
defer close(stopCh)
informers.Start(ctx.Done())
go pdbc.Run(ctx)
minAvailable := intstr.FromInt(1)
@@ -362,16 +360,16 @@ func TestSelectorsForPodsWithoutLabels(t *testing.T) {
if err := tc.createPDBFunc(clientSet, pdbName, nsName, minAvailable); err != nil {
t.Errorf("Error creating PodDisruptionBudget: %v", err)
}
waitPDBStable(t, clientSet, 0, nsName, pdbName)
waitPDBStable(ctx, t, clientSet, 0, nsName, pdbName)
// Create a pod and wait for it be reach the running phase.
createPod(t, "pod", nsName, map[string]string{}, clientSet, []metav1.OwnerReference{})
createPod(ctx, t, "pod", nsName, map[string]string{}, clientSet, []metav1.OwnerReference{})
waitToObservePods(t, informers.Core().V1().Pods().Informer(), 1, v1.PodRunning)
// Then verify that the added pod are picked up by the disruption controller.
waitPDBStable(t, clientSet, 1, nsName, pdbName)
waitPDBStable(ctx, t, clientSet, 1, nsName, pdbName)
newPdb, err := clientSet.PolicyV1().PodDisruptionBudgets(nsName).Get(context.TODO(), pdbName, metav1.GetOptions{})
newPdb, err := clientSet.PolicyV1().PodDisruptionBudgets(nsName).Get(ctx, pdbName, metav1.GetOptions{})
if err != nil {
t.Errorf("Error getting PodDisruptionBudget: %v", err)
}
@@ -383,7 +381,7 @@ func TestSelectorsForPodsWithoutLabels(t *testing.T) {
}
}
func createPod(t *testing.T, name, namespace string, labels map[string]string, clientSet clientset.Interface, ownerRefs []metav1.OwnerReference) {
func createPod(ctx context.Context, t *testing.T, name, namespace string, labels map[string]string, clientSet clientset.Interface, ownerRefs []metav1.OwnerReference) {
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
@@ -400,7 +398,7 @@ func createPod(t *testing.T, name, namespace string, labels map[string]string, c
},
},
}
_, err := clientSet.CoreV1().Pods(namespace).Create(context.TODO(), pod, metav1.CreateOptions{})
_, err := clientSet.CoreV1().Pods(namespace).Create(ctx, pod, metav1.CreateOptions{})
if err != nil {
t.Error(err)
}
@@ -410,8 +408,8 @@ func createPod(t *testing.T, name, namespace string, labels map[string]string, c
}
}
func createNs(t *testing.T, name string, clientSet clientset.Interface) {
_, err := clientSet.CoreV1().Namespaces().Create(context.TODO(), &v1.Namespace{
func createNs(ctx context.Context, t *testing.T, name string, clientSet clientset.Interface) {
_, err := clientSet.CoreV1().Namespaces().Create(ctx, &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
@@ -463,9 +461,9 @@ func newCustomResourceDefinition() *apiextensionsv1.CustomResourceDefinition {
}
}
func waitPDBStable(t *testing.T, clientSet clientset.Interface, podNum int32, ns, pdbName string) {
func waitPDBStable(ctx context.Context, t *testing.T, clientSet clientset.Interface, podNum int32, ns, pdbName string) {
if err := wait.PollImmediate(2*time.Second, 60*time.Second, func() (bool, error) {
pdb, err := clientSet.PolicyV1beta1().PodDisruptionBudgets(ns).Get(context.TODO(), pdbName, metav1.GetOptions{})
pdb, err := clientSet.PolicyV1beta1().PodDisruptionBudgets(ns).Get(ctx, pdbName, metav1.GetOptions{})
if err != nil {
return false, err
}