From 4cc45f5b60c567c8a9ccb81fe014332b3a78abff Mon Sep 17 00:00:00 2001 From: Nick Santos Date: Mon, 11 Feb 2019 17:03:12 -0500 Subject: [PATCH] remove a bunch of dead code (#1147) --- internal/k8s/client.go | 6 -- internal/k8s/exploding_client.go | 8 --- internal/k8s/fake_client.go | 8 --- internal/k8s/pod.go | 84 ------------------------ internal/k8s/pod_test.go | 109 ------------------------------- 5 files changed, 215 deletions(-) diff --git a/internal/k8s/client.go b/internal/k8s/client.go index 537041e2cd..629d1b59c0 100644 --- a/internal/k8s/client.go +++ b/internal/k8s/client.go @@ -79,12 +79,6 @@ type Client interface { // Streams the container logs ContainerLogs(ctx context.Context, podID PodID, cName container.Name, n Namespace, startTime time.Time) (io.ReadCloser, error) - // Gets the ID for the Node on which the specified Pod is running - GetNodeForPod(ctx context.Context, podID PodID) (NodeID, error) - - // Finds the PodID for the instance of appName running on the same node as podID - FindAppByNode(ctx context.Context, nodeID NodeID, appName string, options FindAppByNodeOptions) (PodID, error) - // Opens a tunnel to the specified pod+port. Returns the tunnel's local port and a function that closes the tunnel ForwardPort(ctx context.Context, namespace Namespace, podID PodID, optionalLocalPort, remotePort int) (localPort int, closer func(), err error) diff --git a/internal/k8s/exploding_client.go b/internal/k8s/exploding_client.go index 1df481509b..459b6137f1 100644 --- a/internal/k8s/exploding_client.go +++ b/internal/k8s/exploding_client.go @@ -48,14 +48,6 @@ func (ec *explodingClient) ContainerLogs(ctx context.Context, podID PodID, cName return nil, errors.Wrap(ec.err, "could not set up k8s client") } -func (ec *explodingClient) GetNodeForPod(ctx context.Context, podID PodID) (NodeID, error) { - return NodeID(""), errors.Wrap(ec.err, "could not set up k8s client") -} - -func (ec *explodingClient) FindAppByNode(ctx context.Context, nodeID NodeID, appName string, options FindAppByNodeOptions) (PodID, error) { - return PodID(""), errors.Wrap(ec.err, "could not set up k8s client") -} - func (ec *explodingClient) ForwardPort(ctx context.Context, namespace Namespace, podID PodID, optionalLocalPort, remotePort int) (localPort int, closer func(), err error) { return 0, nil, errors.Wrap(ec.err, "could not set up k8s client") } diff --git a/internal/k8s/fake_client.go b/internal/k8s/fake_client.go index 8186549176..46ab3a6b53 100644 --- a/internal/k8s/fake_client.go +++ b/internal/k8s/fake_client.go @@ -253,14 +253,6 @@ func (c *FakeK8sClient) applyWasCalled() bool { return c.Yaml != "" } -func (c *FakeK8sClient) FindAppByNode(ctx context.Context, nodeID NodeID, appName string, options FindAppByNodeOptions) (PodID, error) { - return PodID("pod2"), nil -} - -func (c *FakeK8sClient) GetNodeForPod(ctx context.Context, podID PodID) (NodeID, error) { - return NodeID("node"), nil -} - func (c *FakeK8sClient) ForwardPort(ctx context.Context, namespace Namespace, podID PodID, optionalLocalPort, remotePort int) (int, func(), error) { c.LastForwardPortPodID = podID c.LastForwardPortRemotePort = remotePort diff --git a/internal/k8s/pod.go b/internal/k8s/pod.go index d13e8eb8bd..33c48659ef 100644 --- a/internal/k8s/pod.go +++ b/internal/k8s/pod.go @@ -1,11 +1,9 @@ package k8s import ( - "bufio" "context" "fmt" "io" - "strings" "time" "github.com/windmilleng/tilt/internal/model" @@ -111,85 +109,3 @@ func NamespaceFromPod(pod *v1.Pod) Namespace { func NodeIDFromPod(pod *v1.Pod) NodeID { return NodeID(pod.Spec.NodeName) } - -func (k K8sClient) GetNodeForPod(ctx context.Context, podID PodID) (NodeID, error) { - jsonPath := "-o=jsonpath={.spec.nodeName}" - stdout, stderr, err := k.kubectlRunner.exec(ctx, k.kubeContext, []string{"get", "pods", podID.String(), jsonPath}) - - if err != nil { - return NodeID(""), errors.Wrapf(err, "error finding node for pod '%s':\nstderr: '%s'", podID.String(), stderr) - } - - lines := nonEmptyLines(stdout) - - if len(lines) == 0 { - return NodeID(""), fmt.Errorf("kubectl output did not contain a node name for pod '%s': '%s'", podID, stdout) - } else if len(lines) > 1 { - return NodeID(""), fmt.Errorf("kubectl returned multiple nodes for pod '%s': '%s'", podID, stdout) - } else { - return NodeID(lines[0]), nil - } -} - -type FindAppByNodeOptions struct { - Namespace string - Owner string -} - -type MultipleAppsFoundError struct { - filterDesc string - pods []string -} - -func (m MultipleAppsFoundError) Error() string { - return fmt.Sprintf("found multiple apps matching %s: '%s'", m.filterDesc, m.pods) -} - -func (k K8sClient) FindAppByNode(ctx context.Context, nodeID NodeID, appName string, options FindAppByNodeOptions) (PodID, error) { - jsonPath := fmt.Sprintf(`-o=jsonpath={range .items[?(@.spec.nodeName=="%s")]}{.metadata.name}{"\n"}`, nodeID) - - filterDesc := fmt.Sprintf("name '%s', node '%s'", appName, nodeID.String()) - - labelArg := fmt.Sprintf("-lapp=%s", appName) - if len(options.Owner) > 0 { - labelArg += fmt.Sprintf(",owner=%s", options.Owner) - filterDesc += fmt.Sprintf(", owner '%s'", options.Owner) - } - - args := append([]string{"get", "pods", labelArg}) - - if len(options.Namespace) > 0 { - args = append(args, fmt.Sprintf("--namespace=%s", options.Namespace)) - filterDesc += fmt.Sprintf(", namespace '%s'", options.Namespace) - } - args = append(args, jsonPath) - - stdout, stderr, err := k.kubectlRunner.exec(ctx, k.kubeContext, args) - - if err != nil { - return PodID(""), errors.Wrapf(err, "error finding app with %s:\nstderr: '%s'", filterDesc, stderr) - } - - lines := nonEmptyLines(stdout) - - if len(lines) == 0 { - return PodID(""), fmt.Errorf("unable to find any apps with %s", filterDesc) - } else if len(lines) > 1 { - return PodID(""), MultipleAppsFoundError{filterDesc, lines} - } else { - return PodID(lines[0]), nil - } -} - -func nonEmptyLines(s string) []string { - scanner := bufio.NewScanner(strings.NewReader(s)) - scanner.Split(bufio.ScanWords) - - var ret []string - - for scanner.Scan() { - ret = append(ret, scanner.Text()) - } - - return ret -} diff --git a/internal/k8s/pod_test.go b/internal/k8s/pod_test.go index 8a91bf81bb..02debaf64a 100644 --- a/internal/k8s/pod_test.go +++ b/internal/k8s/pod_test.go @@ -1,14 +1,12 @@ package k8s import ( - "context" "fmt" "testing" "time" "github.com/windmilleng/tilt/internal/model" - "github.com/pkg/errors" "github.com/windmilleng/tilt/internal/container" "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -53,21 +51,6 @@ var fakePodList = podList( fakePod("cockroachdb-2", "cockroachdb/cockroach:v2.0.5"), fakePod(expectedPod, blorgDevImgStr)) -func (c clientTestFixture) FindAppByNodeWithOptions(options FindAppByNodeOptions) (PodID, error) { - c.setOutput("foo") - return c.client.FindAppByNode(context.Background(), NodeID("foo"), "synclet", options) -} - -func (c clientTestFixture) FindAppByNodeWithOutput(output string) (PodID, error) { - c.setOutput(output) - return c.client.FindAppByNode(context.Background(), NodeID("foo"), "synclet", FindAppByNodeOptions{}) -} - -func (c clientTestFixture) FindAppByNodeWithError(err error) (PodID, error) { - c.setError(err) - return c.client.FindAppByNode(context.Background(), NodeID("foo"), "synclet", FindAppByNodeOptions{}) -} - func (c clientTestFixture) AssertCallExistsWithArg(expectedArg string) { foundMatchingCall := false var errorOutput string @@ -155,95 +138,3 @@ func TestPollForPodsWithImageTimesOut(t *testing.T) { assert.Contains(t, err.Error(), "timed out polling for pod running image") } } - -func TestFindAppByNode(t *testing.T) { - f := newClientTestFixture(t) - podId, err := f.FindAppByNodeWithOutput("foobar") - if assert.NoError(t, err) { - assert.Equal(t, PodID("foobar"), podId) - } -} - -func TestFindAppByNodeNotFound(t *testing.T) { - f := newClientTestFixture(t) - _, err := f.FindAppByNodeWithOutput("") - if assert.Error(t, err) { - assert.Contains(t, err.Error(), "unable to find") - } -} - -func TestFindAppByNodeMultipleFound(t *testing.T) { - f := newClientTestFixture(t) - output := "foobar bazquu" - _, err := f.FindAppByNodeWithOutput(output) - if assert.Error(t, err) { - assert.Contains(t, err.Error(), "multiple") - assert.Contains(t, err.Error(), output) - } -} - -func TestFindAppByNodeKubectlError(t *testing.T) { - f := newClientTestFixture(t) - e := errors.New("asdffdsa") - _, err := f.FindAppByNodeWithError(e) - if assert.Error(t, err) { - assert.Contains(t, err.Error(), e.Error()) - } -} - -func TestFindAppByNodeWithOwner(t *testing.T) { - f := newClientTestFixture(t) - _, _ = f.FindAppByNodeWithOptions(FindAppByNodeOptions{Owner: "bob"}) - f.AssertCallExistsWithArg("-lapp=synclet,owner=bob") -} - -func TestFindAppByNodeWithNamespace(t *testing.T) { - f := newClientTestFixture(t) - _, _ = f.FindAppByNodeWithOptions(FindAppByNodeOptions{Namespace: "kube-system"}) - f.AssertCallExistsWithArg("--namespace=kube-system") -} - -func (c clientTestFixture) GetNodeForPodWithOutput(output string) (NodeID, error) { - c.setOutput(output) - return c.client.GetNodeForPod(context.Background(), PodID("foo")) -} - -func (c clientTestFixture) GetNodeForPodWithError(err error) (NodeID, error) { - c.setError(err) - return c.client.GetNodeForPod(context.Background(), PodID("foo")) -} - -func TestGetNodeForPod(t *testing.T) { - f := newClientTestFixture(t) - nodeID, err := f.GetNodeForPodWithOutput("foobar") - if assert.NoError(t, err) { - assert.Equal(t, NodeID("foobar"), nodeID) - } -} - -func TestGetNodeForPodNotFound(t *testing.T) { - f := newClientTestFixture(t) - _, err := f.GetNodeForPodWithOutput("") - if assert.Error(t, err) { - assert.Contains(t, err.Error(), "did not contain") - } -} - -func TestGetNodeForPodMultipleFound(t *testing.T) { - f := newClientTestFixture(t) - output := "foobar\nbazquu\n" - _, err := f.GetNodeForPodWithOutput(output) - if assert.Error(t, err) { - assert.Contains(t, err.Error(), "multiple") - assert.Contains(t, err.Error(), output) - } -} - -func TestGetNodeForPodKubectlError(t *testing.T) { - f := newClientTestFixture(t) - e := errors.New("asdffdsa") - _, err := f.GetNodeForPodWithError(e) - if assert.Error(t, err) { - assert.Contains(t, err.Error(), e.Error()) - } -}