Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Add test for node pod list #1948

Merged
merged 1 commit into from
Feb 4, 2021
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
46 changes: 28 additions & 18 deletions internal/printer/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"fmt"
"strings"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/vmware-tanzu/octant/internal/util/kubernetes"
"github.com/vmware-tanzu/octant/pkg/store"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -84,7 +85,7 @@ func NodeHandler(ctx context.Context, node *corev1.Node, options Options) (compo
if err := nh.Images(options); err != nil {
return nil, errors.Wrap(err, "print node images")
}
if err := nh.Pods(options); err != nil {
if err := nh.Pods(ctx, options); err != nil {
return nil, errors.Wrap(err, "print node pods")
}
return o.ToComponent(ctx, options)
Expand Down Expand Up @@ -362,25 +363,34 @@ var (
nodePodsColumns = component.NewTableCols("Name", "Namespace")
)

func createNodePodsView(node *corev1.Node, options *Options) (*component.Table, error) {
if options == nil {
return nil, errors.New("cannot generate images for nil node")
}

func createNodePodsView(ctx context.Context, node *corev1.Node, options Options) (*component.Table, error) {
table := component.NewTable("Pods", "There are no pods!", nodePodsColumns)

clusterClient, err := options.DashConfig.ClusterClient().KubernetesClient()
if err != nil {
return nil, err
podList := &corev1.PodList{}
objectStore := options.DashConfig.ObjectStore()

key := store.Key{
APIVersion: "v1",
Kind: "Pod",
}

podList, err := clusterClient.CoreV1().Pods("").List(context.Background(), v1.ListOptions{
FieldSelector: fmt.Sprintf("spec.nodeName=%s", node.Name),
})
list, _, err := objectStore.List(ctx, key)
if err != nil {
return nil, err
}

for i := range list.Items {
pod := &corev1.Pod{}
err := kubernetes.FromUnstructured(&list.Items[i], pod)
if err != nil {
return nil, err
}

if pod.Spec.NodeName == node.Name {
podList.Items = append(podList.Items, *pod)
}
}

for _, pod := range podList.Items {

row := component.TableRow{
Expand Down Expand Up @@ -411,7 +421,7 @@ type nodeHandler struct {
resourcesFunc func(*corev1.Node, Options) (*component.Table, error)
conditionsFunc func(*corev1.Node, Options) (*component.Table, error)
imagesFunc func(*corev1.Node, Options) (*component.Table, error)
podsFunc func(*corev1.Node, Options) (*component.Table, error)
podsFunc func(context.Context, *corev1.Node, Options) (*component.Table, error)
object *Object
}

Expand Down Expand Up @@ -520,15 +530,15 @@ func (n *nodeHandler) Images(options Options) error {
return nil
}

func (n *nodeHandler) Pods(options Options) error {
func (n *nodeHandler) Pods(ctx context.Context, options Options) error {
if n.node == nil {
return errors.New("can't display resources for nil node")
}

n.object.RegisterItems(ItemDescriptor{
Width: component.WidthFull,
Func: func() (component.Component, error) {
return n.podsFunc(n.node, options)
return n.podsFunc(ctx, n.node, options)
},
})
return nil
Expand All @@ -538,6 +548,6 @@ func defaultNodeImages(node *corev1.Node, options Options) (*component.Table, er
return createNodeImagesView(node)
}

func defaultNodePods(node *corev1.Node, options Options) (*component.Table, error) {
return createNodePodsView(node, &options)
func defaultNodePods(ctx context.Context, node *corev1.Node, options Options) (*component.Table, error) {
return createNodePodsView(ctx, node, options)
}
46 changes: 46 additions & 0 deletions internal/printer/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"testing"

"github.com/vmware-tanzu/octant/pkg/store"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -149,6 +151,50 @@ func Test_NodeConfiguration(t *testing.T) {
}
}

func Test_createNodePodsView(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()

tpo := newTestPrinterOptions(controller)
printOptions := tpo.ToOptions()

ctx := context.Background()

node := testutil.CreateNode("node-1")
pod := testutil.CreatePod("pod")
pod.Spec.NodeName = "node-1"

podSame := testutil.CreatePod("pod-same-node")
podSame.Spec.NodeName = "node-1"

podDifferent := testutil.CreatePod("pod-different-node")
podDifferent.Spec.NodeName = "node-2"

podKey := store.Key{
APIVersion: "v1",
Kind: "Pod",
}

tpo.objectStore.EXPECT().List(gomock.Any(), podKey).
Return(testutil.ToUnstructuredList(t, pod, podSame, podDifferent), false, nil)

got, err := createNodePodsView(ctx, node, printOptions)
require.NoError(t, err)

nodePodsColumns := component.NewTableCols("Name", "Namespace")
expected := component.NewTableWithRows("Pods", "There are no pods!", nodePodsColumns, []component.TableRow{
{
"Name": component.NewText("pod"),
"Namespace": component.NewText("namespace"),
},
{
"Name": component.NewText("pod-same-node"),
"Namespace": component.NewText("namespace"),
},
})
component.AssertEqual(t, expected, got)
}

func Test_createAddressesView(t *testing.T) {
node := testutil.CreateNode("node-1")
node.Status.Addresses = []corev1.NodeAddress{
Expand Down