Last active
June 19, 2022 13:23
-
-
Save taking/78b6fb7bbdaa95b73ac7c0d0d574d2c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"k8s.io/apimachinery/pkg/api/errors" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/rest" | |
) | |
////////// | |
// 참고 | |
// https://github.com/kubernetes/client-go/tree/f6ce18ae578c8cca64d14ab9687824d9e1305a67/informers/core/v1 | |
// https://github.com/kubernetes/client-go/blob/master/kubernetes/typed/core/v1/pod.go | |
// https://github.com/kubernetes/apimachinery/tree/master/pkg/apis/meta/v1 | |
// | |
// https://gerrit.opnfv.org/gerrit/gitweb?p=ovn4nfv-k8s-plugin.git;hb=refs%2Fchanges%2F23%2F68323%2F4;f=internal%2Fpkg%2Fkube%2Fkube.go | |
// https://www.cnblogs.com/breg/p/11350174.html | |
// https://github.com/gedge-platform/gs-linkgw/blob/main/migration/etri.com/plugin/cmd.go | |
////////// | |
func KubernetesClient() (*kubernetes.Clientset, error) { | |
config := &rest.Config{ | |
Host: "https://111.111.111.111:6443", | |
// APIPath: "/", | |
// Prefix: "", | |
BearerToken: "eyJhbGciOiJSUzI1NiIsImtpZCI6InJvVHROMm1OZjZNOTQ5X21QMmViYi1Cd2oyWlpsV0Y0fggkrb.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJnbS1jbHVzdGVyLWFkbWluLXRva2VuLWRiamdyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImdtLWNsdXN0ZXItYWRtaW4iLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJiMjZkZGZmYS04Y2U2LTRlZTgtYWZiOS0wNjlmMTkxOGY2OWMiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06Z20tY2x1c3Rlci1hZG1pbiJ9.YsoJB5Uhd9l71ophm14yPRXs5G4oB3iPFgCtoiWJ_Og7ry_VdCguzTIdtrAkb5R7kGQbKVmYa7nFOFRNPw1K01AV2SThbuO2IrnyqicLXgA6MsUXCOUk5DxG9R03EfGx4itGIzn7HvoyUqZ3L5IM3YaOvNsYcweeNL7jheVweY8joijMQ4iBsG2WGaeQ1kkYcwj_Et0tBJgBOVS_EQaL5hISTtmbhbfAUJxE1FiUjBn6XX0D8yfO5m7AIQFmjoUQK_TXvxGrOFzS1QFY9Roin-xvWfjIDhNwtlsUmHAo2f-EwpKDr381J7dSTJ7liidOl2Ad1ITl3q_ZhLRb2RIm3Q", | |
TLSClientConfig: rest.TLSClientConfig{Insecure: true}, | |
// Username: clstr.MasterAuth.Username, | |
// Password: clstr.MasterAuth.Password, | |
} | |
kbrnts, err := kubernetes.NewForConfig(config) | |
if err != nil { | |
return nil, err | |
} | |
return kbrnts, nil | |
} | |
func listNodes(client *kubernetes.Clientset) { | |
nodes, err := client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{}) | |
check(err) | |
fmt.Printf("There are %d nodes in the cluster:\n", len(nodes.Items)) | |
for i, node := range nodes.Items { | |
fmt.Printf("%d %s\n", i, node.GetName()) | |
} | |
} | |
func listNamespaces(client *kubernetes.Clientset) { | |
namespacesClient := client.CoreV1().Namespaces() | |
namespaces, err := namespacesClient.List(context.TODO(), metav1.ListOptions{}) | |
check(err) | |
fmt.Printf("There are %d namespaces in the cluster:\n", len(namespaces.Items)) | |
for i, namespace := range namespaces.Items { | |
fmt.Printf("%d %s\n", i, namespace.GetName()) | |
} | |
} | |
func listPods(client *kubernetes.Clientset) { | |
podsClient := client.CoreV1().Pods("default") | |
pods, err := podsClient.List(context.TODO(), metav1.ListOptions{}) | |
check(err) | |
fmt.Printf("There are %d pods in the cluster:\n", len(pods.Items)) | |
for i, pod := range pods.Items { | |
fmt.Printf("%d %s\n", i, pod.GetName()) | |
} | |
} | |
func listDeployments(client *kubernetes.Clientset) { | |
deploymentsClient := client.AppsV1().Deployments("bookinfo") | |
deployments, err := deploymentsClient.List(context.TODO(), metav1.ListOptions{}) | |
check(err) | |
fmt.Printf("There are %d deployments in the cluster:\n", len(deployments.Items)) | |
for i, deployment := range deployments.Items { | |
fmt.Printf("%d %s\n", i, deployment.GetName()) | |
} | |
} | |
func getPods(client *kubernetes.Clientset, PodName string) { | |
podsClient := client.CoreV1().Pods("default") | |
podInfo, err := podsClient.Get(context.TODO(), PodName, metav1.GetOptions{}) | |
if errors.IsNotFound(err) { | |
fmt.Printf("Pod %d not found in default namespace\n", PodName) | |
} else if statusError, isStatus := err.(*errors.StatusError); isStatus { | |
fmt.Printf("Error getting pod %v\n", statusError.ErrStatus.Message) | |
} else if err != nil { | |
panic(err.Error()) | |
} else { | |
fmt.Printf("Found %d pod in default namespace\n", PodName) | |
fmt.Println("%d Infomation is : %d", PodName, podInfo) | |
fmt.Println("\n\n\n\n") | |
fmt.Println("ObjectMeta Name : ", podInfo.ObjectMeta.GetName()) | |
fmt.Println("ObjectMeta Namespace : ", podInfo.ObjectMeta.GetNamespace()) | |
fmt.Println("ObjectMeta Annotations : ", podInfo.ObjectMeta.GetAnnotations()) | |
fmt.Println("ObjectMeta Labels : ", podInfo.ObjectMeta.GetLabels()) | |
// fmt.Println("PodIP : %d", podInfo.Status.PodIP) | |
// fmt.Println("Spec : %d", podInfo.Spec.ServiceAccountName) | |
} | |
} | |
func main() { | |
clientset, err := KubernetesClient() | |
check(err) | |
fmt.Println("--------") | |
listNodes(clientset) | |
fmt.Println("") | |
listNamespaces(clientset) | |
fmt.Println("") | |
listPods(clientset) | |
fmt.Println("") | |
listDeployments(clientset) | |
fmt.Println("") | |
fmt.Println("--------") | |
getPods(clientset, "docker-registry") | |
} | |
// check simply panics if there is an error. | |
func check(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment