-
-
Save richardsonlima/e7ef70359f009f065362d17fe206940a to your computer and use it in GitHub Desktop.
Client-go usage examples
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 ( | |
"fmt" | |
"log" | |
"os" | |
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/tools/clientcmd" | |
) | |
func main() { | |
contextName := "" | |
if len(os.Args) >= 2 { | |
contextName = os.Args[1] | |
} | |
client, err := newClient(contextName) | |
if err != nil { | |
log.Fatal(err) | |
} | |
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(contextName string) (kubernetes.Interface, error) { | |
configOverrides := &clientcmd.ConfigOverrides{CurrentContext: contextName} | |
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() | |
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides).ClientConfig() | |
if err != nil { | |
return nil, err | |
} | |
return kubernetes.NewForConfig(config) | |
} |
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") | |
}) | |
} |
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 ( | |
"fmt" | |
"log" | |
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/tools/clientcmd" | |
) | |
func main() { | |
client, err := newClient() | |
if err != nil { | |
log.Fatal(err) | |
} | |
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() (kubernetes.Interface, error) { | |
kubeConfig, err := clientcmd.BuildConfigFromFlags("", clientcmd.RecommendedHomeFile) | |
if err != nil { | |
return nil, err | |
} | |
return kubernetes.NewForConfig(kubeConfig) | |
} |
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 ( | |
"fmt" | |
"log" | |
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/rest" | |
) | |
func main() { | |
client, err := newClient() | |
if err != nil { | |
log.Fatal(err) | |
} | |
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() (kubernetes.Interface, error) { | |
config, err := rest.InClusterConfig() | |
if err != nil { | |
return nil, err | |
} | |
return kubernetes.NewForConfig(config) | |
} |
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 ( | |
"fmt" | |
"log" | |
core_v1 "k8s.io/api/core/v1" | |
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes/fake" | |
) | |
func main() { | |
client := newClient() | |
if err := addTestData(client); err != nil { | |
log.Fatal(err) | |
} | |
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 addTestData(client *fake.Clientset) error { | |
pod := &core_v1.Pod{ | |
ObjectMeta: meta_v1.ObjectMeta{ | |
Namespace: core_v1.NamespaceDefault, | |
Name: "test", | |
}, | |
} | |
_, err := client.CoreV1().Pods(core_v1.NamespaceDefault).Create(pod) | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment