Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Commit

Permalink
list pods added in k8s client
Browse files Browse the repository at this point in the history
  • Loading branch information
nityanandagohain committed Sep 12, 2020
1 parent c0fa2c7 commit 0b3754c
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions pkg/client/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"fmt"
"path/filepath"
"time"

Expand Down Expand Up @@ -51,23 +52,50 @@ func (k *K8s) ListNamespace() ([]NamespaceInfo, error) {
ns := []NamespaceInfo{}
for _, item := range list.Items {
n := NamespaceInfo{
Name: item.ObjectMeta.Name,
Status: string(item.Status.Phase),
CreatedAt: item.ObjectMeta.CreationTimestamp.Time,
Name: item.ObjectMeta.Name,
Status: string(item.Status.Phase),
CreatedAt: item.ObjectMeta.CreationTimestamp.Time,
}
ns = append(ns, n)
}
return ns, nil
}

// TODO: Create Struct for output similar to namespace
func (k *K8s) ListPods(namespace string) ([]string, error) {
type PodInfo struct {
Name string
Status string
Ready string
Restarts int32
CreatedAt time.Time
}

func (k *K8s) ListPods(namespace string) ([]PodInfo, error) {
ctx := context.TODO()
opts := v1.ListOptions{}
pods, _ := k.client.CoreV1().Pods(namespace).List(ctx, opts)
podList := []string{}
podList := []PodInfo{}
for _, pod := range pods.Items {
podList = append(podList, pod.Name)
restarts := int32(0)
ready := 0
totalContianers := len(pod.Status.ContainerStatuses)
for _, container := range pod.Status.ContainerStatuses {
if container.RestartCount > restarts {
restarts = container.RestartCount
}
if container.State.Running != nil {
ready++
}
}

p := PodInfo{
Name: pod.Name,
Status: string(pod.Status.Phase),
Restarts: restarts,
Ready: fmt.Sprintf("%v/%v", ready, totalContianers),
CreatedAt: pod.ObjectMeta.CreationTimestamp.Time,
}
fmt.Println(p)
podList = append(podList, p)
}
return podList, nil
}

0 comments on commit 0b3754c

Please sign in to comment.