mirror of
https://github.com/ceph/ceph-csi.git
synced 2024-12-19 03:20:20 +00:00
65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
/*
|
|
Copyright 2016 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
// Note: the example only works with the code within the same release/branch.
|
|
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 {
|
|
pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
fmt.Printf("There are %d pods in the cluster\n", len(pods.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().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 {
|
|
fmt.Printf("Error getting pod %v\n", statusError.ErrStatus.Message)
|
|
} else if err != nil {
|
|
panic(err.Error())
|
|
} else {
|
|
fmt.Printf("Found pod\n")
|
|
}
|
|
|
|
time.Sleep(10 * time.Second)
|
|
}
|
|
}
|