Skip to content

Commit

Permalink
feat(controller): Add default container annotation to workflow pod. F…
Browse files Browse the repository at this point in the history
…Ixes: #5643 (#7127)

Signed-off-by: Denis Melnik <denis@codefresh.io>
  • Loading branch information
denis-codefresh authored Nov 5, 2021
1 parent 0482964 commit 52321e2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions workflow/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const (
// DockerSockVolumeName is the volume name for the /var/run/docker.sock host path volume
DockerSockVolumeName = "docker-sock"

// AnnotationKeyDefaultContainer is the annotation that specify container that will be used by default in case of kubectl commands for example
AnnotationKeyDefaultContainer = "kubectl.kubernetes.io/default-container"

// AnnotationKeyNodeID is the ID of the node.
// Historically, the pod name was the same as the node ID.
// Therefore, if it does not exist, then the node ID is the pod name.
Expand Down
11 changes: 11 additions & 0 deletions workflow/controller/workflowpod.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,17 @@ func (woc *wfOperationCtx) createWorkflowPod(ctx context.Context, nodeName strin
// container's PID and root filesystem.
pod.Spec.Containers = append(pod.Spec.Containers, mainCtrs...)

// Configuring default container to be used with commands like "kubectl exec/logs".
// Select "main" container if it's available. In other case use the last container (can happent when pod created from ContainerSet).
defaultContainer := pod.Spec.Containers[len(pod.Spec.Containers)-1].Name
for _, c := range pod.Spec.Containers {
if c.Name == common.MainContainerName {
defaultContainer = common.MainContainerName
break
}
}
pod.ObjectMeta.Annotations[common.AnnotationKeyDefaultContainer] = defaultContainer

// Add init container only if it needs input artifacts. This is also true for
// script templates (which needs to populate the script)
if len(tmpl.Inputs.Artifacts) > 0 || tmpl.GetType() == wfv1.TemplateTypeScript || woc.getContainerRuntimeExecutor() == common.ContainerRuntimeExecutorEmissary {
Expand Down
38 changes: 38 additions & 0 deletions workflow/controller/workflowpod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,44 @@ func TestPodMetadata(t *testing.T) {
assert.Equal(t, "world", pod.ObjectMeta.Labels["template-level-pod-label"])
}

var wfWithContainerSet = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: hello-world-with-container-set
spec:
entrypoint: whalesay
templates:
- name: whalesay
containerSet:
containers:
- name: a
image: docker/whalesay:latest
command: [cowsay]
args: ["hello world"]
- name: b
image: docker/whalesay:latest
command: [cowsay]
args: ["hello world"]
`

func TestPodDefaultContainer(t *testing.T) {
ctx := context.Background()
wf := wfv1.MustUnmarshalWorkflow(wfWithContainerSet)
// change first container name to main
wf.Spec.Templates[0].ContainerSet.Containers[0].Name = common.MainContainerName
woc := newWoc(*wf)
template := woc.execWf.Spec.Templates[0]
pod, _ := woc.createWorkflowPod(ctx, wf.Name, template.ContainerSet.GetContainers(), &wf.Spec.Templates[0], &createWorkflowPodOpts{})
assert.Equal(t, common.MainContainerName, pod.ObjectMeta.Annotations[common.AnnotationKeyDefaultContainer])

wf = wfv1.MustUnmarshalWorkflow(wfWithContainerSet)
woc = newWoc(*wf)
template = woc.execWf.Spec.Templates[0]
pod, _ = woc.createWorkflowPod(ctx, wf.Name, template.ContainerSet.GetContainers(), &template, &createWorkflowPodOpts{})
assert.Equal(t, "b", pod.ObjectMeta.Annotations[common.AnnotationKeyDefaultContainer])
}

func TestGetDeadline(t *testing.T) {
wf := wfv1.MustUnmarshalWorkflow(helloWorldWf)
ctx := context.Background()
Expand Down

0 comments on commit 52321e2

Please sign in to comment.