mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-03 16:16:06 +00:00
Added example for status errors in go client
Kubernetes-commit: 8ca1732023621063d1c91205cdf0efa4b03b5a30
This commit is contained in:
committed by
Kubernetes Publisher
parent
b3357e1e10
commit
2c59aca317
@@ -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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user