-
Notifications
You must be signed in to change notification settings - Fork 8
/
clients.gotmpl
257 lines (212 loc) · 10.2 KB
/
clients.gotmpl
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package {{.Version}}
{{/* Import prefix required because mockgen doesn't handle dot imports correctly */}}
{{ $import_prefix := "" }}
import (
"context"
"github.com/solo-io/skv2/pkg/controllerutils"
"github.com/solo-io/skv2/pkg/multicluster"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
{{- if $.CustomTypesImportPath }}
{{ group_import_name $ }} "{{ $.CustomTypesImportPath }}"
{{ $import_prefix = (printf "%s." (group_import_name $)) }}
{{- end}}
)
// MulticlusterClientset for the {{.Group}}/{{.Version}} APIs
type MulticlusterClientset interface {
// Cluster returns a Clientset for the given cluster
Cluster(cluster string) (Clientset, error)
}
type multiclusterClientset struct {
client multicluster.Client
}
func NewMulticlusterClientset(client multicluster.Client) MulticlusterClientset {
return &multiclusterClientset{client: client}
}
func (m *multiclusterClientset) Cluster(cluster string) (Clientset, error) {
client, err := m.client.Cluster(cluster)
if err != nil {
return nil, err
}
return NewClientset(client), nil
}
// clienset for the {{.Group}}/{{.Version}} APIs
type Clientset interface {
{{- range $resource := $.Resources }}
// clienset for the {{.Group}}/{{.Version}} APIs
{{ pluralize $resource.Kind }}() {{ $resource.Kind }}Client
{{- end }}
}
type clientSet struct {
client client.Client
}
func NewClientsetFromConfig(cfg *rest.Config) (Clientset, error) {
scheme := scheme.Scheme
if err := {{ $import_prefix }}SchemeBuilder.AddToScheme(scheme); err != nil{
return nil, err
}
client, err := client.New(cfg, client.Options{
Scheme: scheme,
})
if err != nil {
return nil, err
}
return NewClientset(client), nil
}
func NewClientset(client client.Client) Clientset {
return &clientSet{client: client}
}
{{- range $resource := $.Resources }}
// clienset for the {{.Group}}/{{.Version}} APIs
func (c *clientSet) {{ pluralize $resource.Kind }}() {{ $resource.Kind }}Client {
return New{{ $resource.Kind }}Client(c.client)
}
{{- end }}
{{- range $resource := $.Resources }}
{{- $kindLowerCamel := lower_camel $resource.Kind }}
// Reader knows how to read and list {{ $resource.Kind }}s.
type {{ $resource.Kind }}Reader interface {
// Get retrieves a {{ $resource.Kind }} for the given object key
{{- if $resource.ClusterScoped }}
Get{{ $resource.Kind }}(ctx context.Context, name string) (*{{ $import_prefix }}{{ $resource.Kind }}, error)
{{- else }}
Get{{ $resource.Kind }}(ctx context.Context, key client.ObjectKey) (*{{ $import_prefix }}{{ $resource.Kind }}, error)
{{- end }}
// List retrieves list of {{ $resource.Kind }}s for a given namespace and list options.
List{{ $resource.Kind }}(ctx context.Context, opts ...client.ListOption) (*{{ $import_prefix }}{{ $resource.Kind }}List, error)
}
// {{ $resource.Kind }}TransitionFunction instructs the {{ $resource.Kind }}Writer how to transition between an existing
// {{ $resource.Kind }} object and a desired on an Upsert
type {{ $resource.Kind }}TransitionFunction func(existing, desired *{{ $import_prefix }}{{ $resource.Kind }}) error
// Writer knows how to create, delete, and update {{ $resource.Kind }}s.
type {{ $resource.Kind }}Writer interface {
// Create saves the {{ $resource.Kind }} object.
Create{{ $resource.Kind }}(ctx context.Context, obj *{{ $import_prefix }}{{ $resource.Kind }}, opts ...client.CreateOption) error
// Delete deletes the {{ $resource.Kind }} object.
{{- if $resource.ClusterScoped }}
Delete{{ $resource.Kind }}(ctx context.Context, name string, opts ...client.DeleteOption) error
{{- else }}
Delete{{ $resource.Kind }}(ctx context.Context, key client.ObjectKey, opts ...client.DeleteOption) error
{{- end}}
// Update updates the given {{ $resource.Kind }} object.
Update{{ $resource.Kind }}(ctx context.Context, obj *{{ $import_prefix }}{{ $resource.Kind }}, opts ...client.UpdateOption) error
// Patch patches the given {{ $resource.Kind }} object.
Patch{{ $resource.Kind }}(ctx context.Context, obj *{{ $import_prefix }}{{ $resource.Kind }}, patch client.Patch, opts ...client.PatchOption) error
// DeleteAllOf deletes all {{ $resource.Kind }} objects matching the given options.
DeleteAllOf{{ $resource.Kind }}(ctx context.Context, opts ...client.DeleteAllOfOption) error
// Create or Update the {{ $resource.Kind }} object.
Upsert{{ $resource.Kind }}(ctx context.Context, obj *{{ $import_prefix }}{{ $resource.Kind }}, transitionFuncs ...{{ $resource.Kind }}TransitionFunction) error
}
// StatusWriter knows how to update status subresource of a {{ $resource.Kind }} object.
type {{ $resource.Kind }}StatusWriter interface {
// Update updates the fields corresponding to the status subresource for the
// given {{ $resource.Kind }} object.
Update{{ $resource.Kind }}Status(ctx context.Context, obj *{{ $import_prefix }}{{ $resource.Kind }}, opts ...client.UpdateOption) error
// Patch patches the given {{ $resource.Kind }} object's subresource.
Patch{{ $resource.Kind }}Status(ctx context.Context, obj *{{ $import_prefix }}{{ $resource.Kind }}, patch client.Patch, opts ...client.PatchOption) error
}
// Client knows how to perform CRUD operations on {{ $resource.Kind }}s.
type {{ $resource.Kind }}Client interface {
{{ $resource.Kind }}Reader
{{ $resource.Kind }}Writer
{{ $resource.Kind }}StatusWriter
}
type {{ $kindLowerCamel }}Client struct {
client client.Client
}
func New{{ $resource.Kind }}Client(client client.Client) *{{ $kindLowerCamel }}Client {
return &{{ $kindLowerCamel }}Client{client: client}
}
{{if $resource.ClusterScoped }}
func (c *{{ $kindLowerCamel }}Client) Get{{ $resource.Kind }}(ctx context.Context, name string) (*{{ $import_prefix }}{{ $resource.Kind }}, error) {
obj := &{{ $import_prefix }}{{ $resource.Kind }}{}
key := client.ObjectKey{
Name: name,
}
if err := c.client.Get(ctx, key, obj); err != nil {
return nil, err
}
return obj, nil
}
{{- else }}
func (c *{{ $kindLowerCamel }}Client) Get{{ $resource.Kind }}(ctx context.Context, key client.ObjectKey) (*{{ $import_prefix }}{{ $resource.Kind }}, error) {
obj := &{{ $import_prefix }}{{ $resource.Kind }}{}
if err := c.client.Get(ctx, key, obj); err != nil {
return nil, err
}
return obj, nil
}
{{- end}}
func (c *{{ $kindLowerCamel }}Client) List{{ $resource.Kind }}(ctx context.Context, opts ...client.ListOption) (*{{ $import_prefix }}{{ $resource.Kind }}List, error) {
list := &{{ $import_prefix }}{{ $resource.Kind }}List{}
if err := c.client.List(ctx, list, opts...); err != nil {
return nil, err
}
return list, nil
}
func (c *{{ $kindLowerCamel }}Client) Create{{ $resource.Kind }}(ctx context.Context, obj *{{ $import_prefix }}{{ $resource.Kind }}, opts ...client.CreateOption) error {
return c.client.Create(ctx, obj, opts...)
}
{{if $resource.ClusterScoped }}
func (c *{{ $kindLowerCamel }}Client) Delete{{ $resource.Kind }}(ctx context.Context, name string, opts ...client.DeleteOption) error {
obj := &{{ $import_prefix }}{{ $resource.Kind }}{}
obj.SetName(name)
return c.client.Delete(ctx, obj, opts...)
}
{{- else }}
func (c *{{ $kindLowerCamel }}Client) Delete{{ $resource.Kind }}(ctx context.Context, key client.ObjectKey, opts ...client.DeleteOption) error {
obj := &{{ $import_prefix }}{{ $resource.Kind }}{}
obj.SetName(key.Name)
obj.SetNamespace(key.Namespace)
return c.client.Delete(ctx, obj, opts...)
}
{{- end}}
func (c *{{ $kindLowerCamel }}Client) Update{{ $resource.Kind }}(ctx context.Context, obj *{{ $import_prefix }}{{ $resource.Kind }}, opts ...client.UpdateOption) error {
return c.client.Update(ctx, obj, opts...)
}
func (c *{{ $kindLowerCamel }}Client) Patch{{ $resource.Kind }}(ctx context.Context, obj *{{ $import_prefix }}{{ $resource.Kind }}, patch client.Patch, opts ...client.PatchOption) error {
return c.client.Patch(ctx, obj, patch, opts...)
}
func (c *{{ $kindLowerCamel }}Client) DeleteAllOf{{ $resource.Kind }}(ctx context.Context, opts ...client.DeleteAllOfOption) error {
obj := &{{ $import_prefix }}{{ $resource.Kind }}{}
return c.client.DeleteAllOf(ctx, obj, opts...)
}
func (c *{{ $kindLowerCamel }}Client) Upsert{{ $resource.Kind }}(ctx context.Context, obj *{{ $import_prefix }}{{ $resource.Kind }}, transitionFuncs ...{{ $resource.Kind }}TransitionFunction) error {
genericTxFunc := func(existing, desired runtime.Object) error {
for _, txFunc := range transitionFuncs {
if err := txFunc(existing.(*{{ $import_prefix }}{{ $resource.Kind }}), desired.(*{{ $import_prefix }}{{ $resource.Kind }})); err != nil {
return err
}
}
return nil
}
_, err := controllerutils.Upsert(ctx, c.client, obj, genericTxFunc)
return err
}
func (c *{{ $kindLowerCamel }}Client) Update{{ $resource.Kind }}Status(ctx context.Context, obj *{{ $import_prefix }}{{ $resource.Kind }}, opts ...client.UpdateOption) error {
return c.client.Status().Update(ctx, obj, opts...)
}
func (c *{{ $kindLowerCamel }}Client) Patch{{ $resource.Kind }}Status(ctx context.Context, obj *{{ $import_prefix }}{{ $resource.Kind }}, patch client.Patch, opts ...client.PatchOption) error {
return c.client.Status().Patch(ctx, obj, patch, opts...)
}
// Provides {{ $resource.Kind }}Clients for multiple clusters.
type Multicluster{{ $resource.Kind }}Client interface {
// Cluster returns a {{ $resource.Kind }}Client for the given cluster
Cluster(cluster string) ({{ $resource.Kind }}Client, error)
}
type multicluster{{ $resource.Kind }}Client struct {
client multicluster.Client
}
func NewMulticluster{{ $resource.Kind }}Client(client multicluster.Client) Multicluster{{ $resource.Kind }}Client {
return &multicluster{{ $resource.Kind }}Client{client: client}
}
func (m *multicluster{{ $resource.Kind }}Client) Cluster(cluster string) ({{ $resource.Kind }}Client, error) {
client, err := m.client.Cluster(cluster)
if err != nil {
return nil, err
}
return New{{ $resource.Kind }}Client(client), nil
}
{{- end }}