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

     1  // Copyright 2018 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 testutil
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"strings"
    21  
    22  	. "github.com/onsi/gomega"
    23  	corev1 "k8s.io/api/core/v1"
    24  	"k8s.io/apimachinery/pkg/api/errors"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	kubeflowv1 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
    29  )
    30  
    31  const (
    32  	DummyPortName string = "dummy"
    33  	DummyPort     int32  = 1221
    34  )
    35  
    36  func NewBaseService(name string, job metav1.Object, refs []metav1.OwnerReference) *corev1.Service {
    37  	return &corev1.Service{
    38  		ObjectMeta: metav1.ObjectMeta{
    39  			Name:            name,
    40  			Labels:          map[string]string{},
    41  			Namespace:       job.GetNamespace(),
    42  			OwnerReferences: refs,
    43  		},
    44  		Spec: corev1.ServiceSpec{
    45  			Ports: []corev1.ServicePort{
    46  				{
    47  					Name: DummyPortName,
    48  					Port: DummyPort,
    49  				},
    50  			},
    51  		},
    52  	}
    53  }
    54  
    55  func NewService(job metav1.Object, typ kubeflowv1.ReplicaType, index int, refs []metav1.OwnerReference) *corev1.Service {
    56  	svc := NewBaseService(fmt.Sprintf("%s-%s-%d", job.GetName(), strings.ToLower(string(typ)), index), job, refs)
    57  	svc.Labels[kubeflowv1.ReplicaTypeLabel] = strings.ToLower(string(typ))
    58  	svc.Labels[kubeflowv1.ReplicaIndexLabel] = fmt.Sprintf("%d", index)
    59  	return svc
    60  }
    61  
    62  // NewServiceList creates count pods with the given phase for the given tfJob
    63  func NewServiceList(count int32, job metav1.Object, typ kubeflowv1.ReplicaType, refs []metav1.OwnerReference) []*corev1.Service {
    64  	services := []*corev1.Service{}
    65  	for i := int32(0); i < count; i++ {
    66  		newService := NewService(job, typ, int(i), refs)
    67  		services = append(services, newService)
    68  	}
    69  	return services
    70  }
    71  
    72  func SetServices(client client.Client, job metav1.Object, typ kubeflowv1.ReplicaType, activeWorkerServices int32,
    73  	refs []metav1.OwnerReference, basicLabels map[string]string) {
    74  	ctx := context.Background()
    75  	for _, svc := range NewServiceList(activeWorkerServices, job, typ, refs) {
    76  		for k, v := range basicLabels {
    77  			svc.Labels[k] = v
    78  		}
    79  		err := client.Create(ctx, svc)
    80  		if errors.IsAlreadyExists(err) {
    81  			return
    82  		} else {
    83  			Expect(err).To(BeNil())
    84  		}
    85  	}
    86  }