-
Notifications
You must be signed in to change notification settings - Fork 741
/
standar-k8s.go
251 lines (213 loc) · 8.34 KB
/
standar-k8s.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
/*
Copyright 2020 BlackRock, Inc.
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 standard_k8s
import (
"fmt"
"strconv"
"time"
"github.com/imdario/mergo"
"github.com/pkg/errors"
"go.uber.org/zap"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
"github.com/argoproj/argo-events/sensors/policy"
"github.com/argoproj/argo-events/sensors/triggers"
"github.com/argoproj/argo-events/store"
)
// StandardK8STrigger implements Trigger interface for standard Kubernetes resources
type StandardK8sTrigger struct {
// K8sClient is kubernetes client
K8sClient kubernetes.Interface
// Dynamic client is Kubernetes dymalic client
DynamicClient dynamic.Interface
// Sensor object
Sensor *v1alpha1.Sensor
// Trigger definition
Trigger *v1alpha1.Trigger
// logger to log stuff
Logger *zap.Logger
namespableDynamicClient dynamic.NamespaceableResourceInterface
}
// NewStandardK8sTrigger returns a new StandardK8STrigger
func NewStandardK8sTrigger(k8sClient kubernetes.Interface, dynamicClient dynamic.Interface, sensor *v1alpha1.Sensor, trigger *v1alpha1.Trigger, logger *zap.Logger) *StandardK8sTrigger {
return &StandardK8sTrigger{
K8sClient: k8sClient,
DynamicClient: dynamicClient,
Sensor: sensor,
Trigger: trigger,
Logger: logger,
}
}
// FetchResource fetches the trigger resource from external source
func (k8sTrigger *StandardK8sTrigger) FetchResource() (interface{}, error) {
trigger := k8sTrigger.Trigger
if trigger.Template.K8s.Source == nil {
return nil, errors.Errorf("trigger source for k8s is empty")
}
creds, err := store.GetCredentials(trigger.Template.K8s.Source)
if err != nil {
return nil, err
}
reader, err := store.GetArtifactReader(trigger.Template.K8s.Source, creds)
if err != nil {
return nil, err
}
var rObj runtime.Object
// uObj will either hold the resource definition stored in the trigger or just
// a stub to provide enough information to fetch the object from K8s cluster
uObj, err := store.FetchArtifact(reader)
if err != nil {
return nil, err
}
k8sTrigger.namespableDynamicClient = k8sTrigger.DynamicClient.Resource(schema.GroupVersionResource{
Group: trigger.Template.K8s.GroupVersionResource.Group,
Version: trigger.Template.K8s.GroupVersionResource.Version,
Resource: trigger.Template.K8s.GroupVersionResource.Resource,
})
if trigger.Template.K8s.LiveObject && trigger.Template.K8s.Operation == v1alpha1.Update {
objName := uObj.GetName()
if objName == "" {
return nil, fmt.Errorf("resource name must be specified for fetching live object")
}
objNamespace := uObj.GetNamespace()
if objNamespace == "" {
return nil, fmt.Errorf("resource namespace must be specified for fetching live object")
}
rObj, err = k8sTrigger.namespableDynamicClient.Namespace(objNamespace).Get(objName, metav1.GetOptions{})
if err != nil {
return nil, err
}
} else {
rObj = uObj
}
return rObj, nil
}
// ApplyResourceParameters applies parameters to the trigger resource
func (k8sTrigger *StandardK8sTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) {
obj, ok := resource.(*unstructured.Unstructured)
if !ok {
return nil, errors.New("failed to interpret the trigger resource")
}
if err := triggers.ApplyResourceParameters(events, k8sTrigger.Trigger.Template.K8s.Parameters, obj); err != nil {
return nil, err
}
return obj, nil
}
// Execute executes the trigger
func (k8sTrigger *StandardK8sTrigger) Execute(events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) {
trigger := k8sTrigger.Trigger
obj, ok := resource.(*unstructured.Unstructured)
if !ok {
return nil, errors.New("failed to interpret the trigger resource")
}
namespace := obj.GetNamespace()
// Defaults to sensor's namespace
if namespace == "" {
namespace = k8sTrigger.Sensor.Namespace
}
obj.SetNamespace(namespace)
op := v1alpha1.Create
if trigger.Template.K8s.Operation != "" {
op = trigger.Template.K8s.Operation
}
// We might have a client from FetchResource() already, or we might not have one yet.
if k8sTrigger.namespableDynamicClient == nil {
k8sTrigger.namespableDynamicClient = k8sTrigger.DynamicClient.Resource(schema.GroupVersionResource{
Group: trigger.Template.K8s.GroupVersionResource.Group,
Version: trigger.Template.K8s.GroupVersionResource.Version,
Resource: trigger.Template.K8s.GroupVersionResource.Resource,
})
}
switch op {
case v1alpha1.Create:
k8sTrigger.Logger.Info("creating the object...")
// Add labels
labels := obj.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels["events.argoproj.io/sensor"] = k8sTrigger.Sensor.Name
labels["events.argoproj.io/trigger"] = trigger.Template.Name
labels["events.argoproj.io/action-timestamp"] = strconv.Itoa(int(time.Now().UnixNano() / int64(time.Millisecond)))
obj.SetLabels(labels)
return k8sTrigger.namespableDynamicClient.Namespace(namespace).Create(obj, metav1.CreateOptions{})
case v1alpha1.Update:
k8sTrigger.Logger.Info("updating the object...")
oldObj, err := k8sTrigger.namespableDynamicClient.Namespace(namespace).Get(obj.GetName(), metav1.GetOptions{})
if err != nil && apierrors.IsNotFound(err) {
k8sTrigger.Logger.Info("object not found, creating the object...")
return k8sTrigger.namespableDynamicClient.Namespace(namespace).Create(obj, metav1.CreateOptions{})
} else if err != nil {
return nil, errors.Errorf("failed to retrieve existing object. err: %+v\n", err)
}
if err := mergo.Merge(oldObj, obj, mergo.WithOverride); err != nil {
return nil, errors.Errorf("failed to update the object. err: %+v\n", err)
}
return k8sTrigger.namespableDynamicClient.Namespace(namespace).Update(oldObj, metav1.UpdateOptions{})
case v1alpha1.Patch:
k8sTrigger.Logger.Info("patching the object...")
_, err := k8sTrigger.namespableDynamicClient.Namespace(namespace).Get(obj.GetName(), metav1.GetOptions{})
if err != nil && apierrors.IsNotFound(err) {
k8sTrigger.Logger.Info("object not found, creating the object...")
return k8sTrigger.namespableDynamicClient.Namespace(namespace).Create(obj, metav1.CreateOptions{})
} else if err != nil {
return nil, errors.Errorf("failed to retrieve existing object. err: %+v\n", err)
}
if k8sTrigger.Trigger.Template.K8s.PatchStrategy == "" {
k8sTrigger.Trigger.Template.K8s.PatchStrategy = k8stypes.MergePatchType
}
body, err := obj.MarshalJSON()
if err != nil {
return nil, errors.Errorf("failed to marshal object into JSON schema. err: %+v\n", err)
}
return k8sTrigger.namespableDynamicClient.Namespace(namespace).Patch(obj.GetName(), k8sTrigger.Trigger.Template.K8s.PatchStrategy, body, metav1.PatchOptions{})
default:
return nil, errors.Errorf("unknown operation type %s", string(op))
}
}
// ApplyPolicy applies the policy on the trigger
func (k8sTrigger *StandardK8sTrigger) ApplyPolicy(resource interface{}) error {
trigger := k8sTrigger.Trigger
if trigger.Policy == nil || trigger.Policy.K8s == nil || trigger.Policy.K8s.Labels == nil {
return nil
}
obj, ok := resource.(*unstructured.Unstructured)
if !ok {
return errors.New("failed to interpret the trigger resource")
}
p := policy.NewResourceLabels(trigger, k8sTrigger.namespableDynamicClient, obj)
if p == nil {
return nil
}
err := p.ApplyPolicy()
if err != nil {
switch err {
case wait.ErrWaitTimeout:
if trigger.Policy.K8s.ErrorOnBackoffTimeout {
return errors.Errorf("failed to determine status of the triggered resource. setting trigger state as failed")
}
return nil
default:
return err
}
}
return nil
}