-
Notifications
You must be signed in to change notification settings - Fork 10
/
hooks.go
211 lines (182 loc) · 5.71 KB
/
hooks.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
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 workspace
import (
"context"
"errors"
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
ackcondition "github.com/aws-controllers-k8s/runtime/pkg/condition"
ackrequeue "github.com/aws-controllers-k8s/runtime/pkg/requeue"
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
svcsdk "github.com/aws/aws-sdk-go/service/prometheusservice"
corev1 "k8s.io/api/core/v1"
svcapitypes "github.com/aws-controllers-k8s/prometheusservice-controller/apis/v1alpha1"
)
// workspaceCreating returns true if the supplied workspace is in the process
// of being created
func workspaceCreating(r *resource) bool {
if r.ko.Status.Status == nil {
return false
}
ws := *r.ko.Status.Status.StatusCode
return ws == string(svcapitypes.WorkspaceStatusCode_CREATING)
}
// workspaceCreating returns true if the supplied workspace is in an active state
func workspaceActive(r *resource) bool {
if r.ko.Status.Status == nil {
return false
}
ws := *r.ko.Status.Status.StatusCode
return ws == string(svcapitypes.WorkspaceStatusCode_ACTIVE)
}
// customUpdateWorkspace patches each of the resource properties in the backend AWS
// service API and returns a new resource with updated fields.
func (rm *resourceManager) customUpdateWorkspace(
ctx context.Context,
desired *resource,
latest *resource,
delta *ackcompare.Delta,
) (*resource, error) {
var err error
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.customUpdateWorkspace")
defer exit(err)
// Merge in the information we read from the API call above to the copy of
// the original Kubernetes object we passed to the function
ko := desired.ko.DeepCopy()
rm.setStatusDefaults(ko)
// Check if the state is active before updating
if !workspaceActive(latest) {
msg := "Cannot update workspace as it is not active, current status=" + string(*latest.ko.Status.Status.StatusCode)
ackcondition.SetSynced(desired, corev1.ConditionFalse, &msg, nil)
return desired, ackrequeue.NeededAfter(
errors.New(msg),
ackrequeue.DefaultRequeueAfterDuration,
)
}
if delta.DifferentAt("Spec.Tags") {
err = rm.updateWorkspaceTags(ctx, latest, desired)
if err != nil {
return nil, err
}
}
if delta.DifferentAt("Spec.Alias") {
err = rm.updateWorkspaceAlias(ctx, desired)
if err != nil {
return nil, err
}
}
return &resource{ko}, nil
}
// updateWorkspaceTags uses TagResource and UntagResource to add, remove and update
// workspace tags.
func (rm *resourceManager) updateWorkspaceTags(
ctx context.Context,
latest *resource,
desired *resource,
) error {
var err error
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.updateWorkspaceTags")
defer exit(err)
addedOrUpdated, removed := compareMaps(latest.ko.Spec.Tags, desired.ko.Spec.Tags)
if len(removed) > 0 {
removeTags := []*string{}
for i := range removed {
removeTags = append(removeTags, &removed[i])
}
input := &svcsdk.UntagResourceInput{
ResourceArn: (*string)(desired.ko.Status.ACKResourceMetadata.ARN),
TagKeys: removeTags,
}
_, err = rm.sdkapi.UntagResourceWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "UntagResource", err)
if err != nil {
return err
}
}
if len(addedOrUpdated) > 0 {
input := &svcsdk.TagResourceInput{
ResourceArn: (*string)(desired.ko.Status.ACKResourceMetadata.ARN),
Tags: addedOrUpdated,
}
_, err = rm.sdkapi.TagResourceWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "TagResource", err)
if err != nil {
return err
}
}
return nil
}
// updateWorkspaceAlias calls updateWorkspaceAlias to update a specific workspace
// alias.
func (rm *resourceManager) updateWorkspaceAlias(
ctx context.Context,
desired *resource,
) error {
var err error
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.updateWorkspaceAlias")
defer exit(err)
input := &svcsdk.UpdateWorkspaceAliasInput{
WorkspaceId: desired.ko.Status.WorkspaceID,
Alias: desired.ko.Spec.Alias,
}
_, err = rm.sdkapi.UpdateWorkspaceAliasWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "UpdateWorkspaceAlias", err)
if err != nil {
return err
}
return nil
}
// compareMaps compares two string to string maps and returns two outputs: a
// map of the new and updated key/values observed, and a list of the keys of the
// removed values.
func compareMaps(
a map[string]*string,
b map[string]*string,
) (addedOrUpdated map[string]*string, removed []string) {
addedOrUpdated = map[string]*string{}
visited := make(map[string]bool, len(a))
for keyA, valueA := range a {
valueB, found := b[keyA]
if !found {
removed = append(removed, keyA)
continue
}
if *valueA != *valueB {
addedOrUpdated[keyA] = valueB
}
visited[keyA] = true
}
for keyB, valueB := range b {
_, found := a[keyB]
if !found {
addedOrUpdated[keyB] = valueB
}
}
return
}
func customPreCompare(
delta *ackcompare.Delta,
a *resource,
b *resource,
) {
if len(a.ko.Spec.Tags) != len(b.ko.Spec.Tags) {
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
} else if a.ko.Spec.Tags != nil && b.ko.Spec.Tags != nil {
if !ackcompare.MapStringStringPEqual(a.ko.Spec.Tags, b.ko.Spec.Tags) {
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
}
}
}