This repository was archived by the owner on May 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathservice.go
69 lines (63 loc) · 1.75 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package nginxingresscontroller
import (
"reflect"
k8sv1alpha1 "github.com/nginxinc/nginx-ingress-operator/pkg/apis/k8s/v1alpha1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
func serviceForNginxIngressController(instance *k8sv1alpha1.NginxIngressController) *corev1.Service {
extraLabels := map[string]string{}
if instance.Spec.Service != nil {
extraLabels = instance.Spec.Service.ExtraLabels
}
return &corev1.Service{
ObjectMeta: v1.ObjectMeta{
Name: instance.Name,
Namespace: instance.Namespace,
Labels: extraLabels,
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "http",
Protocol: "TCP",
Port: 80,
TargetPort: intstr.IntOrString{
Type: 0,
IntVal: 80,
},
},
{
Name: "https",
Protocol: "TCP",
Port: 443,
TargetPort: intstr.IntOrString{
Type: 0,
IntVal: 443,
},
},
},
Selector: map[string]string{"app": instance.Name},
Type: corev1.ServiceType(instance.Spec.ServiceType),
},
}
}
func hasServiceChanged(svc *corev1.Service, instance *k8sv1alpha1.NginxIngressController) bool {
if svc.Spec.Type != corev1.ServiceType(instance.Spec.ServiceType) {
return true
}
if instance.Spec.Service != nil && !reflect.DeepEqual(svc.Labels, instance.Spec.Service.ExtraLabels) {
return true
}
return svc.Labels != nil && instance.Spec.Service == nil
}
func updateService(svc *corev1.Service, instance *k8sv1alpha1.NginxIngressController) *corev1.Service {
svc.Spec.Type = corev1.ServiceType(instance.Spec.ServiceType)
if instance.Spec.Service != nil {
svc.Labels = instance.Spec.Service.ExtraLabels
} else {
svc.Labels = nil
}
return svc
}