Skip to content

Commit

Permalink
Feature: support kubectl default-container annotation (#17)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
ArtemTrofimushkin authored Feb 21, 2022
1 parent 1425096 commit 81c18cd
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 5 deletions.
9 changes: 7 additions & 2 deletions cli/cmd/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/kubernetes/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

// ContainerInfo is information about container struct
type ContainerInfo struct {
Runtime string
ID string
Runtime string
}

type containerConfig struct {
Expand All @@ -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],
}
}

Expand Down
2 changes: 2 additions & 0 deletions internal/kubernetes/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

type PodInfo struct {
Annotations map[string]string
Name string
Node string
containers []core.Container
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
22 changes: 22 additions & 0 deletions test/integration/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 81c18cd

Please sign in to comment.