Created
December 23, 2017 03:57
-
-
Save ks888/029f5ec72eb4a03e3bc5658dcb1d1c2c to your computer and use it in GitHub Desktop.
Client-go usage example 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"log" | |
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/apimachinery/pkg/runtime" | |
"k8s.io/client-go/kubernetes/fake" | |
"k8s.io/client-go/testing" | |
) | |
func main() { | |
client := newClient() | |
setServerError(client) | |
pods, err := client.CoreV1().Pods("").List(meta_v1.ListOptions{}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, pod := range pods.Items { | |
fmt.Println(pod.Name) | |
} | |
} | |
func newClient() *fake.Clientset { | |
return fake.NewSimpleClientset() | |
} | |
func setServerError(client *fake.Clientset) { | |
client.Fake.PrependReactor("list", "pods", func(action testing.Action) (bool, runtime.Object, error) { | |
return true, nil, errors.New("server error") | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment