Skip to content

Commit 81c18cd

Browse files
Feature: support kubectl default-container annotation (dodopizza#17)
* feat(cli): add support for default-container well known annotation Signed-off-by: ArtemTrofimushkin <artemtrofimushkin@gmail.com> * chore(test): add test cases for default-container Signed-off-by: ArtemTrofimushkin <artemtrofimushkin@gmail.com> * chore(tests): sync test names with flags package Signed-off-by: ArtemTrofimushkin <artemtrofimushkin@gmail.com> * fix(cli): resolve lint issues Signed-off-by: ArtemTrofimushkin <artemtrofimushkin@gmail.com>
1 parent 1425096 commit 81c18cd

7 files changed

Lines changed: 49 additions & 5 deletions

File tree

‎cli/cmd/launch.go‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ func (cb *CommandBuilder) launch() error {
3131
return errors.Wrap(err, "Failed to get info about target pod")
3232
}
3333

34-
targetContainer, err := targetPod.FindContainerInfo(cb.CommonOptions.Container)
34+
targetContainerName := cb.CommonOptions.Container
35+
if targetContainerName == "" {
36+
targetContainerName = targetPod.Annotations["kubectl.kubernetes.io/default-container"]
37+
}
38+
39+
targetContainer, err := targetPod.FindContainerInfo(targetContainerName)
3540
if err != nil {
3641
return errors.Wrap(err, "Failed to get info about target container")
3742
}
@@ -40,7 +45,7 @@ func (cb *CommandBuilder) launch() error {
4045
NewJobRunSpec(cb.args(targetContainer), cb.CommonOptions.Image, targetPod).
4146
WithContainerFSVolume(targetContainer)
4247

43-
if targetPod.ContainsMountedTmp(cb.CommonOptions.Container) {
48+
if targetPod.ContainsMountedTmp(targetContainerName) {
4449
jobSpec.WithContainerMountsVolume(targetContainer)
4550
}
4651

‎internal/kubernetes/containers.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111

1212
// ContainerInfo is information about container struct
1313
type ContainerInfo struct {
14-
Runtime string
1514
ID string
15+
Runtime string
1616
}
1717

1818
type containerConfig struct {
@@ -30,8 +30,8 @@ func NewContainerInfo(cs *core.ContainerStatus) *ContainerInfo {
3030
containerInfo := strings.Split(cs.ContainerID, "://")
3131

3232
return &ContainerInfo{
33-
Runtime: containerInfo[0],
3433
ID: containerInfo[1],
34+
Runtime: containerInfo[0],
3535
}
3636
}
3737

‎internal/kubernetes/pods.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
)
1616

1717
type PodInfo struct {
18+
Annotations map[string]string
1819
Name string
1920
Node string
2021
containers []core.Container
@@ -24,6 +25,7 @@ type PodInfo struct {
2425
// NewPodInfo returns PodInfo generated from core.Pod spec
2526
func NewPodInfo(pod *core.Pod) *PodInfo {
2627
return &PodInfo{
28+
Annotations: pod.Annotations,
2729
Name: pod.Name,
2830
Node: pod.Spec.NodeName,
2931
containers: pod.Spec.Containers,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ func Test_DumpSubcommand(t *testing.T) {
4141
},
4242
pod: multiContainerPod(),
4343
},
44+
{
45+
name: "MultiContainer pod with default-container annotation",
46+
args: []string{},
47+
pod: multiContainerPodWithDefaultContainer(),
48+
},
4449
{
4550
name: "MultiContainer pod with shared mount",
4651
args: []string{
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func Test_GCDumpSubcommand(t *testing.T) {
5050
},
5151
pod: multiContainerPod(),
5252
},
53+
{
54+
name: "MultiContainer pod with default-container annotation",
55+
args: []string{},
56+
pod: multiContainerPodWithDefaultContainer(),
57+
},
5358
{
5459
name: "MultiContainer pod with shared mount",
5560
args: []string{
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,18 @@ func Test_TraceSubcommand(t *testing.T) {
5151
pod: singleContainerPod(),
5252
},
5353
{
54-
name: "Multicontainer pod",
54+
name: "MultiContainer pod",
5555
args: []string{
5656
"--container",
5757
targetContainerName,
5858
},
5959
pod: multiContainerPod(),
6060
},
61+
{
62+
name: "MultiContainer pod with default-container annotation",
63+
args: []string{},
64+
pod: multiContainerPodWithDefaultContainer(),
65+
},
6166
{
6267
name: "MultiContainer pod with shared mount",
6368
args: []string{

‎test/integration/helpers_test.go‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,28 @@ func multiContainerPod() *core.Pod {
118118
}
119119
}
120120

121+
func multiContainerPodWithDefaultContainer() *core.Pod {
122+
objectMeta := generateRandomPodMeta()
123+
objectMeta.Annotations = map[string]string{
124+
"kubectl.kubernetes.io/default-container": targetContainerName,
125+
}
126+
return &core.Pod{
127+
ObjectMeta: objectMeta,
128+
Spec: core.PodSpec{
129+
Containers: []core.Container{
130+
{
131+
Name: targetContainerName,
132+
Image: sampleAppImage,
133+
},
134+
{
135+
Name: "sidecar",
136+
Image: "gcr.io/google_containers/pause-amd64:3.1",
137+
},
138+
},
139+
},
140+
}
141+
}
142+
121143
func multiContainerPodWithSharedMount() *core.Pod {
122144
return &core.Pod{
123145
ObjectMeta: generateRandomPodMeta(),

0 commit comments

Comments
 (0)