blob: a05ef8905737b2f8a7862d95c8e346f2b1e526e9 (
plain) (
blame)
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
|
package k8sclient
import (
// "fmt"
"net/url"
)
// API strings
const (
apiBase = "/api/v1"
apiNamespaces = "/namespaces"
apiServices = "/services"
)
// Defaults
const (
defaultBaseUrl = "http://localhost:8080"
)
type K8sConnector struct {
baseUrl string
}
func (c *K8sConnector) SetBaseUrl(u string) error {
validUrl, error := url.Parse(u)
if error != nil {
return error
}
c.baseUrl = validUrl.String()
return nil
}
func (c *K8sConnector) GetBaseUrl() string {
return c.baseUrl
}
func (c *K8sConnector) GetResourceList() *ResourceList {
resources := new(ResourceList)
error := getJson((c.baseUrl + apiBase), resources)
if error != nil {
return nil
}
return resources
}
func (c *K8sConnector) GetNamespaceList() *NamespaceList {
namespaces := new(NamespaceList)
error := getJson((c.baseUrl + apiBase + apiNamespaces), namespaces)
if error != nil {
return nil
}
return namespaces
}
func (c *K8sConnector) GetServiceList() *ServiceList {
services := new(ServiceList)
error := getJson((c.baseUrl + apiBase + apiServices), services)
if error != nil {
return nil
}
return services
}
func (c *K8sConnector) GetServicesByNamespace() map[string][]ServiceItem {
// GetServicesByNamespace returns a map of namespacename :: [ kubernetesServiceItem ]
items := make(map[string][]ServiceItem)
k8sServiceList := c.GetServiceList()
k8sItemList := k8sServiceList.Items
for _, i := range k8sItemList {
namespace := i.Metadata.Namespace
items[namespace] = append(items[namespace], i)
}
return items
}
func (c *K8sConnector) GetServiceItemInNamespace(namespace string, servicename string) *ServiceItem {
// GetServiceItemInNamespace returns the ServiceItem that matches servicename in the namespace
itemMap := c.GetServicesByNamespace()
// TODO: Handle case where namesapce == nil
for _, x := range itemMap[namespace] {
if x.Metadata.Name == servicename {
return &x
}
}
// No matching item found in namespace
return nil
}
func NewK8sConnector(baseurl string) *K8sConnector {
k := new(K8sConnector)
k.SetBaseUrl(baseurl)
return k
}
|