mkdir [project]
cd [project]
export GO111MODULE=on
go mod init
go get k8s.io/[email protected]
[copy in main.go]
go build main.go
./main
Created
July 19, 2019 17:29
-
-
Save trevorlinton/2b20b5ba7a99b6d1dad57a876f5675d9 to your computer and use it in GitHub Desktop.
Create kubernetes client
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
// Note: the example only works with the code within the same release/branch. | |
package main | |
import ( | |
"flag" | |
"fmt" | |
"os" | |
"path/filepath" | |
"k8s.io/apimachinery/pkg/api/errors" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/tools/clientcmd" | |
"k8s.io/client-go/rest" | |
// | |
// Uncomment to load all auth plugins | |
// _ "k8s.io/client-go/plugin/pkg/client/auth" | |
// | |
// Or uncomment to load specific auth plugins | |
// _ "k8s.io/client-go/plugin/pkg/client/auth/azure" | |
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" | |
// _ "k8s.io/client-go/plugin/pkg/client/auth/oidc" | |
// _ "k8s.io/client-go/plugin/pkg/client/auth/openstack" | |
) | |
func main() { | |
var kubeconfig *string | |
var namespace *string | |
if home := 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") | |
} | |
namespace = flag.String("namespace", "default", "The namespace to list pods from") | |
if namespace == nil { | |
panic(fmt.Errorf("Unable to find null namespace")) | |
} | |
flag.Parse() | |
// use the current context in kubeconfig | |
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) | |
if err != nil { | |
// See if we are in cluster if we fail to read a kube config. | |
config, err = rest.InClusterConfig() | |
if err != nil { | |
panic(err.Error()) | |
} | |
} | |
// create the clientset | |
clientset, err := kubernetes.NewForConfig(config) | |
if err != nil { | |
panic(err.Error()) | |
} | |
pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{}) | |
if err != nil { | |
panic(err.Error()) | |
} | |
fmt.Printf("There are %d pods in the cluster\n", len(pods.Items)) | |
// Examples for error handling: | |
// - Use helper functions like e.g. errors.IsNotFound() | |
// - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message | |
pods, err = clientset.CoreV1().Pods(*namespace).List(metav1.ListOptions{}) | |
if errors.IsNotFound(err) { | |
fmt.Printf("Pods in namespace %s not found\n", *namespace) | |
} else if statusError, isStatus := err.(*errors.StatusError); isStatus { | |
fmt.Printf("Error getting pods in namespace %s: %v\n", | |
*namespace, statusError.ErrStatus.Message) | |
} else if err != nil { | |
panic(err.Error()) | |
} else { | |
fmt.Printf("Found pods in namespace %s\n", *namespace) | |
} | |
fmt.Printf("%#+v\n", pods); | |
} | |
func homeDir() string { | |
if h := os.Getenv("HOME"); h != "" { | |
return h | |
} | |
return os.Getenv("USERPROFILE") // windows | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment