Skip to content

Commit

Permalink
k8s: implement the local registry discovery protocol. Fixes #3435
Browse files Browse the repository at this point in the history
  • Loading branch information
nicks committed Jun 12, 2020
1 parent 514ae79 commit 80d8898
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
3 changes: 1 addition & 2 deletions internal/k8s/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
61 changes: 61 additions & 0 deletions internal/k8s/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}

0 comments on commit 80d8898

Please sign in to comment.