-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient_example_test.go
67 lines (56 loc) · 1.38 KB
/
client_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package client_test
import (
"context"
"fmt"
client "github.com/castai/k8s-client-go"
corev1 "github.com/castai/k8s-client-go/types/core/v1"
metav1 "github.com/castai/k8s-client-go/types/meta/v1"
)
func ExampleObjectAPI() {
kc, err := client.NewInCluster()
if err != nil {
// Handle err
return
}
ctx := context.Background()
endpointsAPI := client.NewObjectAPI[corev1.Endpoints](kc)
endpoints, err := endpointsAPI.Get(ctx, "kube-system", "kubelet", metav1.GetOptions{})
if err != nil {
// Handle err
return
}
fmt.Printf("%+v\n", endpoints)
events, err := endpointsAPI.Watch(ctx, "kube-system", "kubelet", metav1.ListOptions{})
if err != nil {
// Handle err
return
}
for e := range events.ResultChan() {
fmt.Printf("%s: %+v\n", e.Type, e.Object)
}
// Custom types
podAPI := client.NewObjectAPI[CustomPod](kc)
pod, err := podAPI.Get(ctx, "kube-system", "core-dns-123", metav1.GetOptions{})
if err != nil {
// Handle err
return
}
fmt.Printf("%+v\n", pod)
}
type CustomPod struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
}
func (p CustomPod) GetObjectMeta() metav1.ObjectMeta {
return p.ObjectMeta
}
func (p CustomPod) GetTypeMeta() metav1.TypeMeta {
return p.TypeMeta
}
func (p CustomPod) GVR() metav1.GroupVersionResource {
return metav1.GroupVersionResource{
Group: "",
Version: "v1",
Resource: "pods",
}
}