forked from vxcontrol/rancher-letsencrypt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wait.go
68 lines (58 loc) · 1.77 KB
/
wait.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package rancher
import (
"errors"
"fmt"
"time"
rancherClient "github.com/rancher/go-rancher/v2"
)
func backoff(maxDuration time.Duration, timeoutMessage string, f func() (bool, error)) error {
startTime := time.Now()
waitTime := 150 * time.Millisecond
maxWaitTime := 2 * time.Second
for {
if time.Since(startTime) > maxDuration {
return errors.New(timeoutMessage)
}
if done, err := f(); err != nil {
return err
} else if done {
return nil
}
time.Sleep(waitTime)
waitTime *= 2
if waitTime > maxWaitTime {
waitTime = maxWaitTime
}
}
}
// WaitFor waits for a resource to reach a certain state.
func (r *Client) WaitFor(resource *rancherClient.Resource, output interface{}, transitioning func() string) error {
return backoff(2*time.Minute, fmt.Sprintf("Time out waiting for %s:%s to become active", resource.Type, resource.Id), func() (bool, error) {
err := r.client.Reload(resource, output)
if err != nil {
return false, err
}
if transitioning() != "yes" {
return true, nil
}
return false, nil
})
}
// WaitService waits for a loadbalancer resource to transition
func (r *Client) WaitService(service *rancherClient.Service) error {
return r.WaitFor(&service.Resource, service, func() string {
return service.Transitioning
})
}
// WaitLoadBalancerService waits for a loadbalancer service resource to transition
func (r *Client) WaitLoadBalancerService(lb *rancherClient.LoadBalancerService) error {
return r.WaitFor(&lb.Resource, lb, func() string {
return lb.Transitioning
})
}
// WaitCertificate waits for a certificate resource to transition
func (r *Client) WaitCertificate(certificate *rancherClient.Certificate) error {
return r.WaitFor(&certificate.Resource, certificate, func() string {
return certificate.Transitioning
})
}