Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCPBUGS-13786: gather PDBs only from openshift namespaces #780

Merged
merged 4 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/gathered-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -1576,7 +1576,8 @@ Collects the cluster's `PodDisruptionBudgets`.
- 4.5.15+

### Changes
None
- The gatherer was changed to gather pdbs only from namespaces with "openshift" prefix
and the limit of gathered records to 100 since 4.14.


## SAPConfig
Expand Down
25 changes: 15 additions & 10 deletions pkg/gatherers/clusterconfig/pod_disruption_budgets.go
Original file line number Diff line number Diff line change
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 Collects the cluster's `PodDisruptionBudgets`.
Expand All @@ -37,7 +38,8 @@ const (
// - 4.5.15+
//
// ### Changes
// None
// - The gatherer was changed to gather pdbs only from namespaces with "openshift" prefix
// and the limit of gathered records to 100 since 4.14.
func (g *Gatherer) GatherPodDisruptionBudgets(ctx context.Context) ([]record.Record, []error) {
gatherPolicyClient, err := policyclient.NewForConfig(g.gatherKubeConfig)
if err != nil {
Expand All @@ -48,20 +50,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
Original file line number Diff line number Diff line change
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])},
rhrmo marked this conversation as resolved.
Show resolved Hide resolved
},
}, 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())
}
})
}
}