-
-
Save kainlite/7f387e1ef62dd88a1e8804810f5be46a to your computer and use it in GitHub Desktop.
Simple Kubernetes client-go example code
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 ( | |
"flag" | |
"fmt" | |
"os" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/rest" | |
"k8s.io/client-go/tools/clientcmd" | |
) | |
func main() { | |
kubeconfig := "" | |
flag.StringVar(&kubeconfig, "kubeconfig", kubeconfig, "kubeconfig file") | |
flag.Parse() | |
if kubeconfig == "" { | |
kubeconfig = os.Getenv("KUBECONFIG") | |
} | |
var ( | |
config *rest.Config | |
err error | |
) | |
if kubeconfig != "" { | |
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig) | |
} else { | |
config, err = rest.InClusterConfig() | |
} | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "error creating client: %v", err) | |
os.Exit(1) | |
} | |
client := kubernetes.NewForConfigOrDie(config) | |
list, err := client.CoreV1().Nodes().List(metav1.ListOptions{}) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "error listing nodes: %v", err) | |
os.Exit(1) | |
} | |
for _, node := range list.Items { | |
fmt.Printf("Node: %s\n", node.Name) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment