sigs.k8s.io/kueue@v0.6.2/pkg/util/testingjobs/xgboostjob/wrappers.go (about) 1 /* 2 Copyright 2023 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package testing 18 19 import ( 20 kftraining "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1" 21 corev1 "k8s.io/api/core/v1" 22 "k8s.io/apimachinery/pkg/api/resource" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/apimachinery/pkg/types" 25 "k8s.io/utils/ptr" 26 27 "sigs.k8s.io/kueue/pkg/controller/constants" 28 ) 29 30 // XGBoostJobWrapper wraps a Job. 31 type XGBoostJobWrapper struct{ kftraining.XGBoostJob } 32 33 // MakeXGBoostJob creates a wrapper for a suspended job with a single container and parallelism=1. 34 func MakeXGBoostJob(name, ns string) *XGBoostJobWrapper { 35 return &XGBoostJobWrapper{kftraining.XGBoostJob{ 36 ObjectMeta: metav1.ObjectMeta{ 37 Name: name, 38 Namespace: ns, 39 Annotations: make(map[string]string, 1), 40 }, 41 Spec: kftraining.XGBoostJobSpec{ 42 RunPolicy: kftraining.RunPolicy{ 43 Suspend: ptr.To(true), 44 }, 45 XGBReplicaSpecs: map[kftraining.ReplicaType]*kftraining.ReplicaSpec{ 46 kftraining.XGBoostJobReplicaTypeMaster: { 47 Replicas: ptr.To[int32](1), 48 Template: corev1.PodTemplateSpec{ 49 Spec: corev1.PodSpec{ 50 RestartPolicy: "Never", 51 Containers: []corev1.Container{ 52 { 53 Name: "c", 54 Image: "pause", 55 Command: []string{}, 56 Resources: corev1.ResourceRequirements{Requests: corev1.ResourceList{}}, 57 }, 58 }, 59 NodeSelector: map[string]string{}, 60 }, 61 }, 62 }, 63 kftraining.XGBoostJobReplicaTypeWorker: { 64 Replicas: ptr.To[int32](1), 65 Template: corev1.PodTemplateSpec{ 66 Spec: corev1.PodSpec{ 67 RestartPolicy: "Never", 68 Containers: []corev1.Container{ 69 { 70 Name: "c", 71 Image: "pause", 72 Command: []string{}, 73 Resources: corev1.ResourceRequirements{Requests: corev1.ResourceList{}}, 74 }, 75 }, 76 NodeSelector: map[string]string{}, 77 }, 78 }, 79 }, 80 }, 81 }, 82 }} 83 } 84 85 // PriorityClass updates job priorityclass. 86 func (j *XGBoostJobWrapper) PriorityClass(pc string) *XGBoostJobWrapper { 87 if j.Spec.RunPolicy.SchedulingPolicy == nil { 88 j.Spec.RunPolicy.SchedulingPolicy = &kftraining.SchedulingPolicy{} 89 } 90 j.Spec.RunPolicy.SchedulingPolicy.PriorityClass = pc 91 return j 92 } 93 94 // Obj returns the inner Job. 95 func (j *XGBoostJobWrapper) Obj() *kftraining.XGBoostJob { 96 return &j.XGBoostJob 97 } 98 99 // Queue updates the queue name of the job. 100 func (j *XGBoostJobWrapper) Queue(queue string) *XGBoostJobWrapper { 101 if j.Labels == nil { 102 j.Labels = make(map[string]string) 103 } 104 j.Labels[constants.QueueLabel] = queue 105 return j 106 } 107 108 // Request updates a resource request to the default container. 109 func (j *XGBoostJobWrapper) Request(replicaType kftraining.ReplicaType, r corev1.ResourceName, v string) *XGBoostJobWrapper { 110 j.Spec.XGBReplicaSpecs[replicaType].Template.Spec.Containers[0].Resources.Requests[r] = resource.MustParse(v) 111 return j 112 } 113 114 // Image updates images of the job. 115 func (j *XGBoostJobWrapper) Image(image string) *XGBoostJobWrapper { 116 j.Spec.XGBReplicaSpecs[kftraining.XGBoostJobReplicaTypeMaster].Template.Spec.Containers[0].Image = image 117 j.Spec.XGBReplicaSpecs[kftraining.XGBoostJobReplicaTypeWorker].Template.Spec.Containers[0].Image = image 118 return j 119 } 120 121 // Args updates args of the job. 122 func (j *XGBoostJobWrapper) Args(args []string) *XGBoostJobWrapper { 123 j.Spec.XGBReplicaSpecs[kftraining.XGBoostJobReplicaTypeMaster].Template.Spec.Containers[0].Args = args 124 j.Spec.XGBReplicaSpecs[kftraining.XGBoostJobReplicaTypeWorker].Template.Spec.Containers[0].Args = args 125 return j 126 } 127 128 // Parallelism updates job parallelism. 129 func (j *XGBoostJobWrapper) Parallelism(p int32) *XGBoostJobWrapper { 130 j.Spec.XGBReplicaSpecs[kftraining.XGBoostJobReplicaTypeWorker].Replicas = ptr.To(p) 131 return j 132 } 133 134 // Suspend updates the suspend status of the job. 135 func (j *XGBoostJobWrapper) Suspend(s bool) *XGBoostJobWrapper { 136 j.Spec.RunPolicy.Suspend = &s 137 return j 138 } 139 140 // UID updates the uid of the job. 141 func (j *XGBoostJobWrapper) UID(uid string) *XGBoostJobWrapper { 142 j.ObjectMeta.UID = types.UID(uid) 143 return j 144 } 145 146 // NodeSelector updates the nodeSelector of job. 147 func (j *XGBoostJobWrapper) NodeSelector(k, v string) *XGBoostJobWrapper { 148 if j.Spec.XGBReplicaSpecs[kftraining.XGBoostJobReplicaTypeMaster].Template.Spec.NodeSelector == nil { 149 j.Spec.XGBReplicaSpecs[kftraining.XGBoostJobReplicaTypeMaster].Template.Spec.NodeSelector = make(map[string]string) 150 } 151 if j.Spec.XGBReplicaSpecs[kftraining.XGBoostJobReplicaTypeWorker].Template.Spec.NodeSelector == nil { 152 j.Spec.XGBReplicaSpecs[kftraining.XGBoostJobReplicaTypeWorker].Template.Spec.NodeSelector = make(map[string]string) 153 } 154 j.Spec.XGBReplicaSpecs[kftraining.XGBoostJobReplicaTypeMaster].Template.Spec.NodeSelector[k] = v 155 j.Spec.XGBReplicaSpecs[kftraining.XGBoostJobReplicaTypeWorker].Template.Spec.NodeSelector[k] = v 156 return j 157 } 158 159 // Active updates the replicaStatus for Active of job. 160 func (j *XGBoostJobWrapper) Active(rType kftraining.ReplicaType, c int32) *XGBoostJobWrapper { 161 if j.Status.ReplicaStatuses == nil { 162 j.Status.ReplicaStatuses = make(map[kftraining.ReplicaType]*kftraining.ReplicaStatus) 163 } 164 j.Status.ReplicaStatuses[rType] = &kftraining.ReplicaStatus{ 165 Active: c, 166 } 167 return j 168 }