Skip to content

Commit 7c4b3a8

Browse files
author
Marcell Sevcsik
committed
Adds tests for GatherPodDisruptionBudgets
1 parent 4899c6a commit 7c4b3a8

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

Diff for: pkg/controller/operator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (s *Support) Run(ctx context.Context, controller *controllercmd.ControllerC
153153

154154
// the gatherers periodically check the state of the cluster and report any
155155
// config to the recorder
156-
configPeriodic := clusterconfig.New(gatherConfigClient, gatherKubeClient.CoreV1(), gatherKubeClient.CertificatesV1beta1(), metricsClient, registryClient.ImageregistryV1(), crdClient, gatherNetworkClient, *policyClient)
156+
configPeriodic := clusterconfig.New(gatherConfigClient, gatherKubeClient.CoreV1(), gatherKubeClient.CertificatesV1beta1(), metricsClient, registryClient.ImageregistryV1(), crdClient, gatherNetworkClient, policyClient)
157157
periodic := periodic.New(configObserver, recorder, map[string]gather.Interface{
158158
"config": configPeriodic,
159159
})

Diff for: pkg/gather/clusterconfig/clusterconfig.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ const (
5454
// will be listed in a single request to reduce memory usage.
5555
imageGatherPodLimit = 200
5656

57+
gatherPodDisruptionBudgetLimit = 5000
58+
5759
// metricsAlertsLinesLimit is the maximal number of lines read from monitoring Prometheus
5860
// 500 KiB of alerts is limit, one alert line has typically 450 bytes => 1137 lines.
5961
// This number has been rounded to 1000 for simplicity.
@@ -99,22 +101,17 @@ type Gatherer struct {
99101
certClient certificatesv1beta1.CertificatesV1beta1Interface
100102
registryClient imageregistryv1.ImageregistryV1Interface
101103
crdClient apixv1beta1client.ApiextensionsV1beta1Interface
102-
policyClient policyclient.PolicyV1beta1Client
104+
policyClient policyclient.PolicyV1beta1Interface
103105
lock sync.Mutex
104106
lastVersion *configv1.ClusterVersion
105107
}
106108

107109
// New creates new Gatherer
108110
func New(client configv1client.ConfigV1Interface, coreClient corev1client.CoreV1Interface, certClient certificatesv1beta1.CertificatesV1beta1Interface, metricsClient rest.Interface,
109-
registryClient imageregistryv1.ImageregistryV1Interface, crdClient apixv1beta1client.ApiextensionsV1beta1Interface, networkClient networkv1client.NetworkV1Interface, policyClient policyclient.PolicyV1beta1Client) *Gatherer {
110-
return &Gatherer{
111-
client: client,
112-
coreClient: coreClient,
113-
certClient: certClient,
111+
registryClient imageregistryv1.ImageregistryV1Interface, crdClient apixv1beta1client.ApiextensionsV1beta1Interface, networkClient networkv1client.NetworkV1Interface, policyClient policyclient.PolicyV1beta1Interface) *Gatherer {
112+
113+
registryClient imageregistryv1.ImageregistryV1Interface, policyClient policyclient.PolicyV1beta1Interface) *Gatherer {
114114
metricsClient: metricsClient,
115-
registryClient: registryClient,
116-
crdClient: crdClient,
117-
networkClient: networkClient,
118115
policyClient: policyClient,
119116
}
120117
}
@@ -151,7 +148,7 @@ func (i *Gatherer) Gather(ctx context.Context, recorder record.Interface) error
151148
//
152149
func GatherPodDisruptionBudgets(i *Gatherer) func() ([]record.Record, []error) {
153150
return func() ([]record.Record, []error) {
154-
pdbs, err := i.policyClient.PodDisruptionBudgets("").List(metav1.ListOptions{})
151+
pdbs, err := i.policyClient.PodDisruptionBudgets("").List(metav1.ListOptions{Limit: gatherPodDisruptionBudgetLimit,})
155152
if err != nil {
156153
return nil, []error{err}
157154
}

Diff for: pkg/gather/clusterconfig/clusterconfig_test.go

+53-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import (
1414
corev1 "k8s.io/api/core/v1"
1515
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
1616
apixv1beta1clientfake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
17+
policyv1beta1 "k8s.io/api/policy/v1beta1"
1718
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1819
"k8s.io/apimachinery/pkg/runtime"
20+
"k8s.io/apimachinery/pkg/util/intstr"
1921
kubefake "k8s.io/client-go/kubernetes/fake"
2022
"k8s.io/klog"
2123

@@ -171,6 +173,57 @@ func TestGatherClusterPruner(t *testing.T) {
171173
}
172174
}
173175

176+
func TestGatherPodDisruptionBudgets(t *testing.T){
177+
coreClient := kubefake.NewSimpleClientset()
178+
179+
fakeNamespace := "fake-namespace"
180+
181+
// name -> MinAvailabel
182+
fakePDBs := map[string]string {
183+
"pdb-four": "4",
184+
"pdb-eight": "8",
185+
"pdb-ten": "10",
186+
}
187+
for name, minAvailable := range fakePDBs{
188+
_, err := coreClient.PolicyV1beta1().
189+
PodDisruptionBudgets(fakeNamespace).
190+
Create(&policyv1beta1.PodDisruptionBudget{
191+
ObjectMeta: metav1.ObjectMeta{
192+
Namespace: fakeNamespace,
193+
Name: name,
194+
},
195+
Spec: policyv1beta1.PodDisruptionBudgetSpec{
196+
MinAvailable: &intstr.IntOrString{StrVal: minAvailable},
197+
},
198+
})
199+
if err != nil {
200+
t.Fatalf("unable to create fake pdbs: %v", err)
201+
}
202+
}
203+
204+
gatherer := &Gatherer{policyClient: coreClient.PolicyV1beta1()}
205+
206+
records, errs := GatherPodDisruptionBudgets(gatherer)()
207+
if len(errs) > 0 {
208+
t.Errorf("unexpected errors: %#v", errs)
209+
return
210+
}
211+
if len(records) != len(fakePDBs) {
212+
t.Fatalf("unexpected number of records gathered: %d (expected %d)", len(records), len(fakePDBs))
213+
}
214+
for _, rec := range records {
215+
pdba, ok := rec.Item.(PodDisruptionBudgetsAnonymizer)
216+
if !ok {
217+
t.Fatal("pdb item has invalid type")
218+
}
219+
name := pdba.PodDisruptionBudget.ObjectMeta.Name
220+
minAvailable := pdba.PodDisruptionBudget.Spec.MinAvailable.StrVal
221+
if pdba.PodDisruptionBudget.Spec.MinAvailable.StrVal != fakePDBs[name] {
222+
t.Fatalf("pdb item has mismatched MinAvailable value, %q != %q", fakePDBs[name], minAvailable)
223+
}
224+
}
225+
}
226+
174227
func TestGatherClusterImageRegistry(t *testing.T) {
175228
tests := []struct {
176229
name string
@@ -337,7 +390,6 @@ func TestGatherContainerImages(t *testing.T) {
337390
}
338391

339392
coreClient := kubefake.NewSimpleClientset()
340-
341393
for index, containerImage := range mockContainers {
342394
_, err := coreClient.CoreV1().
343395
Pods(fakeNamespace).

0 commit comments

Comments
 (0)