-
Notifications
You must be signed in to change notification settings - Fork 290
/
registry.go
151 lines (126 loc) · 4.3 KB
/
registry.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package k8s
import (
"context"
"fmt"
"net"
"sync"
"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"
)
const annotationRegistry = "tilt.dev/registry"
const annotationRegistryFromCluster = "tilt.dev/registry-from-cluster"
const microk8sRegistryNamespace = "container-registry"
const microk8sRegistryName = "registry"
type RuntimeSource interface {
Runtime(ctx context.Context) container.Runtime
}
type NaiveRuntimeSource struct {
runtime container.Runtime
}
func NewNaiveRuntimeSource(r container.Runtime) NaiveRuntimeSource {
return NaiveRuntimeSource{runtime: r}
}
func (s NaiveRuntimeSource) Runtime(ctx context.Context) container.Runtime {
return s.runtime
}
type registryAsync struct {
env Env
core apiv1.CoreV1Interface
runtimeSource RuntimeSource
registry container.Registry
once sync.Once
}
func newRegistryAsync(env Env, core apiv1.CoreV1Interface, runtimeSource RuntimeSource) *registryAsync {
return ®istryAsync{
env: env,
core: core,
runtimeSource: runtimeSource,
}
}
func (r *registryAsync) inferRegistryFromMicrok8s(ctx context.Context) container.Registry {
// If Microk8s is using the docker runtime, we can just use the microk8s docker daemon
// instead of the registry.
runtime := r.runtimeSource.Runtime(ctx)
if runtime == container.RuntimeDocker {
return container.Registry{}
}
// Microk8s might have a registry enabled.
// https://microk8s.io/docs/working
svc, err := r.core.Services(microk8sRegistryNamespace).Get(microk8sRegistryName, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
logger.Get(ctx).Warnf("You are running microk8s without a local image registry.\n" +
"Run: `sudo microk8s.enable registry`\n" +
"Tilt will use the local registry to speed up builds")
} else {
logger.Get(ctx).Debugf("Error fetching services: %v", err)
}
return container.Registry{}
}
portSpecs := svc.Spec.Ports
if len(portSpecs) == 0 {
return container.Registry{}
}
// Check to make sure localhost resolves to an IPv4 address. If it doesn't,
// then we won't be able to connect to the registry. See:
// https://github.com/windmilleng/tilt/issues/2369
ips, err := net.LookupIP("localhost")
if err != nil || len(ips) == 0 || ips[0].To4() == nil {
logger.Get(ctx).Warnf("Your /etc/hosts is resolving localhost to ::1 (IPv6).\n" +
"This breaks the microk8s image registry.\n" +
"Please fix your /etc/hosts to default to IPv4. This will make image pushes much faster.")
return container.Registry{}
}
portSpec := portSpecs[0]
host := fmt.Sprintf("localhost:%d", portSpec.NodePort)
reg, err := container.NewRegistry(host)
if err != nil {
logger.Get(ctx).Warnf("Error validating private registry host %q: %v", host, err)
return container.Registry{}
}
return reg
}
// If this node has the Tilt registry annotations on it, then we can
// infer it was set up with a Tilt script and thus has a local registry.
func (r *registryAsync) inferRegistryFromTiltNodeAnnotations(ctx context.Context) container.Registry {
nodeList, err := r.core.Nodes().List(metav1.ListOptions{Limit: 1})
if err != nil || len(nodeList.Items) == 0 {
return container.Registry{}
}
node := nodeList.Items[0]
annotations := node.Annotations
fromLocal := annotations[annotationRegistry]
fromCluster := annotations[annotationRegistryFromCluster]
if fromLocal != "" {
reg, err := container.NewRegistryWithHostFromCluster(fromLocal, fromCluster)
if err != nil {
logger.Get(ctx).Warnf("Local registry read from node failed to parse (%s, %s): %v", fromLocal, fromCluster, err)
return container.Registry{}
}
return reg
}
return container.Registry{}
}
func (r *registryAsync) Registry(ctx context.Context) container.Registry {
r.once.Do(func() {
// Auto-infer the microk8s local registry.
if r.env == EnvMicroK8s {
reg := r.inferRegistryFromMicrok8s(ctx)
if !reg.Empty() {
r.registry = reg
return
}
}
reg := r.inferRegistryFromTiltNodeAnnotations(ctx)
if !reg.Empty() {
r.registry = reg
}
})
return r.registry
}
func (c K8sClient) LocalRegistry(ctx context.Context) container.Registry {
return c.registryAsync.Registry(ctx)
}