From 8ca1732023621063d1c91205cdf0efa4b03b5a30 Mon Sep 17 00:00:00 2001 From: Marc Sluiter Date: Mon, 29 May 2017 20:39:02 +0200 Subject: [PATCH 1/4] Added example for status errors in go client --- .../examples/in-cluster-client-configuration/main.go | 12 ++++++++++++ .../out-of-cluster-client-configuration/main.go | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/staging/src/k8s.io/client-go/examples/in-cluster-client-configuration/main.go b/staging/src/k8s.io/client-go/examples/in-cluster-client-configuration/main.go index b94ff440a87..1617b38310d 100644 --- a/staging/src/k8s.io/client-go/examples/in-cluster-client-configuration/main.go +++ b/staging/src/k8s.io/client-go/examples/in-cluster-client-configuration/main.go @@ -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) } } diff --git a/staging/src/k8s.io/client-go/examples/out-of-cluster-client-configuration/main.go b/staging/src/k8s.io/client-go/examples/out-of-cluster-client-configuration/main.go index b76278b7e94..484accfee13 100644 --- a/staging/src/k8s.io/client-go/examples/out-of-cluster-client-configuration/main.go +++ b/staging/src/k8s.io/client-go/examples/out-of-cluster-client-configuration/main.go @@ -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) } } From 601ab10dbd08debca4b8a835f8dea00a26666060 Mon Sep 17 00:00:00 2001 From: Marc Sluiter Date: Mon, 29 May 2017 23:21:23 +0200 Subject: [PATCH 2/4] Updated error handling example based on PR feedback --- .../examples/in-cluster-client-configuration/main.go | 8 ++++++-- .../examples/out-of-cluster-client-configuration/main.go | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/staging/src/k8s.io/client-go/examples/in-cluster-client-configuration/main.go b/staging/src/k8s.io/client-go/examples/in-cluster-client-configuration/main.go index 1617b38310d..ee996a747bc 100644 --- a/staging/src/k8s.io/client-go/examples/in-cluster-client-configuration/main.go +++ b/staging/src/k8s.io/client-go/examples/in-cluster-client-configuration/main.go @@ -45,10 +45,14 @@ func main() { } fmt.Printf("There are %d pods in the cluster\n", len(pods.Items)) - // Example for handling status errors + // 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().Pods("").Get("ExamplePodName", metav1.GetOptions{}) - if statusError, isStatus := err.(*errors.StatusError); isStatus && statusError.Status().Reason == metav1.StatusReasonNotFound { + if errors.IsNotFound(err) { fmt.Printf("Pod not found\n") + } else if statusError, isStatus := err.(*errors.StatusError); isStatus { + fmt.Printf("Error getting pod %v\n", statusError.ErrStatus.Message) } else if err != nil { panic(err.Error()) } else { diff --git a/staging/src/k8s.io/client-go/examples/out-of-cluster-client-configuration/main.go b/staging/src/k8s.io/client-go/examples/out-of-cluster-client-configuration/main.go index 484accfee13..5d0f619ec1c 100644 --- a/staging/src/k8s.io/client-go/examples/out-of-cluster-client-configuration/main.go +++ b/staging/src/k8s.io/client-go/examples/out-of-cluster-client-configuration/main.go @@ -59,10 +59,14 @@ func main() { } fmt.Printf("There are %d pods in the cluster\n", len(pods.Items)) - // Example for handling status errors + // 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().Pods("").Get("ExamplePodName", metav1.GetOptions{}) - if statusError, isStatus := err.(*errors.StatusError); isStatus && statusError.Status().Reason == metav1.StatusReasonNotFound { + if errors.IsNotFound(err) { fmt.Printf("Pod not found\n") + } else if statusError, isStatus := err.(*errors.StatusError); isStatus { + fmt.Printf("Error getting pod %v\n", statusError.ErrStatus.Message) } else if err != nil { panic(err.Error()) } else { From a71bb87ec714276264282197c99bbd3f86fd7b55 Mon Sep 17 00:00:00 2001 From: Marc Sluiter Date: Tue, 30 May 2017 11:36:19 +0200 Subject: [PATCH 3/4] Added namespace and better pod name in client go example --- .../client-go/examples/in-cluster-client-configuration/main.go | 2 +- .../examples/out-of-cluster-client-configuration/main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/staging/src/k8s.io/client-go/examples/in-cluster-client-configuration/main.go b/staging/src/k8s.io/client-go/examples/in-cluster-client-configuration/main.go index ee996a747bc..f7150681759 100644 --- a/staging/src/k8s.io/client-go/examples/in-cluster-client-configuration/main.go +++ b/staging/src/k8s.io/client-go/examples/in-cluster-client-configuration/main.go @@ -48,7 +48,7 @@ func main() { // 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().Pods("").Get("ExamplePodName", metav1.GetOptions{}) + _, err = clientset.CoreV1().Pods("default").Get("example-xxxxx", metav1.GetOptions{}) if errors.IsNotFound(err) { fmt.Printf("Pod not found\n") } else if statusError, isStatus := err.(*errors.StatusError); isStatus { diff --git a/staging/src/k8s.io/client-go/examples/out-of-cluster-client-configuration/main.go b/staging/src/k8s.io/client-go/examples/out-of-cluster-client-configuration/main.go index 5d0f619ec1c..252307b1826 100644 --- a/staging/src/k8s.io/client-go/examples/out-of-cluster-client-configuration/main.go +++ b/staging/src/k8s.io/client-go/examples/out-of-cluster-client-configuration/main.go @@ -62,7 +62,7 @@ func main() { // 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().Pods("").Get("ExamplePodName", metav1.GetOptions{}) + _, err = clientset.CoreV1().Pods("default").Get("example-xxxxx", metav1.GetOptions{}) if errors.IsNotFound(err) { fmt.Printf("Pod not found\n") } else if statusError, isStatus := err.(*errors.StatusError); isStatus { From 92ac232790afefad522016f76b48dbb697a8e0f7 Mon Sep 17 00:00:00 2001 From: Marc Sluiter Date: Wed, 31 May 2017 17:14:27 +0200 Subject: [PATCH 4/4] run update-bazel.sh --- .../client-go/examples/in-cluster-client-configuration/BUILD | 1 + .../client-go/examples/out-of-cluster-client-configuration/BUILD | 1 + 2 files changed, 2 insertions(+) diff --git a/staging/src/k8s.io/client-go/examples/in-cluster-client-configuration/BUILD b/staging/src/k8s.io/client-go/examples/in-cluster-client-configuration/BUILD index 038801eed63..c6a83a87201 100644 --- a/staging/src/k8s.io/client-go/examples/in-cluster-client-configuration/BUILD +++ b/staging/src/k8s.io/client-go/examples/in-cluster-client-configuration/BUILD @@ -19,6 +19,7 @@ go_library( srcs = ["main.go"], tags = ["automanaged"], deps = [ + "//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/client-go/kubernetes:go_default_library", "//vendor/k8s.io/client-go/rest:go_default_library", diff --git a/staging/src/k8s.io/client-go/examples/out-of-cluster-client-configuration/BUILD b/staging/src/k8s.io/client-go/examples/out-of-cluster-client-configuration/BUILD index 92f4666d997..b38ada6f51c 100644 --- a/staging/src/k8s.io/client-go/examples/out-of-cluster-client-configuration/BUILD +++ b/staging/src/k8s.io/client-go/examples/out-of-cluster-client-configuration/BUILD @@ -19,6 +19,7 @@ go_library( srcs = ["main.go"], tags = ["automanaged"], deps = [ + "//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/client-go/kubernetes:go_default_library", "//vendor/k8s.io/client-go/tools/clientcmd:go_default_library",