Skip to content

Commit

Permalink
update operations to match realGCP
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Sep 17, 2024
1 parent e981e6d commit 6f0b538
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 240 deletions.
8 changes: 7 additions & 1 deletion mockgcp/mockcompute/globalforwardingrulesv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func (s *GlobalForwardingRulesV1) Get(ctx context.Context, req *pb.GetGlobalForw

obj := &pb.ForwardingRule{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
if status.Code(err) == codes.NotFound {
return nil, status.Errorf(codes.NotFound, "The resource '%s' was not found", fqn)
}
return nil, err
}

Expand Down Expand Up @@ -160,10 +163,13 @@ func (s *GlobalForwardingRulesV1) SetLabels(ctx context.Context, req *pb.SetLabe
op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLink,
OperationType: PtrTo("SetLabels"),
OperationType: PtrTo("setLabels"),
User: PtrTo("[email protected]"),
// SetLabels operation has EndTime in response
EndTime: PtrTo("2024-04-01T12:34:56.123456Z"),
// SetLabels operation finished super fast
Progress: PtrTo(int32(100)),
Status: PtrTo(pb.Operation_DONE),
}
return s.startGlobalLRO(ctx, name.Project.ID, op, func() (proto.Message, error) {
return obj, nil
Expand Down
10 changes: 7 additions & 3 deletions mockgcp/mockcompute/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,17 @@ func (s *computeOperations) startLRO0(ctx context.Context, op *pb.Operation, fqn
op.InsertTime = PtrTo(formatTime(now))
op.Id = PtrTo(uint64(nanos))

op.Progress = PtrTo(int32(0))
if op.Progress == nil {
op.Progress = PtrTo(int32(0))
}

if op.Status == nil {
op.Status = PtrTo(pb.Operation_RUNNING)
}

op.Kind = PtrTo("compute#operation")
op.SelfLink = PtrTo("https://www.googleapis.com/compute/v1/" + fqn)

op.Status = PtrTo(pb.Operation_RUNNING)

log.Info("storing operation", "fqn", fqn)
if err := s.storage.Create(ctx, fqn, op); err != nil {
return nil, err
Expand Down
9 changes: 8 additions & 1 deletion mockgcp/mockcompute/regionalforwardingrulev1.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func (s *RegionalForwardingRulesV1) Get(ctx context.Context, req *pb.GetForwardi

obj := &pb.ForwardingRule{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
if status.Code(err) == codes.NotFound {
return nil, status.Errorf(codes.NotFound, "The resource '%s' was not found", fqn)
}
return nil, err
}

Expand All @@ -64,6 +67,7 @@ func (s *RegionalForwardingRulesV1) Insert(ctx context.Context, req *pb.InsertFo
obj.CreationTimestamp = PtrTo(s.nowString())
obj.Id = &id
obj.Kind = PtrTo("compute#forwardingRule")
obj.Region = PtrTo(fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/regions/%s", name.Project.ID, name.Region))
// If below values are not provided by user, it appears to default by GCP
if obj.LabelFingerprint == nil {
obj.LabelFingerprint = PtrTo(computeFingerprint(obj))
Expand Down Expand Up @@ -161,10 +165,13 @@ func (s *RegionalForwardingRulesV1) SetLabels(ctx context.Context, req *pb.SetLa
op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLink,
OperationType: PtrTo("SetLabels"),
OperationType: PtrTo("setLabels"),
User: PtrTo("[email protected]"),
// SetLabels operation has EndTime in response
EndTime: PtrTo("2024-04-01T12:34:56.123456Z"),
// SetLabels operation finished super fast
Progress: PtrTo(int32(100)),
Status: PtrTo(pb.Operation_DONE),
}
return s.startRegionalLRO(ctx, name.Project.ID, name.Region, op, func() (proto.Message, error) {
return obj, nil
Expand Down
33 changes: 21 additions & 12 deletions pkg/controller/direct/compute/forwardingrule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,11 @@ func (a *forwardingRuleAdapter) Create(ctx context.Context, createOp *directbase
if err != nil {
return fmt.Errorf("creating ComputeForwardingRule %s: %w", a.fullyQualifiedName(), err)
}
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("waiting ComputeForwardingRule %s create failed: %w", a.fullyQualifiedName(), err)
if !op.Done() {
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("waiting ComputeForwardingRule %s create failed: %w", a.fullyQualifiedName(), err)
}
}
log.V(2).Info("successfully created ComputeForwardingRule", "name", a.fullyQualifiedName())
// Get the created resource
Expand Down Expand Up @@ -412,11 +414,14 @@ func (a *forwardingRuleAdapter) Update(ctx context.Context, updateOp *directbase
if err != nil {
return fmt.Errorf("updating ComputeForwardingRule labels %s: %w", a.fullyQualifiedName(), err)
}
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("waiting ComputeForwardingRule %s update labels failed: %w", a.fullyQualifiedName(), err)
if !op.Done() {
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("waiting ComputeForwardingRule %s update labels failed: %w", a.fullyQualifiedName(), err)
}
}
log.V(2).Info("successfully updated ComputeForwardingRule labels", "name", a.fullyQualifiedName())

// Get the updated resource
if a.id.location == "global" {
getReq := &computepb.GetGlobalForwardingRuleRequest{
Expand Down Expand Up @@ -457,9 +462,11 @@ func (a *forwardingRuleAdapter) Update(ctx context.Context, updateOp *directbase
if err != nil {
return fmt.Errorf("updating ComputeForwardingRule target %s: %w", a.fullyQualifiedName(), err)
}
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("waiting ComputeForwardingRule %s update target failed: %w", a.fullyQualifiedName(), err)
if !op.Done() {
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("waiting ComputeForwardingRule %s update target failed: %w", a.fullyQualifiedName(), err)
}
}
log.V(2).Info("successfully updated ComputeForwardingRule target", "name", a.fullyQualifiedName())
}
Expand Down Expand Up @@ -544,9 +551,11 @@ func (a *forwardingRuleAdapter) Delete(ctx context.Context, deleteOp *directbase
if err != nil {
return false, fmt.Errorf("deleting ComputeForwardingRule %s: %w", a.id.forwardingRule, err)
}
err = op.Wait(ctx)
if err != nil {
return false, fmt.Errorf("waiting ComputeForwardingRule %s delete failed: %w", a.id.forwardingRule, err)
if !op.Done() {
err = op.Wait(ctx)
if err != nil {
return false, fmt.Errorf("waiting ComputeForwardingRule %s delete failed: %w", a.id.forwardingRule, err)
}
}
log.V(2).Info("successfully deleted ComputeForwardingRule", "name", a.id.forwardingRule)
return true, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,11 +731,11 @@ X-Xss-Protection: 0
"errors": [
{
"domain": "global",
"message": "forwardingRule \"projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}\" not found",
"message": "The resource 'projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}' was not found",
"reason": "notFound"
}
],
"message": "forwardingRule \"projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}\" not found"
"message": "The resource 'projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}' was not found"
}
}

Expand Down Expand Up @@ -895,41 +895,7 @@ X-Xss-Protection: 0
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "SetLabels",
"progress": 0,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
"status": "RUNNING",
"targetId": "${forwardingRulesId}",
"targetLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}",
"user": "[email protected]"
}

---

GET https://compute.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}
Content-Type: application/json
User-Agent: kcc/controller-manager
x-goog-request-params: project=${projectId}&operation=${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

{
"endTime": "2024-04-01T12:34:56.123456Z",
"id": "000000000000000000000",
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "SetLabels",
"operationType": "setLabels",
"progress": 100,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,11 +731,11 @@ X-Xss-Protection: 0
"errors": [
{
"domain": "global",
"message": "forwardingRule \"projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}\" not found",
"message": "The resource 'projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}' was not found",
"reason": "notFound"
}
],
"message": "forwardingRule \"projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}\" not found"
"message": "The resource 'projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}' was not found"
}
}

Expand Down Expand Up @@ -921,41 +921,7 @@ X-Xss-Protection: 0
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "SetLabels",
"progress": 0,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
"status": "RUNNING",
"targetId": "${forwardingRulesId}",
"targetLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}",
"user": "[email protected]"
}

---

GET https://compute.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}
Content-Type: application/json
User-Agent: kcc/controller-manager
x-goog-request-params: project=${projectId}&operation=${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

{
"endTime": "2024-04-01T12:34:56.123456Z",
"id": "000000000000000000000",
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "SetLabels",
"operationType": "setLabels",
"progress": 100,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1026,11 +1026,11 @@ X-Xss-Protection: 0
"errors": [
{
"domain": "global",
"message": "forwardingRule \"projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}\" not found",
"message": "The resource 'projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}' was not found",
"reason": "notFound"
}
],
"message": "forwardingRule \"projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}\" not found"
"message": "The resource 'projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}' was not found"
}
}

Expand Down Expand Up @@ -1188,41 +1188,7 @@ X-Xss-Protection: 0
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "SetLabels",
"progress": 0,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
"status": "RUNNING",
"targetId": "${forwardingRulesId}",
"targetLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}",
"user": "[email protected]"
}

---

GET https://compute.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}
Content-Type: application/json
User-Agent: kcc/controller-manager
x-goog-request-params: project=${projectId}&operation=${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

{
"endTime": "2024-04-01T12:34:56.123456Z",
"id": "000000000000000000000",
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "SetLabels",
"operationType": "setLabels",
"progress": 100,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,11 +846,11 @@ X-Xss-Protection: 0
"errors": [
{
"domain": "global",
"message": "forwardingRule \"projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}\" not found",
"message": "The resource 'projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}' was not found",
"reason": "notFound"
}
],
"message": "forwardingRule \"projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}\" not found"
"message": "The resource 'projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}' was not found"
}
}

Expand Down Expand Up @@ -1008,41 +1008,7 @@ X-Xss-Protection: 0
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "SetLabels",
"progress": 0,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
"status": "RUNNING",
"targetId": "${forwardingRulesId}",
"targetLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}",
"user": "[email protected]"
}

---

GET https://compute.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}
Content-Type: application/json
User-Agent: kcc/controller-manager
x-goog-request-params: project=${projectId}&operation=${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

{
"endTime": "2024-04-01T12:34:56.123456Z",
"id": "000000000000000000000",
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "SetLabels",
"operationType": "setLabels",
"progress": 100,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
Expand Down
Loading

0 comments on commit 6f0b538

Please sign in to comment.