From 27efa03d0e52d65a69e010479152c5e5275f1979 Mon Sep 17 00:00:00 2001 From: Yuwen Ma Date: Fri, 14 Jun 2024 23:50:49 +0000 Subject: [PATCH] feat: CBWP update with ETag --- apis/cloudbuild/v1alpha1/conversion.go | 2 ++ apis/cloudbuild/v1alpha1/workerpool_types.go | 2 ++ apis/cloudbuild/v1alpha1/zz_generated.deepcopy.go | 5 +++++ ...oudbuildworkerpools.cloudbuild.cnrm.cloud.google.com.yaml | 2 ++ mockgcp/mockcloudbuild/workerpool.go | 4 +++- pkg/controller/direct/cloudbuild/workerpool_controller.go | 1 + .../_generated_object_cloudbuildworkerpool.golden.yaml | 1 + .../basic/cloudbuild/v1alpha1/cloudbuildworkerpool/_http.log | 2 ++ 8 files changed, 18 insertions(+), 1 deletion(-) diff --git a/apis/cloudbuild/v1alpha1/conversion.go b/apis/cloudbuild/v1alpha1/conversion.go index d47c3dc00ed..8496a55bac2 100644 --- a/apis/cloudbuild/v1alpha1/conversion.go +++ b/apis/cloudbuild/v1alpha1/conversion.go @@ -25,6 +25,8 @@ func Convert_WorkerPool_API_v1_To_KRM_status(in *cloudbuildpb.WorkerPool, out *C return nil } out.ObservedState = &CloudBuildWorkerPoolObservedState{} + + out.ObservedState.ETag = &in.Etag if err := Convert_PrivatePoolV1Config_API_v1_To_KRM(in.GetPrivatePoolV1Config(), out.ObservedState); err != nil { return err } diff --git a/apis/cloudbuild/v1alpha1/workerpool_types.go b/apis/cloudbuild/v1alpha1/workerpool_types.go index b98f8842a70..9d64be6f38c 100644 --- a/apis/cloudbuild/v1alpha1/workerpool_types.go +++ b/apis/cloudbuild/v1alpha1/workerpool_types.go @@ -88,6 +88,8 @@ type CloudBuildWorkerPoolObservedState struct { // +required WorkerConfig *WorkerConfig `json:"workerConfig,omitempty"` NetworkConfig *NetworkConfigState `json:"networkConfig,omitempty"` + + ETag *string `json:"eTag,omitempty"` } type NetworkConfigState struct { diff --git a/apis/cloudbuild/v1alpha1/zz_generated.deepcopy.go b/apis/cloudbuild/v1alpha1/zz_generated.deepcopy.go index 4c688371a1e..94ec0d95dd2 100644 --- a/apis/cloudbuild/v1alpha1/zz_generated.deepcopy.go +++ b/apis/cloudbuild/v1alpha1/zz_generated.deepcopy.go @@ -92,6 +92,11 @@ func (in *CloudBuildWorkerPoolObservedState) DeepCopyInto(out *CloudBuildWorkerP *out = new(NetworkConfigState) **out = **in } + if in.ETag != nil { + in, out := &in.ETag, &out.ETag + *out = new(string) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CloudBuildWorkerPoolObservedState. diff --git a/config/crds/resources/apiextensions.k8s.io_v1_customresourcedefinition_cloudbuildworkerpools.cloudbuild.cnrm.cloud.google.com.yaml b/config/crds/resources/apiextensions.k8s.io_v1_customresourcedefinition_cloudbuildworkerpools.cloudbuild.cnrm.cloud.google.com.yaml index 2fc2ff23ded..ae999167a18 100644 --- a/config/crds/resources/apiextensions.k8s.io_v1_customresourcedefinition_cloudbuildworkerpools.cloudbuild.cnrm.cloud.google.com.yaml +++ b/config/crds/resources/apiextensions.k8s.io_v1_customresourcedefinition_cloudbuildworkerpools.cloudbuild.cnrm.cloud.google.com.yaml @@ -180,6 +180,8 @@ spec: description: The creation timestamp of the workerpool. format: date-time type: string + eTag: + type: string networkConfig: properties: egressOption: diff --git a/mockgcp/mockcloudbuild/workerpool.go b/mockgcp/mockcloudbuild/workerpool.go index 393a1c9fdab..859b93c0d58 100644 --- a/mockgcp/mockcloudbuild/workerpool.go +++ b/mockgcp/mockcloudbuild/workerpool.go @@ -77,6 +77,7 @@ func (s *CloudBuildV1) CreateWorkerPool(ctx context.Context, req *pb.CreateWorke result.CreateTime = now result.UpdateTime = now result.State = pb.WorkerPool_RUNNING + result.Etag = "W/11111111111111111111111111111111" return result, nil }) } @@ -99,7 +100,7 @@ func (s *CloudBuildV1) UpdateWorkerPool(ctx context.Context, req *pb.UpdateWorke f := target.FieldByName(path) if f.IsValid() && f.CanSet() { switch f.Kind() { - case reflect.Int: + case reflect.Int, reflect.Int64: intVal := source.FieldByName(path).Int() f.SetInt(intVal) case reflect.String: @@ -122,6 +123,7 @@ func (s *CloudBuildV1) UpdateWorkerPool(ctx context.Context, req *pb.UpdateWorke result := proto.Clone(obj).(*pb.WorkerPool) result.UpdateTime = now result.State = pb.WorkerPool_RUNNING + result.Etag = "W/22222222222222222222222222222222" return result, nil }) } diff --git a/pkg/controller/direct/cloudbuild/workerpool_controller.go b/pkg/controller/direct/cloudbuild/workerpool_controller.go index fce6fee12fc..8008d812229 100644 --- a/pkg/controller/direct/cloudbuild/workerpool_controller.go +++ b/pkg/controller/direct/cloudbuild/workerpool_controller.go @@ -259,6 +259,7 @@ func (a *Adapter) Update(ctx context.Context, u *unstructured.Unstructured) erro wp := &cloudbuildpb.WorkerPool{ Name: a.fullyQualifiedName(), + Etag: a.actual.Etag, } desired := a.desired.DeepCopy() err := krm.Convert_WorkerPool_KRM_To_API_v1(desired, wp) diff --git a/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1alpha1/cloudbuildworkerpool/_generated_object_cloudbuildworkerpool.golden.yaml b/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1alpha1/cloudbuildworkerpool/_generated_object_cloudbuildworkerpool.golden.yaml index 843b50127f1..5a79f5ee4af 100644 --- a/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1alpha1/cloudbuildworkerpool/_generated_object_cloudbuildworkerpool.golden.yaml +++ b/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1alpha1/cloudbuildworkerpool/_generated_object_cloudbuildworkerpool.golden.yaml @@ -33,6 +33,7 @@ status: observedGeneration: 2 observedState: createTime: "1970-01-01T00:00:00Z" + eTag: W/22222222222222222222222222222222 networkConfig: egressOption: NO_PUBLIC_EGRESS peeredNetwork: projects/${projectId}/global/networks/computenetwork-${uniqueId} diff --git a/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1alpha1/cloudbuildworkerpool/_http.log b/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1alpha1/cloudbuildworkerpool/_http.log index d38bcce2cff..69b98084c3b 100644 --- a/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1alpha1/cloudbuildworkerpool/_http.log +++ b/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1alpha1/cloudbuildworkerpool/_http.log @@ -593,6 +593,7 @@ X-Xss-Protection: 0 "@type": "type.googleapis.com/google.devtools.cloudbuild.v1.WorkerPool", "createTime": "2024-04-01T12:34:56.123456Z", "displayName": "New CloudBuild WorkerPool", + "etag": "abcdef0123A=", "name": "projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}", "privatePoolV1Config": { "networkConfig": { @@ -715,6 +716,7 @@ X-Xss-Protection: 0 "response": { "@type": "type.googleapis.com/google.devtools.cloudbuild.v1.WorkerPool", "displayName": "New CloudBuild WorkerPool", + "etag": "abcdef0123A=", "name": "projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}", "privatePoolV1Config": { "networkConfig": {