Skip to content

Commit

Permalink
no parent; mockgcp with StartLRO
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwenma committed Jun 5, 2024
1 parent 1eb417f commit 70624d0
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 126 deletions.
11 changes: 4 additions & 7 deletions apis/cloudbuild/v1beta1/workerpool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand Down
26 changes: 5 additions & 21 deletions apis/cloudbuild/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion mockgcp/mockcloudbuild/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
26 changes: 20 additions & 6 deletions mockgcp/mockcloudbuild/workerpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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, "/")

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/controller/direct/cloudbuild/workerpool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +21,8 @@ spec:
workerConfig:
diskSizeGb: 150
machineType: e2-medium
projectRef:
external: projects/${projectId}
status:
conditions:
- lastTransitionTime: "1970-01-01T00:00:00Z"
Expand Down
Loading

0 comments on commit 70624d0

Please sign in to comment.