Skip to content

Commit

Permalink
Fix pod summary for large numbers of pods
Browse files Browse the repository at this point in the history
This change fixes a bug which caused clusters with more than 500 pods to
fail pod summary step.

The code assumed that if result.Infos had more than one element, the k8s
request returned more than 1 resource type which is not correct for a
table.

The result specifies pods as a resource type, so the assumption was
wrong. Multiple Infos objects resulted from client dividing requests
into chunks, as specified by RequestChunksOf function on request
builder.

The change merges all Info objects into one table and returns it.

Signed-off-by: Maciej Kwiek <[email protected]>
  • Loading branch information
nebril authored and tklauser committed Jul 12, 2023
1 parent 128db00 commit 4aedb5d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
10 changes: 6 additions & 4 deletions k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/strategicpatch"
Expand Down Expand Up @@ -820,14 +821,15 @@ func (c *Client) GetPodsTable(_ context.Context) (*metav1.Table, error) {
if r.Err() != nil {
return nil, r.Err()
}
i, err := r.Infos()
infos, err := r.Infos()
if err != nil {
return nil, err
}
if len(i) != 1 {
return nil, fmt.Errorf("expected a single kind of resource (got %d)", len(i))
objects := make([]runtime.Object, 0, len(infos))
for _, info := range infos {
objects = append(objects, info.Object)
}
return unstructuredToTable(i[0].Object)
return unstructuredSliceToTable(objects)
}

func (c *Client) ListUnstructured(ctx context.Context, gvr schema.GroupVersionResource, namespace *string, o metav1.ListOptions) (*unstructured.UnstructuredList, error) {
Expand Down
26 changes: 26 additions & 0 deletions k8s/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,29 @@ func unstructuredToTable(o runtime.Object) (*metav1.Table, error) {
}
return t, nil
}

func unstructuredSliceToTable(objects []runtime.Object) (*metav1.Table, error) {
if len(objects) == 0 {
return nil, fmt.Errorf("empty object list provided")
}

mainKind := objects[0].GetObjectKind().GroupVersionKind()
mainTable, err := unstructuredToTable(objects[0])
if err != nil {
return nil, err
}

for _, object := range objects[1:] {
if mainKind != object.GetObjectKind().GroupVersionKind() {
// Make sure that table is only populated with the same kind
continue
}
tempTable, err := unstructuredToTable(object)
if err != nil {
return nil, err
}
mainTable.Rows = append(mainTable.Rows, tempTable.Rows...)
}

return mainTable, nil
}

0 comments on commit 4aedb5d

Please sign in to comment.