Skip to content

Commit

Permalink
cluster: unify minikube and kind registry configs (#322)
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Santos <[email protected]>
  • Loading branch information
nicks authored Nov 28, 2023
1 parent 226851c commit 4eeafc1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 71 deletions.
40 changes: 40 additions & 0 deletions pkg/cluster/admin_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cluster

import (
"context"
"fmt"
"strings"

"github.com/pkg/errors"
"github.com/tilt-dev/ctlptl/internal/exec"
"github.com/tilt-dev/ctlptl/pkg/api"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

func applyContainerdPatchRegistryApiV2(
ctx context.Context, runner exec.CmdRunner, iostreams genericclioptions.IOStreams,
nodes []string, desired *api.Cluster, registry *api.Registry) error {
for _, node := range nodes {
contents := fmt.Sprintf(`[host."http://%s:%d"]
`, registry.Name, registry.Status.ContainerPort)

localRegistryDir := fmt.Sprintf("/etc/containerd/certs.d/localhost:%d", registry.Status.HostPort)
err := runner.RunIO(ctx,
genericclioptions.IOStreams{In: strings.NewReader(contents), Out: iostreams.Out, ErrOut: iostreams.ErrOut},
"docker", "exec", "-i", node, "sh", "-c",
fmt.Sprintf("mkdir -p %s && cp /dev/stdin %s/hosts.toml", localRegistryDir, localRegistryDir))
if err != nil {
return errors.Wrap(err, "configuring registry")
}

networkRegistryDir := fmt.Sprintf("/etc/containerd/certs.d/%s:%d", registry.Name, registry.Status.ContainerPort)
err = runner.RunIO(ctx,
genericclioptions.IOStreams{In: strings.NewReader(contents), Out: iostreams.Out, ErrOut: iostreams.ErrOut},
"docker", "exec", "-i", node, "sh", "-c",
fmt.Sprintf("mkdir -p %s && cp /dev/stdin %s/hosts.toml", networkRegistryDir, networkRegistryDir))
if err != nil {
return errors.Wrap(err, "configuring registry")
}
}
return nil
}
25 changes: 1 addition & 24 deletions pkg/cluster/admin_kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,30 +187,7 @@ func (a *kindAdmin) applyContainerdPatchRegistryApiV2(ctx context.Context, desir
if err != nil {
return errors.Wrap(err, "configuring registry")
}

for _, node := range nodes {
contents := fmt.Sprintf(`[host."http://%s:%d"]
`, registry.Name, registry.Status.ContainerPort)

localRegistryDir := fmt.Sprintf("/etc/containerd/certs.d/localhost:%d", registry.Status.HostPort)
err := a.runner.RunIO(ctx,
genericclioptions.IOStreams{In: strings.NewReader(contents), Out: a.iostreams.Out, ErrOut: a.iostreams.ErrOut},
"docker", "exec", "-i", node, "sh", "-c",
fmt.Sprintf("mkdir -p %s && cp /dev/stdin %s/hosts.toml", localRegistryDir, localRegistryDir))
if err != nil {
return errors.Wrap(err, "configuring registry")
}

networkRegistryDir := fmt.Sprintf("/etc/containerd/certs.d/%s:%d", registry.Name, registry.Status.ContainerPort)
err = a.runner.RunIO(ctx,
genericclioptions.IOStreams{In: strings.NewReader(contents), Out: a.iostreams.Out, ErrOut: a.iostreams.ErrOut},
"docker", "exec", "-i", node, "sh", "-c",
fmt.Sprintf("mkdir -p %s && cp /dev/stdin %s/hosts.toml", networkRegistryDir, networkRegistryDir))
if err != nil {
return errors.Wrap(err, "configuring registry")
}
}
return nil
return applyContainerdPatchRegistryApiV2(ctx, a.runner, a.iostreams, nodes, desired, registry)
}

func (a *kindAdmin) getNodes(ctx context.Context, cluster string) ([]string, error) {
Expand Down
63 changes: 16 additions & 47 deletions pkg/cluster/admin_minikube.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,13 @@ func (a *minikubeAdmin) ensureRegistryDisconnected(ctx context.Context, registry
return nil
}

// We want to make sure that the image is pullable from either:
// localhost:[registry-port] or
// [registry-name]:5000
// by cloning the registry config created by minikube's --insecure-registry.
func (a *minikubeAdmin) applyContainerdPatchRegistryApiV2(ctx context.Context, desired *api.Cluster, registry *api.Registry, networkMode container.NetworkMode) error {
func (a *minikubeAdmin) getNodes(ctx context.Context, name string) ([]string, error) {
nodeOutput := bytes.NewBuffer(nil)
err := a.runner.RunIO(ctx,
genericclioptions.IOStreams{Out: nodeOutput, ErrOut: a.iostreams.ErrOut},
"minikube", "-p", desired.Name, "node", "list")
"minikube", "-p", name, "node", "list")
if err != nil {
return errors.Wrap(err, "configuring minikube registry")
return nil, errors.Wrap(err, "configuring minikube registry")
}

nodes := []string{}
Expand All @@ -250,30 +246,20 @@ func (a *minikubeAdmin) applyContainerdPatchRegistryApiV2(ctx context.Context, d
}
nodes = append(nodes, node)
}
return nodes, nil
}

for _, node := range nodes {
networkHost := registry.Status.IPAddress
if networkMode.IsUserDefined() {
networkHost = registry.Name
}

err := a.runner.RunIO(ctx,
a.iostreams,
"minikube", "-p", desired.Name, "--node", node,
"ssh", "sudo", "cp", `\-R`,
fmt.Sprintf(`/etc/containerd/certs.d/%s\:%d`, networkHost, registry.Status.ContainerPort),
fmt.Sprintf(`/etc/containerd/certs.d/localhost\:%d`, registry.Status.HostPort))
if err != nil {
return errors.Wrap(err, "configuring minikube registry")
}

err = a.runner.RunIO(ctx, a.iostreams, "minikube", "-p", desired.Name, "--node", node,
"ssh", "sudo", "systemctl", "restart", "containerd")
if err != nil {
return errors.Wrap(err, "configuring minikube registry")
}
// We want to make sure that the image is pullable from either:
// localhost:[registry-port] or
// [registry-name]:5000
// by cloning the registry config created by minikube's --insecure-registry.
func (a *minikubeAdmin) applyContainerdPatchRegistryApiV2(ctx context.Context, desired *api.Cluster, registry *api.Registry, networkMode container.NetworkMode) error {
nodes, err := a.getNodes(ctx, desired.Name)
if err != nil {
return errors.Wrap(err, "configuring minikube registry")
}
return nil

return applyContainerdPatchRegistryApiV2(ctx, a.runner, a.iostreams, nodes, desired, registry)
}

// We still patch containerd so that the user can push/pull from localhost.
Expand All @@ -282,28 +268,11 @@ func (a *minikubeAdmin) applyContainerdPatchRegistryApiV2(ctx context.Context, d
func (a *minikubeAdmin) applyContainerdPatchRegistryApiV1(ctx context.Context, desired *api.Cluster, registry *api.Registry, networkMode container.NetworkMode) error {
configPath := "/etc/containerd/config.toml"

nodeOutput := bytes.NewBuffer(nil)
err := a.runner.RunIO(ctx,
genericclioptions.IOStreams{Out: nodeOutput, ErrOut: a.iostreams.ErrOut},
"minikube", "-p", desired.Name, "node", "list")
nodes, err := a.getNodes(ctx, desired.Name)
if err != nil {
return errors.Wrap(err, "configuring minikube registry")
}

nodes := []string{}
nodeOutputSplit := strings.Split(nodeOutput.String(), "\n")
for _, line := range nodeOutputSplit {
fields := strings.Fields(line)
if len(fields) == 0 {
continue
}
node := strings.TrimSpace(fields[0])
if node == "" {
continue
}
nodes = append(nodes, node)
}

for _, node := range nodes {
networkHost := registry.Status.IPAddress
if networkMode.IsUserDefined() {
Expand Down

0 comments on commit 4eeafc1

Please sign in to comment.