mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-05 17:10:27 +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
@@ -21,6 +21,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
"k8s.io/client-go/rest"
|
"k8s.io/client-go/rest"
|
||||||
@@ -43,6 +44,17 @@ func main() {
|
|||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
|
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)
|
time.Sleep(10 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -24,6 +24,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
@@ -57,6 +58,17 @@ func main() {
|
|||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
|
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)
|
time.Sleep(10 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user