-
Notifications
You must be signed in to change notification settings - Fork 290
/
runtime.go
65 lines (55 loc) · 1.59 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package k8s
import (
"context"
"net/http"
"sync"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiv1 "k8s.io/client-go/kubernetes/typed/core/v1"
"github.com/windmilleng/tilt/internal/container"
"github.com/windmilleng/tilt/pkg/logger"
)
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)
statusErr, isStatusErr := err.(*apiErrors.StatusError)
if isStatusErr {
status := statusErr.ErrStatus
if status.Code == http.StatusForbidden {
logger.Get(ctx).Infof(
"ERROR: Tilt could not read your node configuration\n"+
" Ask your Kubernetes admin for access to run `kubectl get nodes`.\n"+
" Detail: %v", err)
}
}
}
if nodeList == nil || 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)
}