-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutils.go
123 lines (108 loc) · 2.95 KB
/
utils.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
package utils
import (
"strings"
"time"
log "github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
autoscalingv1 "k8s.io/api/autoscaling/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/duration"
"k8s.io/client-go/kubernetes"
"github.com/guessi/kubectl-grep/pkg/client"
"github.com/guessi/kubectl-grep/pkg/options"
)
var (
clientset *kubernetes.Clientset
)
func init() {
clientset = client.InitClient()
}
// setOptions - set common options for clientset
func setOptions(opt *options.SearchOptions) (string, *metav1.ListOptions) {
var namespace string
if len(opt.Namespace) <= 0 {
namespace = "default"
} else {
namespace = opt.Namespace
}
if opt.AllNamespaces {
namespace = ""
}
listOptions := &metav1.ListOptions{
LabelSelector: opt.Selector,
FieldSelector: opt.FieldSelector,
}
return namespace, listOptions
}
// DaemonsetList - return a list of DaemonSet(s)
func DaemonsetList(opt *options.SearchOptions) *appsv1.DaemonSetList {
ns, o := setOptions(opt)
list, err := clientset.AppsV1().DaemonSets(ns).List(*o)
if err != nil {
log.WithFields(log.Fields{
"err": err.Error(),
}).Debug("Unable to get DaemonSet List")
}
return list
}
// DeploymentList - return a list of Deployment(s)
func DeploymentList(opt *options.SearchOptions) *appsv1.DeploymentList {
ns, o := setOptions(opt)
list, err := clientset.AppsV1().Deployments(ns).List(*o)
if err != nil {
log.WithFields(log.Fields{
"err": err.Error(),
}).Debug("Unable to get Deployment List")
}
return list
}
// HpaList - return a list of HPA(s)
func HpaList(opt *options.SearchOptions) *autoscalingv1.HorizontalPodAutoscalerList {
ns, o := setOptions(opt)
list, err := clientset.AutoscalingV1().HorizontalPodAutoscalers(ns).List(*o)
if err != nil {
log.WithFields(log.Fields{
"err": err.Error(),
}).Debug("Unable to get HPA List")
}
return list
}
// PodList - return a list of Pod(s)
func PodList(opt *options.SearchOptions) *corev1.PodList {
ns, o := setOptions(opt)
list, err := clientset.CoreV1().Pods(ns).List(*o)
if err != nil {
log.WithFields(log.Fields{
"err": err.Error(),
}).Debug("Unable to get Pod List")
}
return list
}
// NodeList - return a list of Node(s)
func NodeList(opt *options.SearchOptions) *corev1.NodeList {
_, o := setOptions(opt)
list, err := clientset.CoreV1().Nodes().List(*o)
if err != nil {
log.WithFields(log.Fields{
"err": err.Error(),
}).Debug("Unable to get Node List")
}
return list
}
// TrimQuoteAndSpace - remove Spaces, Tabs, SingleQuotes, DoubleQuites
func TrimQuoteAndSpace(input string) string {
if len(input) >= 2 {
if input[0] == '"' && input[len(input)-1] == '"' {
return input[1 : len(input)-1]
}
if input[0] == '\'' && input[len(input)-1] == '\'' {
return input[1 : len(input)-1]
}
}
return strings.TrimSpace(input)
}
// GetAge - return human readable time expression
func GetAge(d time.Duration) string {
return duration.HumanDuration(d)
}