Skip to content

Commit 65620ef

Browse files
dtaniwakiDuske
authored andcommitted
Support PodSecurityContext (argoproj#1463)
1 parent a3e841f commit 65620ef

File tree

6 files changed

+80
-3
lines changed

6 files changed

+80
-3
lines changed

api/openapi-spec/swagger.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,10 @@
896896
"description": "Script runs a portion of code against an interpreter",
897897
"$ref": "#/definitions/io.argoproj.workflow.v1alpha1.ScriptTemplate"
898898
},
899+
"securityContext": {
900+
"description": "SecurityContext holds pod-level security attributes and common container settings. Optional: Defaults to empty. See type description for default values of each field.",
901+
"$ref": "#/definitions/io.k8s.api.core.v1.PodSecurityContext"
902+
},
899903
"serviceAccountName": {
900904
"description": "ServiceAccountName to apply to workflow pods",
901905
"type": "string"
@@ -1220,6 +1224,10 @@
12201224
"description": "Set scheduler name for all pods. Will be overridden if container/script template's scheduler name is set. Default scheduler will be used if neither specified.",
12211225
"type": "string"
12221226
},
1227+
"securityContext": {
1228+
"description": "SecurityContext holds pod-level security attributes and common container settings. Optional: Defaults to empty. See type description for default values of each field.",
1229+
"$ref": "#/definitions/io.k8s.api.core.v1.PodSecurityContext"
1230+
},
12231231
"serviceAccountName": {
12241232
"description": "ServiceAccountName is the name of the ServiceAccount to run all pods of the workflow as.",
12251233
"type": "string"

pkg/apis/workflow/v1alpha1/openapi_generated.go

Lines changed: 14 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/workflow/v1alpha1/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ type WorkflowSpec struct {
167167

168168
// HostAliases is an optional list of hosts and IPs that will be injected into the pod spec
169169
HostAliases []apiv1.HostAlias `json:"hostAliases,omitempty"`
170+
171+
// SecurityContext holds pod-level security attributes and common container settings.
172+
// Optional: Defaults to empty. See type description for default values of each field.
173+
// +optional
174+
SecurityContext *apiv1.PodSecurityContext `json:"securityContext,omitempty"`
170175
}
171176

172177
// Template is a reusable and composable unit of execution in a workflow
@@ -261,6 +266,11 @@ type Template struct {
261266

262267
// HostAliases is an optional list of hosts and IPs that will be injected into the pod spec
263268
HostAliases []apiv1.HostAlias `json:"hostAliases,omitempty"`
269+
270+
// SecurityContext holds pod-level security attributes and common container settings.
271+
// Optional: Defaults to empty. See type description for default values of each field.
272+
// +optional
273+
SecurityContext *apiv1.PodSecurityContext `json:"securityContext,omitempty"`
264274
}
265275

266276
// Inputs are the mechanism for passing parameters, artifacts, volumes from one template to another

pkg/apis/workflow/v1alpha1/zz_generated.deepcopy.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workflow/controller/workflowpod.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,12 @@ func addSchedulingConstraints(pod *apiv1.Pod, wfSpec *wfv1.WorkflowSpec, tmpl *w
515515
pod.Spec.HostAliases = append(pod.Spec.HostAliases, wfSpec.HostAliases...)
516516
pod.Spec.HostAliases = append(pod.Spec.HostAliases, tmpl.HostAliases...)
517517

518+
// set pod security context
519+
if tmpl.SecurityContext != nil {
520+
pod.Spec.SecurityContext = tmpl.SecurityContext
521+
} else if wfSpec.SecurityContext != nil {
522+
pod.Spec.SecurityContext = wfSpec.SecurityContext
523+
}
518524
}
519525

520526
// addVolumeReferences adds any volumeMounts that a container/sidecar is referencing, to the pod.spec.volumes

workflow/controller/workflowpod_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package controller
33
import (
44
"encoding/json"
55
"fmt"
6-
"github.com/argoproj/argo/workflow/config"
76
"testing"
87

8+
"github.com/argoproj/argo/workflow/config"
9+
910
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
1011
"github.com/argoproj/argo/workflow/common"
1112
"github.com/ghodss/yaml"
@@ -656,3 +657,33 @@ func TestTmplLevelHostAliases(t *testing.T) {
656657
assert.NotNil(t, pod.Spec.HostAliases)
657658

658659
}
660+
661+
// TestWFLevelSecurityContext verifies the ability to carry forward workflow level SecurityContext to Podspec
662+
func TestWFLevelSecurityContext(t *testing.T) {
663+
woc := newWoc()
664+
runAsUser := int64(1234)
665+
woc.wf.Spec.SecurityContext = &apiv1.PodSecurityContext{
666+
RunAsUser: &runAsUser,
667+
}
668+
woc.executeContainer(woc.wf.Spec.Entrypoint, &woc.wf.Spec.Templates[0], "")
669+
podName := getPodName(woc.wf)
670+
pod, err := woc.controller.kubeclientset.CoreV1().Pods("").Get(podName, metav1.GetOptions{})
671+
assert.Nil(t, err)
672+
assert.NotNil(t, pod.Spec.SecurityContext)
673+
assert.Equal(t, runAsUser, *pod.Spec.SecurityContext.RunAsUser)
674+
}
675+
676+
// TestTmplLevelSecurityContext verifies the ability to carry forward template level SecurityContext to Podspec
677+
func TestTmplLevelSecurityContext(t *testing.T) {
678+
woc := newWoc()
679+
runAsUser := int64(1234)
680+
woc.wf.Spec.Templates[0].SecurityContext = &apiv1.PodSecurityContext{
681+
RunAsUser: &runAsUser,
682+
}
683+
woc.executeContainer(woc.wf.Spec.Entrypoint, &woc.wf.Spec.Templates[0], "")
684+
podName := getPodName(woc.wf)
685+
pod, err := woc.controller.kubeclientset.CoreV1().Pods("").Get(podName, metav1.GetOptions{})
686+
assert.Nil(t, err)
687+
assert.NotNil(t, pod.Spec.SecurityContext)
688+
assert.Equal(t, runAsUser, *pod.Spec.SecurityContext.RunAsUser)
689+
}

0 commit comments

Comments
 (0)