Created
September 26, 2017 14:58
-
-
Save jianchen2580/0898781d395fb88fadb57fd94f31a06c 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 ( | |
"fmt" | |
"time" | |
"k8s.io/apimachinery/pkg/api/errors" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/rest" | |
) | |
func main() { | |
// creates the in-cluster config | |
config, err := rest.InClusterConfig() | |
if err != nil { | |
panic(err.Error()) | |
} | |
// creates the clientset | |
clientset, err := kubernetes.NewForConfig(config) | |
if err != nil { | |
panic(err.Error()) | |
} | |
for { | |
nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{}) | |
if err != nil { | |
panic(err.Error()) | |
} | |
fmt.Printf("There are %d pods in the cluster\n", len(nodes.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 | |
_, err = clientset.CoreV1().Nodes().Get("example-xxxxx", metav1.GetOptions{}) | |
if errors.IsNotFound(err) { | |
fmt.Printf("Node not found\n") | |
} else if statusError, isStatus := err.(*errors.StatusError); isStatus { | |
fmt.Printf("Error getting node %v\n", statusError.ErrStatus.Message) | |
} else if err != nil { | |
panic(err.Error()) | |
} else { | |
fmt.Printf("Found node\n") | |
} | |
time.Sleep(10 * time.Second) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment