Skip to content

Commit

Permalink
Add Custom Scheduler Name to Build and BuildRun objects
Browse files Browse the repository at this point in the history
Signed-off-by: Dylan Orzel <dorzel@redhat.com>
  • Loading branch information
dorzel committed Jan 21, 2025
1 parent 51b28b8 commit 213215f
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 1 deletion.
12 changes: 12 additions & 0 deletions deploy/crds/shipwright.io_buildruns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7439,6 +7439,10 @@ spec:
format: duration
type: string
type: object
schedulerName:
description: SchedulerName specifies the scheduler to be used
to dispatch the Pod
type: string
source:
description: |-
Source refers to the location where the source code is,
Expand Down Expand Up @@ -9753,6 +9757,10 @@ spec:
format: duration
type: string
type: object
schedulerName:
description: SchedulerName specifies the scheduler to be used to dispatch
the Pod
type: string
serviceAccount:
description: |-
ServiceAccount refers to the kubernetes serviceaccount
Expand Down Expand Up @@ -11941,6 +11949,10 @@ spec:
format: duration
type: string
type: object
schedulerName:
description: SchedulerName specifies the scheduler to be used
to dispatch the Pod
type: string
source:
description: |-
Source refers to the location where the source code is,
Expand Down
4 changes: 4 additions & 0 deletions deploy/crds/shipwright.io_builds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2818,6 +2818,10 @@ spec:
format: duration
type: string
type: object
schedulerName:
description: SchedulerName specifies the scheduler to be used to dispatch
the Pod
type: string
source:
description: |-
Source refers to the location where the source code is,
Expand Down
7 changes: 6 additions & 1 deletion pkg/apis/build/v1beta1/build_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ const (
NodeSelectorNotValid BuildReason = "NodeSelectorNotValid"
// TolerationNotValid indicates that the Toleration value is not valid
TolerationNotValid BuildReason = "TolerationNotValid"

// SchedulerNameNotValid indicates that the Scheduler name is not valid
SchedulerNameNotValid BuildReason = "SchedulerNameNotValid"
// AllValidationsSucceeded indicates a Build was successfully validated
AllValidationsSucceeded = "all validations succeeded"
)
Expand Down Expand Up @@ -191,6 +192,10 @@ type BuildSpec struct {
// +patchMergeKey=Key
// +patchStrategy=merge
Tolerations []corev1.Toleration `json:"tolerations,omitempty" patchStrategy:"merge" patchMergeKey:"Key"`

// SchedulerName specifies the scheduler to be used to dispatch the Pod
// +optional
SchedulerName string `json:"schedulerName,omitempty"`
}

// BuildVolume is a volume that will be mounted in build pod during build step
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/build/v1beta1/buildrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ type BuildRunSpec struct {
// +patchMergeKey=Key
// +patchStrategy=merge
Tolerations []corev1.Toleration `json:"tolerations,omitempty" patchStrategy:"merge" patchMergeKey:"Key"`

// SchedulerName specifies the scheduler to be used to dispatch the Pod
// +optional
SchedulerName string `json:"schedulerName,omitempty"`
}

// BuildRunRequestedState defines the buildrun state the user can provide to override whatever is the current state.
Expand Down
1 change: 1 addition & 0 deletions pkg/reconciler/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var validationTypes = [...]string{
validate.Triggers,
validate.NodeSelector,
validate.Tolerations,
validate.SchedulerName,
}

// ReconcileBuild reconciles a Build object
Expand Down
1 change: 1 addition & 0 deletions pkg/reconciler/buildrun/buildrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req
validate.NewEnv(build),
validate.NewNodeSelector(build),
validate.NewTolerations(build),
validate.NewSchedulerName(build),
)

// an internal/technical error during validation happened
Expand Down
1 change: 1 addition & 0 deletions pkg/reconciler/buildrun/resources/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func GetBuildObject(ctx context.Context, client client.Client, buildRun *buildv1
// explicitly setting them here is required for validation to happen.
build.Spec.NodeSelector = buildRun.Spec.NodeSelector
build.Spec.Tolerations = buildRun.Spec.Tolerations
build.Spec.SchedulerName = buildRun.Spec.SchedulerName
return nil
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/reconciler/buildrun/resources/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ func GenerateTaskRun(
taskRunPodTemplate.Tolerations = taskRunTolerations
}

// Set custom scheduler name if specified, giving preference to BuildRun values
if buildRun.Spec.SchedulerName != "" {
taskRunPodTemplate.SchedulerName = buildRun.Spec.SchedulerName
} else {
if build.Spec.SchedulerName != "" {
taskRunPodTemplate.SchedulerName = build.Spec.SchedulerName
}
}

if !(taskRunPodTemplate.Equals(&pod.PodTemplate{})) {
expectedTaskRun.Spec.PodTemplate = taskRunPodTemplate
}
Expand Down
37 changes: 37 additions & 0 deletions pkg/validate/scheduler_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright The Shipwright Contributors
//
// SPDX-License-Identifier: Apache-2.0

package validate

import (
"context"
"strings"

"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/utils/ptr"

build "github.com/shipwright-io/build/pkg/apis/build/v1beta1"
)

// SchedulerNameRef contains all required fields
// to validate a Scheduler name
type SchedulerNameRef struct {
Build *build.Build // build instance for analysis
}

func NewSchedulerName(build *build.Build) *SchedulerNameRef {
return &SchedulerNameRef{build}
}

// ValidatePath implements BuildPath interface and validates
// that SchedulerName values are valid
func (b *SchedulerNameRef) ValidatePath(_ context.Context) error {
if b.Build.Spec.SchedulerName != "" {
if errs := validation.IsQualifiedName(b.Build.Spec.SchedulerName); len(errs) > 0 {
b.Build.Status.Reason = ptr.To(build.SchedulerNameNotValid)
b.Build.Status.Message = ptr.To(strings.Join(errs, ", "))
}
}
return nil
}
4 changes: 4 additions & 0 deletions pkg/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const (
NodeSelector = "nodeselector"
// Tolerations for validating `spec.tolerations` entry
Tolerations = "tolerations"
// SchedulerName for validating `spec.schedulerName` entry
SchedulerName = "schedulername"
)

const (
Expand Down Expand Up @@ -83,6 +85,8 @@ func NewValidation(
return &NodeSelectorRef{Build: build}, nil
case Tolerations:
return &TolerationsRef{Build: build}, nil
case SchedulerName:
return &SchedulerNameRef{Build: build}, nil
default:
return nil, fmt.Errorf("unknown validation type")
}
Expand Down

0 comments on commit 213215f

Please sign in to comment.