package main
import (
"flag"
"fmt"
"reflect"
"encoding/json"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/api/unversioned"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/rest"
)
func main() {
kubeconfig := flag.String("kubeconfig", "/root/.kube/config", "Path to a kube config. Only required if out-of-cluster.")
fmt.Println(kubeconfig)
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
fmt.Println("error1")
}
gv := &unversioned.GroupVersion{"", "v1"}
resource := &unversioned.APIResource{Name: "pods", Namespaced: true}
config.ContentConfig = rest.ContentConfig{GroupVersion: gv}
config.APIPath = "/api"
dynamicClient, err := dynamic.NewClient(config)
if err != nil {
fmt.Println("dynamic NewCLient error")
}
obj, err := dynamicClient.Resource(resource, "").List(&v1.ListOptions{})
if err != nil {
fmt.Println("dynamicClient Resource error")
}
js, err := json.Marshal(reflect.ValueOf(obj).Elem().Interface())
if err != nil {
fmt.Println("error")
}
podlist := v1.PodList{}
json.Unmarshal(js, &podlist)
fmt.Println(podlist)
fmt.Println("------------------------")
obj, err = dynamicClient.Resource(resource, "default").Get("nginx-1487191267-b4w5j")
if err != nil {
fmt.Println("dynamicClient Resource error")
}
js, err = json.Marshal(obj)
if err != nil {
fmt.Println("error")
}
pod := v1.Pod{}
json.Unmarshal(js, &pod)
fmt.Println(pod)
}