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

     1  package v1
     2  
     3  import (
     4  	"strings"
     5  
     6  	corev1 "k8s.io/api/core/v1"
     7  	"k8s.io/utils/pointer"
     8  )
     9  
    10  func getDefaultContainerIndex(spec *corev1.PodSpec, defaultContainerName string) int {
    11  	for i, container := range spec.Containers {
    12  		if container.Name == defaultContainerName {
    13  			return i
    14  		}
    15  	}
    16  	return 0
    17  }
    18  
    19  func hasDefaultPort(spec *corev1.PodSpec, containerIndex int, defaultPortName string) bool {
    20  	for _, port := range spec.Containers[containerIndex].Ports {
    21  		if port.Name == defaultPortName {
    22  			return true
    23  		}
    24  	}
    25  	return false
    26  }
    27  
    28  func setDefaultPort(spec *corev1.PodSpec, defaultPortName string, defaultPort int32, defaultContainerIndex int) {
    29  	spec.Containers[defaultContainerIndex].Ports = append(spec.Containers[defaultContainerIndex].Ports,
    30  		corev1.ContainerPort{
    31  			Name:          defaultPortName,
    32  			ContainerPort: defaultPort,
    33  		})
    34  }
    35  
    36  func setDefaultRestartPolicy(replicaSpec *ReplicaSpec, defaultRestartPolicy RestartPolicy) {
    37  	if replicaSpec != nil && replicaSpec.RestartPolicy == "" {
    38  		replicaSpec.RestartPolicy = defaultRestartPolicy
    39  	}
    40  }
    41  
    42  func setDefaultReplicas(replicaSpec *ReplicaSpec, replicas int32) {
    43  	if replicaSpec != nil && replicaSpec.Replicas == nil {
    44  		replicaSpec.Replicas = pointer.Int32(replicas)
    45  	}
    46  }
    47  
    48  // setTypeNameToCamelCase sets the name of the replica type from any case to correct case.
    49  // E.g. from server to Server; from WORKER to Worker.
    50  func setTypeNameToCamelCase(replicaSpecs map[ReplicaType]*ReplicaSpec, typ ReplicaType) {
    51  	for t := range replicaSpecs {
    52  		if strings.EqualFold(string(t), string(typ)) && t != typ {
    53  			spec := replicaSpecs[t]
    54  			delete(replicaSpecs, t)
    55  			replicaSpecs[typ] = spec
    56  			return
    57  		}
    58  	}
    59  }
    60  
    61  func CleanPodPolicyPointer(cleanPodPolicy CleanPodPolicy) *CleanPodPolicy {
    62  	return &cleanPodPolicy
    63  }