/
k8s.go
125 lines (112 loc) · 3.48 KB
/
k8s.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
package k8s
import (
"fmt"
"net/url"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const (
All = "all"
Authority = "authority"
DaemonSet = "daemonset"
Deployment = "deployment"
Namespace = "namespace"
Pod = "pod"
ReplicationController = "replicationcontroller"
ReplicaSet = "replicaset"
Service = "service"
StatefulSet = "statefulset"
)
// resources to query in StatSummary when Resource.Type is "all"
var StatAllResourceTypes = []string{
Deployment,
ReplicationController,
Pod,
Service,
Authority,
}
func generateKubernetesApiBaseUrlFor(schemeHostAndPort string, namespace string, extraPathStartingWithSlash string) (*url.URL, error) {
if string(extraPathStartingWithSlash[0]) != "/" {
return nil, fmt.Errorf("Path must start with a [/], was [%s]", extraPathStartingWithSlash)
}
baseURL, err := generateBaseKubernetesApiUrl(schemeHostAndPort)
if err != nil {
return nil, err
}
urlString := fmt.Sprintf("%snamespaces/%s%s", baseURL.String(), namespace, extraPathStartingWithSlash)
url, err := url.Parse(urlString)
if err != nil {
return nil, fmt.Errorf("error generating namespace URL for Kubernetes API from [%s]", urlString)
}
return url, nil
}
func generateBaseKubernetesApiUrl(schemeHostAndPort string) (*url.URL, error) {
urlString := fmt.Sprintf("%s/api/v1/", schemeHostAndPort)
url, err := url.Parse(urlString)
if err != nil {
return nil, fmt.Errorf("error generating base URL for Kubernetes API from [%s]", urlString)
}
return url, nil
}
func getConfig(fpath string) (*rest.Config, error) {
rules := clientcmd.NewDefaultClientConfigLoadingRules()
if fpath != "" {
rules.ExplicitPath = fpath
}
overrides := &clientcmd.ConfigOverrides{}
return clientcmd.
NewNonInteractiveDeferredLoadingClientConfig(rules, overrides).
ClientConfig()
}
// CanonicalResourceNameFromFriendlyName returns a canonical name from common shorthands used in command line tools.
// This works based on https://github.com/kubernetes/kubernetes/blob/63ffb1995b292be0a1e9ebde6216b83fc79dd988/pkg/kubectl/kubectl.go#L39
// This also works for non-k8s resources, e.g. authorities
func CanonicalResourceNameFromFriendlyName(friendlyName string) (string, error) {
switch friendlyName {
case "deploy", "deployment", "deployments":
return Deployment, nil
case "ds", "daemonset", "daemonsets":
return DaemonSet, nil
case "ns", "namespace", "namespaces":
return Namespace, nil
case "po", "pod", "pods":
return Pod, nil
case "rc", "replicationcontroller", "replicationcontrollers":
return ReplicationController, nil
case "svc", "service", "services":
return Service, nil
case "sts", "statefulset", "statefulsets":
return StatefulSet, nil
case "au", "authority", "authorities":
return Authority, nil
case "all":
return All, nil
}
return "", fmt.Errorf("cannot find Kubernetes canonical name from friendly name [%s]", friendlyName)
}
// Return a the shortest name for a k8s canonical name.
// Essentially the reverse of CanonicalResourceNameFromFriendlyName
func ShortNameFromCanonicalResourceName(canonicalName string) string {
switch canonicalName {
case Deployment:
return "deploy"
case DaemonSet:
return "ds"
case Namespace:
return "ns"
case Pod:
return "po"
case ReplicationController:
return "rc"
case ReplicaSet:
return "rs"
case Service:
return "svc"
case StatefulSet:
return "sts"
case Authority:
return "au"
default:
return ""
}
}