sigs.k8s.io/kueue@v0.6.2/pkg/util/testingjobs/mpijob/wrappers_mpijob.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 common "github.com/kubeflow/common/pkg/apis/common/v1" 21 kubeflow "github.com/kubeflow/mpi-operator/pkg/apis/kubeflow/v2beta1" 22 corev1 "k8s.io/api/core/v1" 23 "k8s.io/apimachinery/pkg/api/resource" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/types" 26 "k8s.io/utils/ptr" 27 28 "sigs.k8s.io/kueue/pkg/controller/constants" 29 ) 30 31 // MPIJobWrapper wraps a Job. 32 type MPIJobWrapper struct{ kubeflow.MPIJob } 33 34 // MakeMPIJob creates a wrapper for a suspended job with a single container and parallelism=1. 35 func MakeMPIJob(name, ns string) *MPIJobWrapper { 36 return &MPIJobWrapper{kubeflow.MPIJob{ 37 ObjectMeta: metav1.ObjectMeta{ 38 Name: name, 39 Namespace: ns, 40 Annotations: make(map[string]string, 1), 41 }, 42 Spec: kubeflow.MPIJobSpec{ 43 RunPolicy: kubeflow.RunPolicy{ 44 Suspend: ptr.To(true), 45 }, 46 MPIReplicaSpecs: map[kubeflow.MPIReplicaType]*common.ReplicaSpec{ 47 kubeflow.MPIReplicaTypeLauncher: { 48 Replicas: ptr.To[int32](1), 49 Template: corev1.PodTemplateSpec{ 50 Spec: corev1.PodSpec{ 51 RestartPolicy: corev1.RestartPolicyNever, 52 Containers: []corev1.Container{ 53 { 54 Name: "c", 55 Image: "pause", 56 Command: []string{}, 57 Resources: corev1.ResourceRequirements{Requests: corev1.ResourceList{}}, 58 }, 59 }, 60 NodeSelector: map[string]string{}, 61 }, 62 }, 63 }, 64 kubeflow.MPIReplicaTypeWorker: { 65 Replicas: ptr.To[int32](1), 66 Template: corev1.PodTemplateSpec{ 67 Spec: corev1.PodSpec{ 68 RestartPolicy: corev1.RestartPolicyNever, 69 Containers: []corev1.Container{ 70 { 71 Name: "c", 72 Image: "pause", 73 Command: []string{}, 74 Resources: corev1.ResourceRequirements{Requests: corev1.ResourceList{}}, 75 }, 76 }, 77 NodeSelector: map[string]string{}, 78 }, 79 }, 80 }, 81 }, 82 }, 83 }} 84 } 85 86 // PriorityClass updates job priorityclass. 87 func (j *MPIJobWrapper) PriorityClass(pc string) *MPIJobWrapper { 88 if j.Spec.RunPolicy.SchedulingPolicy == nil { 89 j.Spec.RunPolicy.SchedulingPolicy = &kubeflow.SchedulingPolicy{} 90 } 91 j.Spec.RunPolicy.SchedulingPolicy.PriorityClass = pc 92 return j 93 } 94 95 // WorkloadPriorityClass updates job workloadpriorityclass. 96 func (j *MPIJobWrapper) WorkloadPriorityClass(wpc string) *MPIJobWrapper { 97 if j.Labels == nil { 98 j.Labels = make(map[string]string) 99 } 100 j.Labels[constants.WorkloadPriorityClassLabel] = wpc 101 return j 102 } 103 104 // Obj returns the inner Job. 105 func (j *MPIJobWrapper) Obj() *kubeflow.MPIJob { 106 return &j.MPIJob 107 } 108 109 // Queue updates the queue name of the job. 110 func (j *MPIJobWrapper) Queue(queue string) *MPIJobWrapper { 111 if j.Labels == nil { 112 j.Labels = make(map[string]string) 113 } 114 j.Labels[constants.QueueLabel] = queue 115 return j 116 } 117 118 // Request adds a resource request to the default container. 119 func (j *MPIJobWrapper) Request(replicaType kubeflow.MPIReplicaType, r corev1.ResourceName, v string) *MPIJobWrapper { 120 j.Spec.MPIReplicaSpecs[replicaType].Template.Spec.Containers[0].Resources.Requests[r] = resource.MustParse(v) 121 return j 122 } 123 124 // Parallelism updates job parallelism. 125 func (j *MPIJobWrapper) Parallelism(p int32) *MPIJobWrapper { 126 j.Spec.MPIReplicaSpecs[kubeflow.MPIReplicaTypeWorker].Replicas = ptr.To(p) 127 return j 128 } 129 130 // Suspend updates the suspend status of the job. 131 func (j *MPIJobWrapper) Suspend(s bool) *MPIJobWrapper { 132 j.Spec.RunPolicy.Suspend = &s 133 return j 134 } 135 136 // UID updates the uid of the job. 137 func (j *MPIJobWrapper) UID(uid string) *MPIJobWrapper { 138 j.ObjectMeta.UID = types.UID(uid) 139 return j 140 } 141 142 // PodAnnotation sets annotation at the pod template level 143 func (j *MPIJobWrapper) PodAnnotation(replicaType kubeflow.MPIReplicaType, k, v string) *MPIJobWrapper { 144 if j.Spec.MPIReplicaSpecs[replicaType].Template.Annotations == nil { 145 j.Spec.MPIReplicaSpecs[replicaType].Template.Annotations = make(map[string]string) 146 } 147 j.Spec.MPIReplicaSpecs[replicaType].Template.Annotations[k] = v 148 return j 149 } 150 151 // PodLabel sets label at the pod template level 152 func (j *MPIJobWrapper) PodLabel(replicaType kubeflow.MPIReplicaType, k, v string) *MPIJobWrapper { 153 if j.Spec.MPIReplicaSpecs[replicaType].Template.Labels == nil { 154 j.Spec.MPIReplicaSpecs[replicaType].Template.Labels = make(map[string]string) 155 } 156 j.Spec.MPIReplicaSpecs[replicaType].Template.Labels[k] = v 157 return j 158 }