/
client.go
119 lines (103 loc) · 2.99 KB
/
client.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package client
import (
"context"
"github.com/kyma-incubator/compass/components/operations-controller/api/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)
type OperationsClient struct {
restClient rest.Interface
namespace string
}
type OperationsClientInterface interface {
Operations(string) OperationsInterface
}
func NewForConfig(cfg *rest.Config) (OperationsClientInterface, error) {
if err := v1alpha1.AddToScheme(scheme.Scheme); err != nil {
return nil, err
}
cfg.ContentConfig.GroupVersion = &schema.GroupVersion{Group: v1alpha1.GroupVersion.Group, Version: v1alpha1.GroupVersion.Version}
cfg.APIPath = "/apis"
cfg.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{
CodecFactory: scheme.Codecs,
}
cfg.UserAgent = rest.DefaultKubernetesUserAgent()
c, err := rest.RESTClientFor(cfg)
if err != nil {
return nil, err
}
return &OperationsClient{
restClient: c,
}, nil
}
func (c *OperationsClient) Operations(namespace string) OperationsInterface {
c.namespace = namespace
return c
}
func (c *OperationsClient) List(ctx context.Context, opts metav1.ListOptions) (*v1alpha1.OperationList, error) {
result := v1alpha1.OperationList{}
err := c.restClient.Get().
Namespace(c.namespace).
Resource("operations").
VersionedParams(&opts, scheme.ParameterCodec).
Do(ctx).
Into(&result)
return &result, err
}
func (c *OperationsClient) Get(ctx context.Context, name string, opts metav1.GetOptions) (*v1alpha1.Operation, error) {
result := v1alpha1.Operation{}
err := c.restClient.
Get().
Namespace(c.namespace).
Resource("operations").
Name(name).
VersionedParams(&opts, scheme.ParameterCodec).
Do(ctx).
Into(&result)
return &result, err
}
func (c *OperationsClient) Create(ctx context.Context, operation *v1alpha1.Operation) (*v1alpha1.Operation, error) {
result := v1alpha1.Operation{}
err := c.restClient.
Post().
Namespace(c.namespace).
Resource("operations").
Body(operation).
Do(ctx).
Into(&result)
return &result, err
}
func (c *OperationsClient) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
return c.restClient.
Delete().
Namespace(c.namespace).
Resource("operations").
Name(name).
VersionedParams(&opts, scheme.ParameterCodec).
Do(ctx).
Error()
}
func (c *OperationsClient) Update(ctx context.Context, operation *v1alpha1.Operation) (*v1alpha1.Operation, error) {
err := c.restClient.
Put().
Namespace(c.namespace).
Resource("operations").
Name(operation.Name).
Body(operation).
Do(ctx).
Into(operation)
return operation, err
}
func (c *OperationsClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
opts.Watch = true
return c.restClient.
Get().
Namespace(c.namespace).
Resource("operations").
VersionedParams(&opts, scheme.ParameterCodec).
Watch(ctx)
}