-
Notifications
You must be signed in to change notification settings - Fork 235
/
workerpool_controller.go
388 lines (339 loc) · 13.2 KB
/
workerpool_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
383
384
385
386
387
388
/*
Copyright 2024.
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 cloudbuild
import (
"context"
"fmt"
"reflect"
"strings"
gcp "cloud.google.com/go/cloudbuild/apiv1/v2"
cloudbuildpb "cloud.google.com/go/cloudbuild/apiv1/v2/cloudbuildpb"
"google.golang.org/api/option"
krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/cloudbuild/v1beta1"
refs "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"
"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.GroupVersionKind, NewModel)
}
func NewModel(ctx context.Context, config *config.ControllerConfig) (directbase.Model, error) {
return &model{config: *config}, nil
}
const ctrlName = "cloudbuild-controller"
var _ directbase.Model = &model{}
type model struct {
config config.ControllerConfig
}
func (m *model) client(ctx context.Context) (*gcp.Client, error) {
var opts []option.ClientOption
if m.config.UserAgent != "" {
opts = append(opts, option.WithUserAgent(m.config.UserAgent))
}
if m.config.HTTPClient != nil {
opts = append(opts, option.WithHTTPClient(m.config.HTTPClient))
}
if m.config.UserProjectOverride && m.config.BillingProject != "" {
opts = append(opts, option.WithQuotaProject(m.config.BillingProject))
}
gcpClient, err := gcp.NewRESTClient(ctx, opts...)
if err != nil {
return nil, fmt.Errorf("building cloudbuild client: %w", err)
}
return gcpClient, err
}
func (m *model) AdapterForObject(ctx context.Context, reader client.Reader, u *unstructured.Unstructured) (directbase.Adapter, error) {
obj := &krm.CloudBuildWorkerPool{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, &obj); err != nil {
return nil, fmt.Errorf("error converting to %T: %w", obj, err)
}
// Get ResourceID
resourceID := direct.ValueOf(obj.Spec.ResourceID)
if resourceID == "" {
resourceID = obj.GetName()
}
if resourceID == "" {
return nil, fmt.Errorf("cannot resolve resource ID")
}
// Get GCP Project
projectRef, err := refs.ResolveProject(ctx, reader, obj, obj.Spec.ProjectRef)
if err != nil {
return nil, err
}
projectID := projectRef.ProjectID
if projectID == "" {
return nil, fmt.Errorf("cannot resolve project")
}
// Get location
location := obj.Spec.Location
// Get computeNetwork
if obj.Spec.PrivatePoolConfig.NetworkConfig != nil {
networkRef, err := refs.ResolveComputeNetwork(ctx, reader, obj, &obj.Spec.PrivatePoolConfig.NetworkConfig.PeeredNetworkRef)
if err != nil {
return nil, err
}
obj.Spec.PrivatePoolConfig.NetworkConfig.PeeredNetworkRef.External = networkRef.String()
}
// Get CloudBuild GCP client
gcpClient, err := m.client(ctx)
if err != nil {
return nil, err
}
return &Adapter{
resourceID: resourceID,
projectID: projectID,
location: location,
gcpClient: gcpClient,
desired: obj,
}, nil
}
func (m *model) AdapterForURL(ctx context.Context, url string) (directbase.Adapter, error) {
return nil, nil
}
type Adapter struct {
resourceID string
projectID string
location string
gcpClient *gcp.Client
desired *krm.CloudBuildWorkerPool
actual *cloudbuildpb.WorkerPool
}
var _ directbase.Adapter = &Adapter{}
func (a *Adapter) Find(ctx context.Context) (bool, error) {
if a.resourceID == "" {
return false, nil
}
req := &cloudbuildpb.GetWorkerPoolRequest{Name: a.fullyQualifiedName()}
workerpoolpb, err := a.gcpClient.GetWorkerPool(ctx, req)
if err != nil {
if direct.IsNotFound(err) {
return false, nil
}
return false, fmt.Errorf("getting cloudbuildworkerpool %q: %w", a.fullyQualifiedName(), err)
}
a.actual = workerpoolpb
return true, nil
}
func (a *Adapter) Create(ctx context.Context, u *unstructured.Unstructured) error {
log := klog.FromContext(ctx).WithName(ctrlName)
log.V(2).Info("creating object", "u", u)
projectID := a.projectID
if projectID == "" {
return fmt.Errorf("project is empty")
}
if a.resourceID == "" {
return fmt.Errorf("resourceID is empty")
}
desired := a.desired.DeepCopy()
mapCtx := &direct.MapContext{}
wp := CloudBuildWorkerPoolSpec_ToProto(mapCtx, &desired.Spec)
if mapCtx.Err() != nil {
return mapCtx.Err()
}
wp.Name = a.fullyQualifiedName()
req := &cloudbuildpb.CreateWorkerPoolRequest{
Parent: a.getParent(),
WorkerPoolId: a.resourceID,
WorkerPool: wp,
}
op, err := a.gcpClient.CreateWorkerPool(ctx, req)
if err != nil {
return fmt.Errorf("cloudbuildworkerpool %s creating failed: %w", wp.Name, err)
}
created, err := op.Wait(ctx)
if err != nil {
return fmt.Errorf("cloudbuildworkerpool %s waiting creation failed: %w", wp.Name, err)
}
status := &krm.CloudBuildWorkerPoolStatus{}
status.ObservedState = CloudBuildWorkerPoolObservedState_FromProto(mapCtx, created)
if mapCtx.Err() != nil {
return mapCtx.Err()
}
resRef, err := NewResourceRef(created)
if err != nil {
return err
}
status.ExternalRef = resRef.GetExternalReference()
return setStatus(u, status)
}
func (a *Adapter) Update(ctx context.Context, u *unstructured.Unstructured) error {
if err := a.ValidateExternalResource(); err != nil {
return err
}
updateMask := &fieldmaskpb.FieldMask{}
if !reflect.DeepEqual(a.desired.Spec.DisplayName, a.actual.DisplayName) {
updateMask.Paths = append(updateMask.Paths, "display_name")
}
typedConfig, ok := a.actual.Config.(*cloudbuildpb.WorkerPool_PrivatePoolV1Config)
if !ok {
return fmt.Errorf("unable to convert cloudbuildworkerpool %s config to workerpool PrivatePoolV1Config", a.actual.Name)
}
actualConfig := typedConfig.PrivatePoolV1Config
desiredConfig := a.desired.Spec.PrivatePoolConfig
if desiredConfig.NetworkConfig != nil {
switch actualConfig.NetworkConfig.EgressOption {
case cloudbuildpb.PrivatePoolV1Config_NetworkConfig_EGRESS_OPTION_UNSPECIFIED:
if !reflect.DeepEqual(direct.ValueOf(desiredConfig.NetworkConfig.EgressOption), "UNSPECIFIED") {
updateMask.Paths = append(updateMask.Paths, "private_pool_v1_config.network_config.egress_option")
}
case cloudbuildpb.PrivatePoolV1Config_NetworkConfig_NO_PUBLIC_EGRESS:
if !reflect.DeepEqual(direct.ValueOf(desiredConfig.NetworkConfig.EgressOption), "NO_PUBLIC_EGRESS") {
updateMask.Paths = append(updateMask.Paths, "private_pool_v1_config.network_config.egress_option")
}
case cloudbuildpb.PrivatePoolV1Config_NetworkConfig_PUBLIC_EGRESS:
if !reflect.DeepEqual(direct.ValueOf(desiredConfig.NetworkConfig.EgressOption), "PUBLIC_EGRESS") {
updateMask.Paths = append(updateMask.Paths, "private_pool_v1_config.network_config.egress_option")
}
}
expectedIPRange := direct.ValueOf(desiredConfig.NetworkConfig.PeeredNetworkIPRange)
if expectedIPRange != "" && !reflect.DeepEqual(expectedIPRange, actualConfig.NetworkConfig.PeeredNetworkIpRange) {
updateMask.Paths = append(updateMask.Paths, "private_pool_v1_config.network_config.peered_network_ip_range")
}
// TODO: better handle the network complexity
// 1. peered_network is an immutable field. whether/when shall we validate
// 2. the gcp workerpool stores the network with "project_number", different from the spec which uses the "project_id".
// * projects/<project_number>/global/networks/<network_id>
// * projects/<project_id>/global/networks/<network_id>
desiredNetwork := strings.Split(desiredConfig.NetworkConfig.PeeredNetworkRef.External, "/")
actualNetwork := strings.Split(actualConfig.NetworkConfig.PeeredNetwork, "/")
if len(desiredNetwork) == 5 && len(actualNetwork) == 5 && !reflect.DeepEqual(desiredNetwork[4], actualNetwork[4]) {
return fmt.Errorf("peered_network is immutable field")
}
}
if !reflect.DeepEqual(desiredConfig.WorkerConfig.DiskSizeGb, actualConfig.WorkerConfig.DiskSizeGb) {
updateMask.Paths = append(updateMask.Paths, "private_pool_v1_config.worker_config.disk_size_gb")
}
if !reflect.DeepEqual(desiredConfig.WorkerConfig.MachineType, actualConfig.WorkerConfig.MachineType) {
updateMask.Paths = append(updateMask.Paths, "private_pool_v1_config.worker_config.machine_type")
}
if len(updateMask.Paths) == 0 {
klog.Warningf("unexpected empty update mask, desired: %v, actual: %v", a.desired, a.actual)
return nil
}
desired := a.desired.DeepCopy()
mapCtx := &direct.MapContext{}
wp := CloudBuildWorkerPoolSpec_ToProto(mapCtx, &desired.Spec)
if mapCtx.Err() != nil {
return mapCtx.Err()
}
wp.Name = a.fullyQualifiedName()
wp.Etag = a.actual.Etag
req := &cloudbuildpb.UpdateWorkerPoolRequest{
WorkerPool: wp,
UpdateMask: updateMask,
}
op, err := a.gcpClient.UpdateWorkerPool(ctx, req)
if err != nil {
return fmt.Errorf("cloudbuildworkerpool %s updating failed: %w", wp.Name, err)
}
updated, err := op.Wait(ctx)
if err != nil {
return fmt.Errorf("cloudbuildworkerpool %s waiting update failed: %w", wp.Name, err)
}
status := &krm.CloudBuildWorkerPoolStatus{}
status.ObservedState = CloudBuildWorkerPoolObservedState_FromProto(mapCtx, updated)
if mapCtx.Err() != nil {
return fmt.Errorf("update workerpool status %w", mapCtx.Err())
}
// This value should not be updated. Just in case.
resRef, err := NewResourceRef(updated)
if err != nil {
return err
}
status.ExternalRef = resRef.GetExternalReference()
return setStatus(u, status)
}
func (a *Adapter) Export(ctx context.Context) (*unstructured.Unstructured, error) {
return nil, nil
}
// Delete implements the Adapter interface.
// TODO: Delete can rely on status.externalRef and do not need spec.projectRef.
func (a *Adapter) Delete(ctx context.Context) (bool, error) {
if err := a.ValidateExternalResource(); err != nil {
return false, err
}
req := &cloudbuildpb.DeleteWorkerPoolRequest{Name: a.fullyQualifiedName(), AllowMissing: true}
op, err := a.gcpClient.DeleteWorkerPool(ctx, req)
if err != nil {
// likely a server bug. worker_pool can be successfully deleted.
if !strings.Contains(err.Error(), "(line 12:3): missing \"value\" field") {
return false, fmt.Errorf("deleting cloudbuildworkerpool %s: %w", a.fullyQualifiedName(), err)
}
}
err = op.Wait(ctx)
if err != nil {
// likely a server bug. worker_pool can be successfully deleted.
if !strings.Contains(err.Error(), "(line 12:3): missing \"value\" field") {
return false, fmt.Errorf("waiting delete cloudbuildworkerpool %s: %w", a.fullyQualifiedName(), err)
}
}
return true, nil
}
// ValidateExternalResource compares the `status.externalRef` with the `spec` Project, Location and
// (external) resourceID to make sure those fields are immutable and matches the previous deployed value.
func (a *Adapter) ValidateExternalResource() error {
actualResRef, err := NewResourceRef(a.actual)
if err != nil {
return err
}
desiredExternalRef := "https://cloudbuild.googleapis.com/v1/" + a.fullyQualifiedName()
if direct.ValueOf(actualResRef.GetExternalReference()) == desiredExternalRef {
return nil
}
// Give user guidance on how to fix the CloudBuildWorkerPool spec.
if a.desired.Spec.ResourceID != nil && direct.ValueOf(a.desired.Spec.ResourceID) != actualResRef.GetResourceID() {
return fmt.Errorf("`spec.resourceID` is immutable field, expect %s, got %s",
actualResRef.GetResourceID(), *a.desired.Spec.ResourceID)
}
if a.desired.Spec.Location != actualResRef.GetLocation() {
return fmt.Errorf("`spec.location` is immutable field, expect %s, got %s",
actualResRef.GetLocation(), a.desired.Spec.Location)
}
// TODO: Some Selflink may change the project from projectID to projectNum.
/*
if a.desired.Spec.ProjectRef.Name != "" {
return fmt.Errorf("`spec.projectRef.name` is immutable field, expect project %s",
actualExternalRef.GetProject())
}
if a.desired.Spec.ProjectRef.External != "" {
return fmt.Errorf("`spec.projectRef.external` is immutable field, expect project %s",
actualExternalRef.GetProject())
}*/
return nil
}
func (a *Adapter) fullyQualifiedName() string {
return fmt.Sprintf("projects/%s/locations/%s/workerPools/%s", a.projectID, a.location, a.resourceID)
}
func (a *Adapter) getParent() string {
return fmt.Sprintf("projects/%s/locations/%s", a.projectID, a.location)
}
func setStatus(u *unstructured.Unstructured, typedStatus any) error {
status, err := runtime.DefaultUnstructuredConverter.ToUnstructured(typedStatus)
if err != nil {
return fmt.Errorf("error converting status to unstructured: %w", err)
}
old, _, _ := unstructured.NestedMap(u.Object, "status")
if old != nil {
status["conditions"] = old["conditions"]
status["observedGeneration"] = old["observedGeneration"]
}
u.Object["status"] = status
return nil
}