/
services.go
68 lines (61 loc) · 1.97 KB
/
services.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
package sentinel
import (
"fmt"
saasv1alpha1 "github.com/3scale-ops/saas-operator/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
const (
statefulsetPodSelectorLabelKey string = "statefulset.kubernetes.io/pod-name"
)
func (gen *Generator) statefulSetService() *corev1.Service {
return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: gen.GetComponent() + "-headless",
Namespace: gen.GetNamespace(),
Labels: gen.GetLabels(),
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
ClusterIP: corev1.ClusterIPNone,
Ports: []corev1.ServicePort{},
Selector: gen.GetSelector(),
},
}
}
func (gen *Generator) podServices(index int) *corev1.Service {
return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: gen.PodServiceName(index),
Namespace: gen.GetNamespace(),
Labels: gen.GetLabels(),
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
Ports: []corev1.ServicePort{{
Name: gen.GetComponent(),
Protocol: corev1.ProtocolTCP,
Port: int32(saasv1alpha1.SentinelPort),
TargetPort: intstr.FromString(gen.GetComponent()),
}},
Selector: map[string]string{
statefulsetPodSelectorLabelKey: fmt.Sprintf("%s-%d", gen.GetComponent(), index),
},
},
}
}
// PodServiceName generates the name of the pod specific Service
func (gen *Generator) PodServiceName(index int) string {
return fmt.Sprintf("%s-%d", gen.GetComponent(), index)
}
// SentinelEndpoints returns the list of redis URLs of all the sentinels
// These URLs point to the Pod specific Service of each sentinel Pod
func (gen *Generator) SentinelURIs() []string {
urls := make([]string, 0, *gen.Spec.Replicas)
for idx := 0; idx < int(*gen.Spec.Replicas); idx++ {
urls = append(urls,
fmt.Sprintf("redis://%s.%s.svc.cluster.local:%d", gen.PodServiceName(idx), gen.GetNamespace(), saasv1alpha1.SentinelPort))
}
return urls
}