Skip to content

Commit

Permalink
remove a bunch of dead code (#1147)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicks authored Feb 11, 2019
1 parent ea6d728 commit 4cc45f5
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 215 deletions.
6 changes: 0 additions & 6 deletions internal/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 0 additions & 8 deletions internal/k8s/exploding_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
8 changes: 0 additions & 8 deletions internal/k8s/fake_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
84 changes: 0 additions & 84 deletions internal/k8s/pod.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package k8s

import (
"bufio"
"context"
"fmt"
"io"
"strings"
"time"

"github.com/windmilleng/tilt/internal/model"
Expand Down Expand Up @@ -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
}
109 changes: 0 additions & 109 deletions internal/k8s/pod_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
}

0 comments on commit 4cc45f5

Please sign in to comment.