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
package providermain
import (
"context"
"time"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/nodemarket"
)
const PathOutageMin = 30 * time.Second
type pathClock struct {
observed bool
up bool
coldStart bool
downSince time.Time
upSince time.Time
}
func (c *pathClock) observe(up bool, now time.Time) {
switch {
case !up && (c.up || !c.observed):
c.downSince = now
c.coldStart = !c.observed
case up && c.observed && !c.up:
if c.coldStart || now.Sub(c.downSince) >= PathOutageMin {
c.upSince = now
}
c.downSince = time.Time{}
c.coldStart = false
}
c.up = up
c.observed = true
}
func (c *pathClock) holding() bool {
return c.observed && !c.up
}
func (c *pathClock) rebase(since time.Time) time.Time {
if c.upSince.After(since) {
return c.upSince
}
return since
}
func clusterPathUp(tenant tenantClient, links nodemarket.Links, workspaceOf func(ctx context.Context, cluster string) (string, error)) func(ctx context.Context, cluster string) bool {
return func(ctx context.Context, cluster string) bool {
client, err := tenant(ctx, cluster)
if err != nil {
return false
}
if _, err := client.Discovery().RESTClient().Get().AbsPath("/readyz").DoRaw(ctx); err != nil {
return false
}
if links == nil {
return true
}
workspace, err := workspaceOf(ctx, cluster)
if err != nil {
return false
}
for _, name := range []string{nodemarket.ApiserverLinkName(cluster), nodemarket.OpenBaoLinkName(cluster)} {
link, err := links.ReadClusterLink(ctx, workspace, name)
if err != nil || link == nil || link.Status.Phase != v1alpha1.ClusterLinkListening {
return false
}
}
return true
}
}