Skip to content

Commit

Permalink
Move pod template to pod package
Browse files Browse the repository at this point in the history
This commits moves the pod template related files to the
pkg/apis/pipeline/pod/template.go. No functional changes.
  • Loading branch information
JeromeJu authored and tekton-robot committed Aug 16, 2022
1 parent 8381d0d commit 8413f85
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 128 deletions.
97 changes: 97 additions & 0 deletions pkg/apis/pipeline/pod/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,100 @@ func (tpl *Template) ToAffinityAssistantTemplate() *AffinityAssistantTemplate {
ImagePullSecrets: tpl.ImagePullSecrets,
}
}

// PodTemplate holds pod specific configuration
//nolint:revive
type PodTemplate = Template

// MergePodTemplateWithDefault merges 2 PodTemplates together. If the same
// field is set on both templates, the value from tpl will overwrite the value
// from defaultTpl.
func MergePodTemplateWithDefault(tpl, defaultTpl *PodTemplate) *PodTemplate {
switch {
case defaultTpl == nil:
// No configured default, just return the template
return tpl
case tpl == nil:
// No template, just return the default template
return defaultTpl
default:
// Otherwise, merge fields
if tpl.NodeSelector == nil {
tpl.NodeSelector = defaultTpl.NodeSelector
}
if tpl.Tolerations == nil {
tpl.Tolerations = defaultTpl.Tolerations
}
if tpl.Affinity == nil {
tpl.Affinity = defaultTpl.Affinity
}
if tpl.SecurityContext == nil {
tpl.SecurityContext = defaultTpl.SecurityContext
}
if tpl.Volumes == nil {
tpl.Volumes = defaultTpl.Volumes
}
if tpl.RuntimeClassName == nil {
tpl.RuntimeClassName = defaultTpl.RuntimeClassName
}
if tpl.AutomountServiceAccountToken == nil {
tpl.AutomountServiceAccountToken = defaultTpl.AutomountServiceAccountToken
}
if tpl.DNSPolicy == nil {
tpl.DNSPolicy = defaultTpl.DNSPolicy
}
if tpl.DNSConfig == nil {
tpl.DNSConfig = defaultTpl.DNSConfig
}
if tpl.EnableServiceLinks == nil {
tpl.EnableServiceLinks = defaultTpl.EnableServiceLinks
}
if tpl.PriorityClassName == nil {
tpl.PriorityClassName = defaultTpl.PriorityClassName
}
if tpl.SchedulerName == "" {
tpl.SchedulerName = defaultTpl.SchedulerName
}
if tpl.ImagePullSecrets == nil {
tpl.ImagePullSecrets = defaultTpl.ImagePullSecrets
}
if tpl.HostAliases == nil {
tpl.HostAliases = defaultTpl.HostAliases
}
if tpl.HostNetwork == false && defaultTpl.HostNetwork == true {
tpl.HostNetwork = true
}
if tpl.TopologySpreadConstraints == nil {
tpl.TopologySpreadConstraints = defaultTpl.TopologySpreadConstraints
}
return tpl
}
}

// AAPodTemplate holds pod specific configuration for the affinity-assistant
type AAPodTemplate = AffinityAssistantTemplate

// MergeAAPodTemplateWithDefault is the same as MergePodTemplateWithDefault but
// for AffinityAssistantPodTemplates.
func MergeAAPodTemplateWithDefault(tpl, defaultTpl *AAPodTemplate) *AAPodTemplate {
switch {
case defaultTpl == nil:
// No configured default, just return the template
return tpl
case tpl == nil:
// No template, just return the default template
return defaultTpl
default:
// Otherwise, merge fields
if tpl.NodeSelector == nil {
tpl.NodeSelector = defaultTpl.NodeSelector
}
if tpl.Tolerations == nil {
tpl.Tolerations = defaultTpl.Tolerations
}
if tpl.ImagePullSecrets == nil {
tpl.ImagePullSecrets = defaultTpl.ImagePullSecrets
}
return tpl
}
}
3 changes: 2 additions & 1 deletion pkg/apis/pipeline/v1alpha1/run_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

apisconfig "github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
pod "github.com/tektoncd/pipeline/pkg/apis/pipeline/pod"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
runv1alpha1 "github.com/tektoncd/pipeline/pkg/apis/run/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -73,7 +74,7 @@ type RunSpec struct {

// PodTemplate holds pod specific configuration
// +optional
PodTemplate *v1beta1.PodTemplate `json:"podTemplate,omitempty"`
PodTemplate *pod.PodTemplate `json:"podTemplate,omitempty"`

// Time after which the custom-task times out.
// Refer Go's ParseDuration documentation for expected format: https://golang.org/pkg/time/#ParseDuration
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1beta1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/apis/pipeline/v1beta1/pipelinerun_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/tektoncd/pipeline/pkg/apis/config"
pod "github.com/tektoncd/pipeline/pkg/apis/pipeline/pod"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
)
Expand Down Expand Up @@ -49,7 +50,7 @@ func (prs *PipelineRunSpec) SetDefaults(ctx context.Context) {
}

