Skip to content

Commit c7c9fb0

Browse files
committed
Add Custom Scheduler Name to Build and BuildRun objects
Signed-off-by: Dylan Orzel <dorzel@redhat.com>
1 parent 77457e9 commit c7c9fb0

File tree

10 files changed

+79
-1
lines changed

10 files changed

+79
-1
lines changed

deploy/crds/shipwright.io_buildruns.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -7439,6 +7439,10 @@ spec:
74397439
format: duration
74407440
type: string
74417441
type: object
7442+
schedulerName:
7443+
description: SchedulerName specifies the scheduler to be used
7444+
to dispatch the Pod
7445+
type: string
74427446
source:
74437447
description: |-
74447448
Source refers to the location where the source code is,
@@ -9753,6 +9757,10 @@ spec:
97539757
format: duration
97549758
type: string
97559759
type: object
9760+
schedulerName:
9761+
description: SchedulerName specifies the scheduler to be used to dispatch
9762+
the Pod
9763+
type: string
97569764
serviceAccount:
97579765
description: |-
97589766
ServiceAccount refers to the kubernetes serviceaccount
@@ -11941,6 +11949,10 @@ spec:
1194111949
format: duration
1194211950
type: string
1194311951
type: object
11952+
schedulerName:
11953+
description: SchedulerName specifies the scheduler to be used
11954+
to dispatch the Pod
11955+
type: string
1194411956
source:
1194511957
description: |-
1194611958
Source refers to the location where the source code is,

deploy/crds/shipwright.io_builds.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -2818,6 +2818,10 @@ spec:
28182818
format: duration
28192819
type: string
28202820
type: object
2821+
schedulerName:
2822+
description: SchedulerName specifies the scheduler to be used to dispatch
2823+
the Pod
2824+
type: string
28212825
source:
28222826
description: |-
28232827
Source refers to the location where the source code is,

pkg/apis/build/v1beta1/build_types.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ const (
8080
NodeSelectorNotValid BuildReason = "NodeSelectorNotValid"
8181
// TolerationNotValid indicates that the Toleration value is not valid
8282
TolerationNotValid BuildReason = "TolerationNotValid"
83-
83+
// SchedulerNameNotValid indicates that the Scheduler name is not valid
84+
SchedulerNameNotValid BuildReason = "SchedulerNameNotValid"
8485
// AllValidationsSucceeded indicates a Build was successfully validated
8586
AllValidationsSucceeded = "all validations succeeded"
8687
)
@@ -191,6 +192,10 @@ type BuildSpec struct {
191192
// +patchMergeKey=Key
192193
// +patchStrategy=merge
193194
Tolerations []corev1.Toleration `json:"tolerations,omitempty" patchStrategy:"merge" patchMergeKey:"Key"`
195+
196+
// SchedulerName specifies the scheduler to be used to dispatch the Pod
197+
// +optional
198+
SchedulerName string `json:"schedulerName,omitempty"`
194199
}
195200

196201
// BuildVolume is a volume that will be mounted in build pod during build step

pkg/apis/build/v1beta1/buildrun_types.go

+4
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ type BuildRunSpec struct {
121121
// +patchMergeKey=Key
122122
// +patchStrategy=merge
123123
Tolerations []corev1.Toleration `json:"tolerations,omitempty" patchStrategy:"merge" patchMergeKey:"Key"`
124+
125+
// SchedulerName specifies the scheduler to be used to dispatch the Pod
126+
// +optional
127+
SchedulerName string `json:"schedulerName,omitempty"`
124128
}
125129

126130
// BuildRunRequestedState defines the buildrun state the user can provide to override whatever is the current state.

pkg/reconciler/build/build.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var validationTypes = [...]string{
3535
validate.Triggers,
3636
validate.NodeSelector,
3737
validate.Tolerations,
38+
validate.SchedulerName,
3839
}
3940

4041
// ReconcileBuild reconciles a Build object

pkg/reconciler/buildrun/buildrun.go

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req
162162
validate.NewEnv(build),
163163
validate.NewNodeSelector(build),
164164
validate.NewTolerations(build),
165+
validate.NewSchedulerName(build),
165166
)
166167

167168
// an internal/technical error during validation happened

pkg/reconciler/buildrun/resources/build.go

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func GetBuildObject(ctx context.Context, client client.Client, buildRun *buildv1
4444
// explicitly setting them here is required for validation to happen.
4545
build.Spec.NodeSelector = buildRun.Spec.NodeSelector
4646
build.Spec.Tolerations = buildRun.Spec.Tolerations
47+
build.Spec.SchedulerName = buildRun.Spec.SchedulerName
4748
return nil
4849
}
4950

pkg/reconciler/buildrun/resources/taskrun.go

+9
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,15 @@ func GenerateTaskRun(
255255
taskRunPodTemplate.Tolerations = taskRunTolerations
256256
}
257257

258+
// Set custom scheduler name if specified, giving preference to BuildRun values
259+
if buildRun.Spec.SchedulerName != "" {
260+
taskRunPodTemplate.SchedulerName = buildRun.Spec.SchedulerName
261+
} else {
262+
if build.Spec.SchedulerName != "" {
263+
taskRunPodTemplate.SchedulerName = build.Spec.SchedulerName
264+
}
265+
}
266+
258267
if !(taskRunPodTemplate.Equals(&pod.PodTemplate{})) {
259268
expectedTaskRun.Spec.PodTemplate = taskRunPodTemplate
260269
}

pkg/validate/scheduler_name.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright The Shipwright Contributors
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package validate
6+
7+
import (
8+
"context"
9+
"strings"
10+
11+
"k8s.io/apimachinery/pkg/util/validation"
12+
"k8s.io/utils/ptr"
13+
14+
build "github.com/shipwright-io/build/pkg/apis/build/v1beta1"
15+
)
16+
17+
// SchedulerNameRef contains all required fields
18+
// to validate a Scheduler name
19+
type SchedulerNameRef struct {
20+
Build *build.Build // build instance for analysis
21+
}
22+
23+
func NewSchedulerName(build *build.Build) *SchedulerNameRef {
24+
return &SchedulerNameRef{build}
25+
}
26+
27+
// ValidatePath implements BuildPath interface and validates
28+
// that SchedulerName values are valid
29+
func (b *SchedulerNameRef) ValidatePath(_ context.Context) error {
30+
if b.Build.Spec.SchedulerName != "" {
31+
if errs := validation.IsQualifiedName(b.Build.Spec.SchedulerName); len(errs) > 0 {
32+
b.Build.Status.Reason = ptr.To(build.SchedulerNameNotValid)
33+
b.Build.Status.Message = ptr.To(strings.Join(errs, ", "))
34+
}
35+
}
36+
return nil
37+
}

pkg/validate/validate.go

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const (
3939
NodeSelector = "nodeselector"
4040
// Tolerations for validating `spec.tolerations` entry
4141
Tolerations = "tolerations"
42+
// SchedulerName for validating `spec.schedulerName` entry
43+
SchedulerName = "schedulername"
4244
)
4345

4446
const (
@@ -83,6 +85,8 @@ func NewValidation(
8385
return &NodeSelectorRef{Build: build}, nil
8486
case Tolerations:
8587
return &TolerationsRef{Build: build}, nil
88+
case SchedulerName:
89+
return &SchedulerNameRef{Build: build}, nil
8690
default:
8791
return nil, fmt.Errorf("unknown validation type")
8892
}

0 commit comments

Comments
 (0)