Added example for status errors in go client

This commit is contained in:
Marc Sluiter 2017-05-29 20:39:02 +02:00
parent a57c33bd28
commit 8ca1732023
2 changed files with 24 additions and 0 deletions

View File

@ -21,6 +21,7 @@ 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"
@ -43,6 +44,17 @@ func main() {
panic(err.Error())
}
fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
// Example for handling status errors
_, err = clientset.CoreV1().Pods("").Get("ExamplePodName", metav1.GetOptions{})
if statusError, isStatus := err.(*errors.StatusError); isStatus && statusError.Status().Reason == metav1.StatusReasonNotFound {
fmt.Printf("Pod not found\n")
} else if err != nil {
panic(err.Error())
} else {
fmt.Printf("Found pod\n")
}
time.Sleep(10 * time.Second)
}
}

View File

@ -24,6 +24,7 @@ import (
"path/filepath"
"time"
"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"
@ -57,6 +58,17 @@ func main() {
panic(err.Error())
}
fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
// Example for handling status errors
_, err = clientset.CoreV1().Pods("").Get("ExamplePodName", metav1.GetOptions{})
if statusError, isStatus := err.(*errors.StatusError); isStatus && statusError.Status().Reason == metav1.StatusReasonNotFound {
fmt.Printf("Pod not found\n")
} else if err != nil {
panic(err.Error())
} else {
fmt.Printf("Found pod\n")
}
time.Sleep(10 * time.Second)
}
}