defaultPodTemplate := cfg.Defaults.DefaultPodTemplate
prs.PodTemplate = MergePodTemplateWithDefault(prs.PodTemplate, defaultPodTemplate)
prs.PodTemplate = pod.MergePodTemplateWithDefault(prs.PodTemplate, defaultPodTemplate)

if prs.PipelineSpec != nil {
prs.PipelineSpec.SetDefaults(ctx)
Expand Down
9 changes: 5 additions & 4 deletions pkg/apis/pipeline/v1beta1/pipelinerun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/config"
apisconfig "github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
pod "github.com/tektoncd/pipeline/pkg/apis/pipeline/pod"
runv1alpha1 "github.com/tektoncd/pipeline/pkg/apis/run/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -217,7 +218,7 @@ type PipelineRunSpec struct {
// +optional
Timeout *metav1.Duration `json:"timeout,omitempty"`
// PodTemplate holds pod specific configuration
PodTemplate *PodTemplate `json:"podTemplate,omitempty"`
PodTemplate *pod.PodTemplate `json:"podTemplate,omitempty"`
// Workspaces holds a set of workspace bindings that must match names
// with those declared in the pipeline.
// +optional
Expand Down Expand Up @@ -508,9 +509,9 @@ type PipelineTaskRun struct {
// PipelineTaskRunSpec can be used to configure specific
// specs for a concrete Task
type PipelineTaskRunSpec struct {
PipelineTaskName string `json:"pipelineTaskName,omitempty"`
TaskServiceAccountName string `json:"taskServiceAccountName,omitempty"`
TaskPodTemplate *PodTemplate `json:"taskPodTemplate,omitempty"`
PipelineTaskName string `json:"pipelineTaskName,omitempty"`
TaskServiceAccountName string `json:"taskServiceAccountName,omitempty"`
TaskPodTemplate *pod.PodTemplate `json:"taskPodTemplate,omitempty"`
// +listType=atomic
StepOverrides []TaskRunStepOverride `json:"stepOverrides,omitempty"`
// +listType=atomic
Expand Down
115 changes: 0 additions & 115 deletions pkg/apis/pipeline/v1beta1/pod.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1beta1/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"paths": {},
"definitions": {
"pod.AffinityAssistantTemplate": {
"description": "AAPodTemplate holds pod specific configuration for the affinity-assistant",
"description": "AffinityAssistantTemplate holds pod specific configuration and is a subset of the generic pod Template",
"type": "object",
"properties": {
"imagePullSecrets": {
Expand Down Expand Up @@ -40,7 +40,7 @@
}
},
"pod.Template": {
"description": "PodTemplate holds pod specific configuration",
"description": "Template holds pod specific configuration",
"type": "object",
"properties": {
"affinity": {
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/pipeline/v1beta1/taskrun_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/tektoncd/pipeline/pkg/apis/config"
pod "github.com/tektoncd/pipeline/pkg/apis/pipeline/pod"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
)
Expand Down Expand Up @@ -63,7 +64,7 @@ func (trs *TaskRunSpec) SetDefaults(ctx context.Context) {
}

defaultPodTemplate := cfg.Defaults.DefaultPodTemplate
trs.PodTemplate = MergePodTemplateWithDefault(trs.PodTemplate, defaultPodTemplate)
trs.PodTemplate = pod.MergePodTemplateWithDefault(trs.PodTemplate, defaultPodTemplate)

// If this taskrun has an embedded task, apply the usual task defaults
if trs.TaskSpec != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/pipeline/v1beta1/taskrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/config"
apisconfig "github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
pod "github.com/tektoncd/pipeline/pkg/apis/pipeline/pod"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -61,7 +62,7 @@ type TaskRunSpec struct {
// +optional
Timeout *metav1.Duration `json:"timeout,omitempty"`
// PodTemplate holds pod specific configuration
PodTemplate *PodTemplate `json:"podTemplate,omitempty"`
PodTemplate *pod.PodTemplate `json:"podTemplate,omitempty"`
// Workspaces is a list of WorkspaceBindings from volumes to workspaces.
// +optional
// +listType=atomic
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/pipelinerun/affinity_assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func affinityAssistantStatefulSet(name string, pr *v1beta1.PipelineRun, claimNam
tpl := &pod.AffinityAssistantTemplate{}
// merge pod template from spec and default if any of them are defined
if pr.Spec.PodTemplate != nil || defaultAATpl != nil {
tpl = v1beta1.MergeAAPodTemplateWithDefault(pr.Spec.PodTemplate.ToAffinityAssistantTemplate(), defaultAATpl)
tpl = pod.MergeAAPodTemplateWithDefault(pr.Spec.PodTemplate.ToAffinityAssistantTemplate(), defaultAATpl)
}

containers := []corev1.Container{{
Expand Down

0 comments on commit 8413f85

Please sign in to comment.