From 70624d04a64dbfb3eaef49d8b39de0a63826b7dd Mon Sep 17 00:00:00 2001 From: Yuwen Ma Date: Wed, 5 Jun 2024 01:33:22 +0000 Subject: [PATCH] no parent; mockgcp with StartLRO --- apis/cloudbuild/v1beta1/workerpool_types.go | 11 +-- .../v1beta1/zz_generated.deepcopy.go | 26 ++---- ...ools.cloudbuild.cnrm.cloud.google.com.yaml | 81 +++++++++---------- mockgcp/mockcloudbuild/service.go | 3 +- mockgcp/mockcloudbuild/workerpool.go | 26 ++++-- .../v1beta1/cloudbuildworkerpool_types.go | 16 ++-- .../v1beta1/zz_generated.deepcopy.go | 19 +---- .../cloudbuild/workerpool_controller.go | 4 +- ...ed_object_cloudbuildworkerpool.golden.yaml | 7 +- .../v1beta1/cloudbuildworkerpool/_http.log | 62 +++++++++++++- .../v1beta1/cloudbuildworkerpool/create.yaml | 7 +- .../v1beta1/cloudbuildworkerpool/update.yaml | 7 +- 12 files changed, 143 insertions(+), 126 deletions(-) diff --git a/apis/cloudbuild/v1beta1/workerpool_types.go b/apis/cloudbuild/v1beta1/workerpool_types.go index aa0e4e780ef..a718390bd03 100644 --- a/apis/cloudbuild/v1beta1/workerpool_types.go +++ b/apis/cloudbuild/v1beta1/workerpool_types.go @@ -28,16 +28,13 @@ type CloudBuildWorkerPoolSpec struct { DisplayName string `json:"displayName,omitempty"` ResourceID *string `json:"resourceID,omitempty"` // +required - Parent Parent `json:"parent,omitempty"` + ProjectRef *refv1beta1.ProjectRef `json:"projectRef"` + // +required + Location string `json:"location"` // +required PrivatePoolConfig *PrivatePoolV1Config `json:"privatePoolV1Config,omitempty"` } -type Parent struct { - ProjectRef *refv1beta1.ProjectRef `json:"projectRef"` - Location string `json:"location"` -} - type PrivatePoolV1Config struct { // +required WorkerConfig *WorkerConfig `json:"workerConfig,omitempty"` @@ -103,7 +100,7 @@ type NetworkConfigFromGCP struct { PeeredNetworkIpRange string `json:"peeredNetworkIpRange,omitempty"` } -// +kubebuilder:metadata:labels="cnrm.cloud.google.com/managed-by-kcc=true";"cnrm.cloud.google.com/directcrd=true";"cnrm.cloud.google.com/stability-level=beta" +// +kubebuilder:metadata:labels="cnrm.cloud.google.com/managed-by-kcc=true";"cnrm.cloud.google.com/stability-level=beta" // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // CloudBuildWorkerPool is the Schema for the CloudBuild WorkerPool API // +kubebuilder:subresource:status diff --git a/apis/cloudbuild/v1beta1/zz_generated.deepcopy.go b/apis/cloudbuild/v1beta1/zz_generated.deepcopy.go index a38bc60da74..433c6c889ba 100644 --- a/apis/cloudbuild/v1beta1/zz_generated.deepcopy.go +++ b/apis/cloudbuild/v1beta1/zz_generated.deepcopy.go @@ -102,7 +102,11 @@ func (in *CloudBuildWorkerPoolSpec) DeepCopyInto(out *CloudBuildWorkerPoolSpec) *out = new(string) **out = **in } - in.Parent.DeepCopyInto(&out.Parent) + if in.ProjectRef != nil { + in, out := &in.ProjectRef, &out.ProjectRef + *out = new(refsv1beta1.ProjectRef) + **out = **in + } if in.PrivatePoolConfig != nil { in, out := &in.PrivatePoolConfig, &out.PrivatePoolConfig *out = new(PrivatePoolV1Config) @@ -196,26 +200,6 @@ func (in *NetworkConfigFromGCP) DeepCopy() *NetworkConfigFromGCP { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Parent) DeepCopyInto(out *Parent) { - *out = *in - if in.ProjectRef != nil { - in, out := &in.ProjectRef, &out.ProjectRef - *out = new(refsv1beta1.ProjectRef) - **out = **in - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Parent. -func (in *Parent) DeepCopy() *Parent { - if in == nil { - return nil - } - out := new(Parent) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *PrivatePoolV1Config) DeepCopyInto(out *PrivatePoolV1Config) { *out = *in 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 dfaf822a0ab..1f942f98221 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 @@ -5,7 +5,6 @@ metadata: cnrm.cloud.google.com/version: 0.0.0-dev creationTimestamp: null labels: - cnrm.cloud.google.com/directcrd: "true" cnrm.cloud.google.com/managed-by-kcc: "true" cnrm.cloud.google.com/stability-level: beta name: cloudbuildworkerpools.cloudbuild.cnrm.cloud.google.com @@ -42,51 +41,10 @@ spec: properties: displayName: type: string + location: + type: string name: type: string - parent: - properties: - location: - type: string - projectRef: - description: The Project that this resource belongs to. - oneOf: - - not: - required: - - external - required: - - name - - kind - - not: - anyOf: - - required: - - name - - required: - - namespace - - required: - - kind - required: - - external - properties: - external: - description: The `projectID` field of a project, when not - managed by KCC. - type: string - kind: - description: The kind of the Project resource; optional but - must be `Project` if provided. - type: string - name: - description: The `name` field of a `Project` resource. - type: string - namespace: - description: The `namespace` field of a `Project` resource. - type: string - type: object - required: - - location - - projectRef - type: object privatePoolV1Config: properties: networkConfig: @@ -137,11 +95,44 @@ spec: required: - workerConfig type: object + projectRef: + description: The Project that this resource belongs to. + oneOf: + - not: + required: + - external + required: + - name + - not: + anyOf: + - required: + - name + - required: + - namespace + required: + - external + properties: + external: + description: The `projectID` field of a project, when not managed + by KCC. + type: string + kind: + description: The kind of the Project resource; optional but must + be `Project` if provided. + type: string + name: + description: The `name` field of a `Project` resource. + type: string + namespace: + description: The `namespace` field of a `Project` resource. + type: string + type: object resourceID: type: string required: - - parent + - location - privatePoolV1Config + - projectRef type: object status: description: CloudBuildWorkerPoolStatus defines the observed state of diff --git a/mockgcp/mockcloudbuild/service.go b/mockgcp/mockcloudbuild/service.go index df1863c1c5e..b185266b578 100644 --- a/mockgcp/mockcloudbuild/service.go +++ b/mockgcp/mockcloudbuild/service.go @@ -56,7 +56,8 @@ func (s *MockService) Register(grpcServer *grpc.Server) { } func (s *MockService) NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (http.Handler, error) { - mux, err := httpmux.NewServeMux(ctx, conn, httpmux.Options{}, pb.RegisterCloudBuildHandler) + mux, err := httpmux.NewServeMux(ctx, conn, httpmux.Options{}, pb.RegisterCloudBuildHandler, + s.operations.RegisterOperationsPath("/v1/{prefix=**}/operations/{name}")) if err != nil { return nil, err } diff --git a/mockgcp/mockcloudbuild/workerpool.go b/mockgcp/mockcloudbuild/workerpool.go index b44ab6ecd98..393a1c9fdab 100644 --- a/mockgcp/mockcloudbuild/workerpool.go +++ b/mockgcp/mockcloudbuild/workerpool.go @@ -63,18 +63,22 @@ func (s *CloudBuildV1) CreateWorkerPool(ctx context.Context, req *pb.CreateWorke obj := proto.Clone(req.GetWorkerPool()).(*pb.WorkerPool) obj.Name = fqn - obj.CreateTime = now - obj.UpdateTime = now if err := s.storage.Create(ctx, fqn, obj); err != nil { return nil, err } - obj.State = pb.WorkerPool_RUNNING metadata := &pb.CreateWorkerPoolOperationMetadata{ WorkerPool: fqn, CreateTime: now, CompleteTime: now, } - return s.operations.DoneLRO(ctx, name.String(), metadata, obj) + return s.operations.StartLRO(ctx, name.String(), metadata, func() (proto.Message, error) { + // Many fields are not populated in the LRO result + result := proto.Clone(obj).(*pb.WorkerPool) + result.CreateTime = now + result.UpdateTime = now + result.State = pb.WorkerPool_RUNNING + return result, nil + }) } func (s *CloudBuildV1) UpdateWorkerPool(ctx context.Context, req *pb.UpdateWorkerPoolRequest) (*longrunningpb.Operation, error) { @@ -109,11 +113,17 @@ func (s *CloudBuildV1) UpdateWorkerPool(ctx context.Context, req *pb.UpdateWorke return nil, err } metadata := &pb.UpdateWorkerPoolOperationMetadata{ - WorkerPool: fqn, + WorkerPool: name.String(), CreateTime: now, CompleteTime: now, } - return s.operations.DoneLRO(ctx, name.String(), metadata, obj) + return s.operations.StartLRO(ctx, name.String(), metadata, func() (proto.Message, error) { + // Many fields are not populated in the LRO result + result := proto.Clone(obj).(*pb.WorkerPool) + result.UpdateTime = now + result.State = pb.WorkerPool_RUNNING + return result, nil + }) } func (s *CloudBuildV1) DeleteWorkerPool(ctx context.Context, req *pb.DeleteWorkerPoolRequest) (*longrunningpb.Operation, error) { @@ -153,6 +163,10 @@ func (n *workerPoolName) String() string { return "projects/" + n.Project.ID + "/locations/" + n.Location + "/workerPools/" + n.WorkerPoolName } +func (n *workerPoolName) GetParent() string { + return "projects/" + n.Project.ID + "/locations/" + n.Location +} + func (s *MockService) parseWorkerPoolName(name string) (*workerPoolName, error) { tokens := strings.Split(name, "/") diff --git a/pkg/clients/generated/apis/cloudbuild/v1beta1/cloudbuildworkerpool_types.go b/pkg/clients/generated/apis/cloudbuild/v1beta1/cloudbuildworkerpool_types.go index e85f822383f..0469b8e382d 100644 --- a/pkg/clients/generated/apis/cloudbuild/v1beta1/cloudbuildworkerpool_types.go +++ b/pkg/clients/generated/apis/cloudbuild/v1beta1/cloudbuildworkerpool_types.go @@ -45,13 +45,6 @@ type WorkerpoolNetworkConfig struct { PeeredNetworkRef v1alpha1.ResourceRef `json:"peeredNetworkRef"` } -type WorkerpoolParent struct { - Location string `json:"location"` - - /* The Project that this resource belongs to. */ - ProjectRef v1alpha1.ResourceRef `json:"projectRef"` -} - type WorkerpoolPrivatePoolV1Config struct { // +optional NetworkConfig *WorkerpoolNetworkConfig `json:"networkConfig,omitempty"` @@ -71,13 +64,16 @@ type CloudBuildWorkerPoolSpec struct { // +optional DisplayName *string `json:"displayName,omitempty"` + Location string `json:"location"` + // +optional Name *string `json:"name,omitempty"` - Parent WorkerpoolParent `json:"parent"` - PrivatePoolV1Config WorkerpoolPrivatePoolV1Config `json:"privatePoolV1Config"` + /* The Project that this resource belongs to. */ + ProjectRef v1alpha1.ResourceRef `json:"projectRef"` + // +optional ResourceID *string `json:"resourceID,omitempty"` } @@ -136,7 +132,7 @@ type CloudBuildWorkerPoolStatus struct { // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // +kubebuilder:resource:categories=,shortName= // +kubebuilder:subresource:status -// +kubebuilder:metadata:labels="cnrm.cloud.google.com/directcrd=true";"cnrm.cloud.google.com/managed-by-kcc=true";"cnrm.cloud.google.com/stability-level=beta" +// +kubebuilder:metadata:labels="cnrm.cloud.google.com/managed-by-kcc=true";"cnrm.cloud.google.com/stability-level=beta" // CloudBuildWorkerPool is the Schema for the cloudbuild API // +k8s:openapi-gen=true diff --git a/pkg/clients/generated/apis/cloudbuild/v1beta1/zz_generated.deepcopy.go b/pkg/clients/generated/apis/cloudbuild/v1beta1/zz_generated.deepcopy.go index eb0e29ac804..892850b4a3f 100644 --- a/pkg/clients/generated/apis/cloudbuild/v1beta1/zz_generated.deepcopy.go +++ b/pkg/clients/generated/apis/cloudbuild/v1beta1/zz_generated.deepcopy.go @@ -323,8 +323,8 @@ func (in *CloudBuildWorkerPoolSpec) DeepCopyInto(out *CloudBuildWorkerPoolSpec) *out = new(string) **out = **in } - out.Parent = in.Parent in.PrivatePoolV1Config.DeepCopyInto(&out.PrivatePoolV1Config) + out.ProjectRef = in.ProjectRef if in.ResourceID != nil { in, out := &in.ResourceID, &out.ResourceID *out = new(string) @@ -1332,23 +1332,6 @@ func (in *WorkerpoolObservedStateStatus) DeepCopy() *WorkerpoolObservedStateStat return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *WorkerpoolParent) DeepCopyInto(out *WorkerpoolParent) { - *out = *in - out.ProjectRef = in.ProjectRef - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new WorkerpoolParent. -func (in *WorkerpoolParent) DeepCopy() *WorkerpoolParent { - if in == nil { - return nil - } - out := new(WorkerpoolParent) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *WorkerpoolPrivatePoolV1Config) DeepCopyInto(out *WorkerpoolPrivatePoolV1Config) { *out = *in diff --git a/pkg/controller/direct/cloudbuild/workerpool_controller.go b/pkg/controller/direct/cloudbuild/workerpool_controller.go index 468a8b1a03a..643a60b16cd 100644 --- a/pkg/controller/direct/cloudbuild/workerpool_controller.go +++ b/pkg/controller/direct/cloudbuild/workerpool_controller.go @@ -92,7 +92,7 @@ func (m *model) AdapterForObject(ctx context.Context, reader client.Reader, u *u } // Get GCP Project - projectRef, err := references.ResolveProject(ctx, reader, obj, obj.Spec.Parent.ProjectRef) + projectRef, err := references.ResolveProject(ctx, reader, obj, obj.Spec.ProjectRef) if err != nil { return nil, err } @@ -102,7 +102,7 @@ func (m *model) AdapterForObject(ctx context.Context, reader client.Reader, u *u } // Get location - location := obj.Spec.Parent.Location + location := obj.Spec.Location // Get computeNetwork if obj.Spec.PrivatePoolConfig.NetworkConfig != nil { diff --git a/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/_generated_object_cloudbuildworkerpool.golden.yaml b/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/_generated_object_cloudbuildworkerpool.golden.yaml index 0073791c4c0..6b66ebd271e 100644 --- a/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/_generated_object_cloudbuildworkerpool.golden.yaml +++ b/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/_generated_object_cloudbuildworkerpool.golden.yaml @@ -11,10 +11,7 @@ metadata: namespace: ${uniqueId} spec: displayName: New CloudBuild WorkerPool - parent: - location: us-central1 - projectRef: - external: projects/${projectId} + location: us-central1 privatePoolV1Config: networkConfig: egressOption: NO_PUBLIC_EGRESS @@ -24,6 +21,8 @@ spec: workerConfig: diskSizeGb: 150 machineType: e2-medium + projectRef: + external: projects/${projectId} status: conditions: - lastTransitionTime: "1970-01-01T00:00:00Z" 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 b97d2997a39..85a9d1bcd02 100644 --- a/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/_http.log +++ b/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/_http.log @@ -556,6 +556,34 @@ X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 +{ + "metadata": { + "@type": "type.googleapis.com/google.devtools.cloudbuild.v1.CreateWorkerPoolOperationMetadata", + "completeTime": "2024-04-01T12:34:56.123456Z", + "createTime": "2024-04-01T12:34:56.123456Z", + "workerPool": "projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}" + }, + "name": "projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}/operations/${operationID}" +} + +--- + +GET https://cloudbuild.googleapis.com/v1/projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}/operations/${operationID} +Content-Type: application/json +x-goog-api-client: gl-go/1.23.0-20240419-RC02 gapic/UNKNOWN gax/2.12.3 rest/UNKNOWN +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2FworkerPools%2Fcloudbuildworkerpool-${uniqueId}%2Foperations%2F${operationID} + +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 + { "done": true, "metadata": { @@ -605,7 +633,6 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { - "createTime": "2024-04-01T12:34:56.123456Z", "displayName": "New CloudBuild WorkerPool", "name": "projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}", "privatePoolV1Config": { @@ -618,8 +645,7 @@ X-Xss-Protection: 0 "diskSizeGb": "100", "machineType": "e2-medium" } - }, - "updateTime": "2024-04-01T12:34:56.123456Z" + } } --- @@ -656,6 +682,34 @@ X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 +{ + "metadata": { + "@type": "type.googleapis.com/google.devtools.cloudbuild.v1.UpdateWorkerPoolOperationMetadata", + "completeTime": "2024-04-01T12:34:56.123456Z", + "createTime": "2024-04-01T12:34:56.123456Z", + "workerPool": "projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}" + }, + "name": "projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}/operations/${operationID}" +} + +--- + +GET https://cloudbuild.googleapis.com/v1/projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}/operations/${operationID} +Content-Type: application/json +x-goog-api-client: gl-go/1.23.0-20240419-RC02 gapic/UNKNOWN gax/2.12.3 rest/UNKNOWN +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2FworkerPools%2Fcloudbuildworkerpool-${uniqueId}%2Foperations%2F${operationID} + +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 + { "done": true, "metadata": { @@ -667,7 +721,6 @@ X-Xss-Protection: 0 "name": "projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}/operations/${operationID}", "response": { "@type": "type.googleapis.com/google.devtools.cloudbuild.v1.WorkerPool", - "createTime": "2024-04-01T12:34:56.123456Z", "displayName": "New CloudBuild WorkerPool", "name": "projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}", "privatePoolV1Config": { @@ -681,6 +734,7 @@ X-Xss-Protection: 0 "machineType": "e2-medium" } }, + "state": "RUNNING", "updateTime": "2024-04-01T12:34:56.123456Z" } } diff --git a/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/create.yaml b/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/create.yaml index c88b707b73f..da9695a02e8 100644 --- a/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/create.yaml +++ b/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/create.yaml @@ -16,10 +16,9 @@ kind: CloudBuildWorkerPool metadata: name: cloudbuildworkerpool-${uniqueId} spec: - parent: - projectRef: - external: projects/${projectId} - location: us-central1 + projectRef: + external: projects/${projectId} + location: us-central1 displayName: New CloudBuild WorkerPool privatePoolV1Config: workerConfig: diff --git a/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/update.yaml b/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/update.yaml index c38e3c72dd9..0016fdd4194 100644 --- a/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/update.yaml +++ b/pkg/test/resourcefixture/testdata/basic/cloudbuild/v1beta1/cloudbuildworkerpool/update.yaml @@ -16,10 +16,9 @@ kind: CloudBuildWorkerPool metadata: name: cloudbuildworkerpool-${uniqueId} spec: - parent: - projectRef: - external: projects/${projectId} - location: us-central1 + projectRef: + external: projects/${projectId} + location: us-central1 displayName: New CloudBuild WorkerPool privatePoolV1Config: workerConfig: