diff --git a/pkg/controller/direct/cloudbuild/workerpool_controller.go b/pkg/controller/direct/cloudbuild/workerpool_controller.go index 77c457dc1e..711f528e41 100644 --- a/pkg/controller/direct/cloudbuild/workerpool_controller.go +++ b/pkg/controller/direct/cloudbuild/workerpool_controller.go @@ -134,21 +134,10 @@ func (m *model) AdapterForObject(ctx context.Context, reader client.Reader, u *u } } - // Get computeNetwork - networkSpec := obj.Spec.PrivatePoolConfig.NetworkConfig - if networkSpec != nil { - if err := networkSpec.PeeredNetworkRef.Normalize(ctx, reader, obj); err != nil { - return nil, err - } - - projectsClient, err := m.projectsClient(ctx) - if err != nil { - return nil, err - } - - if err := networkSpec.PeeredNetworkRef.ConvertToProjectNumber(ctx, projectsClient); err != nil { - return nil, err - } + // Get Project GCP client + projectClient, err := m.projectsClient(ctx) + if err != nil { + return nil, err } // Get CloudBuild GCP client @@ -156,10 +145,13 @@ func (m *model) AdapterForObject(ctx context.Context, reader client.Reader, u *u if err != nil { return nil, err } + return &Adapter{ - id: id, - gcpClient: gcpClient, - desired: obj, + id: id, + projectClient: projectClient, + gcpClient: gcpClient, + reader: reader, + desired: obj, }, nil } @@ -191,10 +183,12 @@ func (m *model) AdapterForURL(ctx context.Context, url string) (directbase.Adapt } type Adapter struct { - id *CloudBuildWorkerPoolIdentity - gcpClient *gcp.Client - desired *krm.CloudBuildWorkerPool - actual *cloudbuildpb.WorkerPool + id *CloudBuildWorkerPoolIdentity + projectClient *cloudresourcemanager.ProjectsClient + gcpClient *gcp.Client + reader client.Reader + desired *krm.CloudBuildWorkerPool + actual *cloudbuildpb.WorkerPool } var _ directbase.Adapter = &Adapter{} @@ -216,6 +210,11 @@ func (a *Adapter) Find(ctx context.Context) (bool, error) { func (a *Adapter) Create(ctx context.Context, createOp *directbase.CreateOperation) error { u := createOp.GetUnstructured() + err := a.resolveDependencies(ctx, a.reader, a.desired) + if err != nil { + return err + } + log := klog.FromContext(ctx).WithName(ctrlName) log.V(2).Info("creating object", "u", u) @@ -252,6 +251,12 @@ func (a *Adapter) Create(ctx context.Context, createOp *directbase.CreateOperati func (a *Adapter) Update(ctx context.Context, updateOp *directbase.UpdateOperation) error { u := updateOp.GetUnstructured() + + err := a.resolveDependencies(ctx, a.reader, a.desired) + if err != nil { + return err + } + log := klog.FromContext(ctx).WithName(ctrlName) desired := a.desired.DeepCopy() @@ -356,3 +361,18 @@ func setStatus(u *unstructured.Unstructured, typedStatus any) error { return nil } + +func (a *Adapter) resolveDependencies(ctx context.Context, reader client.Reader, obj *krm.CloudBuildWorkerPool) error { + // Resolve computeNetwork + networkSpec := obj.Spec.PrivatePoolConfig.NetworkConfig + if networkSpec != nil { + if err := networkSpec.PeeredNetworkRef.Normalize(ctx, reader, obj); err != nil { + return err + } + + if err := networkSpec.PeeredNetworkRef.ConvertToProjectNumber(ctx, a.projectClient); err != nil { + return err + } + } + return nil +} diff --git a/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/_http.log b/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/_http.log index c6ec1b9cbf..28890891a7 100644 --- a/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/_http.log +++ b/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/_http.log @@ -582,12 +582,12 @@ X-Xss-Protection: 0 --- -GET https://cloudresourcemanager.googleapis.com/v3/projects/${projectId}?%24alt=json%3Benum-encoding%3Dint +GET https://cloudbuild.googleapis.com/v1/projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}?%24alt=json%3Benum-encoding%3Dint Content-Type: application/json User-Agent: kcc/controller-manager -x-goog-request-params: name=projects%2F${projectId} +x-goog-request-params: location=us-central1 -200 OK +404 Not Found Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF @@ -599,21 +599,21 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { - "createTime": "2024-04-01T12:34:56.123456Z", - "etag": "abcdef0123A=", - "name": "projects/${projectNumber}", - "projectId": "${projectId}", - "state": 1 + "error": { + "code": 404, + "message": "Requested entity was not found.", + "status": "NOT_FOUND" + } } --- -GET https://cloudbuild.googleapis.com/v1/projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +GET https://cloudresourcemanager.googleapis.com/v3/projects/${projectId}?%24alt=json%3Benum-encoding%3Dint Content-Type: application/json User-Agent: kcc/controller-manager -x-goog-request-params: location=us-central1 +x-goog-request-params: name=projects%2F${projectId} -404 Not Found +200 OK Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF @@ -625,11 +625,11 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { - "error": { - "code": 404, - "message": "Requested entity was not found.", - "status": "NOT_FOUND" - } + "createTime": "2024-04-01T12:34:56.123456Z", + "etag": "abcdef0123A=", + "name": "projects/${projectNumber}", + "projectId": "${projectId}", + "state": 1 } --- @@ -726,10 +726,10 @@ X-Xss-Protection: 0 --- -GET https://cloudresourcemanager.googleapis.com/v3/projects/${projectId}?%24alt=json%3Benum-encoding%3Dint +GET https://cloudbuild.googleapis.com/v1/projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}?%24alt=json%3Benum-encoding%3Dint Content-Type: application/json User-Agent: kcc/controller-manager -x-goog-request-params: name=projects%2F${projectId} +x-goog-request-params: location=us-central1 200 OK Cache-Control: private @@ -744,18 +744,31 @@ X-Xss-Protection: 0 { "createTime": "2024-04-01T12:34:56.123456Z", + "displayName": "New CloudBuild WorkerPool", "etag": "abcdef0123A=", - "name": "projects/${projectNumber}", - "projectId": "${projectId}", - "state": 1 + "name": "projects/${projectNumber}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}", + "privatePoolV1Config": { + "networkConfig": { + "egressOption": 1, + "peeredNetwork": "projects/${projectNumber}/global/networks/computenetwork-${uniqueId}", + "peeredNetworkIpRange": "/29" + }, + "workerConfig": { + "diskSizeGb": "100", + "machineType": "e2-medium" + } + }, + "state": 2, + "uid": "111111111111111111111", + "updateTime": "2024-04-01T12:34:56.123456Z" } --- -GET https://cloudbuild.googleapis.com/v1/projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +GET https://cloudresourcemanager.googleapis.com/v3/projects/${projectId}?%24alt=json%3Benum-encoding%3Dint Content-Type: application/json User-Agent: kcc/controller-manager -x-goog-request-params: location=us-central1 +x-goog-request-params: name=projects%2F${projectId} 200 OK Cache-Control: private @@ -770,23 +783,10 @@ X-Xss-Protection: 0 { "createTime": "2024-04-01T12:34:56.123456Z", - "displayName": "New CloudBuild WorkerPool", "etag": "abcdef0123A=", - "name": "projects/${projectNumber}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}", - "privatePoolV1Config": { - "networkConfig": { - "egressOption": 1, - "peeredNetwork": "projects/${projectNumber}/global/networks/computenetwork-${uniqueId}", - "peeredNetworkIpRange": "/29" - }, - "workerConfig": { - "diskSizeGb": "100", - "machineType": "e2-medium" - } - }, - "state": 2, - "uid": "111111111111111111111", - "updateTime": "2024-04-01T12:34:56.123456Z" + "name": "projects/${projectNumber}", + "projectId": "${projectId}", + "state": 1 } --- @@ -923,136 +923,6 @@ X-Xss-Protection: 0 --- -GET https://cloudresourcemanager.googleapis.com/v3/projects/${projectId}?%24alt=json%3Benum-encoding%3Dint -Content-Type: application/json -User-Agent: kcc/controller-manager -x-goog-request-params: name=projects%2F${projectId} - -200 OK -Cache-Control: private -Content-Type: application/json; charset=UTF-8 -Server: ESF -Vary: Origin -Vary: X-Origin -Vary: Referer -X-Content-Type-Options: nosniff -X-Frame-Options: SAMEORIGIN -X-Xss-Protection: 0 - -{ - "createTime": "2024-04-01T12:34:56.123456Z", - "etag": "abcdef0123A=", - "name": "projects/${projectNumber}", - "projectId": "${projectId}", - "state": 1 -} - ---- - -GET https://cloudbuild.googleapis.com/v1/projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}?%24alt=json%3Benum-encoding%3Dint -Content-Type: application/json -User-Agent: kcc/controller-manager -x-goog-request-params: location=us-central1 - -200 OK -Cache-Control: private -Content-Type: application/json; charset=UTF-8 -Server: ESF -Vary: Origin -Vary: X-Origin -Vary: Referer -X-Content-Type-Options: nosniff -X-Frame-Options: SAMEORIGIN -X-Xss-Protection: 0 - -{ - "createTime": "2024-04-01T12:34:56.123456Z", - "displayName": "Updated CloudBuild WorkerPool", - "etag": "abcdef0123A=", - "name": "projects/${projectNumber}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}", - "privatePoolV1Config": { - "networkConfig": { - "egressOption": 2, - "peeredNetwork": "projects/${projectNumber}/global/networks/computenetwork-${uniqueId}", - "peeredNetworkIpRange": "/29" - }, - "workerConfig": { - "diskSizeGb": "150", - "machineType": "e2-highmem-4" - } - }, - "state": 2, - "uid": "111111111111111111111", - "updateTime": "2024-04-01T12:34:56.123456Z" -} - ---- - -GET https://cloudresourcemanager.googleapis.com/v3/projects/${projectId}?%24alt=json%3Benum-encoding%3Dint -Content-Type: application/json -User-Agent: kcc/controller-manager -x-goog-request-params: name=projects%2F${projectId} - -200 OK -Cache-Control: private -Content-Type: application/json; charset=UTF-8 -Server: ESF -Vary: Origin -Vary: X-Origin -Vary: Referer -X-Content-Type-Options: nosniff -X-Frame-Options: SAMEORIGIN -X-Xss-Protection: 0 - -{ - "createTime": "2024-04-01T12:34:56.123456Z", - "etag": "abcdef0123A=", - "name": "projects/${projectNumber}", - "projectId": "${projectId}", - "state": 1 -} - ---- - -GET https://cloudbuild.googleapis.com/v1/projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}?%24alt=json%3Benum-encoding%3Dint -Content-Type: application/json -User-Agent: kcc/controller-manager -x-goog-request-params: location=us-central1 - -200 OK -Cache-Control: private -Content-Type: application/json; charset=UTF-8 -Server: ESF -Vary: Origin -Vary: X-Origin -Vary: Referer -X-Content-Type-Options: nosniff -X-Frame-Options: SAMEORIGIN -X-Xss-Protection: 0 - -{ - "createTime": "2024-04-01T12:34:56.123456Z", - "displayName": "Updated CloudBuild WorkerPool", - "etag": "abcdef0123A=", - "name": "projects/${projectNumber}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}", - "privatePoolV1Config": { - "networkConfig": { - "egressOption": 2, - "peeredNetwork": "projects/${projectNumber}/global/networks/computenetwork-${uniqueId}", - "peeredNetworkIpRange": "/29" - }, - "workerConfig": { - "diskSizeGb": "150", - "machineType": "e2-highmem-4" - } - }, - "state": 2, - "uid": "111111111111111111111", - "updateTime": "2024-04-01T12:34:56.123456Z" -} - ---- - DELETE https://cloudbuild.googleapis.com/v1/projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}?%24alt=json%3Benum-encoding%3Dint&allowMissing=true Content-Type: application/json User-Agent: kcc/controller-manager