Skip to content

Instantly share code, notes, and snippets.

@slayer321
Created December 1, 2022 07:21
Show Gist options
  • Save slayer321/7aef7f629af5df6023dbd8a6f6381801 to your computer and use it in GitHub Desktop.
Save slayer321/7aef7f629af5df6023dbd8a6f6381801 to your computer and use it in GitHub Desktop.
Get time and name of pods
package main
import (
"context"
"flag"
"fmt"
"path/filepath"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
func main() {
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
getpods, _ := clientset.CoreV1().Pods("default").List(context.TODO(), v1.ListOptions{})
for _, val := range getpods.Items {
fmt.Println(val.Name)
fmt.Println(val.CreationTimestamp)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment