-
Notifications
You must be signed in to change notification settings - Fork 5
/
tools.go
121 lines (112 loc) · 2.73 KB
/
tools.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
package service
import (
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
type ServicePort struct {
Name string
Protocol string
Port int32
TargetPort intstr.IntOrString
NodePort int32
}
// GetType get the type of the service.
func (h *Handler) GetType(object interface{}) (string, error) {
switch val := object.(type) {
case string:
svc, err := h.Get(val)
if err != nil {
return "", err
}
return string(svc.Spec.Type), nil
case *corev1.Service:
return string(val.Spec.Type), nil
case corev1.Service:
return string(val.Spec.Type), nil
default:
return "", ErrInvalidToolsType
}
}
// GetClusterIP returns the cluster ip of the service.
func (h *Handler) GetClusterIP(object interface{}) (string, error) {
switch val := object.(type) {
case string:
svc, err := h.Get(val)
if err != nil {
return "", err
}
return svc.Spec.ClusterIP, nil
case *corev1.Service:
return val.Spec.ClusterIP, nil
case corev1.Service:
return val.Spec.ClusterIP, nil
default:
return "", ErrInvalidToolsType
}
}
// GetExternalIPs returns the external ip list of the service.
func (h *Handler) GetExternalIPs(object interface{}) ([]string, error) {
switch val := object.(type) {
case string:
svc, err := h.Get(val)
if err != nil {
return nil, err
}
return svc.Spec.ExternalIPs, nil
case *corev1.Service:
return val.Spec.ExternalIPs, nil
case corev1.Service:
return val.Spec.ExternalIPs, nil
default:
return nil, ErrInvalidToolsType
}
}
// GetPorts get the port list of the service.
func (h *Handler) GetPorts(object interface{}) ([]ServicePort, error) {
switch val := object.(type) {
case string:
svc, err := h.Get(val)
if err != nil {
return nil, err
}
return h.getPorts(svc), nil
case *corev1.Service:
return h.getPorts(val), nil
case corev1.Service:
return h.getPorts(&val), nil
default:
return nil, ErrInvalidToolsType
}
}
func (h *Handler) getPorts(svc *corev1.Service) []ServicePort {
var pl []ServicePort
for _, port := range svc.Spec.Ports {
p := ServicePort{
Name: port.Name,
Protocol: string(port.Protocol),
Port: port.Port,
TargetPort: port.TargetPort,
NodePort: port.NodePort,
}
pl = append(pl, p)
}
return pl
}
// GetAge returns the age of the service.
func (h *Handler) GetAge(object interface{}) (time.Duration, error) {
switch val := object.(type) {
case string:
svc, err := h.Get(val)
if err != nil {
return time.Duration(0), err
}
return time.Now().Sub(svc.CreationTimestamp.Time), nil
case *corev1.Service:
return time.Now().Sub(val.CreationTimestamp.Time), nil
case corev1.Service:
return time.Now().Sub(val.CreationTimestamp.Time), nil
default:
return time.Duration(0), ErrInvalidToolsType
}
}