github.com/kubeflow/training-operator@v1.7.0/pkg/apis/kubeflow.org/v1/paddlepaddle_validation.go (about)

     1  // Copyright 2022 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 ValidateV1PaddleJob(paddleJob *PaddleJob) error {
    24  	if errors := apimachineryvalidation.NameIsDNS1035Label(paddleJob.ObjectMeta.Name, false); errors != nil {
    25  		return fmt.Errorf("PaddleJob name is invalid: %v", errors)
    26  	}
    27  	if err := validatePaddleReplicaSpecs(paddleJob.Spec.PaddleReplicaSpecs); err != nil {
    28  		return err
    29  	}
    30  	return nil
    31  }
    32  
    33  func validatePaddleReplicaSpecs(specs map[ReplicaType]*ReplicaSpec) error {
    34  	if specs == nil {
    35  		return fmt.Errorf("PaddleJobSpec is not valid")
    36  	}
    37  	for rType, value := range specs {
    38  		if value == nil || len(value.Template.Spec.Containers) == 0 {
    39  			return fmt.Errorf("PaddleJobSpec is not valid: containers definition expected in %v", rType)
    40  		}
    41  		// Make sure the replica type is valid.
    42  		validReplicaTypes := []ReplicaType{PaddleJobReplicaTypeMaster, PaddleJobReplicaTypeWorker}
    43  
    44  		isValidReplicaType := false
    45  		for _, t := range validReplicaTypes {
    46  			if t == rType {
    47  				isValidReplicaType = true
    48  				break
    49  			}
    50  		}
    51  
    52  		if !isValidReplicaType {
    53  			return fmt.Errorf("PaddleReplicaType is %v but must be one of %v", rType, validReplicaTypes)
    54  		}
    55  
    56  		//Make sure the image is defined in the container
    57  		defaultContainerPresent := false
    58  		for _, container := range value.Template.Spec.Containers {
    59  			if container.Image == "" {
    60  				msg := fmt.Sprintf("PaddleJobSpec is not valid: Image is undefined in the container of %v", rType)
    61  				return fmt.Errorf(msg)
    62  			}
    63  			if container.Name == PaddleJobDefaultContainerName {
    64  				defaultContainerPresent = true
    65  			}
    66  		}
    67  		//Make sure there has at least one container named "paddle"
    68  		if !defaultContainerPresent {
    69  			msg := fmt.Sprintf("PaddleJobSpec is not valid: There is no container named %s in %v", PaddleJobDefaultContainerName, rType)
    70  			return fmt.Errorf(msg)
    71  		}
    72  
    73  	}
    74  
    75  	return nil
    76  }