-
Notifications
You must be signed in to change notification settings - Fork 303
/
registry.go
209 lines (177 loc) · 6.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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/tilt-dev/clusterid"
"github.com/tilt-dev/localregistry-go"
"github.com/tilt-dev/tilt/internal/container"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/logger"
)
// Recommended in Tilt-specific scripts
const tiltAnnotationRegistry = "tilt.dev/registry"
const tiltAnnotationRegistryFromCluster = "tilt.dev/registry-from-cluster"
// Recommended in Kind's scripts
// https://kind.sigs.k8s.io/docs/user/local-registry/
// There's active work underway to standardize this.
const kindAnnotationRegistry = "kind.x-k8s.io/registry"
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 clusterid.Product
core apiv1.CoreV1Interface
runtimeSource RuntimeSource
registry *v1alpha1.RegistryHosting
once sync.Once
}
func newRegistryAsync(env clusterid.Product, core apiv1.CoreV1Interface, runtimeSource RuntimeSource) *registryAsync {
return ®istryAsync{
env: env,
core: core,
runtimeSource: runtimeSource,
}
}
func (r *registryAsync) inferRegistryFromMicrok8s(ctx context.Context) *v1alpha1.RegistryHosting {
// 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 nil
}
// Microk8s might have a registry enabled.
// https://microk8s.io/docs/working
svc, err := r.core.Services(microk8sRegistryNamespace).Get(ctx, 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 nil
}
portSpecs := svc.Spec.Ports
if len(portSpecs) == 0 {
return nil
}
// 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/tilt-dev/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 nil
}
portSpec := portSpecs[0]
host := fmt.Sprintf("localhost:%d", portSpec.NodePort)
reg := v1alpha1.RegistryHosting{Host: host}
if err := reg.Validate(ctx); err != nil {
logger.Get(ctx).Warnf("Error validating private registry host %q: %v", host, err.ToAggregate())
return nil
}
return ®
}
// 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) inferRegistryFromNodeAnnotations(ctx context.Context) *v1alpha1.RegistryHosting {
nodeList, err := r.core.Nodes().List(ctx, metav1.ListOptions{Limit: 1})
if err != nil || len(nodeList.Items) == 0 {
return nil
}
node := nodeList.Items[0]
annotations := node.Annotations
var reg *v1alpha1.RegistryHosting
if tiltReg := annotations[tiltAnnotationRegistry]; tiltReg != "" {
reg = &v1alpha1.RegistryHosting{
Host: annotations[tiltAnnotationRegistry],
HostFromContainerRuntime: annotations[tiltAnnotationRegistryFromCluster],
}
} else if kindReg := annotations[kindAnnotationRegistry]; kindReg != "" {
reg = &v1alpha1.RegistryHosting{Host: kindReg}
}
if reg != nil {
if err := reg.Validate(ctx); err != nil {
logger.Get(ctx).Warnf("Local registry read from node failed: %v", err.ToAggregate())
return nil
}
}
return reg
}
// Implements the local registry discovery standard.
func (r *registryAsync) inferRegistryFromConfigMap(ctx context.Context) (registry *v1alpha1.RegistryHosting, help string) {
hosting, err := localregistry.Discover(ctx, r.core)
if err != nil {
logger.Get(ctx).Debugf("Local registry discovery error: %v", err)
return nil, ""
}
if hosting.Host == "" {
return nil, hosting.Help
}
registry = &v1alpha1.RegistryHosting{
Host: hosting.Host,
HostFromClusterNetwork: hosting.HostFromClusterNetwork,
HostFromContainerRuntime: hosting.HostFromContainerRuntime,
Help: hosting.Help,
}
if err := registry.Validate(ctx); err != nil {
logger.Get(ctx).Debugf("Local registry discovery error: %v", err.ToAggregate())
return nil, hosting.Help
}
return registry, hosting.Help
}
func (r *registryAsync) Registry(ctx context.Context) *v1alpha1.RegistryHosting {
r.once.Do(func() {
reg, help := r.inferRegistryFromConfigMap(ctx)
if !container.IsEmptyRegistry(reg) {
r.registry = reg
return
}
// Auto-infer the microk8s local registry.
if r.env == clusterid.ProductMicroK8s {
reg := r.inferRegistryFromMicrok8s(ctx)
if !container.IsEmptyRegistry(reg) {
r.registry = reg
return
}
}
reg = r.inferRegistryFromNodeAnnotations(ctx)
if !container.IsEmptyRegistry(reg) {
r.registry = reg
}
if container.IsEmptyRegistry(r.registry) {
if help != "" {
logger.Get(ctx).Warnf("You are running without a local image registry.\n"+
"Tilt can use the local registry to speed up builds.\n"+
"Instructions: %s", help)
} else if r.env == clusterid.ProductKIND {
logger.Get(ctx).Warnf("You are running Kind without a local image registry.\n" +
"Tilt can use the local registry to speed up builds.\n" +
"Instructions: https://github.com/tilt-dev/kind-local")
}
}
})
return r.registry
}
func (c K8sClient) LocalRegistry(ctx context.Context) *v1alpha1.RegistryHosting {
return c.registryAsync.Registry(ctx)
}