github.com/kubeflow/training-operator@v1.7.0/pkg/apis/kubeflow.org/v1/xgboost_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  	apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
    21  )
    22  
    23  func ValidateV1XGBoostJob(xgboostJob *XGBoostJob) error {
    24  	if errors := apimachineryvalidation.NameIsDNS1035Label(xgboostJob.ObjectMeta.Name, false); errors != nil {
    25  		return fmt.Errorf("XGBoostJob name is invalid: %v", errors)
    26  	}
    27  	if err := validateXGBoostReplicaSpecs(xgboostJob.Spec.XGBReplicaSpecs); err != nil {
    28  		return err
    29  	}
    30  	return nil
    31  }
    32  
    33  func validateXGBoostReplicaSpecs(specs map[ReplicaType]*ReplicaSpec) error {
    34  	if specs == nil {
    35  		return fmt.Errorf("XGBoostJobSpec is not valid")
    36  	}
    37  	masterExists := false
    38  	for rType, value := range specs {
    39  		if value == nil || len(value.Template.Spec.Containers) == 0 {
    40  			return fmt.Errorf("XGBoostJobSpec is not valid: containers definition expected in %v", rType)
    41  		}
    42  		// Make sure the replica type is valid.
    43  		validReplicaTypes := []ReplicaType{XGBoostJobReplicaTypeMaster, XGBoostJobReplicaTypeWorker}
    44  
    45  		isValidReplicaType := false
    46  		for _, t := range validReplicaTypes {
    47  			if t == rType {
    48  				isValidReplicaType = true
    49  				break
    50  			}
    51  		}
    52  
    53  		if !isValidReplicaType {
    54  			return fmt.Errorf("XGBoostReplicaType is %v but must be one of %v", rType, validReplicaTypes)
    55  		}
    56  
    57  		//Make sure the image is defined in the container
    58  		defaultContainerPresent := false
    59  		for _, container := range value.Template.Spec.Containers {
    60  			if container.Image == "" {
    61  				msg := fmt.Sprintf("XGBoostReplicaType is not valid: Image is undefined in the container of %v", rType)
    62  				return fmt.Errorf(msg)
    63  			}
    64  			if container.Name == XGBoostJobDefaultContainerName {
    65  				defaultContainerPresent = true
    66  			}
    67  		}
    68  		//Make sure there has at least one container named "xgboost"
    69  		if !defaultContainerPresent {
    70  			msg := fmt.Sprintf("XGBoostReplicaType is not valid: There is no container named %s in %v", XGBoostJobDefaultContainerName, rType)
    71  			return fmt.Errorf(msg)
    72  		}
    73  		if rType == XGBoostJobReplicaTypeMaster {
    74  			masterExists = true
    75  			if value.Replicas != nil && int(*value.Replicas) != 1 {
    76  				return fmt.Errorf("XGBoostReplicaType is not valid: There must be only 1 master replica")
    77  			}
    78  		}
    79  
    80  	}
    81  
    82  	if !masterExists {
    83  		return fmt.Errorf("XGBoostReplicaType is not valid: Master ReplicaSpec must be present")
    84  	}
    85  	return nil
    86  
    87  }