From d1ab88afdca20eec65f7d8e5c33499305c81715e Mon Sep 17 00:00:00 2001 From: Nick Santos Date: Mon, 16 Nov 2020 21:15:40 -0500 Subject: [PATCH] cluster: add HostFromClusterNetwork to minikube (#53) --- pkg/cluster/admin.go | 5 ++++- pkg/cluster/admin_docker_desktop.go | 4 ++-- pkg/cluster/admin_kind.go | 4 ++-- pkg/cluster/admin_minikube.go | 19 +++++++++++++++---- pkg/cluster/cluster.go | 5 ++++- pkg/cluster/cluster_test.go | 4 ++-- 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/pkg/cluster/admin.go b/pkg/cluster/admin.go index a648b74..4ceb752 100644 --- a/pkg/cluster/admin.go +++ b/pkg/cluster/admin.go @@ -12,6 +12,9 @@ import ( type Admin interface { EnsureInstalled(ctx context.Context) error Create(ctx context.Context, desired *api.Cluster, registry *api.Registry) error - LocalRegistryHosting(registry *api.Registry) *localregistry.LocalRegistryHostingV1 + + // Infers the LocalRegistryHosting that this admin will try to configure. + LocalRegistryHosting(ctx context.Context, desired *api.Cluster, registry *api.Registry) (*localregistry.LocalRegistryHostingV1, error) + Delete(ctx context.Context, config *api.Cluster) error } diff --git a/pkg/cluster/admin_docker_desktop.go b/pkg/cluster/admin_docker_desktop.go index e6f9aef..5eee00e 100644 --- a/pkg/cluster/admin_docker_desktop.go +++ b/pkg/cluster/admin_docker_desktop.go @@ -32,8 +32,8 @@ func (a *dockerDesktopAdmin) Create(ctx context.Context, desired *api.Cluster, r return fmt.Errorf("docker-desktop Kubernetes clusters are only available on macos and windows") } -func (a *dockerDesktopAdmin) LocalRegistryHosting(registry *api.Registry) *localregistry.LocalRegistryHostingV1 { - return nil +func (a *dockerDesktopAdmin) LocalRegistryHosting(ctx context.Context, desired *api.Cluster, registry *api.Registry) (*localregistry.LocalRegistryHostingV1, error) { + return nil, nil } func (a *dockerDesktopAdmin) Delete(ctx context.Context, config *api.Cluster) error { diff --git a/pkg/cluster/admin_kind.go b/pkg/cluster/admin_kind.go index 0ef698b..67d201e 100644 --- a/pkg/cluster/admin_kind.go +++ b/pkg/cluster/admin_kind.go @@ -97,12 +97,12 @@ func (a *kindAdmin) inKindNetwork(registry *api.Registry) bool { return false } -func (a *kindAdmin) LocalRegistryHosting(registry *api.Registry) *localregistry.LocalRegistryHostingV1 { +func (a *kindAdmin) LocalRegistryHosting(ctx context.Context, desired *api.Cluster, registry *api.Registry) (*localregistry.LocalRegistryHostingV1, error) { return &localregistry.LocalRegistryHostingV1{ Host: fmt.Sprintf("localhost:%d", registry.Status.HostPort), HostFromClusterNetwork: fmt.Sprintf("%s:%d", registry.Name, registry.Status.ContainerPort), Help: "https://github.com/tilt-dev/ctlptl", - } + }, nil } func (a *kindAdmin) Delete(ctx context.Context, config *api.Cluster) error { diff --git a/pkg/cluster/admin_minikube.go b/pkg/cluster/admin_minikube.go index 1070b9a..eeb4182 100644 --- a/pkg/cluster/admin_minikube.go +++ b/pkg/cluster/admin_minikube.go @@ -161,11 +161,22 @@ func (a *minikubeAdmin) inRegistryNetwork(registry *api.Registry, networkMode co return false } -func (a *minikubeAdmin) LocalRegistryHosting(registry *api.Registry) *localregistry.LocalRegistryHostingV1 { - return &localregistry.LocalRegistryHostingV1{ - Host: fmt.Sprintf("localhost:%d", registry.Status.HostPort), - Help: "https://github.com/tilt-dev/ctlptl", +func (a *minikubeAdmin) LocalRegistryHosting(ctx context.Context, desired *api.Cluster, registry *api.Registry) (*localregistry.LocalRegistryHostingV1, error) { + container, err := a.dockerClient.ContainerInspect(ctx, desired.Name) + if err != nil { + return nil, errors.Wrap(err, "inspecting minikube cluster") + } + networkMode := container.HostConfig.NetworkMode + networkHost := registry.Status.IPAddress + if networkMode.IsUserDefined() { + networkHost = registry.Name } + + return &localregistry.LocalRegistryHostingV1{ + Host: fmt.Sprintf("localhost:%d", registry.Status.HostPort), + HostFromClusterNetwork: fmt.Sprintf("%s:%d", networkHost, registry.Status.ContainerPort), + Help: "https://github.com/tilt-dev/ctlptl", + }, nil } func (a *minikubeAdmin) Delete(ctx context.Context, config *api.Cluster) error { diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index fefe540..8c4c27c 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -539,7 +539,10 @@ func (c *Controller) Apply(ctx context.Context, desired *api.Cluster) (*api.Clus // Create a configmap on the cluster, so that other tools know that a registry // has been configured. func (c *Controller) createRegistryHosting(ctx context.Context, admin Admin, cluster *api.Cluster, reg *api.Registry) error { - hosting := admin.LocalRegistryHosting(reg) + hosting, err := admin.LocalRegistryHosting(ctx, cluster, reg) + if err != nil { + return err + } if hosting == nil { return nil } diff --git a/pkg/cluster/cluster_test.go b/pkg/cluster/cluster_test.go index 9922215..ab779b0 100644 --- a/pkg/cluster/cluster_test.go +++ b/pkg/cluster/cluster_test.go @@ -408,11 +408,11 @@ func (a *fakeAdmin) Create(ctx context.Context, config *api.Cluster, registry *a return nil } -func (a *fakeAdmin) LocalRegistryHosting(registry *api.Registry) *localregistry.LocalRegistryHostingV1 { +func (a *fakeAdmin) LocalRegistryHosting(ctx context.Context, cluster *api.Cluster, registry *api.Registry) (*localregistry.LocalRegistryHostingV1, error) { return &localregistry.LocalRegistryHostingV1{ Host: fmt.Sprintf("localhost:%d", registry.Status.HostPort), Help: "https://github.com/tilt-dev/ctlptl", - } + }, nil } func (a *fakeAdmin) Delete(ctx context.Context, config *api.Cluster) error {