-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
196 lines (181 loc) · 6.31 KB
/
node.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
package kubeutils
import (
"fmt"
"strings"
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/klog"
)
const (
nodeConfigMapNamePrefix = "unreachable-nodes-from.mprodr"
configMapKeyLastChecked = "lastChecked"
configMapKeyUnreachableNodes = "unreachableNodesCSV"
configMapKeyCheckedBy = "checkedByIP"
configMapLabelName = "unreachable-nodes"
configMapLabelValue = "true"
configMapValidFor = 60 * time.Second
)
// NetNode provides details of which node can't be contacted
type NetNode struct {
IP string
Node *v1.Node
}
// GetUnreadyNodes returns all the nodes that could be down
func GetUnreadyNodes(c clientset.Interface) (*v1.NodeList, error) {
// First get all the nodes
nodes, err := c.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
// Maybe we should be retrying...?
return nil, fmt.Errorf("can't list nodes: %s", err)
}
unReadyNodes := make([]v1.Node, 0)
for _, n := range nodes.Items {
for _, c := range n.Status.Conditions {
if c.Type == v1.NodeReady {
klog.V(5).Infof("got node type %s for node %s (with status %s)", v1.NodeReady, n.Name, c.Status)
if c.Status != v1.ConditionTrue {
klog.V(5).Infof("NotReady Node found %s", n.Name)
unReadyNodes = append(unReadyNodes, n)
}
continue // no need to inspect other node conditions
}
}
}
return &v1.NodeList{Items: unReadyNodes}, nil
}
// GetNodeInternalIP returns the internal IP address of the node object
// Maybe we should error if there's more than a single IP unless
// opted in (as workloads could be commiting remote data)
func GetNodeInternalIP(node *v1.Node) (string, error) {
host := ""
for _, address := range node.Status.Addresses {
if address.Type == v1.NodeInternalIP {
if address.Address != "" {
host = address.Address
break
}
}
}
if host == "" {
return "", fmt.Errorf("Couldn't get the internal IP of host %s with addresses %v", node.Name, node.Status.Addresses)
}
return host, nil
}
// ReportUnreachableIPs records the ip addresses that can't be contacted
// - Used by the detector thread to report all node(s) that are unreachable (from a given source)
func ReportUnreachableIPs(c clientset.Interface, unreachableNodes []NetNode, reportingNodeIP string, namespace string) error {
/*
Create a unique configmap for the detector with shared label e.g.:
kind: ConfgMap
metadata:
name: unreachable-nodes-from.mprodr.x.x.x.x
labels:
source: mpodr-unreachable-results
data:
lastChecked: datetime
unreachableNodes: name,name,name
checkedBy: ip
*/
var unreachableNodeNames []string
for _, un := range unreachableNodes {
unreachableNodeNames = append(unreachableNodeNames, un.Node.Name)
}
cmName := getConfigMapName(reportingNodeIP)
cm := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: cmName,
Labels: map[string]string{
configMapLabelName: configMapLabelValue,
},
},
Data: map[string]string{
configMapKeyLastChecked: fmt.Sprintf("%s", time.Now().Format(time.RFC3339)),
configMapKeyUnreachableNodes: strings.Join(unreachableNodeNames, ","),
configMapKeyCheckedBy: reportingNodeIP,
},
}
// Discover if object exists and create / update as appropriate:
var create bool
_, err := c.CoreV1().ConfigMaps(namespace).Get(cmName, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
create = true
} else {
return fmt.Errorf("error discovering if configmap %s exists: %s", cmName, err)
}
} else {
create = false
}
if create {
_, err = c.CoreV1().ConfigMaps(namespace).Create(cm)
} else {
_, err = c.CoreV1().ConfigMaps(namespace).Update(cm)
}
return err
}
// GetUnreachableNodes get nodes that are REPORTED as unreachanble by the function above
// - used from the monitor thread to provide a consensus of node Unreachability
func GetUnreachableNodes(c clientset.Interface, namespace string) ([]*v1.Node, error) {
/*
1. Select configmaps by label
2. Verify the reported datetime is within threasholds
3. Get a list of Unreachable nodes that have a quorum of results
*/
var unreachableNodes []*v1.Node
// get ConfigMaps "reporting node Unreachable"
cmOptions := metav1.ListOptions{
LabelSelector: configMapLabelName + "=" + configMapLabelValue,
}
cmList, err := c.CoreV1().ConfigMaps(namespace).List(cmOptions)
if err != nil {
return nil, fmt.Errorf("error getting configmaps: %s", err)
}
klog.V(4).Infof("got %d configmaps with matching labels", len(cmList.Items))
allNodes, err := c.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
// Maybe we should be retrying...?
return nil, fmt.Errorf("can't list nodes: %s", err)
}
unreadyNodes, err := GetUnreadyNodes(c)
if err != nil {
// Maybe we should be retrying...?
return nil, fmt.Errorf("problem getting list of UnReady nodes %s", err)
}
reportingQuorum := len(allNodes.Items) - len(unreadyNodes.Items)
klog.V(4).Infof("got results from %d nodes ", reportingQuorum)
unReachableReportCountsByHost := make(map[string]int)
for _, cm := range cmList.Items {
// check the cm checkTime is valid:
reportTimeStr := cm.Data[configMapKeyLastChecked]
reportTime, err := time.Parse(time.RFC3339, reportTimeStr)
if err != nil {
klog.Errorf("cannot parse datetime value %s in configmap %s error=%s", reportTimeStr, cm.Name, err)
// discount this report
continue
}
klog.V(4).Infof("got a report time %s from a node", reportTime)
if reportTime.Before(time.Now().Add(configMapValidFor)) {
// REAP contender
for _, nodeName := range strings.Split((cm.Data[configMapKeyUnreachableNodes]), ",") {
unReachableReportCountsByHost[nodeName]++
}
}
}
// Work out if all nodes that have reported agree (above the quorum threashold)
for _, node := range unreadyNodes.Items {
if _, ok := unReachableReportCountsByHost[node.Name]; ok {
klog.V(4).Infof("%d nodes have reported %s as unreachable (quorum is %d)", unReachableReportCountsByHost[node.Name], node.Name, reportingQuorum)
if unReachableReportCountsByHost[node.Name] >= reportingQuorum {
unreachableNodes = append(unreachableNodes, &node)
}
}
}
return unreachableNodes, nil
}
func getConfigMapName(sourceIP string) string {
return fmt.Sprintf("%s.%s", nodeConfigMapNamePrefix, sourceIP)
}