-
Notifications
You must be signed in to change notification settings - Fork 288
/
runtime.go
51 lines (43 loc) · 1.16 KB
/
runtime.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
package k8s
import (
"context"
"sync"
"github.com/windmilleng/tilt/internal/container"
"github.com/windmilleng/tilt/internal/logger"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiv1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
type runtimeAsync struct {
core apiv1.CoreV1Interface
runtime container.Runtime
once sync.Once
}
func newRuntimeAsync(core apiv1.CoreV1Interface) *runtimeAsync {
return &runtimeAsync{
core: core,
runtime: container.RuntimeUnknown,
}
}
func (r *runtimeAsync) Runtime(ctx context.Context) container.Runtime {
r.once.Do(func() {
nodeList, err := r.core.Nodes().List(metav1.ListOptions{
Limit: 1,
})
if err != nil {
logger.Get(ctx).Debugf("Error fetching nodes: %v", err)
}
if len(nodeList.Items) == 0 {
return
}
node := nodeList.Items[0]
info := node.Status.NodeInfo
r.runtime = container.RuntimeFromVersionString(info.ContainerRuntimeVersion)
})
return r.runtime
}
func (c K8sClient) ContainerRuntime(ctx context.Context) container.Runtime {
return c.runtimeAsync.Runtime(ctx)
}
func ProvideContainerRuntime(ctx context.Context, kCli Client) container.Runtime {
return kCli.ContainerRuntime(ctx)
}