-
Notifications
You must be signed in to change notification settings - Fork 231
/
etcd.go
138 lines (107 loc) · 3.74 KB
/
etcd.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
/*
Copyright 2019 The KubeOne 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 tasks
import (
"net/url"
clientv3 "go.etcd.io/etcd/client/v3"
"k8c.io/kubeone/pkg/clusterstatus/preflightstatus"
"k8c.io/kubeone/pkg/etcdutil"
"k8c.io/kubeone/pkg/fail"
"k8c.io/kubeone/pkg/state"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/sets"
dynclient "sigs.k8s.io/controller-runtime/pkg/client"
)
func repairClusterIfNeeded(s *state.State) error {
s.Logger.Info("Check if cluster needs any repairs...")
leader, err := s.Cluster.Leader()
if err != nil {
return err
}
etcdcfg, err := etcdutil.NewClientConfig(s, leader)
if err != nil {
return err
}
etcdcli, err := clientv3.New(*etcdcfg)
if err != nil {
return fail.Etcd(err, "initializing new clientv3")
}
defer etcdcli.Close()
ctx := s.Context
etcdRing, err := etcdcli.MemberList(ctx)
if err != nil {
return fail.Etcd(err, "getting members list")
}
knownHostsIdentities := sets.NewString()
knownEtcdMembersIdentities := sets.NewString()
for _, host := range s.Cluster.ControlPlane.Hosts {
knownHostsIdentities.Insert(host.Hostname, host.PublicAddress, host.PrivateAddress)
}
membersToDelete := make(map[string]uint64)
for _, peer := range etcdRing.Members {
knownEtcdMembersIdentities.Insert(peer.Name)
peerIdentities := []string{peer.Name}
for _, endpoint := range peer.ClientURLs {
endpointURL, uerr := url.Parse(endpoint)
if uerr != nil {
s.Logger.Errorf("failed to parse etcd clientURL: %v", uerr)
continue
}
peerIdentities = append(peerIdentities, endpointURL.Hostname())
endpointStatus, serr := etcdcli.Status(ctx, endpointURL.Host)
if serr != nil {
s.Logger.Errorf("failed etcd member %v endpoint status, error: %v", peer, serr)
s.Logger.Warnf("scheduling etcd member %v to delete", peer)
membersToDelete[peer.Name] = peer.ID
}
if endpointStatus != nil && len(endpointStatus.Errors) > 0 {
s.Logger.Errorf("etcd peer experience errors: %v", endpointStatus.Errors)
}
}
if !knownHostsIdentities.HasAny(peerIdentities...) {
s.Logger.Warnf("scheduling etcd member %v to delete", peer)
membersToDelete[peer.Name] = peer.ID
}
}
for memberName, memberID := range membersToDelete {
knownEtcdMembersIdentities.Delete(memberName)
s.Logger.Warnf("removing etcd member %q, for it's not alive", memberName)
if _, err = etcdcli.MemberRemove(ctx, memberID); err != nil {
return fail.Etcd(err, "removing %d member", memberID)
}
}
nodes := corev1.NodeList{}
nodeListOpts := dynclient.ListOptions{
LabelSelector: labels.SelectorFromSet(labels.Set{preflightstatus.LabelControlPlaneNode: ""}),
}
if err = s.DynamicClient.List(ctx, &nodes, &nodeListOpts); err != nil {
return fail.KubeClient(err, "getting %T", nodes)
}
for _, node := range nodes.Items {
var deleteThisNode bool
if _, ok := membersToDelete[node.Name]; ok {
deleteThisNode = true
}
if !knownEtcdMembersIdentities.Has(node.Name) {
deleteThisNode = true
}
if deleteThisNode {
s.Logger.Warnf("Removing kubernets Node object %q, for it's not alive", node.Name)
if err = s.DynamicClient.Delete(ctx, node.DeepCopy()); err != nil {
return fail.KubeClient(err, "deleting %s Node", node.Name)
}
}
}
return nil
}