Skip to content

Commit

Permalink
Fix flaky samples
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Nov 14, 2024
1 parent ae288b9 commit dcc0fe3
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 191 deletions.
64 changes: 42 additions & 22 deletions pkg/controller/direct/cloudbuild/workerpool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,32 +134,24 @@ 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
gcpClient, err := m.client(ctx)
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
}

Expand Down Expand Up @@ -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{}
Expand All @@ -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)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}

---
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}

---
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dcc0fe3

Please sign in to comment.