-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.go
148 lines (135 loc) · 3.58 KB
/
url.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// url.go is WiP for experimental and testing purposes only.
package k8s
import (
"fmt"
"strings"
)
// URL is the selfLink target for a k8s resource
type URL struct {
SelfLink string
}
// SelfLinker implements the selfLink return interface
type SelfLinker interface {
FindSelfLinks(kind, name string) ([]string, error)
}
// URLGetter implements the []URL return interface
type URLGetter interface {
GetAllURLs(s string) ([]URL, error)
}
// GetAllURLs searches for URLs using either a k8s KClient or RawClient
func GetAllURLs(client URLGetter, s string) ([]URL, error) {
urls, err := client.GetAllURLs(s)
return urls, err
}
// FindSelfLinks returns a []string of selfLinks
func FindSelfLinks(client SelfLinker, kind, name string) ([]string, error) {
urls, err := client.FindSelfLinks(kind, name)
return urls, err
}
// ToResource converts a URL into a Resource (ToDo*)
func (url *URL) ToResource() {
}
// FindSelfLinks returns all resources matching the kind and name specified
func (rc *RawClient) FindSelfLinks(kind, name string) ([]string, error) {
var results []string
all, err := rc.RawAll(kind)
if err != nil {
return results, err
}
results = findSelfLinks(all, name)
return results, nil
}
// GetAllURLs retreives all selfLink URLs for the specified kind as indicated by k (ie. "pods")
func (rc *RawClient) GetAllURLs(kind string) ([]URL, error) {
var urls []URL
all, err := rc.RawAll(kind)
if err != nil {
return urls, err
}
links := returnSelfLinks(all)
for _, l := range links {
url := URL{
SelfLink: l,
}
urls = append(urls, url)
}
return urls, nil
}
// GetAllURLs retreives all selfLink URLs for the specified kind as indicated by kind (ie. "pods")
func (kc *KClient) GetAllURLs(kind string) ([]URL, error) {
var urls []URL
switch kind {
case "pods":
resource, err := kc.cs.CoreV1().Pods(kc.ns).List(kc.listOpts)
if err != nil {
return urls, err
}
for _, i := range resource.Items {
url := URL{}
url.SelfLink = i.SelfLink
urls = append(urls, url)
}
case "nodes":
resource, err := kc.cs.CoreV1().Nodes().List(kc.listOpts)
if err != nil {
return urls, err
}
for _, i := range resource.Items {
url := URL{}
url.SelfLink = i.SelfLink
urls = append(urls, url)
}
case "rs":
resource, err := kc.cs.ExtensionsV1beta1().ReplicaSets(kc.ns).List(kc.listOpts)
if err != nil {
return urls, err
}
for _, i := range resource.Items {
url := URL{}
url.SelfLink = i.SelfLink
urls = append(urls, url)
}
case "deploys":
resource, err := kc.cs.ExtensionsV1beta1().Deployments(kc.ns).List(kc.listOpts)
if err != nil {
return urls, err
}
for _, i := range resource.Items {
url := URL{}
url.SelfLink = i.SelfLink
urls = append(urls, url)
}
default:
return urls, fmt.Errorf("No resources found for the given request")
}
return urls, nil
}
// FindSelfLinks returns all resources matching the kind and name specified
func (kc *KClient) FindSelfLinks(kind, name string) ([]string, error) {
var results []string
switch kind {
case "pods":
resource, err := kc.cs.CoreV1().Pods(kc.ns).List(kc.listOpts)
if err != nil {
return results, err
}
for _, i := range resource.Items {
if strings.Contains(i.Name, name) {
results = append(results, i.SelfLink)
}
}
case "nodes":
resource, err := kc.cs.CoreV1().Nodes().List(kc.listOpts)
if err != nil {
return results, err
}
for _, i := range resource.Items {
if strings.Contains(i.Name, name) {
results = append(results, i.SelfLink)
}
}
default:
return results, fmt.Errorf("No resources found for the given request")
}
return results, nil
}