/
utils.go
66 lines (56 loc) · 1.52 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
package controllers
import (
"fmt"
"strings"
instance "github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"k8s.io/api/core/v1"
)
func (c *NodeController) getInstanceFromNodeName(nodeName string) (*instance.Server, error) {
instanceAPI := instance.NewAPI(c.scwClient)
instanceResp, err := instanceAPI.ListServers(&instance.ListServersRequest{
Name: scw.StringPtr(nodeName),
})
if err != nil {
return nil, err
}
if len(instanceResp.Servers) != 1 {
return nil, fmt.Errorf("got %d servers instead of 1", len(instanceResp.Servers))
}
return instanceResp.Servers[0], nil
}
func (c *NodeController) getFreeIP() (*instance.IP, error) {
instanceAPI := instance.NewAPI(c.scwClient)
ipsList, err := instanceAPI.ListIPs(&instance.ListIPsRequest{}, scw.WithAllPages())
if err != nil {
return nil, err
}
for _, ip := range ipsList.IPs {
if ip.Server == nil && stringInSlice(ip.Address.String(), c.reservedIPs) {
return ip, nil
}
}
return nil, nil
}
func stringInSlice(s string, slice []string) bool {
for _, i := range slice {
if i == s {
return true
}
}
return false
}
func isPublicSvc(svc *v1.Service) bool {
return svc.Spec.Type == v1.ServiceTypeLoadBalancer || svc.Spec.Type == v1.ServiceTypeNodePort
}
func getRegionalizedID(r string) (string, string, error) {
split := strings.Split(r, "/")
switch len(split) {
case 1:
return split[0], "", nil
case 2:
return split[1], split[0], nil
default:
return "", "", fmt.Errorf("couldn't parse ID %s", r)
}
}