diff --git a/cli/cmd/launch.go b/cli/cmd/launch.go index b8f3cd7..f615ada 100644 --- a/cli/cmd/launch.go +++ b/cli/cmd/launch.go @@ -31,7 +31,12 @@ func (cb *CommandBuilder) launch() error { return errors.Wrap(err, "Failed to get info about target pod") } - targetContainer, err := targetPod.FindContainerInfo(cb.CommonOptions.Container) + targetContainerName := cb.CommonOptions.Container + if targetContainerName == "" { + targetContainerName = targetPod.Annotations["kubectl.kubernetes.io/default-container"] + } + + targetContainer, err := targetPod.FindContainerInfo(targetContainerName) if err != nil { return errors.Wrap(err, "Failed to get info about target container") } @@ -40,7 +45,7 @@ func (cb *CommandBuilder) launch() error { NewJobRunSpec(cb.args(targetContainer), cb.CommonOptions.Image, targetPod). WithContainerFSVolume(targetContainer) - if targetPod.ContainsMountedTmp(cb.CommonOptions.Container) { + if targetPod.ContainsMountedTmp(targetContainerName) { jobSpec.WithContainerMountsVolume(targetContainer) } diff --git a/internal/kubernetes/containers.go b/internal/kubernetes/containers.go index ba1f67e..f842895 100644 --- a/internal/kubernetes/containers.go +++ b/internal/kubernetes/containers.go @@ -11,8 +11,8 @@ import ( // ContainerInfo is information about container struct type ContainerInfo struct { - Runtime string ID string + Runtime string } type containerConfig struct { @@ -30,8 +30,8 @@ func NewContainerInfo(cs *core.ContainerStatus) *ContainerInfo { containerInfo := strings.Split(cs.ContainerID, "://") return &ContainerInfo{ - Runtime: containerInfo[0], ID: containerInfo[1], + Runtime: containerInfo[0], } } diff --git a/internal/kubernetes/pods.go b/internal/kubernetes/pods.go index bafa5ee..de7c72d 100644 --- a/internal/kubernetes/pods.go +++ b/internal/kubernetes/pods.go @@ -15,6 +15,7 @@ import ( ) type PodInfo struct { + Annotations map[string]string Name string Node string containers []core.Container @@ -24,6 +25,7 @@ type PodInfo struct { // NewPodInfo returns PodInfo generated from core.Pod spec func NewPodInfo(pod *core.Pod) *PodInfo { return &PodInfo{ + Annotations: pod.Annotations, Name: pod.Name, Node: pod.Spec.NodeName, containers: pod.Spec.Containers, diff --git a/test/integration/manageddump_test.go b/test/integration/dotnet_dump_test.go similarity index 91% rename from test/integration/manageddump_test.go rename to test/integration/dotnet_dump_test.go index 9a638aa..6e3c580 100644 --- a/test/integration/manageddump_test.go +++ b/test/integration/dotnet_dump_test.go @@ -41,6 +41,11 @@ func Test_DumpSubcommand(t *testing.T) { }, pod: multiContainerPod(), }, + { + name: "MultiContainer pod with default-container annotation", + args: []string{}, + pod: multiContainerPodWithDefaultContainer(), + }, { name: "MultiContainer pod with shared mount", args: []string{ diff --git a/test/integration/gcdump_test.go b/test/integration/dotnet_gcdump_test.go similarity index 91% rename from test/integration/gcdump_test.go rename to test/integration/dotnet_gcdump_test.go index de90b4c..52e0dc3 100644 --- a/test/integration/gcdump_test.go +++ b/test/integration/dotnet_gcdump_test.go @@ -50,6 +50,11 @@ func Test_GCDumpSubcommand(t *testing.T) { }, pod: multiContainerPod(), }, + { + name: "MultiContainer pod with default-container annotation", + args: []string{}, + pod: multiContainerPodWithDefaultContainer(), + }, { name: "MultiContainer pod with shared mount", args: []string{ diff --git a/test/integration/trace_test.go b/test/integration/dotnet_trace_test.go similarity index 90% rename from test/integration/trace_test.go rename to test/integration/dotnet_trace_test.go index abfc8d0..ef16b1a 100644 --- a/test/integration/trace_test.go +++ b/test/integration/dotnet_trace_test.go @@ -51,13 +51,18 @@ func Test_TraceSubcommand(t *testing.T) { pod: singleContainerPod(), }, { - name: "Multicontainer pod", + name: "MultiContainer pod", args: []string{ "--container", targetContainerName, }, pod: multiContainerPod(), }, + { + name: "MultiContainer pod with default-container annotation", + args: []string{}, + pod: multiContainerPodWithDefaultContainer(), + }, { name: "MultiContainer pod with shared mount", args: []string{ diff --git a/test/integration/helpers_test.go b/test/integration/helpers_test.go index 3d8768b..b445dfb 100644 --- a/test/integration/helpers_test.go +++ b/test/integration/helpers_test.go @@ -118,6 +118,28 @@ func multiContainerPod() *core.Pod { } } +func multiContainerPodWithDefaultContainer() *core.Pod { + objectMeta := generateRandomPodMeta() + objectMeta.Annotations = map[string]string{ + "kubectl.kubernetes.io/default-container": targetContainerName, + } + return &core.Pod{ + ObjectMeta: objectMeta, + Spec: core.PodSpec{ + Containers: []core.Container{ + { + Name: targetContainerName, + Image: sampleAppImage, + }, + { + Name: "sidecar", + Image: "gcr.io/google_containers/pause-amd64:3.1", + }, + }, + }, + } +} + func multiContainerPodWithSharedMount() *core.Pod { return &core.Pod{ ObjectMeta: generateRandomPodMeta(),