-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathops_kongupstream.go
More file actions
212 lines (187 loc) · 7.58 KB
/
ops_kongupstream.go
File metadata and controls
212 lines (187 loc) · 7.58 KB
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
package ops
import (
"context"
"fmt"
sdkkonnectgo "github.com/Kong/sdk-konnect-go"
sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components"
sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
commonv1alpha1 "github.com/kong/kong-operator/v2/api/common/v1alpha1"
configurationv1alpha1 "github.com/kong/kong-operator/v2/api/configuration/v1alpha1"
)
func createUpstream(
ctx context.Context,
sdk sdkkonnectgo.UpstreamsSDK,
upstream *configurationv1alpha1.KongUpstream,
) error {
if upstream.GetControlPlaneID() == "" {
return CantPerformOperationWithoutControlPlaneIDError{Entity: upstream, Op: CreateOp}
}
resp, err := sdk.CreateUpstream(ctx,
upstream.Status.Konnect.ControlPlaneID,
kongUpstreamToSDKUpstreamInput(upstream),
)
if errWrap := wrapErrIfKonnectOpFailed(err, CreateOp, upstream); errWrap != nil {
return errWrap
}
if resp == nil || resp.Upstream == nil || resp.Upstream.ID == nil {
return fmt.Errorf("failed creating %s: %w", upstream.GetTypeName(), ErrNilResponse)
}
upstream.SetKonnectID(*resp.Upstream.ID)
return nil
}
// updateUpstream updates the Konnect Upstream entity.
// It is assumed that provided KongUpstream has Konnect ID set in status.
// It returns an error if the KongUpstream does not have a ControlPlaneRef or
// if the operation fails.
func updateUpstream(
ctx context.Context,
sdk sdkkonnectgo.UpstreamsSDK,
upstream *configurationv1alpha1.KongUpstream,
) error {
if upstream.GetControlPlaneID() == "" {
return CantPerformOperationWithoutControlPlaneIDError{Entity: upstream, Op: UpdateOp}
}
id := upstream.GetKonnectStatus().GetKonnectID()
_, err := sdk.UpsertUpstream(ctx,
sdkkonnectops.UpsertUpstreamRequest{
ControlPlaneID: upstream.GetControlPlaneID(),
UpstreamID: id,
Upstream: kongUpstreamToSDKUpstreamInput(upstream),
},
)
if errWrap := wrapErrIfKonnectOpFailed(err, UpdateOp, upstream); errWrap != nil {
return errWrap
}
return nil
}
// deleteUpstream deletes a KongUpstream in Konnect.
// It is assumed that provided KongUpstream has Konnect ID set in status.
// It returns an error if the operation fails.
func deleteUpstream(
ctx context.Context,
sdk sdkkonnectgo.UpstreamsSDK,
upstream *configurationv1alpha1.KongUpstream,
) error {
id := upstream.GetKonnectStatus().GetKonnectID()
_, err := sdk.DeleteUpstream(ctx, upstream.Status.Konnect.ControlPlaneID, id)
if errWrap := wrapErrIfKonnectOpFailed(err, DeleteOp, upstream); errWrap != nil {
return handleDeleteError(ctx, err, upstream)
}
return nil
}
func adoptUpstream(
ctx context.Context,
sdk sdkkonnectgo.UpstreamsSDK,
upstream *configurationv1alpha1.KongUpstream,
) error {
cpID := upstream.GetControlPlaneID()
adoptOptions := upstream.Spec.Adopt
konnectID := adoptOptions.Konnect.ID
if cpID == "" {
return KonnectEntityAdoptionMissingControlPlaneIDError{}
}
resp, err := sdk.GetUpstream(ctx, konnectID, cpID)
if err != nil {
return KonnectEntityAdoptionFetchError{
KonnectID: konnectID,
Err: err,
}
}
uidTag, hasUIDTag := findUIDTag(resp.Upstream.Tags)
if hasUIDTag && extractUIDFromTag(uidTag) != string(upstream.UID) {
return KonnectEntityAdoptionUIDTagConflictError{
KonnectID: konnectID,
ActualUIDTag: extractUIDFromTag(uidTag),
}
}
adoptMode := adoptOptions.Mode
if adoptMode == "" {
adoptMode = commonv1alpha1.AdoptModeOverride
}
switch adoptMode {
case commonv1alpha1.AdoptModeOverride:
upstreamCopy := upstream.DeepCopy()
upstreamCopy.SetKonnectID(konnectID)
if err = updateUpstream(ctx, sdk, upstreamCopy); err != nil {
return err
}
case commonv1alpha1.AdoptModeMatch:
// When adopting in match mode, we return error if the upstream does not match.
// when it matches, we do nothing but fill the Konnect ID to mark that the adoption is successful.
if !upstreamMatch(resp.Upstream, upstream) {
return KonnectEntityAdoptionNotMatchError{
KonnectID: konnectID,
}
}
default:
return fmt.Errorf("failed to adopt: adopt mode %q not supported", adoptMode)
}
upstream.SetKonnectID(konnectID)
return nil
}
func kongUpstreamToSDKUpstreamInput(
upstream *configurationv1alpha1.KongUpstream,
) sdkkonnectcomp.Upstream {
return sdkkonnectcomp.Upstream{
Algorithm: upstream.Spec.Algorithm,
ClientCertificate: upstream.Spec.ClientCertificate,
HashFallback: upstream.Spec.HashFallback,
HashFallbackHeader: upstream.Spec.HashFallbackHeader,
HashFallbackQueryArg: upstream.Spec.HashFallbackQueryArg,
HashFallbackURICapture: upstream.Spec.HashFallbackURICapture,
HashOn: upstream.Spec.HashOn,
HashOnCookie: upstream.Spec.HashOnCookie,
HashOnCookiePath: upstream.Spec.HashOnCookiePath,
HashOnHeader: upstream.Spec.HashOnHeader,
HashOnQueryArg: upstream.Spec.HashOnQueryArg,
HashOnURICapture: upstream.Spec.HashOnURICapture,
Healthchecks: upstream.Spec.Healthchecks,
HostHeader: upstream.Spec.HostHeader,
Name: upstream.Spec.Name,
Slots: upstream.Spec.Slots,
Tags: GenerateTagsForObject(upstream, upstream.Spec.Tags...),
UseSrvName: upstream.Spec.UseSrvName,
}
}
// getKongUpstreamForUID lists upstreams in Konnect with given k8s uid as its tag.
func getKongUpstreamForUID(
ctx context.Context,
sdk sdkkonnectgo.UpstreamsSDK,
u *configurationv1alpha1.KongUpstream,
) (string, error) {
cpID := u.GetControlPlaneID()
reqList := sdkkonnectops.ListUpstreamRequest{
// NOTE: only filter on object's UID.
// Other fields like name might have changed in the meantime but that's OK.
// Those will be enforced via subsequent updates.
ControlPlaneID: cpID,
Tags: new(UIDLabelForObject(u)),
}
resp, err := sdk.ListUpstream(ctx, reqList)
if err != nil {
return "", fmt.Errorf("failed listing %s: %w", u.GetTypeName(), err)
}
if resp == nil || resp.Object == nil {
return "", fmt.Errorf("failed listing %s: %w", u.GetTypeName(), ErrNilResponse)
}
_, id, err := getMatchingEntryFromListResponseData(sliceToEntityWithIDPtrSlice(resp.Object.Data), u)
return id, err
}
func upstreamMatch(konnectUpstream *sdkkonnectcomp.Upstream, upstream *configurationv1alpha1.KongUpstream) bool {
upstreamInput := kongUpstreamToSDKUpstreamInput(upstream)
return equalWithDefault(konnectUpstream.Algorithm, upstreamInput.Algorithm, sdkkonnectcomp.UpstreamAlgorithmRoundRobin) &&
equalWithDefault(konnectUpstream.HashFallback, upstreamInput.HashFallback, sdkkonnectcomp.HashFallbackNone) &&
equalWithDefault(konnectUpstream.HashFallbackHeader, upstreamInput.HashFallbackHeader, "") &&
equalWithDefault(konnectUpstream.HashFallbackQueryArg, upstreamInput.HashFallbackQueryArg, "") &&
equalWithDefault(konnectUpstream.HashFallbackURICapture, upstreamInput.HashFallbackURICapture, "") &&
equalWithDefault(konnectUpstream.HashOn, upstreamInput.HashOn, sdkkonnectcomp.HashOnNone) &&
equalWithDefault(konnectUpstream.HashOnCookie, upstreamInput.HashOnCookie, "") &&
equalWithDefault(konnectUpstream.HashOnCookiePath, upstreamInput.HashOnCookiePath, "/") &&
equalWithDefault(konnectUpstream.HashOnHeader, upstreamInput.HashOnHeader, "") &&
equalWithDefault(konnectUpstream.HashOnQueryArg, upstreamInput.HashOnQueryArg, "") &&
equalWithDefault(konnectUpstream.HashOnURICapture, upstreamInput.HashOnURICapture, "") &&
equalWithDefault(konnectUpstream.Healthchecks, upstreamInput.Healthchecks, sdkkonnectcomp.Healthchecks{}) &&
equalWithDefault(konnectUpstream.HostHeader, upstreamInput.HostHeader, "") &&
konnectUpstream.Name == upstreamInput.Name &&
equalWithDefault(konnectUpstream.Slots, upstreamInput.Slots, 10000)
}