From 1eebafade632697cbda400c82f287148547093ec Mon Sep 17 00:00:00 2001 From: Nick Santos Date: Mon, 20 Nov 2023 16:40:58 -0500 Subject: [PATCH] Revert "cluster: update kind to use new containerd patch format (#314)" (#320) This reverts commit 5d03bf0f1720a3448f745cc2cb338ecdce58854e. Signed-off-by: Nick Santos --- pkg/api/types.go | 3 +- pkg/cluster/admin.go | 12 ---- pkg/cluster/admin_docker_desktop.go | 1 + pkg/cluster/admin_kind.go | 106 ++++------------------------ pkg/cluster/admin_kind_test.go | 3 +- pkg/cluster/admin_minikube.go | 12 ++-- pkg/cluster/docker.go | 1 - 7 files changed, 22 insertions(+), 116 deletions(-) diff --git a/pkg/api/types.go b/pkg/api/types.go index a315d39..e2ffd1d 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -1,11 +1,10 @@ package api import ( + "github.com/tilt-dev/ctlptl/pkg/api/k3dv1alpha4" "github.com/tilt-dev/localregistry-go" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/kind/pkg/apis/config/v1alpha4" - - "github.com/tilt-dev/ctlptl/pkg/api/k3dv1alpha4" ) // TypeMeta partially copies apimachinery/pkg/apis/meta/v1.TypeMeta diff --git a/pkg/cluster/admin.go b/pkg/cluster/admin.go index 30ecad2..9de34ce 100644 --- a/pkg/cluster/admin.go +++ b/pkg/cluster/admin.go @@ -31,15 +31,3 @@ type Admin interface { type AdminInContainer interface { ModifyConfigInContainer(ctx context.Context, cluster *api.Cluster, containerID string, dockerClient dctr.Client, configWriter configWriter) error } - -// Containerd made major changes to their config format for -// configuring registries. Each cluster has its own way -// of detecting this. - -type containerdRegistryAPI int - -const ( - containerdRegistryV1 containerdRegistryAPI = iota - containerdRegistryV2 - containerdRegistryBroken -) diff --git a/pkg/cluster/admin_docker_desktop.go b/pkg/cluster/admin_docker_desktop.go index 3603ff1..fec2aa5 100644 --- a/pkg/cluster/admin_docker_desktop.go +++ b/pkg/cluster/admin_docker_desktop.go @@ -12,6 +12,7 @@ import ( // The DockerDesktop manages the Kubernetes cluster for DockerDesktop. // This is a bit different than the other admins, due to the overlap +// type dockerDesktopAdmin struct { os string host string diff --git a/pkg/cluster/admin_kind.go b/pkg/cluster/admin_kind.go index 8e79fad..3e3ed8f 100644 --- a/pkg/cluster/admin_kind.go +++ b/pkg/cluster/admin_kind.go @@ -56,7 +56,7 @@ func (a *kindAdmin) EnsureInstalled(ctx context.Context) error { return nil } -func (a *kindAdmin) kindClusterConfig(desired *api.Cluster, registry *api.Registry, registryAPI containerdRegistryAPI) *v1alpha4.Cluster { +func (a *kindAdmin) kindClusterConfig(desired *api.Cluster, registry *api.Registry) *v1alpha4.Cluster { kindConfig := desired.KindV1Alpha4Cluster if kindConfig == nil { kindConfig = &v1alpha4.Cluster{} @@ -67,22 +67,13 @@ func (a *kindAdmin) kindClusterConfig(desired *api.Cluster, registry *api.Regist kindConfig.APIVersion = "kind.x-k8s.io/v1alpha4" if registry != nil { - if registryAPI == containerdRegistryV2 { - // Point to the registry config path. - // We'll add these files post-creation. - patch := `[plugins."io.containerd.grpc.v1.cri".registry] - config_path = "/etc/containerd/certs.d" -` - kindConfig.ContainerdConfigPatches = append(kindConfig.ContainerdConfigPatches, patch) - } else { - patch := fmt.Sprintf(`[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:%d"] + patch := fmt.Sprintf(`[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:%d"] endpoint = ["http://%s:%d"] [plugins."io.containerd.grpc.v1.cri".registry.mirrors."%s:%d"] endpoint = ["http://%s:%d"] `, registry.Status.HostPort, registry.Name, registry.Status.ContainerPort, - registry.Name, registry.Status.ContainerPort, registry.Name, registry.Status.ContainerPort) - kindConfig.ContainerdConfigPatches = append(kindConfig.ContainerdConfigPatches, patch) - } + registry.Name, registry.Status.ContainerPort, registry.Name, registry.Status.ContainerPort) + kindConfig.ContainerdConfigPatches = append(kindConfig.ContainerdConfigPatches, patch) } return kindConfig } @@ -116,22 +107,13 @@ func (a *kindAdmin) Create(ctx context.Context, desired *api.Cluster, registry * } } - kindVersion, err := a.getKindVersion(ctx) - if err != nil { - return errors.Wrap(err, "creating cluster") - } - - registryAPI := containerdRegistryV2 - kindVersionSemver, err := semver.ParseTolerant(kindVersion) - if err != nil { - return errors.Wrap(err, "parsing kind version") - } - if kindVersionSemver.LT(semver.MustParse("0.20.0")) { - registryAPI = containerdRegistryV1 - } - args := []string{"create", "cluster", "--name", kindName} if desired.KubernetesVersion != "" { + kindVersion, err := a.getKindVersion(ctx) + if err != nil { + return errors.Wrap(err, "creating cluster") + } + node, err := a.getNodeImage(ctx, kindVersion, desired.KubernetesVersion) if err != nil { return errors.Wrap(err, "creating cluster") @@ -139,7 +121,7 @@ func (a *kindAdmin) Create(ctx context.Context, desired *api.Cluster, registry * args = append(args, "--image", node) } - kindConfig := a.kindClusterConfig(desired, registry, registryAPI) + kindConfig := a.kindClusterConfig(desired, registry) buf := bytes.NewBuffer(nil) encoder := yaml.NewEncoder(buf) err = encoder.Encode(kindConfig) @@ -158,73 +140,15 @@ func (a *kindAdmin) Create(ctx context.Context, desired *api.Cluster, registry * networkName := kindNetworkName() - if registry != nil { - if !a.inKindNetwork(registry, networkName) { - _, _ = fmt.Fprintf(a.iostreams.ErrOut, " Connecting kind to registry %s\n", registry.Name) - err := a.dockerClient.NetworkConnect(ctx, networkName, registry.Name, nil) - if err != nil { - return errors.Wrap(err, "connecting registry") - } - } - - if registryAPI == containerdRegistryV2 { - err = a.applyContainerdPatchRegistryApiV2(ctx, desired, registry) - if err != nil { - return err - } - } - } - - return nil -} - -// We want to make sure that the image is pullable from either: -// localhost:[registry-port] or -// [registry-name]:5000 -// by configuring containerd to rewrite the url. -func (a *kindAdmin) applyContainerdPatchRegistryApiV2(ctx context.Context, desired *api.Cluster, registry *api.Registry) error { - nodes, err := a.getNodes(ctx, desired.Name) - if err != nil { - return errors.Wrap(err, "configuring registry") - } - - for _, node := range nodes { - registryDir := fmt.Sprintf("/etc/containerd/certs.d/localhost:%d", registry.Status.HostPort) - contents := fmt.Sprintf(`[host."http://%s:%d"] -`, registry.Name, registry.Status.ContainerPort) - iostreams := a.iostreams - iostreams.In = strings.NewReader(contents) - err := a.runner.RunIO(ctx, - iostreams, - "docker", "exec", "-i", node, "sh", "-c", - fmt.Sprintf("mkdir -p %s && cp /dev/stdin %s/hosts.toml", registryDir, registryDir)) + if registry != nil && !a.inKindNetwork(registry, networkName) { + _, _ = fmt.Fprintf(a.iostreams.ErrOut, " Connecting kind to registry %s\n", registry.Name) + err := a.dockerClient.NetworkConnect(ctx, networkName, registry.Name, nil) if err != nil { - return errors.Wrap(err, "configuring registry") + return errors.Wrap(err, "connecting registry") } } - return nil -} -func (a *kindAdmin) getNodes(ctx context.Context, cluster string) ([]string, error) { - kindName := strings.TrimPrefix(cluster, "kind-") - buf := bytes.NewBuffer(nil) - iostreams := a.iostreams - iostreams.Out = buf - err := a.runner.RunIO(ctx, iostreams, - "kind", "get", "nodes", "--name", kindName) - if err != nil { - return nil, errors.Wrap(err, "kind get nodes") - } - - scanner := bufio.NewScanner(buf) - result := []string{} - for scanner.Scan() { - line := strings.TrimSpace(scanner.Text()) - if line != "" { - result = append(result, line) - } - } - return result, nil + return nil } func (a *kindAdmin) clusterExists(ctx context.Context, cluster string) (bool, error) { diff --git a/pkg/cluster/admin_kind_test.go b/pkg/cluster/admin_kind_test.go index 0b62d86..7297692 100644 --- a/pkg/cluster/admin_kind_test.go +++ b/pkg/cluster/admin_kind_test.go @@ -6,9 +6,8 @@ import ( "testing" "github.com/stretchr/testify/assert" - "k8s.io/cli-runtime/pkg/genericclioptions" - "github.com/tilt-dev/ctlptl/internal/exec" + "k8s.io/cli-runtime/pkg/genericclioptions" ) func TestNodeImage(t *testing.T) { diff --git a/pkg/cluster/admin_minikube.go b/pkg/cluster/admin_minikube.go index 8b70c97..72b6863 100644 --- a/pkg/cluster/admin_minikube.go +++ b/pkg/cluster/admin_minikube.go @@ -88,12 +88,8 @@ func (a *minikubeAdmin) Create(ctx context.Context, desired *api.Cluster, regist if err != nil { return err } - registryAPI := containerdRegistryV1 - if v.GTE(v1_26) && v.LT(v1_27) { - registryAPI = containerdRegistryBroken - } else if v.GTE(v1_27) { - registryAPI = containerdRegistryV2 - } + isRegistryApiBroken := v.GTE(v1_26) && v.LT(v1_27) + isRegistryApiV2 := v.GTE(v1_26) clusterName := desired.Name if registry != nil { @@ -143,7 +139,7 @@ func (a *minikubeAdmin) Create(ctx context.Context, desired *api.Cluster, regist // https://github.com/tilt-dev/ctlptl/issues/239 if registry != nil { - if registryAPI == containerdRegistryBroken { + if isRegistryApiBroken { return fmt.Errorf( "Error: Local registries are broken in minikube v1.26.\n" + "See: https://github.com/kubernetes/minikube/issues/14480 .\n" + @@ -172,7 +168,7 @@ func (a *minikubeAdmin) Create(ctx context.Context, desired *api.Cluster, regist return err } - if registryAPI == containerdRegistryV2 { + if isRegistryApiV2 { err = a.applyContainerdPatchRegistryApiV2(ctx, desired, registry, networkMode) if err != nil { return err diff --git a/pkg/cluster/docker.go b/pkg/cluster/docker.go index 43f201d..2450eef 100644 --- a/pkg/cluster/docker.go +++ b/pkg/cluster/docker.go @@ -5,7 +5,6 @@ import ( "os" "github.com/docker/docker/pkg/stringid" - "github.com/tilt-dev/ctlptl/internal/dctr" )