-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathops_kongservice.go
More file actions
234 lines (206 loc) · 6.91 KB
/
ops_kongservice.go
File metadata and controls
234 lines (206 loc) · 6.91 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package ops
import (
"context"
"fmt"
"net/url"
"strconv"
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 createService(
ctx context.Context,
sdk sdkkonnectgo.ServicesSDK,
svc *configurationv1alpha1.KongService,
) error {
cpID := svc.GetControlPlaneID()
if cpID == "" {
return CantPerformOperationWithoutControlPlaneIDError{Entity: svc, Op: CreateOp}
}
if svc.Spec.Name == nil || *svc.Spec.Name == "" {
serviceID := string(svc.GetUID())
resp, err := sdk.UpsertService(ctx, sdkkonnectops.UpsertServiceRequest{
ControlPlaneID: cpID,
ServiceID: serviceID,
Service: kongServiceToSDKServiceInput(svc),
})
if errWrap := wrapErrIfKonnectOpFailed(err, CreateOp, svc); errWrap != nil {
return errWrap
}
if resp == nil || resp.Service == nil || resp.Service.ID == nil {
return fmt.Errorf("failed creating %s: %w", svc.GetTypeName(), ErrNilResponse)
}
svc.SetKonnectID(*resp.Service.ID)
return nil
}
resp, err := sdk.CreateService(ctx, cpID, kongServiceToSDKServiceInput(svc))
if errWrap := wrapErrIfKonnectOpFailed(err, CreateOp, svc); errWrap != nil {
return errWrap
}
if resp == nil || resp.Service == nil || resp.Service.ID == nil {
return fmt.Errorf("failed creating %s: %w", svc.GetTypeName(), ErrNilResponse)
}
svc.SetKonnectID(*resp.Service.ID)
return nil
}
// updateService updates the Konnect Service entity.
// It is assumed that provided KongService has Konnect ID set in status.
// It returns an error if the KongService does not have a ControlPlaneRef or
// if the operation fails.
func updateService(
ctx context.Context,
sdk sdkkonnectgo.ServicesSDK,
svc *configurationv1alpha1.KongService,
) error {
if svc.GetControlPlaneID() == "" {
return CantPerformOperationWithoutControlPlaneIDError{Entity: svc, Op: UpdateOp}
}
id := svc.GetKonnectStatus().GetKonnectID()
_, err := sdk.UpsertService(ctx,
sdkkonnectops.UpsertServiceRequest{
ControlPlaneID: svc.GetControlPlaneID(),
ServiceID: id,
Service: kongServiceToSDKServiceInput(svc),
},
)
if errWrap := wrapErrIfKonnectOpFailed(err, UpdateOp, svc); errWrap != nil {
return errWrap
}
return nil
}
// deleteService deletes a KongService in Konnect.
// It is assumed that provided KongService has Konnect ID set in status.
// It returns an error if the operation fails.
func deleteService(
ctx context.Context,
sdk sdkkonnectgo.ServicesSDK,
svc *configurationv1alpha1.KongService,
) error {
id := svc.GetKonnectStatus().GetKonnectID()
_, err := sdk.DeleteService(ctx, svc.Status.Konnect.ControlPlaneID, id)
if errWrap := wrapErrIfKonnectOpFailed(err, DeleteOp, svc); errWrap != nil {
return handleDeleteError(ctx, err, svc)
}
return nil
}
func adoptService(
ctx context.Context,
sdk sdkkonnectgo.ServicesSDK,
svc *configurationv1alpha1.KongService,
) error {
cpID := svc.GetControlPlaneID()
adoptOptions := svc.Spec.Adopt
konnectID := adoptOptions.Konnect.ID
if cpID == "" {
return KonnectEntityAdoptionMissingControlPlaneIDError{}
}
resp, err := sdk.GetService(ctx, konnectID, cpID)
if err != nil {
return KonnectEntityAdoptionFetchError{
KonnectID: konnectID,
Err: err,
}
}
uidTag, hasUIDTag := findUIDTag(resp.Service.Tags)
if hasUIDTag && extractUIDFromTag(uidTag) != string(svc.UID) {
return KonnectEntityAdoptionUIDTagConflictError{
KonnectID: konnectID,
ActualUIDTag: extractUIDFromTag(uidTag),
}
}
adoptMode := adoptOptions.Mode
if adoptMode == "" {
adoptMode = commonv1alpha1.AdoptModeOverride
}
switch adoptOptions.Mode {
case commonv1alpha1.AdoptModeOverride:
svcCopy := svc.DeepCopy()
svcCopy.SetKonnectID(konnectID)
if err = updateService(ctx, sdk, svcCopy); err != nil {
return err
}
case commonv1alpha1.AdoptModeMatch:
// When adopting in match mode, we return error if the service does not match.
// when it matches, we do nothing but fill the Konnect ID to mark that the adoption is successful.
if !serviceMatch(resp.Service, svc) {
return KonnectEntityAdoptionNotMatchError{
KonnectID: konnectID,
}
}
default:
return fmt.Errorf("failed to adopt: adopt mode %q not supported", adoptMode)
}
svc.SetKonnectID(konnectID)
return nil
}
func kongServiceToSDKServiceInput(
svc *configurationv1alpha1.KongService,
) sdkkonnectcomp.Service {
s := sdkkonnectcomp.Service{
URL: svc.Spec.URL,
ConnectTimeout: svc.Spec.ConnectTimeout,
Enabled: svc.Spec.Enabled,
Host: svc.Spec.Host,
Name: svc.Spec.Name,
Path: svc.Spec.Path,
ReadTimeout: svc.Spec.ReadTimeout,
Retries: svc.Spec.Retries,
Tags: GenerateTagsForObject(svc, svc.Spec.Tags...),
TLSVerify: svc.Spec.TLSVerify,
TLSVerifyDepth: svc.Spec.TLSVerifyDepth,
WriteTimeout: svc.Spec.WriteTimeout,
}
if svc.Spec.Port != 0 {
s.Port = new(svc.Spec.Port)
}
if svc.Spec.Protocol != "" {
s.Protocol = new(svc.Spec.Protocol)
}
return s
}
// getKongServiceForUID returns the Konnect ID of the KongService
// that matches the UID of the provided KongService.
func getKongServiceForUID(
ctx context.Context,
sdk sdkkonnectgo.ServicesSDK,
svc *configurationv1alpha1.KongService,
) (string, error) {
reqList := sdkkonnectops.ListServiceRequest{
// 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.
Tags: new(UIDLabelForObject(svc)),
ControlPlaneID: svc.GetControlPlaneID(),
}
resp, err := sdk.ListService(ctx, reqList)
if err != nil {
return "", fmt.Errorf("failed listing %s: %w", svc.GetTypeName(), err)
}
if resp == nil || resp.Object == nil {
return "", fmt.Errorf("failed listing %s: %w", svc.GetTypeName(), ErrNilResponse)
}
_, id, err := getMatchingEntryFromListResponseData(sliceToEntityWithIDPtrSlice(resp.Object.Data), svc)
return id, err
}
// serviceMatch compares the existing service fetched from Konnect and the spec of the KongService
// for adopting in match mode.
func serviceMatch(konnectService *sdkkonnectcomp.ServiceOutput, svc *configurationv1alpha1.KongService) bool {
spec := svc.Spec
if spec.URL != nil {
parsedURL, err := url.Parse(*spec.URL)
if err != nil {
return false
}
spec.Protocol = sdkkonnectcomp.Protocol(parsedURL.Scheme)
spec.Host = parsedURL.Hostname()
spec.Port, _ = strconv.ParseInt(parsedURL.Port(), 10, 64)
spec.Path = new(parsedURL.Path)
}
return equalWithDefault(konnectService.Name, spec.Name, "") &&
konnectService.Host == spec.Host &&
equalWithDefault(konnectService.Port, &spec.Port, 80) &&
equalWithDefault(konnectService.Protocol, &spec.Protocol, "http") &&
equalWithDefault(konnectService.Path, spec.Path, "/")
}