-
Notifications
You must be signed in to change notification settings - Fork 5
/
tools.go
64 lines (58 loc) · 1.4 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
package configmap
import (
"fmt"
"time"
corev1 "k8s.io/api/core/v1"
)
var ERR_TYPE = fmt.Errorf("type must be *corev1.ConfigMap, corev1.ConfigMap or string")
// GetData get configmap data.
func (h *Handler) GetData(object interface{}) (map[string]string, error) {
switch val := object.(type) {
case string:
cm, err := h.Get(val)
if err != nil {
return nil, err
}
return cm.Data, nil
case *corev1.ConfigMap:
return val.Data, nil
case corev1.ConfigMap:
return val.Data, nil
default:
return nil, ERR_TYPE
}
}
// NumData get the number of configmap data.
func (h *Handler) NumData(object interface{}) (int, error) {
switch val := object.(type) {
case string:
cm, err := h.Get(val)
if err != nil {
return 0, err
}
return len(cm.Data), nil
case *corev1.ConfigMap:
return len(val.Data), nil
case corev1.ConfigMap:
return len(val.Data), nil
default:
return 0, ERR_TYPE
}
}
// GetAge returns configmap age.
func (h *Handler) GetAge(object interface{}) (time.Duration, error) {
switch val := object.(type) {
case string:
cm, err := h.Get(val)
if err != nil {
return time.Duration(int64(0)), err
}
return time.Now().Sub(cm.CreationTimestamp.Time), nil
case *corev1.ConfigMap:
return time.Now().Sub(val.CreationTimestamp.Time), nil
case corev1.ConfigMap:
return time.Now().Sub(val.CreationTimestamp.Time), nil
default:
return time.Duration(int64(0)), ERR_TYPE
}
}