Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add serviceAccount and runAsUser to kaniko build (resolves #3267) #3965

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/content/en/schemas/v2beta3.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