Skip to content

Commit

Permalink
add serviceAccount and runAsUser to kaniko build (resolves #3267)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSel committed Apr 27, 2020
1 parent 8afae1e commit 46f7c5c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/content/en/schemas/v2beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,16 @@
"description": "define the resource requirements for the kaniko pod.",
"x-intellij-html-description": "define the resource requirements for the kaniko pod."
},
"runAsUser": {
"type": "integer",
"description": "defines the UID to request for running the container. If omitted, no SeurityContext will be specified for the pod and will therefore be inherited from the service account.",
"x-intellij-html-description": "defines the UID to request for running the container. If omitted, no SeurityContext will be specified for the pod and will therefore be inherited from the service account."
},
"serviceAccount": {
"type": "string",
"description": "describes the Kubernetes service account to use for the pod. Defaults to 'default'.",
"x-intellij-html-description": "describes the Kubernetes service account to use for the pod. Defaults to 'default'."
},
"timeout": {
"type": "string",
"description": "amount of time (in seconds) that this build is allowed to run. Defaults to 20 minutes (`20m`).",
Expand All @@ -653,6 +663,8 @@
"namespace",
"timeout",
"dockerConfig",
"serviceAccount",
"runAsUser",
"resources",
"concurrency",
"volumes",
Expand Down
13 changes: 13 additions & 0 deletions pkg/skaffold/build/cluster/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ func (b *Builder) kanikoPodSpec(artifact *latest.KanikoArtifact, tag string) (*v
addSecretVolume(pod, constants.DefaultKanikoDockerConfigSecretName, constants.DefaultKanikoDockerConfigPath, b.ClusterDetails.DockerConfig.SecretName)
}

// Add Service Account
if b.ClusterDetails.ServiceAccountName != "" {
pod.Spec.ServiceAccountName = b.ClusterDetails.ServiceAccountName
}

// Add SecurityContext for runAsUser
if b.ClusterDetails.RunAsUser != nil {
if pod.Spec.SecurityContext == nil {
pod.Spec.SecurityContext = &v1.PodSecurityContext{}
}
pod.Spec.SecurityContext.RunAsUser = b.ClusterDetails.RunAsUser
}

// Add used-defines Volumes
pod.Spec.Volumes = append(pod.Spec.Volumes, b.Volumes...)

Expand Down
7 changes: 7 additions & 0 deletions pkg/skaffold/build/cluster/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,16 @@ func TestKanikoPodSpec(t *testing.T) {
},
}

var runAsUser int64 = 0
builder := &Builder{
ClusterDetails: &latest.ClusterDetails{
Namespace: "ns",
PullSecretName: "secret",
PullSecretMountPath: "/secret",
HTTPProxy: "http://proxy",
HTTPSProxy: "https://proxy",
ServiceAccountName: "aVerySpecialSA",
RunAsUser: &runAsUser,
Resources: &latest.ResourceRequirements{
Requests: &latest.ResourceRequirement{
CPU: "0.1",
Expand Down Expand Up @@ -305,6 +308,10 @@ func TestKanikoPodSpec(t *testing.T) {
},
},
}},
ServiceAccountName: "aVerySpecialSA",
SecurityContext: &v1.PodSecurityContext{
RunAsUser: &runAsUser,
},
RestartPolicy: v1.RestartPolicyNever,
Volumes: []v1.Volume{
{
Expand Down
9 changes: 9 additions & 0 deletions pkg/skaffold/schema/latest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,15 @@ type ClusterDetails struct {
// DockerConfig describes how to mount the local Docker configuration into a pod.
DockerConfig *DockerConfig `yaml:"dockerConfig,omitempty"`

// ServiceAccountName describes the Kubernetes service account to use for the pod.
// Defaults to 'default'.
ServiceAccountName string `yaml:"serviceAccount,omitempty"`

// RunAsUser defines the UID to request for running the container.
// If omitted, no SeurityContext will be specified for the pod and will therefore be inherited
// from the service account.
RunAsUser *int64 `yaml:"runAsUser,omitempty"`

// Resources define the resource requirements for the kaniko pod.
Resources *ResourceRequirements `yaml:"resources,omitempty"`

Expand Down

0 comments on commit 46f7c5c

Please sign in to comment.