Skip to content

Commit

Permalink
[release-4.12] OCPBUGS-15459: gather PDBs only from openshift namespa…
Browse files Browse the repository at this point in the history
…ces (#804) (#806)

* [release-4.12] OCPBUGS-14318: gather PDBs only from openshift namespaces (#786)

* Gather PDB only from openshift namespace

* minor docs update

* minor unit test refactor

* fix based on review

* reverting the gocomment to the original format

---------

Co-authored-by: OpenShift Cherrypick Robot <openshift-cherrypick-robot@redhat.com>
  • Loading branch information
rhrmo and openshift-cherrypick-robot committed Jul 26, 2023
1 parent 1fe479a commit 3274db1
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 50 deletions.
22 changes: 13 additions & 9 deletions pkg/gatherers/clusterconfig/pod_disruption_budgets.go
Expand Up @@ -3,6 +3,7 @@ package clusterconfig
import (
"context"
"fmt"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
policyclient "k8s.io/client-go/kubernetes/typed/policy/v1"
Expand All @@ -11,7 +12,7 @@ import (
)

const (
gatherPodDisruptionBudgetLimit = 5000
gatherPodDisruptionBudgetLimit = 100
)

// GatherPodDisruptionBudgets gathers the cluster's PodDisruptionBudgets.
Expand All @@ -36,20 +37,23 @@ func (g *Gatherer) GatherPodDisruptionBudgets(ctx context.Context) ([]record.Rec
}

func gatherPodDisruptionBudgets(ctx context.Context, policyClient policyclient.PolicyV1Interface) ([]record.Record, []error) {
pdbs, err := policyClient.PodDisruptionBudgets("").List(ctx, metav1.ListOptions{Limit: gatherPodDisruptionBudgetLimit})
pdbs, err := policyClient.PodDisruptionBudgets("").List(ctx, metav1.ListOptions{})
if err != nil {
return nil, []error{err}
}
var records []record.Record
for i := range pdbs.Items {
recordName := fmt.Sprintf("config/pdbs/%s", pdbs.Items[i].GetName())
if pdbs.Items[i].GetNamespace() != "" {
recordName = fmt.Sprintf("config/pdbs/%s/%s", pdbs.Items[i].GetNamespace(), pdbs.Items[i].GetName())
item := &pdbs.Items[i]
if strings.HasPrefix(item.GetNamespace(), "openshift-") {
recordName := fmt.Sprintf("config/pdbs/%s/%s", item.GetNamespace(), item.GetName())
records = append(records, record.Record{
Name: recordName,
Item: record.ResourceMarshaller{Resource: item},
})
if len(records) == gatherPodDisruptionBudgetLimit {
break
}
}
records = append(records, record.Record{
Name: recordName,
Item: record.ResourceMarshaller{Resource: &pdbs.Items[i]},
})
}
return records, nil
}
133 changes: 92 additions & 41 deletions pkg/gatherers/clusterconfig/pod_disruption_budgets_test.go
Expand Up @@ -5,55 +5,106 @@ import (
"testing"

"github.com/openshift/insights-operator/pkg/record"
"github.com/stretchr/testify/assert"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
kubefake "k8s.io/client-go/kubernetes/fake"
)

func Test_PodDisruptionBudgets_Gather(t *testing.T) {
coreClient := kubefake.NewSimpleClientset()
tests := []struct {
name string
// namespace -> pdb
pdbsToNamespace map[string]string
// pdb -> minAvailable
minAvailableToPdb map[string]int
expCount int
}{
{
name: "no PDB",
pdbsToNamespace: nil,
minAvailableToPdb: nil,
expCount: 0,
},
{
name: "one openshift PDB",
pdbsToNamespace: map[string]string{
"openshift-test": "testT",
},
minAvailableToPdb: map[string]int{
"testT": 1,
},
expCount: 1,
},
{
name: "one random PDB",
pdbsToNamespace: map[string]string{
"random": "testR",
},
minAvailableToPdb: map[string]int{
"testR": 1,
},
expCount: 0,
},
{
name: "one openshift, one random PDB",
pdbsToNamespace: map[string]string{
"openshift-test": "testT",
"random": "testR",
},
minAvailableToPdb: map[string]int{
"testT": 1,
"testR": 1,
},
expCount: 1,
},
{
name: "multiple openshift PDBs",
pdbsToNamespace: map[string]string{
"openshift-test": "testT",
"openshift-default": "testD",
},
minAvailableToPdb: map[string]int{
"testT": 1,
"testD": 2,
},
expCount: 2,
},
}

fakeNamespace := "fake-namespace"
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
coreClient := kubefake.NewSimpleClientset()

// name -> MinAvailabel
fakePDBs := map[string]string{
"pdb-four": "4",
"pdb-eight": "8",
"pdb-ten": "10",
}
for name, minAvailable := range fakePDBs {
_, err := coreClient.PolicyV1().
PodDisruptionBudgets(fakeNamespace).
Create(context.Background(), &policyv1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Namespace: fakeNamespace,
Name: name,
},
Spec: policyv1.PodDisruptionBudgetSpec{
MinAvailable: &intstr.IntOrString{StrVal: minAvailable},
},
}, metav1.CreateOptions{})
if err != nil {
t.Fatalf("unable to create fake pdbs: %v", err)
}
}
ctx := context.Background()
records, errs := gatherPodDisruptionBudgets(ctx, coreClient.PolicyV1())
if len(errs) > 0 {
t.Errorf("unexpected errors: %#v", errs)
return
}
if len(records) != len(fakePDBs) {
t.Fatalf("unexpected number of records gathered: %d (expected %d)", len(records), len(fakePDBs))
}
pdba, ok := records[0].Item.(record.ResourceMarshaller).Resource.(*policyv1.PodDisruptionBudget)
if !ok {
t.Fatal("pdb item has invalid type")
}
name := pdba.ObjectMeta.Name
minAvailable := pdba.Spec.MinAvailable.StrVal
if pdba.Spec.MinAvailable.StrVal != fakePDBs[name] {
t.Fatalf("pdb item has mismatched MinAvailable value, %q != %q", fakePDBs[name], minAvailable)
for namespace, name := range test.pdbsToNamespace {
_, err := coreClient.PolicyV1().
PodDisruptionBudgets(namespace).
Create(context.Background(), &policyv1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
},
Spec: policyv1.PodDisruptionBudgetSpec{
MinAvailable: &intstr.IntOrString{IntVal: int32(test.minAvailableToPdb[name])},
},
}, metav1.CreateOptions{})
assert.NoErrorf(t, err, "unable to create fake pdbs: %v", err)
}
ctx := context.Background()
records, errs := gatherPodDisruptionBudgets(ctx, coreClient.PolicyV1())
assert.Emptyf(t, errs, "unexpected errors: %#v", errs)
assert.Equal(t, test.expCount, len(records))
for i := range records {
pdba, ok := records[i].Item.(record.ResourceMarshaller).Resource.(*policyv1.PodDisruptionBudget)
assert.True(t, ok, "pdb item has invalid type")
name := pdba.ObjectMeta.Name
namespace := pdba.ObjectMeta.Namespace
assert.Equal(t, test.pdbsToNamespace[namespace], name)
assert.Equal(t, test.minAvailableToPdb[name], pdba.Spec.MinAvailable.IntValue())
}
})
}
}

0 comments on commit 3274db1

Please sign in to comment.