/
helm_workload_instance_gen.go
289 lines (268 loc) · 8.87 KB
/
helm_workload_instance_gen.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
// generated by 'threeport-sdk gen' for controller scaffolding - do not edit
package helmworkload
import (
"errors"
"fmt"
v0 "github.com/threeport/threeport/pkg/api/v0"
client "github.com/threeport/threeport/pkg/client/v0"
controller "github.com/threeport/threeport/pkg/controller/v0"
notifications "github.com/threeport/threeport/pkg/notifications/v0"
util "github.com/threeport/threeport/pkg/util/v0"
"os"
"os/signal"
"syscall"
"time"
)
// HelmWorkloadInstanceReconciler reconciles system state when a HelmWorkloadInstance
// is created, updated or deleted.
func HelmWorkloadInstanceReconciler(r *controller.Reconciler) {
r.ShutdownWait.Add(1)
reconcilerLog := r.Log.WithValues("reconcilerName", r.Name)
reconcilerLog.Info("reconciler started")
shutdown := false
// create a channel to receive OS signals
osSignals := make(chan os.Signal, 1)
lockReleased := make(chan bool, 1)
// register the os signals channel to receive SIGINT and SIGTERM signals
signal.Notify(osSignals, syscall.SIGINT, syscall.SIGTERM)
for {
// create a fresh log object per reconciliation loop so we don't
// accumulate values across multiple loops
log := r.Log.WithValues("reconcilerName", r.Name)
if shutdown {
break
}
// check for shutdown instruction
select {
case <-r.Shutdown:
shutdown = true
default:
// pull message off queue
msg := r.PullMessage()
if msg == nil {
continue
}
// consume message data to capture notification from API
notif, err := notifications.ConsumeMessage(msg.Data)
if err != nil {
log.Error(
err, "failed to consume message data from NATS",
"msgData", string(msg.Data),
)
r.RequeueRaw(msg)
log.V(1).Info("helm workload instance reconciliation requeued with identical payload and fixed delay")
continue
}
// decode the object that was sent in the notification
var helmWorkloadInstance v0.HelmWorkloadInstance
if err := helmWorkloadInstance.DecodeNotifObject(notif.Object); err != nil {
log.Error(err, "failed to marshal object map from consumed notification message")
r.RequeueRaw(msg)
log.V(1).Info("helm workload instance reconciliation requeued with identical payload and fixed delay")
continue
}
log = log.WithValues("helmWorkloadInstanceID", helmWorkloadInstance.ID)
// back off the requeue delay as needed
requeueDelay := controller.SetRequeueDelay(
notif.CreationTime,
)
// check for lock on object
locked, ok := r.CheckLock(&helmWorkloadInstance)
if locked || ok == false {
r.Requeue(&helmWorkloadInstance, requeueDelay, msg)
log.V(1).Info("helm workload instance reconciliation requeued")
continue
}
// set up handler to unlock and requeue on termination signal
go func() {
select {
case <-osSignals:
log.V(1).Info("received termination signal, performing unlock and requeue of helm workload instance")
r.UnlockAndRequeue(&helmWorkloadInstance, requeueDelay, lockReleased, msg)
case <-lockReleased:
log.V(1).Info("reached end of reconcile loop for helm workload instance, closing out signal handler")
}
}()
// put a lock on the reconciliation of the created object
if ok := r.Lock(&helmWorkloadInstance); !ok {
r.Requeue(&helmWorkloadInstance, requeueDelay, msg)
log.V(1).Info("helm workload instance reconciliation requeued")
continue
}
// retrieve latest version of object
latestHelmWorkloadInstance, err := client.GetHelmWorkloadInstanceByID(
r.APIClient,
r.APIServer,
*helmWorkloadInstance.ID,
)
// check if error is 404 - if object no longer exists, no need to requeue
if errors.Is(err, client.ErrObjectNotFound) {
log.Info(fmt.Sprintf(
"object with ID %d no longer exists - halting reconciliation",
*helmWorkloadInstance.ID,
))
r.ReleaseLock(&helmWorkloadInstance, lockReleased, msg, true)
continue
}
if err != nil {
log.Error(err, "failed to get helm workload instance by ID from API")
r.UnlockAndRequeue(&helmWorkloadInstance, requeueDelay, lockReleased, msg)
continue
}
helmWorkloadInstance = *latestHelmWorkloadInstance
// determine which operation and act accordingly
switch notif.Operation {
case notifications.NotificationOperationCreated:
if helmWorkloadInstance.DeletionScheduled != nil {
log.Info("helm workload instance scheduled for deletion - skipping create")
break
}
customRequeueDelay, err := helmWorkloadInstanceCreated(r, &helmWorkloadInstance, &log)
if err != nil {
log.Error(err, "failed to reconcile created helm workload instance object")
r.UnlockAndRequeue(
&helmWorkloadInstance,
requeueDelay,
lockReleased,
msg,
)
continue
}
if customRequeueDelay != 0 {
log.Info("create requeued for future reconciliation")
r.UnlockAndRequeue(
&helmWorkloadInstance,
customRequeueDelay,
lockReleased,
msg,
)
continue
}
case notifications.NotificationOperationUpdated:
customRequeueDelay, err := helmWorkloadInstanceUpdated(r, &helmWorkloadInstance, &log)
if err != nil {
log.Error(err, "failed to reconcile updated helm workload instance object")
r.UnlockAndRequeue(
&helmWorkloadInstance,
requeueDelay,
lockReleased,
msg,
)
continue
}
if customRequeueDelay != 0 {
log.Info("update requeued for future reconciliation")
r.UnlockAndRequeue(
&helmWorkloadInstance,
customRequeueDelay,
lockReleased,
msg,
)
continue
}
case notifications.NotificationOperationDeleted:
customRequeueDelay, err := helmWorkloadInstanceDeleted(r, &helmWorkloadInstance, &log)
if err != nil {
log.Error(err, "failed to reconcile deleted helm workload instance object")
r.UnlockAndRequeue(
&helmWorkloadInstance,
requeueDelay,
lockReleased,
msg,
)
continue
}
if customRequeueDelay != 0 {
log.Info("deletion requeued for future reconciliation")
r.UnlockAndRequeue(
&helmWorkloadInstance,
customRequeueDelay,
lockReleased,
msg,
)
continue
}
deletionTimestamp := util.TimePtr(time.Now().UTC())
deletedHelmWorkloadInstance := v0.HelmWorkloadInstance{
Common: v0.Common{ID: helmWorkloadInstance.ID},
Reconciliation: v0.Reconciliation{
DeletionAcknowledged: deletionTimestamp,
DeletionConfirmed: deletionTimestamp,
Reconciled: util.BoolPtr(true),
},
}
if err != nil {
log.Error(err, "failed to update helm workload instance to mark as reconciled")
r.UnlockAndRequeue(&helmWorkloadInstance, requeueDelay, lockReleased, msg)
continue
}
_, err = client.UpdateHelmWorkloadInstance(
r.APIClient,
r.APIServer,
&deletedHelmWorkloadInstance,
)
if err != nil {
log.Error(err, "failed to update helm workload instance to mark as deleted")
r.UnlockAndRequeue(&helmWorkloadInstance, requeueDelay, lockReleased, msg)
continue
}
_, err = client.DeleteHelmWorkloadInstance(
r.APIClient,
r.APIServer,
*helmWorkloadInstance.ID,
)
if err != nil {
log.Error(err, "failed to delete helm workload instance")
r.UnlockAndRequeue(&helmWorkloadInstance, requeueDelay, lockReleased, msg)
continue
}
default:
log.Error(
errors.New("unrecognized notifcation operation"),
"notification included an invalid operation",
)
r.UnlockAndRequeue(
&helmWorkloadInstance,
requeueDelay,
lockReleased,
msg,
)
continue
}
// set the object's Reconciled field to true if not deleted
if notif.Operation != notifications.NotificationOperationDeleted {
reconciledHelmWorkloadInstance := v0.HelmWorkloadInstance{
Common: v0.Common{ID: helmWorkloadInstance.ID},
Reconciliation: v0.Reconciliation{Reconciled: util.BoolPtr(true)},
}
updatedHelmWorkloadInstance, err := client.UpdateHelmWorkloadInstance(
r.APIClient,
r.APIServer,
&reconciledHelmWorkloadInstance,
)
if err != nil {
log.Error(err, "failed to update helm workload instance to mark as reconciled")
r.UnlockAndRequeue(&helmWorkloadInstance, requeueDelay, lockReleased, msg)
continue
}
log.V(1).Info(
"helm workload instance marked as reconciled in API",
"helm workload instanceName", updatedHelmWorkloadInstance.Name,
)
}
// release the lock on the reconciliation of the created object
if ok := r.ReleaseLock(&helmWorkloadInstance, lockReleased, msg, true); !ok {
log.Error(errors.New("helm workload instance remains locked - will unlock when TTL expires"), "")
} else {
log.V(1).Info("helm workload instance unlocked")
}
log.Info(fmt.Sprintf(
"helm workload instance successfully reconciled for %s operation",
notif.Operation,
))
}
}
r.Sub.Unsubscribe()
reconcilerLog.Info("reconciler shutting down")
r.ShutdownWait.Done()
}