-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathinstance_controller.go
382 lines (342 loc) · 15.2 KB
/
instance_controller.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License 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 alloydb
import (
"context"
"fmt"
"reflect"
krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/alloydb/v1beta1"
refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry"
gcp "cloud.google.com/go/alloydb/apiv1beta"
alloydbpb "cloud.google.com/go/alloydb/apiv1beta/alloydbpb"
"github.com/google/go-cmp/cmp"
"google.golang.org/api/option"
"google.golang.org/protobuf/types/known/fieldmaskpb"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func init() {
registry.RegisterModel(krm.AlloyDBInstanceGVK, NewInstanceModel)
}
func NewInstanceModel(ctx context.Context, config *config.ControllerConfig) (directbase.Model, error) {
return &instanceModel{config: *config}, nil
}
var _ directbase.Model = &instanceModel{}
type instanceModel struct {
config config.ControllerConfig
}
func (m *instanceModel) client(ctx context.Context) (*gcp.AlloyDBAdminClient, error) {
var opts []option.ClientOption
opts, err := m.config.RESTClientOptions()
if err != nil {
return nil, err
}
gcpClient, err := gcp.NewAlloyDBAdminRESTClient(ctx, opts...)
if err != nil {
return nil, fmt.Errorf("error building AlloyDB client for Instance: %w", err)
}
return gcpClient, err
}
func (m *instanceModel) AdapterForObject(ctx context.Context, reader client.Reader, u *unstructured.Unstructured) (directbase.Adapter, error) {
obj := &krm.AlloyDBInstance{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, &obj); err != nil {
return nil, fmt.Errorf("error converting to %T: %w", obj, err)
}
id, err := krm.NewInstanceIdentity(ctx, reader, obj)
if err != nil {
return nil, err
}
if obj.Spec.InstanceType != nil && obj.Spec.InstanceTypeRef != nil {
return nil, fmt.Errorf("one and only one of 'spec.InstanceTypeRef' " +
"and 'spec.InstanceType' should be configured: both are configured")
}
if obj.Spec.InstanceType == nil && obj.Spec.InstanceTypeRef == nil {
return nil, fmt.Errorf("one and only one of 'spec.InstanceTypeRef' " +
"and 'spec.InstanceType' should be configured: neither is configured")
}
var instanceType *string
if obj.Spec.InstanceType != nil {
if *instanceType == "" {
return nil, fmt.Errorf("'spec.InstanceType' should be configured with a non-empty string")
}
instanceType = obj.Spec.InstanceType
}
if obj.Spec.InstanceTypeRef != nil {
instanceType, err = refsv1beta1.ResolveAlloyDBClusterType(ctx, reader, obj, obj.Spec.InstanceTypeRef)
if err != nil {
return nil, fmt.Errorf("cannot resolve `spec.InstanceTypeRef`: %w", err)
}
}
obj.Spec.InstanceType = instanceType
// Get alloydb GCP client
gcpClient, err := m.client(ctx)
if err != nil {
return nil, err
}
return &instanceAdapter{
id: id,
gcpClient: gcpClient,
desired: obj,
}, nil
}
func (m *instanceModel) AdapterForURL(ctx context.Context, url string) (directbase.Adapter, error) {
// TODO: Support URLs
return nil, nil
}
type instanceAdapter struct {
id *krm.InstanceIdentity
gcpClient *gcp.AlloyDBAdminClient
desired *krm.AlloyDBInstance
actual *alloydbpb.Instance
}
var _ directbase.Adapter = &instanceAdapter{}
// Find retrieves the GCP resource.
// Return true means the object is found. This triggers Adapter `Update` call.
// Return true means the object is not found. This triggers Adapter `Create` call.
// Return a non-nil error requeues the requests.
func (a *instanceAdapter) Find(ctx context.Context) (bool, error) {
log := klog.FromContext(ctx)
log.V(2).Info("getting instance", "name", a.id)
fmt.Printf("getting instance: %v\n", a.id)
req := &alloydbpb.GetInstanceRequest{Name: a.id.String()}
instancepb, err := a.gcpClient.GetInstance(ctx, req)
if err != nil {
log.V(2).Info("error getting instance", "name", a.id, "error", err)
fmt.Printf("instance error: %+v\n", err)
if direct.IsNotFound(err) {
return false, nil
}
return false, fmt.Errorf("getting instance %q: %w", a.id, err)
}
fmt.Printf("retrieved instance: %+v\n", instancepb)
a.actual = instancepb
return true, nil
}
// Create creates the resource in GCP based on `spec` and update the Config Connector object `status` based on the GCP response.
func (a *instanceAdapter) Create(ctx context.Context, createOp *directbase.CreateOperation) error {
log := klog.FromContext(ctx)
log.V(2).Info("creating instance", "name", a.id)
fmt.Printf("creating instance: %v\n", a.id)
mapCtx := &direct.MapContext{}
desired := a.desired.DeepCopy()
resource := AlloyDBInstanceSpec_ToProto(mapCtx, &desired.Spec)
if mapCtx.Err() != nil {
return mapCtx.Err()
}
resource.Labels = make(map[string]string)
for k, v := range a.desired.GetObjectMeta().GetLabels() {
resource.Labels[k] = v
}
resource.Labels["managed-by-cnrm"] = "true"
req := &alloydbpb.CreateInstanceRequest{
Parent: a.id.Parent().String(),
InstanceId: a.id.ID(),
Instance: resource,
}
op, err := a.gcpClient.CreateInstance(ctx, req)
if err != nil {
log.V(2).Info("error creating instance", "name", a.id, "error", err)
return fmt.Errorf("creating instance %s: %w", a.id, err)
}
created, err := op.Wait(ctx)
if err != nil {
log.V(2).Info("error waiting instance creation", "name", a.id, "error", err)
return fmt.Errorf("instance %s waiting creation: %w", a.id, err)
}
log.V(2).Info("successfully created instance", "name", a.id)
status := AlloyDBInstanceStatus_FromProto(mapCtx, created)
if mapCtx.Err() != nil {
return mapCtx.Err()
}
status.ExternalRef = a.id.AsExternalRef()
return createOp.UpdateStatus(ctx, status, nil)
}
// Update updates the resource in GCP based on `spec` and update the Config Connector object `status` based on the GCP response.
func (a *instanceAdapter) Update(ctx context.Context, updateOp *directbase.UpdateOperation) error {
log := klog.FromContext(ctx)
log.V(2).Info("updating instance", "name", a.id)
mapCtx := &direct.MapContext{}
parsedActual := AlloyDBInstanceSpec_FromProto(mapCtx, a.actual)
if mapCtx.Err() != nil {
return mapCtx.Err()
}
updatePaths, err := compareInstance(ctx, parsedActual, &a.desired.Spec)
if err != nil {
return err
}
desiredLabels := a.desired.GetObjectMeta().GetLabels()
desiredLabels["managed-by-cnrm"] = "true"
if !reflect.DeepEqual(a.actual.GetLabels(), desiredLabels) {
log.V(2).Info("'metadata.labels' field is updated (-old +new)", cmp.Diff(a.actual.GetLabels(), desiredLabels))
updatePaths = append(updatePaths, "availability_type")
}
if len(updatePaths) == 0 {
log.V(2).Info("no field needs update", "name", a.id)
return nil
}
fmt.Printf("maqiuyu... updateMasks: %+v\n", updatePaths)
updateMask := &fieldmaskpb.FieldMask{
Paths: updatePaths,
}
desiredPb := AlloyDBInstanceSpec_ToProto(mapCtx, &a.desired.DeepCopy().Spec)
desiredPb.Labels = desiredLabels
desiredPb.Name = a.id.String()
req := &alloydbpb.UpdateInstanceRequest{
UpdateMask: updateMask,
Instance: desiredPb,
}
op, err := a.gcpClient.UpdateInstance(ctx, req)
if err != nil {
log.V(2).Info("error updating instance", "name", a.id, "error", err)
return fmt.Errorf("updating instance %s: %w", a.id, err)
}
updated, err := op.Wait(ctx)
if err != nil {
log.V(2).Info("error waiting instance update", "name", a.id, "error", err)
return fmt.Errorf("instance %s waiting update: %w", a.id, err)
}
log.V(2).Info("successfully updated instance", "name", a.id)
status := AlloyDBInstanceStatus_FromProto(mapCtx, updated)
if mapCtx.Err() != nil {
return mapCtx.Err()
}
return updateOp.UpdateStatus(ctx, status, nil)
}
func compareInstance(ctx context.Context, actual, desired *krm.AlloyDBInstanceSpec) (updatePaths []string, err error) {
log := klog.FromContext(ctx)
updatePaths = make([]string, 0)
if !reflect.DeepEqual(actual.Annotations, desired.Annotations) {
log.V(2).Info("'spec.annotations' field is updated (-old +new)", cmp.Diff(actual.Annotations, desired.Annotations))
updatePaths = append(updatePaths, "annotations")
}
// TODO: Test case with availability type unset.
if desired.AvailabilityType != nil && !reflect.DeepEqual(actual.AvailabilityType, desired.AvailabilityType) {
log.V(2).Info("'spec.availabilityType' field is updated (-old +new)", cmp.Diff(actual.AvailabilityType, desired.AvailabilityType))
updatePaths = append(updatePaths, "availability_type")
}
// TODO: Test "copied" behavior for read pool
// TODO: Test "overridden" behavior for read pool
// Default value of databaseFlags is unknown for a read instance unless we
// make API calls to get the database flags of the primary instance.
if desired.DatabaseFlags != nil && !reflect.DeepEqual(actual.DatabaseFlags, desired.DatabaseFlags) {
log.V(2).Info("'spec.databaseFlags' field is updated (-old +new)", cmp.Diff(actual.DatabaseFlags, desired.DatabaseFlags))
updatePaths = append(updatePaths, "database_flags")
}
if desired.DisplayName != nil && !reflect.DeepEqual(actual.DisplayName, desired.DisplayName) {
log.V(2).Info("'spec.displayName' field is updated (-old +new)", cmp.Diff(actual.DisplayName, desired.DisplayName))
updatePaths = append(updatePaths, "display_name")
}
// TODO: Test zonal instance without specifying this field.
if desired.GceZone != nil && !reflect.DeepEqual(actual.GceZone, desired.GceZone) {
log.V(2).Info("'spec.gceZone' field is updated (-old +new)", cmp.Diff(actual.GceZone, desired.GceZone))
updatePaths = append(updatePaths, "gce_zone")
}
if desired.InstanceType != nil && !reflect.DeepEqual(actual.InstanceType, desired.InstanceType) {
log.V(2).Info("'spec.instanceType' field is updated (-old +new)", cmp.Diff(actual.InstanceType, desired.InstanceType))
return nil, fmt.Errorf("cannot change immutable field %s from %v to %v", "'spec.instanceType'", actual.InstanceType, desired.InstanceType)
}
// TODO: Test machineConfig unset and empty struct
if desired.MachineConfig != nil {
if desired.MachineConfig.CpuCount != nil && !reflect.DeepEqual(actual.MachineConfig.CpuCount, desired.MachineConfig.CpuCount) {
log.V(2).Info("'spec.machineConfig.cpuCount' field is updated (-old +new)", cmp.Diff(actual.MachineConfig.CpuCount, desired.MachineConfig.CpuCount))
updatePaths = append(updatePaths, "machine_config.cpu_count")
}
}
if desired.NetworkConfig != nil {
if desired.NetworkConfig.EnablePublicIp != nil && !reflect.DeepEqual(actual.NetworkConfig.EnablePublicIp, desired.NetworkConfig.EnablePublicIp) {
log.V(2).Info("'spec.networkConfig.enablePublicIp' field is updated (-old +new)", cmp.Diff(actual.NetworkConfig.EnablePublicIp, desired.NetworkConfig.EnablePublicIp))
updatePaths = append(updatePaths, "network_config.enable_public_ip")
}
if desired.NetworkConfig.EnableOutboundPublicIp != nil && !reflect.DeepEqual(actual.NetworkConfig.EnableOutboundPublicIp, desired.NetworkConfig.EnableOutboundPublicIp) {
log.V(2).Info("'spec.networkConfig.enableOutboundPublicIp' field is updated (-old +new)", cmp.Diff(actual.NetworkConfig.EnableOutboundPublicIp, desired.NetworkConfig.EnableOutboundPublicIp))
updatePaths = append(updatePaths, "network_config.enable_outbound_public_ip")
}
if desired.NetworkConfig.AuthorizedExternalNetworks != nil && !reflect.DeepEqual(actual.NetworkConfig.AuthorizedExternalNetworks, desired.NetworkConfig.AuthorizedExternalNetworks) {
log.V(2).Info("'spec.networkConfig.authorizedExternalNetworks' field is updated (-old +new)", cmp.Diff(actual.NetworkConfig.AuthorizedExternalNetworks, desired.NetworkConfig.AuthorizedExternalNetworks))
updatePaths = append(updatePaths, "network_config.authorized_external_networks")
}
}
if desired.ReadPoolConfig != nil {
if desired.ReadPoolConfig.NodeCount != nil && !reflect.DeepEqual(actual.ReadPoolConfig.NodeCount, desired.ReadPoolConfig.NodeCount) {
log.V(2).Info("'spec.readPoolConfig.nodeCount' field is updated (-old +new)", cmp.Diff(actual.ReadPoolConfig.NodeCount, desired.ReadPoolConfig.NodeCount))
updatePaths = append(updatePaths, "read_pool_config.node_count")
}
}
return updatePaths, nil
}
// Export maps the GCP object to a Config Connector resource `spec`.
func (a *instanceAdapter) Export(ctx context.Context) (*unstructured.Unstructured, error) {
if a.actual == nil {
return nil, fmt.Errorf("Find() not called")
}
u := &unstructured.Unstructured{}
obj := &krm.AlloyDBInstance{}
mapCtx := &direct.MapContext{}
obj.Spec = direct.ValueOf(AlloyDBInstanceSpec_FromProto(mapCtx, a.actual))
if mapCtx.Err() != nil {
return nil, mapCtx.Err()
}
uObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}
// Split name into tokens and use ID.
u.SetName(a.actual.Name)
u.SetGroupVersionKind(krm.AlloyDBInstanceGVK)
u.Object = uObj
return u, nil
}
// Delete the resource from GCP service when the corresponding Config Connector resource is deleted.
func (a *instanceAdapter) Delete(ctx context.Context, deleteOp *directbase.DeleteOperation) (bool, error) {
log := klog.FromContext(ctx)
log.V(2).Info("deleting instance", "name", a.id)
// Returning true directly if it is to delete a secondary instance.
// Technically the secondary instance is only abandoned but not deleted.
// This is because deletion of secondary instance is not supported. Instead,
// users should delete the secondary cluster which will forcefully delete
// the associated secondary instance.
instanceType := a.desired.Spec.InstanceType
if instanceType != nil && *instanceType == "SECONDARY" {
log.V(2).Info("This operation didn't delete the secondary instance. You need to delete the associated secondary cluster to delete the secondary instance (and the entire secondary cluster).", "name", a.id)
return true, nil
}
req := &alloydbpb.DeleteInstanceRequest{Name: a.id.String()}
op, err := a.gcpClient.DeleteInstance(ctx, req)
if op != nil {
opMetadata, opErr := op.Metadata()
fmt.Printf("maqiuyu... delete operation: %v\n%v\nMetadata:\n%+v\nErr while getting metadata\n%v\n", op.Name(), op.Done(), opMetadata, opErr)
} else {
fmt.Printf("maqiuyu... delete operation not triggered. Maybe there is an error? %+v\n", err)
}
if err != nil {
log.V(2).Info("error deleting instance", "name", a.id, "error", err)
if direct.IsNotFound(err) {
return false, nil
}
return false, fmt.Errorf("deleting instance %s: %w", a.id, err)
}
err = op.Wait(ctx)
if err != nil {
log.V(2).Info("error waiting instance delete", "name", a.id, "error", err)
return false, fmt.Errorf("waiting delete instance %s: %w", a.id, err)
}
log.V(2).Info("successfully deleted instance", "name", a.id)
return true, nil
}