Skip to content

Commit 3122915

Browse files
Cloudrun: Check knative observed generation when polling (#4793) (#3277)
* Check knative observed generation when polling * Rework to return errors * PR feedback, rename var Signed-off-by: Modular Magician <[email protected]>
1 parent 9b5c154 commit 3122915

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
lines changed

.changelog/4793.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
cloudrun: fixed a bug where resources would return successfully due to responses based on a previous version of the resource
3+
```

google-beta/cloudrun_polling.go

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,63 @@ type KnativeStatus struct {
2626
SelfLink string
2727
}
2828
Status struct {
29-
Conditions []Condition
29+
Conditions []Condition
30+
ObservedGeneration float64
3031
}
3132
}
3233

33-
func PollCheckKnativeStatus(resp map[string]interface{}, respErr error) PollResult {
34-
if respErr != nil {
35-
return ErrorPollResult(respErr)
34+
func getGeneration(res map[string]interface{}) (int, error) {
35+
metadata, ok := res["metadata"]
36+
if !ok {
37+
return 0, fmt.Errorf("Unable to find knative metadata")
3638
}
37-
s := KnativeStatus{}
38-
if err := Convert(resp, &s); err != nil {
39-
return ErrorPollResult(errwrap.Wrapf("unable to get KnativeStatus: {{err}}", err))
39+
m, ok := metadata.(map[string]interface{})
40+
if !ok {
41+
return 0, fmt.Errorf("Unable to find generation in knative metadata")
4042
}
43+
gen, ok := m["generation"]
44+
if !ok {
45+
return 0, fmt.Errorf("Unable to find generation in knative metadata")
46+
}
47+
return int(gen.(float64)), nil
48+
}
4149

42-
for _, condition := range s.Status.Conditions {
43-
if condition.Type == readyStatusType {
44-
log.Printf("[DEBUG] checking KnativeStatus Ready condition %s: %s", condition.Status, condition.Message)
45-
switch condition.Status {
46-
case "True":
47-
// Resource is ready
48-
return SuccessPollResult()
49-
case "Unknown":
50-
// DomainMapping can enter a 'terminal' state where "Ready" status is "Unknown"
51-
// but the resource is waiting for external verification of DNS records.
52-
if condition.Reason == pendingCertificateReason {
50+
func PollCheckKnativeStatusFunc(knativeRestResponse map[string]interface{}) func(resp map[string]interface{}, respErr error) PollResult {
51+
return func(resp map[string]interface{}, respErr error) PollResult {
52+
if respErr != nil {
53+
return ErrorPollResult(respErr)
54+
}
55+
s := KnativeStatus{}
56+
if err := Convert(resp, &s); err != nil {
57+
return ErrorPollResult(errwrap.Wrapf("unable to get KnativeStatus: {{err}}", err))
58+
}
59+
60+
gen, err := getGeneration(knativeRestResponse)
61+
if err != nil {
62+
return ErrorPollResult(errwrap.Wrapf("unable to find Knative generation: {{err}}", err))
63+
}
64+
if int(s.Status.ObservedGeneration) != gen {
65+
return PendingStatusPollResult("waiting for observed generation to match")
66+
}
67+
for _, condition := range s.Status.Conditions {
68+
if condition.Type == readyStatusType {
69+
log.Printf("[DEBUG] checking KnativeStatus Ready condition %s: %s", condition.Status, condition.Message)
70+
switch condition.Status {
71+
case "True":
72+
// Resource is ready
5373
return SuccessPollResult()
74+
case "Unknown":
75+
// DomainMapping can enter a 'terminal' state where "Ready" status is "Unknown"
76+
// but the resource is waiting for external verification of DNS records.
77+
if condition.Reason == pendingCertificateReason {
78+
return SuccessPollResult()
79+
}
80+
return PendingStatusPollResult(fmt.Sprintf("%s:%s", condition.Status, condition.Message))
81+
case "False":
82+
return ErrorPollResult(fmt.Errorf(`resource is in failed state "Ready:False", message: %s`, condition.Message))
5483
}
55-
return PendingStatusPollResult(fmt.Sprintf("%s:%s", condition.Status, condition.Message))
56-
case "False":
57-
return ErrorPollResult(fmt.Errorf(`resource is in failed state "Ready:False", message: %s`, condition.Message))
5884
}
5985
}
86+
return PendingStatusPollResult("no status yet")
6087
}
61-
return PendingStatusPollResult("no status yet")
6288
}

google-beta/resource_cloud_run_domain_mapping.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func resourceCloudRunDomainMappingCreate(d *schema.ResourceData, meta interface{
335335
}
336336
d.SetId(id)
337337

338-
err = PollingWaitTime(resourceCloudRunDomainMappingPollRead(d, meta), PollCheckKnativeStatus, "Creating DomainMapping", d.Timeout(schema.TimeoutCreate), 1)
338+
err = PollingWaitTime(resourceCloudRunDomainMappingPollRead(d, meta), PollCheckKnativeStatusFunc(res), "Creating DomainMapping", d.Timeout(schema.TimeoutCreate), 1)
339339
if err != nil {
340340
return fmt.Errorf("Error waiting to create DomainMapping: %s", err)
341341
}

google-beta/resource_cloud_run_service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ func resourceCloudRunServiceCreate(d *schema.ResourceData, meta interface{}) err
876876
}
877877
d.SetId(id)
878878

879-
err = PollingWaitTime(resourceCloudRunServicePollRead(d, meta), PollCheckKnativeStatus, "Creating Service", d.Timeout(schema.TimeoutCreate), 1)
879+
err = PollingWaitTime(resourceCloudRunServicePollRead(d, meta), PollCheckKnativeStatusFunc(res), "Creating Service", d.Timeout(schema.TimeoutCreate), 1)
880880
if err != nil {
881881
return fmt.Errorf("Error waiting to create Service: %s", err)
882882
}
@@ -1064,7 +1064,7 @@ func resourceCloudRunServiceUpdate(d *schema.ResourceData, meta interface{}) err
10641064
log.Printf("[DEBUG] Finished updating Service %q: %#v", d.Id(), res)
10651065
}
10661066

1067-
err = PollingWaitTime(resourceCloudRunServicePollRead(d, meta), PollCheckKnativeStatus, "Updating Service", d.Timeout(schema.TimeoutUpdate), 1)
1067+
err = PollingWaitTime(resourceCloudRunServicePollRead(d, meta), PollCheckKnativeStatusFunc(res), "Updating Service", d.Timeout(schema.TimeoutUpdate), 1)
10681068
if err != nil {
10691069
return err
10701070
}

0 commit comments

Comments
 (0)