github.com/kubeflow/training-operator@v1.7.0/pkg/core/service.go (about)

     1  /*
     2  Copyright 2023 The Kubeflow 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 core
    18  
    19  import (
    20  	"fmt"
    21  
    22  	apiv1 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
    23  	utillabels "github.com/kubeflow/training-operator/pkg/util/labels"
    24  	log "github.com/sirupsen/logrus"
    25  	v1 "k8s.io/api/core/v1"
    26  	"k8s.io/apimachinery/pkg/labels"
    27  )
    28  
    29  // FilterServicesForReplicaType returns service belong to a replicaType.
    30  func FilterServicesForReplicaType(services []*v1.Service, replicaType string) ([]*v1.Service, error) {
    31  	var result []*v1.Service
    32  
    33  	selector := labels.SelectorFromValidatedSet(labels.Set{
    34  		apiv1.ReplicaTypeLabel: replicaType,
    35  	})
    36  
    37  	for _, service := range services {
    38  		set := labels.Set(service.Labels)
    39  		if !selector.Matches(set) {
    40  			continue
    41  		}
    42  		result = append(result, service)
    43  	}
    44  	return result, nil
    45  }
    46  
    47  // GetServiceSlices returns a slice, which element is the slice of service.
    48  // Assume the return object is serviceSlices, then serviceSlices[i] is an
    49  // array of pointers to services corresponding to Services for replica i.
    50  func GetServiceSlices(services []*v1.Service, replicas int, logger *log.Entry) [][]*v1.Service {
    51  	serviceSlices := make([][]*v1.Service, CalculateServiceSliceSize(services, replicas))
    52  	for _, service := range services {
    53  		index, err := utillabels.ReplicaIndex(service.Labels)
    54  		if err != nil {
    55  			logger.Warningf("Error obtaining index for service %s/%s: %v", service.Namespace, service.Name, err)
    56  			continue
    57  		}
    58  		if index < 0 || index >= replicas {
    59  			logger.Warningf("The label index is not expected: %d, service: %s/%s", index, service.Namespace, service.Name)
    60  		}
    61  
    62  		serviceSlices[index] = append(serviceSlices[index], service)
    63  	}
    64  	return serviceSlices
    65  }
    66  
    67  // CalculateServiceSliceSize compare max pod index with desired replicas and return larger size
    68  func CalculateServiceSliceSize(services []*v1.Service, replicas int) int {
    69  	size := 0
    70  	for _, svc := range services {
    71  		index, err := utillabels.ReplicaIndex(svc.Labels)
    72  		if err != nil {
    73  			continue
    74  		}
    75  		size = MaxInt(size, index)
    76  	}
    77  
    78  	// size comes from index, need to +1 to indicate real size
    79  	return MaxInt(size+1, replicas)
    80  }
    81  
    82  // GetPortsFromJob gets the ports of job container. Port could be nil, if distributed communication strategy doesn't need and no other ports that need to be exposed.
    83  func GetPortsFromJob(spec *apiv1.ReplicaSpec, defaultContainerName string) (map[string]int32, error) {
    84  	ports := make(map[string]int32)
    85  
    86  	containers := spec.Template.Spec.Containers
    87  	for _, container := range containers {
    88  		if container.Name == defaultContainerName {
    89  			containerPorts := container.Ports
    90  			if len(containerPorts) == 0 {
    91  				return nil, nil
    92  			}
    93  			for _, port := range containerPorts {
    94  				ports[port.Name] = port.ContainerPort
    95  			}
    96  			return ports, nil
    97  		}
    98  	}
    99  
   100  	return nil, fmt.Errorf("failed to find the port")
   101  }