-
Notifications
You must be signed in to change notification settings - Fork 37
/
tprClient.go
85 lines (76 loc) · 1.96 KB
/
tprClient.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"k8s.io/client-go/pkg/api"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/runtime"
"k8s.io/client-go/pkg/runtime/schema"
"k8s.io/client-go/pkg/runtime/serializer"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
type podToServiceClient struct {
rest *rest.RESTClient
}
func (c *podToServiceClient) Create(body *PodToService) (*PodToService, error) {
var ret PodToService
err := c.rest.Post().
Resource("podtoservices").
Namespace(api.NamespaceDefault).
Body(body).
Do().Into(&ret)
return &ret, err
}
func (c *podToServiceClient) Update(body *PodToService) (*PodToService, error) {
var ret PodToService
err := c.rest.Put().
Resource("podtoservices").
Namespace(api.NamespaceDefault).
Name(body.Metadata.Name).
Body(body).
Do().Into(&ret)
return &ret, err
}
func (c *podToServiceClient) Get(name string) (*PodToService, error) {
var ret PodToService
err := c.rest.Get().
Resource("podtoservices").
Namespace(api.NamespaceDefault).
Name(name).
Do().Into(&ret)
return &ret, err
}
func configureClient(config *rest.Config) {
groupversion := schema.GroupVersion{
Group: "caesarxuchao.io",
Version: "v1",
}
config.GroupVersion = &groupversion
config.APIPath = "/apis"
// Currently TPR only supports JSON
config.ContentType = runtime.ContentTypeJSON
config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: api.Codecs}
schemeBuilder := runtime.NewSchemeBuilder(
func(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(
groupversion,
&PodToService{},
&PodToServiceList{},
&v1.ListOptions{},
&v1.DeleteOptions{},
)
return nil
})
schemeBuilder.AddToScheme(api.Scheme)
}
func getTPRClientOrDie(kubeconfig string) *podToServiceClient {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
panic(err)
}
configureClient(config)
rest, err := rest.RESTClientFor(config)
if err != nil {
panic(err)
}
return &podToServiceClient{rest}
}