-
Notifications
You must be signed in to change notification settings - Fork 105
/
reconcile.go
81 lines (73 loc) · 2.08 KB
/
reconcile.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
69
70
71
72
73
74
75
76
77
78
79
80
81
package admin
import (
"context"
"fmt"
"time"
"github.com/rilldata/rill/admin/database"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// TriggerReconcileAndAwaitResource triggers a reconcile and polls the runtime until the given resource's spec version has been updated (or ctx is canceled).
func (s *Service) TriggerReconcileAndAwaitResource(ctx context.Context, depl *database.Deployment, name, kind string) error {
rt, err := s.openRuntimeClientForDeployment(depl)
if err != nil {
return err
}
defer rt.Close()
resourceReq := &runtimev1.GetResourceRequest{
InstanceId: depl.RuntimeInstanceID,
Name: &runtimev1.ResourceName{
Kind: kind,
Name: name,
},
}
// Get old spec version
var oldSpecVersion *int64
r, err := rt.GetResource(ctx, resourceReq)
if err == nil {
oldSpecVersion = &r.Resource.Meta.SpecVersion
}
// Trigger reconcile
_, err = rt.CreateTrigger(ctx, &runtimev1.CreateTriggerRequest{
InstanceId: depl.RuntimeInstanceID,
Trigger: &runtimev1.CreateTriggerRequest_PullTriggerSpec{
PullTriggerSpec: &runtimev1.PullTriggerSpec{},
},
})
if err != nil {
return err
}
// Poll every 1 seconds until the resource is found or the ctx is cancelled or times out
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
r, err := rt.GetResource(ctx, resourceReq)
if err != nil {
if s, ok := status.FromError(err); !ok || s.Code() != codes.NotFound {
return fmt.Errorf("failed to poll for resource: %w", err)
}
if oldSpecVersion != nil {
// Success - previously the resource was found, now we cannot find it anymore
return nil
}
// Continue polling
continue
}
if oldSpecVersion == nil {
// Success - previously the resource was not found, now we found one
return nil
}
if *oldSpecVersion != r.Resource.Meta.SpecVersion {
// Success - the spec version has changed
return nil
}
}
}