github.com/kubeflow/training-operator@v1.7.0/test_job/test_util/v1/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 v1 16 17 import ( 18 "fmt" 19 "testing" 20 21 v1 "k8s.io/api/core/v1" 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "k8s.io/client-go/tools/cache" 24 25 testjobv1 "github.com/kubeflow/training-operator/test_job/apis/test_job/v1" 26 ) 27 28 func NewBaseService(name string, testJob *testjobv1.TestJob, t *testing.T) *v1.Service { 29 return &v1.Service{ 30 ObjectMeta: metav1.ObjectMeta{ 31 Name: name, 32 Labels: GenLabels(testJob.Name), 33 Namespace: testJob.Namespace, 34 OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(testJob, controllerKind)}, 35 }, 36 } 37 } 38 39 func NewService(testJob *testjobv1.TestJob, typ string, index int, t *testing.T) *v1.Service { 40 service := NewBaseService(fmt.Sprintf("%s-%d", typ, index), testJob, t) 41 service.Labels[testReplicaTypeLabel] = typ 42 service.Labels[testReplicaIndexLabel] = fmt.Sprintf("%d", index) 43 return service 44 } 45 46 // NewServiceList creates count pods with the given phase for the given Job 47 func NewServiceList(count int32, testJob *testjobv1.TestJob, typ string, t *testing.T) []*v1.Service { 48 services := []*v1.Service{} 49 for i := int32(0); i < count; i++ { 50 newService := NewService(testJob, typ, int(i), t) 51 services = append(services, newService) 52 } 53 return services 54 } 55 56 func SetServices(serviceIndexer cache.Indexer, testJob *testjobv1.TestJob, typ string, activeWorkerServices int32, t *testing.T) { 57 for _, service := range NewServiceList(activeWorkerServices, testJob, typ, t) { 58 if err := serviceIndexer.Add(service); err != nil { 59 t.Errorf("unexpected error when adding service %v", err) 60 } 61 } 62 }