/
pod_manager.go
executable file
·207 lines (177 loc) · 5.7 KB
/
pod_manager.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
/*
Copyright 2020 The SuperEdge 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 check
import (
"context"
"fmt"
"time"
"github.com/superedge/superedge/pkg/edge-health/common"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
runtimeutil "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/informers"
informcorev1 "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
)
const (
EdgeHealthPodLabelKey = "name"
EdgeHealthPodLabelValue = "edge-health"
PodIPIndexKey = "PodIP"
NodeIPIndexKey = "NodeIP"
NodeNameIndexKey = "NodeName"
)
var PodManager *PodController
func NewPodController(clientset kubernetes.Interface) *PodController {
labelSelector := &metav1.LabelSelector{
MatchLabels: map[string]string{EdgeHealthPodLabelKey: EdgeHealthPodLabelValue},
}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
if err != nil {
panic("edge-health pod label selector error")
}
// Make informers that filter out objects that want a non-default service proxy.
informerFactory := informers.NewSharedInformerFactory(clientset, 10*time.Minute)
//Initialize podIndexer
podInformer := informerFactory.InformerFor(&v1.Pod{}, func(k kubernetes.Interface, duration time.Duration) cache.SharedIndexInformer {
return informcorev1.NewFilteredPodInformer(k, common.Namespace, duration,
cache.Indexers{
PodIPIndexKey: PodIPKeyFunc,
NodeIPIndexKey: NodeIPKeyFunc,
NodeNameIndexKey: NodeNameKeyFunc,
},
func(options *metav1.ListOptions) {
// only watch edge-health pod and it's status Running
options.LabelSelector = selector.String()
options.FieldSelector = fields.OneTermEqualSelector("status.phase", string(v1.PodRunning)).String()
},
)
})
n := &PodController{}
podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: n.handlePodAddUpdate,
UpdateFunc: func(old, cur interface{}) {
n.handlePodAddUpdate(cur)
},
DeleteFunc: n.handlePodDelete,
})
n.podStore = podInformer.GetIndexer()
n.clientset = clientset
n.PodInformer = podInformer
n.PodListerSynced = podInformer.HasSynced
PodManager = n
return n
}
type PodController struct {
podStore cache.Indexer
clientset kubernetes.Interface
PodInformer cache.SharedIndexInformer
PodListerSynced cache.InformerSynced
}
func (n *PodController) handlePodAddUpdate(obj interface{}) {
node, ok := obj.(*v1.Pod)
if !ok {
return
}
klog.V(4).Infof("Add/Update Node %s", node.Name)
}
func (n *PodController) handlePodDelete(obj interface{}) {
node, ok := obj.(*v1.Pod)
if !ok {
return
}
klog.V(4).Infof("Delete Node %s", node.Name)
}
func (n *PodController) Run(ctx context.Context) {
defer runtimeutil.HandleCrash()
go n.PodInformer.Run(ctx.Done())
if ok := cache.WaitForCacheSync(
ctx.Done(),
n.PodListerSynced,
); !ok {
klog.Fatal("failed to wait for caches to sync")
}
<-ctx.Done()
}
func (n *PodController) GetPodIPByNodeIP(nodeIP string) (string, error) {
if n == nil || n.podStore == nil {
return "", fmt.Errorf("podStore is not initialized")
}
pods, err := n.podStore.ByIndex(NodeIPIndexKey, nodeIP)
if err != nil {
return "", err
}
if len(pods) < 1 {
return "", fmt.Errorf("failed to get pods by NodeIP %s", nodeIP)
} else if len(pods) > 1 {
klog.V(4).InfoS("GetPodIPByNodeIP return multi pods", "pod length", len(pods))
return "", fmt.Errorf("multi pods return by NodeIP %s", nodeIP)
}
return pods[0].(*v1.Pod).Status.PodIP, nil
}
func (n *PodController) GetNodeIPByNodeName(nodeName string) (string, error) {
if n == nil || n.podStore == nil {
return "", fmt.Errorf("podStore is not initialized")
}
pods, err := n.podStore.ByIndex(NodeNameIndexKey, nodeName)
if err != nil {
return "", err
}
if len(pods) < 1 {
return "", fmt.Errorf("failed to get pods by nodeName %s", nodeName)
} else if len(pods) > 1 {
klog.V(4).InfoS("GetNodeIPByNodeName return multi pods", "pod length", len(pods))
return "", fmt.Errorf("multi pods return by nodeName %s", nodeName)
}
return pods[0].(*v1.Pod).Status.HostIP, nil
}
func (n *PodController) GetNodeNameByNodeIP(nodeIP string) (string, error) {
if n == nil || n.podStore == nil {
return "", fmt.Errorf("podStore is not initialized")
}
pods, err := n.podStore.ByIndex(NodeIPIndexKey, nodeIP)
if err != nil {
return "", err
}
if len(pods) < 1 {
return "", fmt.Errorf("failed to get pods by nodeIP %s", nodeIP)
} else if len(pods) > 1 {
klog.V(4).InfoS("GetNodeNameByNodeIP return multi pods", "pod length", len(pods))
return "", fmt.Errorf("multi pods return by nodeIP %s", nodeIP)
}
return pods[0].(*v1.Pod).Spec.NodeName, nil
}
func PodIPKeyFunc(obj interface{}) ([]string, error) {
pod, ok := obj.(*v1.Pod)
if ok {
return []string{pod.Status.PodIP}, nil
}
return []string{}, nil
}
// TODO: node internal IP in superedge will same in two node in a cluster
func NodeIPKeyFunc(obj interface{}) ([]string, error) {
pod, ok := obj.(*v1.Pod)
if ok {
return []string{pod.Status.HostIP}, nil
}
return []string{}, nil
}
func NodeNameKeyFunc(obj interface{}) ([]string, error) {
pod, ok := obj.(*v1.Pod)
if ok {
return []string{pod.Spec.NodeName}, nil
}
return []string{}, nil
}