-
Notifications
You must be signed in to change notification settings - Fork 162
/
member.go
94 lines (80 loc) · 2.41 KB
/
member.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package identity
import (
"context"
"fmt"
"sort"
"github.com/scylladb/scylla-operator/pkg/controllerhelpers"
"github.com/scylladb/scylla-operator/pkg/naming"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
// Member encapsulates the identity for a single member
// of a Scylla Cluster.
type Member struct {
// Name of the Pod
Name string
// Namespace of the Pod
Namespace string
// IP of the Pod
IP string
// ClusterIP of the member's Service
StaticIP string
Rack string
Datacenter string
Cluster string
ServiceLabels map[string]string
PodID string
Overprovisioned bool
}
func NewMemberFromObjects(service *corev1.Service, pod *corev1.Pod) *Member {
return &Member{
Namespace: service.Namespace,
Name: service.Name,
IP: pod.Status.PodIP,
StaticIP: service.Spec.ClusterIP,
Rack: pod.Labels[naming.RackNameLabel],
Datacenter: pod.Labels[naming.DatacenterNameLabel],
Cluster: pod.Labels[naming.ClusterNameLabel],
ServiceLabels: service.Labels,
PodID: string(pod.UID),
Overprovisioned: pod.Status.QOSClass != corev1.PodQOSGuaranteed,
}
}
func (m *Member) GetSeed(ctx context.Context, coreClient v1.CoreV1Interface) (string, error) {
podList, err := coreClient.Pods(m.Namespace).List(ctx, metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(labels.Set{
naming.ClusterNameLabel: m.Cluster,
}).String(),
})
if err != nil {
return "", err
}
if len(podList.Items) == 0 {
return "", fmt.Errorf("internal error: can't find any pod for this cluster, including itself")
}
var otherPods []*corev1.Pod
for i := range podList.Items {
pod := &podList.Items[i]
if pod.Name != m.Name {
otherPods = append(otherPods, pod)
}
}
if len(otherPods) == 0 {
// We are the only one, assuming first bootstrap.
return m.StaticIP, nil
}
sort.Slice(otherPods, func(i, j int) bool {
if controllerhelpers.IsPodReady(otherPods[i]) && !controllerhelpers.IsPodReady(otherPods[j]) {
return true
}
return podList.Items[i].ObjectMeta.CreationTimestamp.Before(&podList.Items[j].ObjectMeta.CreationTimestamp)
})
pod := otherPods[0]
svc, err := coreClient.Services(m.Namespace).Get(ctx, pod.Name, metav1.GetOptions{})
if err != nil {
return "", err
}
return svc.Spec.ClusterIP, nil
}