-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.go
47 lines (39 loc) · 1.53 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package batch
import (
"github.com/equinor/radix-common/utils/slice"
radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
"github.com/equinor/radix-operator/pkg/apis/utils"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (s *syncer) reconcileService(batchJob *radixv1.RadixBatchJob, rd *radixv1.RadixDeployment, jobComponent *radixv1.RadixDeployJobComponent, existingServices []*corev1.Service) error {
if len(jobComponent.GetPorts()) == 0 {
return nil
}
if isBatchJobStopRequested(batchJob) || isBatchJobDone(s.batch, batchJob.Name) {
return nil
}
if slice.Any(existingServices, func(service *corev1.Service) bool { return isResourceLabeledWithBatchJobName(batchJob.Name, service) }) {
return nil
}
service := s.buildService(batchJob.Name, rd.Spec.AppName, jobComponent.GetPorts())
return s.kubeutil.ApplyService(s.batch.GetNamespace(), service)
}
func (s *syncer) buildService(batchJobName, appName string, componentPorts []radixv1.ComponentPort) *corev1.Service {
serviceName := getKubeServiceName(s.batch.GetName(), batchJobName)
labels := s.batchJobIdentifierLabel(batchJobName, appName)
selector := s.batchJobIdentifierLabel(batchJobName, appName)
service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
Labels: labels,
OwnerReferences: ownerReference(s.batch),
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
Ports: utils.GetServicePorts(componentPorts),
Selector: selector,
},
}
return service
}