-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathclient.go
316 lines (280 loc) · 10 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright © 2019 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v1beta1
import (
"context"
"time"
apis_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
v1beta1 "knative.dev/eventing/pkg/apis/eventing/v1beta1"
"knative.dev/eventing/pkg/client/clientset/versioned/scheme"
client_v1beta1 "knative.dev/eventing/pkg/client/clientset/versioned/typed/eventing/v1beta1"
duckv1 "knative.dev/pkg/apis/duck/v1"
kn_errors "knative.dev/client/pkg/errors"
"knative.dev/client/pkg/util"
"knative.dev/client/pkg/wait"
)
// KnEventingClient to Eventing Sources. All methods are relative to the
// namespace specified during construction
type KnEventingClient interface {
// Namespace in which this client is operating for
Namespace() string
// CreateTrigger is used to create an instance of trigger
CreateTrigger(trigger *v1beta1.Trigger) error
// DeleteTrigger is used to delete an instance of trigger
DeleteTrigger(name string) error
// GetTrigger is used to get an instance of trigger
GetTrigger(name string) (*v1beta1.Trigger, error)
// ListTrigger returns list of trigger CRDs
ListTriggers() (*v1beta1.TriggerList, error)
// UpdateTrigger is used to update an instance of trigger
UpdateTrigger(trigger *v1beta1.Trigger) error
// CreateBroker is used to create an instance of broker
CreateBroker(broker *v1beta1.Broker) error
// GetBroker is used to get an instance of broker
GetBroker(name string) (*v1beta1.Broker, error)
// DeleteBroker is used to delete an instance of broker
DeleteBroker(name string, timeout time.Duration) error
// ListBroker returns list of broker CRDs
ListBrokers() (*v1beta1.BrokerList, error)
}
// KnEventingClient is a combination of Sources client interface and namespace
// Temporarily help to add sources dependencies
// May be changed when adding real sources features
type knEventingClient struct {
client client_v1beta1.EventingV1beta1Interface
namespace string
}
// NewKnEventingClient is to invoke Eventing Sources Client API to create object
func NewKnEventingClient(client client_v1beta1.EventingV1beta1Interface, namespace string) KnEventingClient {
return &knEventingClient{
client: client,
namespace: namespace,
}
}
//CreateTrigger is used to create an instance of trigger
func (c *knEventingClient) CreateTrigger(trigger *v1beta1.Trigger) error {
_, err := c.client.Triggers(c.namespace).Create(context.TODO(), trigger, meta_v1.CreateOptions{})
if err != nil {
return kn_errors.GetError(err)
}
return nil
}
//DeleteTrigger is used to delete an instance of trigger
func (c *knEventingClient) DeleteTrigger(name string) error {
err := c.client.Triggers(c.namespace).Delete(context.TODO(), name, apis_v1.DeleteOptions{})
if err != nil {
return kn_errors.GetError(err)
}
return nil
}
//GetTrigger is used to get an instance of trigger
func (c *knEventingClient) GetTrigger(name string) (*v1beta1.Trigger, error) {
trigger, err := c.client.Triggers(c.namespace).Get(context.TODO(), name, apis_v1.GetOptions{})
if err != nil {
return nil, kn_errors.GetError(err)
}
return trigger, nil
}
func (c *knEventingClient) ListTriggers() (*v1beta1.TriggerList, error) {
triggerList, err := c.client.Triggers(c.namespace).List(context.TODO(), apis_v1.ListOptions{})
if err != nil {
return nil, kn_errors.GetError(err)
}
triggerListNew := triggerList.DeepCopy()
err = updateEventingGVK(triggerListNew)
if err != nil {
return nil, err
}
triggerListNew.Items = make([]v1beta1.Trigger, len(triggerList.Items))
for idx, trigger := range triggerList.Items {
triggerClone := trigger.DeepCopy()
err := updateEventingGVK(triggerClone)
if err != nil {
return nil, err
}
triggerListNew.Items[idx] = *triggerClone
}
return triggerListNew, nil
}
//CreateTrigger is used to create an instance of trigger
func (c *knEventingClient) UpdateTrigger(trigger *v1beta1.Trigger) error {
_, err := c.client.Triggers(c.namespace).Update(context.TODO(), trigger, meta_v1.UpdateOptions{})
if err != nil {
return kn_errors.GetError(err)
}
return nil
}
// Return the client's namespace
func (c *knEventingClient) Namespace() string {
return c.namespace
}
// update with the v1beta1 group + version
func updateEventingGVK(obj runtime.Object) error {
return util.UpdateGroupVersionKindWithScheme(obj, v1beta1.SchemeGroupVersion, scheme.Scheme)
}
// TriggerBuilder is for building the trigger
type TriggerBuilder struct {
trigger *v1beta1.Trigger
}
// NewTriggerBuilder for building trigger object
func NewTriggerBuilder(name string) *TriggerBuilder {
return &TriggerBuilder{trigger: &v1beta1.Trigger{
ObjectMeta: meta_v1.ObjectMeta{
Name: name,
},
}}
}
// NewTriggerBuilderFromExisting for building the object from existing Trigger object
func NewTriggerBuilderFromExisting(trigger *v1beta1.Trigger) *TriggerBuilder {
return &TriggerBuilder{trigger: trigger.DeepCopy()}
}
// Namespace for this trigger
func (b *TriggerBuilder) Namespace(ns string) *TriggerBuilder {
b.trigger.Namespace = ns
return b
}
// Subscriber for the trigger to send to (it's a Sink actually)
func (b *TriggerBuilder) Subscriber(subscriber *duckv1.Destination) *TriggerBuilder {
b.trigger.Spec.Subscriber = *subscriber
return b
}
// Broker to set the broker of trigger object
func (b *TriggerBuilder) Broker(broker string) *TriggerBuilder {
b.trigger.Spec.Broker = broker
return b
}
// InjectBroker to add annotation to setup default broker
func (b *TriggerBuilder) InjectBroker(inject bool) *TriggerBuilder {
if inject {
meta_v1.SetMetaDataAnnotation(&b.trigger.ObjectMeta, v1beta1.InjectionAnnotation, "enabled")
} else {
if meta_v1.HasAnnotation(b.trigger.ObjectMeta, v1beta1.InjectionAnnotation) {
delete(b.trigger.ObjectMeta.Annotations, v1beta1.InjectionAnnotation)
}
}
return b
}
func (b *TriggerBuilder) Filters(filters map[string]string) *TriggerBuilder {
if len(filters) == 0 {
b.trigger.Spec.Filter = &v1beta1.TriggerFilter{}
return b
}
filter := b.trigger.Spec.Filter
if filter == nil {
filter = &v1beta1.TriggerFilter{}
b.trigger.Spec.Filter = filter
}
filter.Attributes = v1beta1.TriggerFilterAttributes{}
for k, v := range filters {
filter.Attributes[k] = v
}
return b
}
// Build to return an instance of trigger object
func (b *TriggerBuilder) Build() *v1beta1.Trigger {
return b.trigger
}
// CreateBroker is used to create an instance of broker
func (c *knEventingClient) CreateBroker(broker *v1beta1.Broker) error {
_, err := c.client.Brokers(c.namespace).Create(context.TODO(), broker, meta_v1.CreateOptions{})
if err != nil {
return kn_errors.GetError(err)
}
return nil
}
// GetBroker is used to get an instance of broker
func (c *knEventingClient) GetBroker(name string) (*v1beta1.Broker, error) {
trigger, err := c.client.Brokers(c.namespace).Get(context.TODO(), name, apis_v1.GetOptions{})
if err != nil {
return nil, kn_errors.GetError(err)
}
return trigger, nil
}
// WatchBroker is used to create watcher object
func (c *knEventingClient) WatchBroker(name string, timeout time.Duration) (watch.Interface, error) {
return wait.NewWatcher(c.client.Brokers(c.namespace).Watch,
c.client.RESTClient(), c.namespace, "brokers", name, timeout)
}
// DeleteBroker is used to delete an instance of broker and wait for completion until given timeout
// For `timeout == 0` delete is performed async without any wait
func (c *knEventingClient) DeleteBroker(name string, timeout time.Duration) error {
if timeout == 0 {
return c.deleteBroker(name, apis_v1.DeletePropagationBackground)
}
waitC := make(chan error)
go func() {
waitForEvent := wait.NewWaitForEvent("broker", c.WatchBroker, func(evt *watch.Event) bool { return evt.Type == watch.Deleted })
err, _ := waitForEvent.Wait(name, wait.Options{Timeout: &timeout}, wait.NoopMessageCallback())
waitC <- err
}()
err := c.deleteBroker(name, apis_v1.DeletePropagationForeground)
if err != nil {
return err
}
return <-waitC
}
// deleteBroker is used to delete an instance of broker
func (c *knEventingClient) deleteBroker(name string, propagationPolicy apis_v1.DeletionPropagation) error {
err := c.client.Brokers(c.namespace).Delete(context.TODO(), name, apis_v1.DeleteOptions{PropagationPolicy: &propagationPolicy})
if err != nil {
return kn_errors.GetError(err)
}
return nil
}
// ListBrokers is used to retrieve the list of broker instances
func (c *knEventingClient) ListBrokers() (*v1beta1.BrokerList, error) {
brokerList, err := c.client.Brokers(c.namespace).List(context.TODO(), apis_v1.ListOptions{})
if err != nil {
return nil, kn_errors.GetError(err)
}
brokerListNew := brokerList.DeepCopy()
err = updateEventingGVK(brokerListNew)
if err != nil {
return nil, err
}
brokerListNew.Items = make([]v1beta1.Broker, len(brokerList.Items))
for idx, trigger := range brokerList.Items {
triggerClone := trigger.DeepCopy()
err := updateEventingGVK(triggerClone)
if err != nil {
return nil, err
}
brokerListNew.Items[idx] = *triggerClone
}
return brokerListNew, nil
}
// BrokerBuilder is for building the broker
type BrokerBuilder struct {
broker *v1beta1.Broker
}
// NewBrokerBuilder for building broker object
func NewBrokerBuilder(name string) *BrokerBuilder {
return &BrokerBuilder{broker: &v1beta1.Broker{
ObjectMeta: meta_v1.ObjectMeta{
Name: name,
},
}}
}
// Namespace for broker builder
func (b *BrokerBuilder) Namespace(ns string) *BrokerBuilder {
b.broker.Namespace = ns
return b
}
// Build to return an instance of broker object
func (b *BrokerBuilder) Build() *v1beta1.Broker {
return b.broker
}