-
Notifications
You must be signed in to change notification settings - Fork 23
/
workspace_controller_run_tasks.go
205 lines (170 loc) · 5.58 KB
/
workspace_controller_run_tasks.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package controllers
import (
"context"
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
tfc "github.com/hashicorp/go-tfe"
)
func runTasksDifference(a, b map[string]*tfc.WorkspaceRunTask) map[string]*tfc.WorkspaceRunTask {
d := make(map[string]*tfc.WorkspaceRunTask)
for k, v := range a {
if _, ok := b[k]; !ok {
d[k] = v
}
}
return d
}
func getRunTasksToCreate(spec, ws map[string]*tfc.WorkspaceRunTask) map[string]*tfc.WorkspaceRunTask {
return runTasksDifference(spec, ws)
}
func getRunTasksToUpdate(spec, ws map[string]*tfc.WorkspaceRunTask) map[string]*tfc.WorkspaceRunTask {
o := make(map[string]*tfc.WorkspaceRunTask)
if len(spec) == 0 || len(ws) == 0 {
return o
}
for ik, iv := range spec {
if wv, ok := ws[ik]; ok {
iv.ID = wv.ID
if !cmp.Equal(iv, wv, cmpopts.IgnoreFields(tfc.WorkspaceRunTask{}, "Workspace")) {
o[ik] = iv
}
}
}
return o
}
func getRunTasksToDelete(spec, ws map[string]*tfc.WorkspaceRunTask) map[string]*tfc.WorkspaceRunTask {
return runTasksDifference(ws, spec)
}
func (r *WorkspaceReconciler) createRunTasks(ctx context.Context, w *workspaceInstance, create map[string]*tfc.WorkspaceRunTask) error {
for _, rt := range create {
_, err := w.tfClient.Client.WorkspaceRunTasks.Create(ctx, w.instance.Status.WorkspaceID, tfc.WorkspaceRunTaskCreateOptions{
Type: "workspace-task",
EnforcementLevel: rt.EnforcementLevel,
RunTask: rt.RunTask,
Stage: &rt.Stage,
})
if err != nil {
w.log.Error(err, "Reconcile Run Tasks", "msg", "failed to create a new run task")
return err
}
}
return nil
}
func (r *WorkspaceReconciler) updateRunTasks(ctx context.Context, w *workspaceInstance, update map[string]*tfc.WorkspaceRunTask) error {
for _, v := range update {
w.log.Info("Reconcile Run Tasks", "msg", "updating run task")
_, err := w.tfClient.Client.WorkspaceRunTasks.Update(ctx, w.instance.Status.WorkspaceID, v.ID, tfc.WorkspaceRunTaskUpdateOptions{
Type: "workspace-task",
EnforcementLevel: v.EnforcementLevel,
Stage: &v.Stage,
})
if err != nil {
w.log.Error(err, "Reconcile Run Tasks", "msg", "failed to update run task")
return err
}
}
return nil
}
func (r *WorkspaceReconciler) deleteRunTasks(ctx context.Context, w *workspaceInstance, delete map[string]*tfc.WorkspaceRunTask) error {
for _, d := range delete {
err := w.tfClient.Client.WorkspaceRunTasks.Delete(ctx, w.instance.Status.WorkspaceID, d.ID)
if err != nil {
w.log.Error(err, "Reconcile Run Tasks", "msg", "failed to delete run task")
return err
}
}
return nil
}
func hasRunTaskName(w *workspaceInstance) bool {
for _, rt := range w.instance.Spec.RunTasks {
if rt.Name != "" {
return true
}
}
return false
}
func (r *WorkspaceReconciler) getInstanceRunTasks(ctx context.Context, w *workspaceInstance) (map[string]*tfc.WorkspaceRunTask, error) {
o := map[string]*tfc.WorkspaceRunTask{}
rl := make(map[string]string)
if hasRunTaskName(w) {
rt, err := w.tfClient.Client.RunTasks.List(ctx, w.instance.Spec.Organization, &tfc.RunTaskListOptions{})
if err != nil {
return o, err
}
for _, t := range rt.Items {
rl[t.Name] = t.ID
}
}
for _, rt := range w.instance.Spec.RunTasks {
id := rt.ID
if rt.Name != "" {
id = rl[rt.Name]
}
o[id] = &tfc.WorkspaceRunTask{
EnforcementLevel: tfc.TaskEnforcementLevel(rt.EnforcementLevel),
Stage: tfc.Stage(rt.Stage),
RunTask: &tfc.RunTask{ID: id},
}
}
return o, nil
}
func (r *WorkspaceReconciler) getWorkspaceRunTasks(ctx context.Context, w *workspaceInstance) (map[string]*tfc.WorkspaceRunTask, error) {
o := map[string]*tfc.WorkspaceRunTask{}
wrt, err := w.tfClient.Client.WorkspaceRunTasks.List(ctx, w.instance.Status.WorkspaceID, &tfc.WorkspaceRunTaskListOptions{})
if err != nil {
return o, err
}
for _, rt := range wrt.Items {
o[rt.RunTask.ID] = &tfc.WorkspaceRunTask{
ID: rt.ID,
EnforcementLevel: rt.EnforcementLevel,
Stage: rt.Stage,
RunTask: &tfc.RunTask{ID: rt.RunTask.ID},
}
}
return o, nil
}
func (r *WorkspaceReconciler) reconcileRunTasks(ctx context.Context, w *workspaceInstance) error {
w.log.Info("Reconcile Run Tasks", "msg", "new reconciliation event")
spec, err := r.getInstanceRunTasks(ctx, w)
if err != nil {
w.log.Error(err, "Reconcile Run Tasks", "msg", "failed to get instance run tasks")
return err
}
ws, err := r.getWorkspaceRunTasks(ctx, w)
if err != nil {
w.log.Error(err, "Reconcile Run Tasks", "msg", "failed to get workspace run tasks")
return err
}
create := getRunTasksToCreate(spec, ws)
if len(create) > 0 {
w.log.Info("Reconcile Run Tasks", "msg", fmt.Sprintf("creating %d run tasks", len(create)))
err := r.createRunTasks(ctx, w, create)
if err != nil {
w.log.Error(err, "Reconcile Run Tasks", "msg", "failed to create a run task")
return err
}
}
update := getRunTasksToUpdate(spec, ws)
if len(update) > 0 {
w.log.Info("Reconcile Run Tasks", "msg", fmt.Sprintf("updating %d run tasks", len(update)))
err := r.updateRunTasks(ctx, w, update)
if err != nil {
w.log.Error(err, "Reconcile Run Tasks", "msg", "failed to update a run task")
return err
}
}
delete := getRunTasksToDelete(spec, ws)
if len(delete) > 0 {
w.log.Info("Reconcile Run Tasks", "msg", fmt.Sprintf("deleting %d run tasks", len(delete)))
err := r.deleteRunTasks(ctx, w, delete)
if err != nil {
w.log.Error(err, "Reconcile Run Tasks", "msg", "failed to delete a run task")
return err
}
}
return nil
}