-
Notifications
You must be signed in to change notification settings - Fork 33
/
endpoint.go
67 lines (60 loc) · 1.96 KB
/
endpoint.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
package resources
import (
"github.com/bonnefoa/kubectl-fzf/v3/internal/util"
corev1 "k8s.io/api/core/v1"
)
// Endpoint is the summary of a kubernetes endpoints
type Endpoints struct {
ResourceMeta
ReadyIps []string
ReadyPods []string
NotReadyIps []string
NotReadyPods []string
}
// NewEndpointsFromRuntime builds a k8s resource from informer result
func NewEndpointsFromRuntime(obj interface{}, config CtorConfig) K8sResource {
e := &Endpoints{}
e.FromRuntime(obj, config)
return e
}
// FromRuntime builds object from the informer's result
func (e *Endpoints) FromRuntime(obj interface{}, config CtorConfig) {
endpoints := obj.(*corev1.Endpoints)
e.FromObjectMeta(endpoints.ObjectMeta, config)
for _, subsets := range endpoints.Subsets {
for _, v := range subsets.Addresses {
e.ReadyIps = append(e.ReadyIps, v.IP)
if v.TargetRef != nil && v.TargetRef.Kind == "Pod" {
e.ReadyPods = append(e.ReadyPods, v.TargetRef.Name)
}
}
for _, v := range subsets.NotReadyAddresses {
e.NotReadyIps = append(e.NotReadyIps, v.IP)
if v.TargetRef != nil && v.TargetRef.Kind == "Pod" {
e.NotReadyPods = append(e.NotReadyPods, v.TargetRef.Name)
}
}
}
}
// HasChanged returns true if the resource's dump needs to be updated
func (e *Endpoints) HasChanged(k K8sResource) bool {
oldE := k.(*Endpoints)
return !(util.StringSlicesEqual(e.ReadyIps, oldE.ReadyIps) &&
util.StringSlicesEqual(e.ReadyPods, oldE.ReadyPods) &&
util.StringSlicesEqual(e.NotReadyIps, oldE.NotReadyIps) &&
util.StringSlicesEqual(e.NotReadyIps, oldE.NotReadyIps))
}
// ToString serializes the object to strings
func (e *Endpoints) ToStrings() []string {
line := []string{
e.Namespace,
e.Name,
e.resourceAge(),
util.JoinSlicesWithMaxOrNone(e.ReadyIps, 20, ","),
util.JoinSlicesWithMaxOrNone(e.ReadyPods, 20, ","),
util.JoinSlicesWithMaxOrNone(e.NotReadyIps, 20, ","),
util.JoinSlicesWithMaxOrNone(e.NotReadyPods, 20, ","),
e.labelsString(),
}
return util.DumpLines(line)
}