Skip to content

Commit

Permalink
Add TopologySpreadConstraints in PodTemplate
Browse files Browse the repository at this point in the history
Add TopologySpreadConstraints in PodTemplate to enable spread Pods
across cluster among topology domains.
  • Loading branch information
seongpyoHong authored and tekton-robot committed Aug 10, 2022
1 parent bf8be59 commit 2323fba
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/podtemplates.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ Pod templates support fields listed in the table below.
<td><code>hostAliases</code></td>
<td>Adds entries to a Pod's `/etc/hosts` to provide Pod-level overrides of hostnames. For further info see [Kubernetes' docs for this field](https://kubernetes.io/docs/tasks/network/customize-hosts-file-for-pods/).</td>
</tr>
<tr>
<td><code>topologySpreadConstraints</code></td>
<td>Specify how Pods are spread across your cluster among topology domains.</td>
</tr>
</tbody>
</table>

Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/pipeline/pod/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ type Template struct {
// HostNetwork specifies whether the pod may use the node network namespace
// +optional
HostNetwork bool `json:"hostNetwork,omitempty"`

// TopologySpreadConstraints controls how Pods are spread across your cluster among
// failure-domains such as regions, zones, nodes, and other user-defined topology domains.
// +optional
// +listType=atomic
TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`
}

// Equals checks if this Template is identical to the given Template.
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/pipeline/pod/zz_generated.deepcopy.go

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

21 changes: 20 additions & 1 deletion pkg/apis/pipeline/v1/openapi_generated.go

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

9 changes: 9 additions & 0 deletions pkg/apis/pipeline/v1/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@
},
"x-kubernetes-list-type": "atomic"
},
"topologySpreadConstraints": {
"description": "TopologySpreadConstraints controls how Pods are spread across your cluster among failure-domains such as regions, zones, nodes, and other user-defined topology domains.",
"type": "array",
"items": {
"default": {},
"$ref": "#/definitions/v1.TopologySpreadConstraint"
},
"x-kubernetes-list-type": "atomic"
},
"volumes": {
"description": "List of volumes that can be mounted by containers belonging to the pod. More info: https://kubernetes.io/docs/concepts/storage/volumes",
"type": "array",
Expand Down
21 changes: 20 additions & 1 deletion 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: 3 additions & 0 deletions pkg/apis/pipeline/v1beta1/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func MergePodTemplateWithDefault(tpl, defaultTpl *PodTemplate) *PodTemplate {
if tpl.HostNetwork == false && defaultTpl.HostNetwork == true {
tpl.HostNetwork = true
}
if tpl.TopologySpreadConstraints == nil {
tpl.TopologySpreadConstraints = defaultTpl.TopologySpreadConstraints
}
return tpl
}
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/apis/pipeline/v1beta1/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@
},
"x-kubernetes-list-type": "atomic"
},
"topologySpreadConstraints": {
"description": "TopologySpreadConstraints controls how Pods are spread across your cluster among failure-domains such as regions, zones, nodes, and other user-defined topology domains.",
"type": "array",
"items": {
"default": {},
"$ref": "#/definitions/v1.TopologySpreadConstraint"
},
"x-kubernetes-list-type": "atomic"
},
"volumes": {
"description": "List of volumes that can be mounted by containers belonging to the pod. More info: https://kubernetes.io/docs/concepts/storage/volumes",
"type": "array",
Expand Down
1 change: 1 addition & 0 deletions pkg/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ func (b *Builder) Build(ctx context.Context, taskRun *v1beta1.TaskRun, taskSpec
PriorityClassName: priorityClassName,
ImagePullSecrets: podTemplate.ImagePullSecrets,
HostAliases: podTemplate.HostAliases,
TopologySpreadConstraints: podTemplate.TopologySpreadConstraints,
ActiveDeadlineSeconds: &activeDeadlineSeconds, // Set ActiveDeadlineSeconds to mark the pod as "terminating" (like a Job)
},
}
Expand Down
72 changes: 72 additions & 0 deletions pkg/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,78 @@ _EOF_
}, runVolume(0)),
ActiveDeadlineSeconds: &defaultActiveDeadlineSeconds,
},
}, {
desc: "using TopologySpreadConstraints",
ts: v1beta1.TaskSpec{
Steps: []v1beta1.Step{
{
Name: "use-topologySpreadConstraints",
Image: "image",
Command: []string{"cmd"}, // avoid entrypoint lookup.
},
},
},
trs: v1beta1.TaskRunSpec{
PodTemplate: &pod.Template{
TopologySpreadConstraints: []corev1.TopologySpreadConstraint{
{
MaxSkew: 1,
TopologyKey: "zone",
WhenUnsatisfiable: corev1.DoNotSchedule,
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"foo": "bar",
},
},
},
},
},
},
want: &corev1.PodSpec{
RestartPolicy: corev1.RestartPolicyNever,
InitContainers: []corev1.Container{entrypointInitContainer(images.EntrypointImage, []v1beta1.Step{{Name: "use-topologySpreadConstraints"}})},
TopologySpreadConstraints: []corev1.TopologySpreadConstraint{
{
MaxSkew: 1,
TopologyKey: "zone",
WhenUnsatisfiable: corev1.DoNotSchedule,
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"foo": "bar",
},
},
},
},
Volumes: append(implicitVolumes, binVolume, runVolume(0), downwardVolume, corev1.Volume{
Name: "tekton-creds-init-home-0",
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{Medium: corev1.StorageMediumMemory}},
}),
Containers: []corev1.Container{{
Name: "step-use-topologySpreadConstraints",
Image: "image",
Command: []string{"/tekton/bin/entrypoint"},
Args: []string{
"-wait_file",
"/tekton/downward/ready",
"-wait_file_content",
"-post_file",
"/tekton/run/0/out",
"-termination_path",
"/tekton/termination",
"-step_metadata_dir",
"/tekton/run/0/status",
"-entrypoint",
"cmd",
"--",
},
VolumeMounts: append([]corev1.VolumeMount{binROMount, runMount(0, false), downwardMount, {
Name: "tekton-creds-init-home-0",
MountPath: "/tekton/creds",
}}, implicitVolumeMounts...),
TerminationMessagePath: "/tekton/termination",
}},
ActiveDeadlineSeconds: &defaultActiveDeadlineSeconds,
},
}} {
t.Run(c.desc, func(t *testing.T) {
names.TestingSeed()
Expand Down

0 comments on commit 2323fba

Please sign in to comment.