From 80d88988157fa51d8f56551640f6d07b4c325db4 Mon Sep 17 00:00:00 2001 From: Nick Santos Date: Fri, 12 Jun 2020 12:59:22 -0400 Subject: [PATCH] k8s: implement the local registry discovery protocol. Fixes https://github.com/tilt-dev/tilt/issues/3435 --- internal/k8s/registry.go | 3 +- internal/k8s/registry_test.go | 61 +++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/internal/k8s/registry.go b/internal/k8s/registry.go index 32a5d60723..7e2e04b7cc 100644 --- a/internal/k8s/registry.go +++ b/internal/k8s/registry.go @@ -6,12 +6,11 @@ import ( "net" "sync" - _ "github.com/tilt-dev/localregistry-go" + "github.com/tilt-dev/localregistry-go" "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/localregistry-go" "github.com/tilt-dev/tilt/internal/container" "github.com/tilt-dev/tilt/pkg/logger" ) diff --git a/internal/k8s/registry_test.go b/internal/k8s/registry_test.go index 0e6bb76cab..438992ffc0 100644 --- a/internal/k8s/registry_test.go +++ b/internal/k8s/registry_test.go @@ -12,6 +12,7 @@ import ( "github.com/tilt-dev/tilt/pkg/logger" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" @@ -89,6 +90,57 @@ func TestRegistryFoundInKindAnnotations(t *testing.T) { assert.Equal(t, "localhost:5000", registry.Host) } +func TestLocalRegistryDiscoveryHelp(t *testing.T) { + cs := &fake.Clientset{} + tracker := ktesting.NewObjectTracker(scheme.Scheme, scheme.Codecs.UniversalDecoder()) + cs.AddReactor("*", "*", ktesting.ObjectReaction(tracker)) + err := addConfigMap(tracker, ` +apiVersion: v1 +kind: ConfigMap +metadata: + name: local-registry-hosting + namespace: kube-public +data: + localRegistryHosting.v1: | + help: "https://fake-domain.tilt.dev/local-registry-help" +`) + require.NoError(t, err) + + core := cs.CoreV1() + registryAsync := newRegistryAsync(EnvKIND6, core, NewNaiveRuntimeSource(container.RuntimeContainerd)) + + out := bytes.NewBuffer(nil) + registry := registryAsync.Registry(newLoggerCtx(out)) + assert.True(t, registry.Empty()) + assert.Contains(t, out.String(), "https://fake-domain.tilt.dev/local-registry-help") +} + +func TestLocalRegistryDiscoveryHost(t *testing.T) { + cs := &fake.Clientset{} + tracker := ktesting.NewObjectTracker(scheme.Scheme, scheme.Codecs.UniversalDecoder()) + cs.AddReactor("*", "*", ktesting.ObjectReaction(tracker)) + err := addConfigMap(tracker, ` +apiVersion: v1 +kind: ConfigMap +metadata: + name: local-registry-hosting + namespace: kube-public +data: + localRegistryHosting.v1: | + host: "localhost:5000" + hostFromContainerRuntime: "registry:5000" + help: "https://fake-domain.tilt.dev/local-registry-help" +`) + require.NoError(t, err) + + core := cs.CoreV1() + registryAsync := newRegistryAsync(EnvKIND6, core, NewNaiveRuntimeSource(container.RuntimeContainerd)) + + registry := registryAsync.Registry(newLoggerCtx(os.Stdout)) + assert.Equal(t, "localhost:5000", registry.Host) + assert.Equal(t, "registry:5000", registry.HostFromCluster()) +} + func TestKINDWarning(t *testing.T) { cs := &fake.Clientset{} core := cs.CoreV1() @@ -156,3 +208,12 @@ func newLoggerCtx(w io.Writer) context.Context { ctx := logger.WithLogger(context.Background(), l) return ctx } + +func addConfigMap(tracker ktesting.ObjectTracker, configMap string) error { + obj, _, err := + scheme.Codecs.UniversalDeserializer().Decode([]byte(configMap), nil, nil) + if err != nil { + return err + } + return tracker.Add(obj) +}