This repository has been archived by the owner on Mar 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 741
/
reconcile.go
221 lines (188 loc) · 6.59 KB
/
reconcile.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// 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 cluster
import (
"context"
"errors"
"fmt"
api "github.com/coreos/etcd-operator/pkg/apis/etcd/v1beta2"
"github.com/coreos/etcd-operator/pkg/util/constants"
"github.com/coreos/etcd-operator/pkg/util/etcdutil"
"github.com/coreos/etcd-operator/pkg/util/k8sutil"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
"k8s.io/api/core/v1"
)
// ErrLostQuorum indicates that the etcd cluster lost its quorum.
var ErrLostQuorum = errors.New("lost quorum")
// reconcile reconciles cluster current state to desired state specified by spec.
// - it tries to reconcile the cluster to desired size.
// - if the cluster needs for upgrade, it tries to upgrade old member one by one.
func (c *Cluster) reconcile(pods []*v1.Pod) error {
c.logger.Infoln("Start reconciling")
defer c.logger.Infoln("Finish reconciling")
defer func() {
c.status.Size = c.members.Size()
}()
sp := c.cluster.Spec
running := podsToMemberSet(pods, c.isSecureClient())
if !running.IsEqual(c.members) || c.members.Size() != sp.Size {
return c.reconcileMembers(running)
}
c.status.ClearCondition(api.ClusterConditionScaling)
if needUpgrade(pods, sp) {
c.status.UpgradeVersionTo(sp.Version)
m := pickOneOldMember(pods, sp.Version)
return c.upgradeOneMember(m.Name)
}
c.status.ClearCondition(api.ClusterConditionUpgrading)
c.status.SetVersion(sp.Version)
c.status.SetReadyCondition()
return nil
}
// reconcileMembers reconciles
// - running pods on k8s and cluster membership
// - cluster membership and expected size of etcd cluster
// Steps:
// 1. Remove all pods from running set that does not belong to member set.
// 2. L consist of remaining pods of runnings
// 3. If L = members, the current state matches the membership state. END.
// 4. If len(L) < len(members)/2 + 1, return quorum lost error.
// 5. Add one missing member. END.
func (c *Cluster) reconcileMembers(running etcdutil.MemberSet) error {
c.logger.Infof("running members: %s", running)
c.logger.Infof("cluster membership: %s", c.members)
unknownMembers := running.Diff(c.members)
if unknownMembers.Size() > 0 {
c.logger.Infof("removing unexpected pods: %v", unknownMembers)
for _, m := range unknownMembers {
if err := c.removePod(m.Name); err != nil {
return err
}
}
}
L := running.Diff(unknownMembers)
if L.Size() == c.members.Size() {
return c.resize()
}
if L.Size() < c.members.Size()/2+1 {
return ErrLostQuorum
}
c.logger.Infof("removing one dead member")
// remove dead members that doesn't have any running pods before doing resizing.
return c.removeDeadMember(c.members.Diff(L).PickOne())
}
func (c *Cluster) resize() error {
if c.members.Size() == c.cluster.Spec.Size {
return nil
}
if c.members.Size() < c.cluster.Spec.Size {
return c.addOneMember()
}
return c.removeOneMember()
}
func (c *Cluster) addOneMember() error {
c.status.SetScalingUpCondition(c.members.Size(), c.cluster.Spec.Size)
cfg := clientv3.Config{
Endpoints: c.members.ClientURLs(),
DialTimeout: constants.DefaultDialTimeout,
TLS: c.tlsConfig,
}
etcdcli, err := clientv3.New(cfg)
if err != nil {
return fmt.Errorf("add one member failed: creating etcd client failed %v", err)
}
defer etcdcli.Close()
newMember := c.newMember()
ctx, cancel := context.WithTimeout(context.Background(), constants.DefaultRequestTimeout)
resp, err := etcdcli.MemberAdd(ctx, []string{newMember.PeerURL()})
cancel()
if err != nil {
return fmt.Errorf("fail to add new member (%s): %v", newMember.Name, err)
}
newMember.ID = resp.Member.ID
c.members.Add(newMember)
if err := c.createPod(c.members, newMember, "existing"); err != nil {
return fmt.Errorf("fail to create member's pod (%s): %v", newMember.Name, err)
}
c.logger.Infof("added member (%s)", newMember.Name)
_, err = c.eventsCli.Create(k8sutil.NewMemberAddEvent(newMember.Name, c.cluster))
if err != nil {
c.logger.Errorf("failed to create new member add event: %v", err)
}
return nil
}
func (c *Cluster) removeOneMember() error {
c.status.SetScalingDownCondition(c.members.Size(), c.cluster.Spec.Size)
return c.removeMember(c.members.PickOne())
}
func (c *Cluster) removeDeadMember(toRemove *etcdutil.Member) error {
c.logger.Infof("removing dead member %q", toRemove.Name)
_, err := c.eventsCli.Create(k8sutil.ReplacingDeadMemberEvent(toRemove.Name, c.cluster))
if err != nil {
c.logger.Errorf("failed to create replacing dead member event: %v", err)
}
return c.removeMember(toRemove)
}
func (c *Cluster) removeMember(toRemove *etcdutil.Member) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("remove member (%s) failed: %v", toRemove.Name, err)
}
}()
err = etcdutil.RemoveMember(c.members.ClientURLs(), c.tlsConfig, toRemove.ID)
if err != nil {
switch err {
case rpctypes.ErrMemberNotFound:
c.logger.Infof("etcd member (%v) has been removed", toRemove.Name)
default:
return err
}
}
c.members.Remove(toRemove.Name)
_, err = c.eventsCli.Create(k8sutil.MemberRemoveEvent(toRemove.Name, c.cluster))
if err != nil {
c.logger.Errorf("failed to create remove member event: %v", err)
}
if err := c.removePod(toRemove.Name); err != nil {
return err
}
if c.isPodPVEnabled() {
err = c.removePVC(k8sutil.PVCNameFromMember(toRemove.Name))
if err != nil {
return err
}
}
c.logger.Infof("removed member (%v) with ID (%d)", toRemove.Name, toRemove.ID)
return nil
}
func (c *Cluster) removePVC(pvcName string) error {
err := c.config.KubeCli.Core().PersistentVolumeClaims(c.cluster.Namespace).Delete(pvcName, nil)
if err != nil && !k8sutil.IsKubernetesResourceNotFoundError(err) {
return fmt.Errorf("remove pvc (%s) failed: %v", pvcName, err)
}
return nil
}
func needUpgrade(pods []*v1.Pod, cs api.ClusterSpec) bool {
return len(pods) == cs.Size && pickOneOldMember(pods, cs.Version) != nil
}
func pickOneOldMember(pods []*v1.Pod, newVersion string) *etcdutil.Member {
for _, pod := range pods {
if k8sutil.GetEtcdVersion(pod) == newVersion {
continue
}
return &etcdutil.Member{Name: pod.Name, Namespace: pod.Namespace}
}
return nil
}