github.com/kubeflow/training-operator@v1.7.0/pkg/apis/kubeflow.org/v1/mxnet_validation.go (about) 1 // Copyright 2021 The Kubeflow Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package v1 16 17 import ( 18 "fmt" 19 20 log "github.com/sirupsen/logrus" 21 apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation" 22 ) 23 24 // ValidateV1MXJob checks that the kubeflowv1.MXJobSpec is valid. 25 func ValidateV1MXJob(mxJob *MXJob) error { 26 if errors := apimachineryvalidation.NameIsDNS1035Label(mxJob.ObjectMeta.Name, false); errors != nil { 27 return fmt.Errorf("MXJob name is invalid: %v", errors) 28 } 29 if err := validateMXReplicaSpecs(mxJob.Spec.MXReplicaSpecs); err != nil { 30 return err 31 } 32 return nil 33 } 34 35 // IsScheduler returns true if the type is Scheduler. 36 func IsScheduler(typ ReplicaType) bool { 37 return typ == MXJobReplicaTypeScheduler 38 } 39 40 func validateMXReplicaSpecs(specs map[ReplicaType]*ReplicaSpec) error { 41 if specs == nil { 42 return fmt.Errorf("MXJobSpec is not valid") 43 } 44 foundScheduler := 0 45 for rType, value := range specs { 46 if value == nil || len(value.Template.Spec.Containers) == 0 { 47 return fmt.Errorf("MXJobSpec is not valid") 48 } 49 if IsScheduler(rType) { 50 foundScheduler++ 51 } 52 // Make sure the image is defined in the container. 53 numNamedMXNet := 0 54 for _, container := range value.Template.Spec.Containers { 55 if container.Image == "" { 56 log.Warn("Image is undefined in the container") 57 return fmt.Errorf("MXJobSpec is not valid") 58 } 59 if container.Name == MXJobDefaultContainerName { 60 numNamedMXNet++ 61 } 62 } 63 // Make sure there has at least one container named "mxnet". 64 if numNamedMXNet == 0 { 65 log.Warnf("There is no container named mxnet in %v", rType) 66 return fmt.Errorf("MXJobSpec is not valid") 67 } 68 } 69 if foundScheduler > 1 { 70 return fmt.Errorf("more than 1 scheduler found") 71 } 72 return nil 73 }