-
Notifications
You must be signed in to change notification settings - Fork 0
/
member.go
176 lines (150 loc) · 3.91 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2016 The etcd-operator Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package etcdutil
import (
"errors"
"fmt"
"net/url"
"regexp"
"strings"
)
type Member struct {
Name string
// Kubernetes namespace this member runs in.
Namespace string
// ID field can be 0, which is unknown ID.
// We know the ID of a member when we get the member information from etcd,
// but not from Kubernetes pod list.
ID uint64
SecurePeer bool
SecureClient bool
// ClusterDomain is the DNS name of the cluster. E.g. .cluster.local.
ClusterDomain string
}
func (m *Member) Addr() string {
return fmt.Sprintf("%s.%s.%s.svc%s", m.Name, clusterNameFromMemberName(m.Name), m.Namespace, m.ClusterDomain)
}
// ClientURL is the client URL for this member
func (m *Member) ClientURL() string {
return fmt.Sprintf("%s://%s:2379", m.clientScheme(), m.Addr())
}
func (m *Member) clientScheme() string {
if m.SecureClient {
return "https"
}
return "http"
}
func (m *Member) peerScheme() string {
if m.SecurePeer {
return "https"
}
return "http"
}
func (m *Member) ListenClientURL() string {
return fmt.Sprintf("%s://0.0.0.0:2379", m.clientScheme())
}
func (m *Member) ListenPeerURL() string {
return fmt.Sprintf("%s://0.0.0.0:2380", m.peerScheme())
}
func (m *Member) PeerURL() string {
return fmt.Sprintf("%s://%s:2380", m.peerScheme(), m.Addr())
}
type MemberSet map[string]*Member
func NewMemberSet(ms ...*Member) MemberSet {
res := MemberSet{}
for _, m := range ms {
res[m.Name] = m
}
return res
}
// the set of all members of s1 that are not members of s2
func (ms MemberSet) Diff(other MemberSet) MemberSet {
diff := MemberSet{}
for n, m := range ms {
if _, ok := other[n]; !ok {
diff[n] = m
}
}
return diff
}
// IsEqual tells whether two member sets are equal by checking
// - they have the same set of members and member equality are judged by Name only.
func (ms MemberSet) IsEqual(other MemberSet) bool {
if ms.Size() != other.Size() {
return false
}
for n := range ms {
if _, ok := other[n]; !ok {
return false
}
}
return true
}
func (ms MemberSet) Size() int {
return len(ms)
}
func (ms MemberSet) String() string {
var mstring []string
for m := range ms {
mstring = append(mstring, m)
}
return strings.Join(mstring, ",")
}
func (ms MemberSet) PickOne() *Member {
for _, m := range ms {
return m
}
panic("empty")
}
func (ms MemberSet) PeerURLPairs() []string {
ps := make([]string, 0)
for _, m := range ms {
ps = append(ps, fmt.Sprintf("%s=%s", m.Name, m.PeerURL()))
}
return ps
}
func (ms MemberSet) Add(m *Member) {
ms[m.Name] = m
}
func (ms MemberSet) Remove(name string) {
delete(ms, name)
}
func (ms MemberSet) ClientURLs() []string {
endpoints := make([]string, 0, len(ms))
for _, m := range ms {
endpoints = append(endpoints, m.ClientURL())
}
return endpoints
}
var validPeerURL = regexp.MustCompile(`^\w+:\/\/[\w\.\-]+(:\d+)?$`)
func MemberNameFromPeerURL(pu string) (string, error) {
// url.Parse has very loose validation. We do our own validation.
if !validPeerURL.MatchString(pu) {
return "", errors.New("invalid PeerURL format")
}
u, err := url.Parse(pu)
if err != nil {
return "", err
}
path := strings.Split(u.Host, ":")[0]
name := strings.Split(path, ".")[0]
return name, err
}
func clusterNameFromMemberName(mn string) string {
i := strings.LastIndex(mn, "-")
if i == -1 {
panic(fmt.Sprintf("unexpected member name: %s", mn))
}
return mn[:i]
}