Skip to content

Commit 3285495

Browse files
author
Marcell Sevcsik
committed
Adds tests for GatherPodDisruptionBudgets
1 parent d181149 commit 3285495

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

pkg/controller/operator.go

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

142142
// the gatherers periodically check the state of the cluster and report any
143143
// config to the recorder
144-
configPeriodic := clusterconfig.New(gatherConfigClient, gatherKubeClient.CoreV1(), gatherKubeClient.CertificatesV1beta1(), metricsClient, registryClient.ImageregistryV1(), *policyClient)
144+
configPeriodic := clusterconfig.New(gatherConfigClient, gatherKubeClient.CoreV1(), gatherKubeClient.CertificatesV1beta1(), metricsClient, registryClient.ImageregistryV1(), policyClient)
145145
periodic := periodic.New(configObserver, recorder, map[string]gather.Interface{
146146
"config": configPeriodic,
147147
})

pkg/gather/clusterconfig/clusterconfig.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ const (
5151
// will be listed in a single request to reduce memory usage.
5252
imageGatherPodLimit = 200
5353

54+
gatherPodDisruptionBudgetLimit = 5000
55+
5456
// metricsAlertsLinesLimit is the maximal number of lines read from monitoring Prometheus
5557
// 500 KiB of alerts is limit, one alert line has typically 450 bytes => 1137 lines.
5658
// This number has been rounded to 1000 for simplicity.
@@ -90,14 +92,14 @@ type Gatherer struct {
9092
metricsClient rest.Interface
9193
certClient certificatesv1beta1.CertificatesV1beta1Interface
9294
registryClient imageregistryv1.ImageregistryV1Interface
93-
policyClient policyclient.PolicyV1beta1Client
95+
policyClient policyclient.PolicyV1beta1Interface
9496
lock sync.Mutex
9597
lastVersion *configv1.ClusterVersion
9698
}
9799

98100
// New creates new Gatherer
99101
func New(client configv1client.ConfigV1Interface, coreClient corev1client.CoreV1Interface, certClient certificatesv1beta1.CertificatesV1beta1Interface, metricsClient rest.Interface,
100-
registryClient imageregistryv1.ImageregistryV1Interface, policyClient policyclient.PolicyV1beta1Client) *Gatherer {
102+
registryClient imageregistryv1.ImageregistryV1Interface, policyClient policyclient.PolicyV1beta1Interface) *Gatherer {
101103
return &Gatherer{
102104
client: client,
103105
coreClient: coreClient,
@@ -138,7 +140,7 @@ func (i *Gatherer) Gather(ctx context.Context, recorder record.Interface) error
138140
//
139141
func GatherPodDisruptionBudgets(i *Gatherer) func() ([]record.Record, []error) {
140142
return func() ([]record.Record, []error) {
141-
pdbs, err := i.policyClient.PodDisruptionBudgets("").List(metav1.ListOptions{})
143+
pdbs, err := i.policyClient.PodDisruptionBudgets("").List(metav1.ListOptions{Limit: gatherPodDisruptionBudgetLimit,})
142144
if err != nil {
143145
return nil, []error{err}
144146
}

pkg/gather/clusterconfig/clusterconfig_test.go

+53-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111

1212
imageregistryv1 "github.com/openshift/api/imageregistry/v1"
1313
corev1 "k8s.io/api/core/v1"
14+
policyv1beta1 "k8s.io/api/policy/v1beta1"
1415
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1516
"k8s.io/apimachinery/pkg/runtime"
17+
"k8s.io/apimachinery/pkg/util/intstr"
1618
kubefake "k8s.io/client-go/kubernetes/fake"
1719
"k8s.io/klog"
1820

@@ -167,6 +169,57 @@ func TestGatherClusterPruner(t *testing.T) {
167169
}
168170
}
169171

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

335388
coreClient := kubefake.NewSimpleClientset()
336-
337389
for index, containerImage := range mockContainers {
338390
_, err := coreClient.CoreV1().
339391
Pods(fakeNamespace).

0 commit comments

Comments
 (0)