-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
net.go
37 lines (34 loc) · 919 Bytes
/
net.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
package api
import (
"fmt"
"net"
"strings"
)
// Derive the default ClusterIP of the kube-dns service from EKS built-in CoreDNS addon
func (details *ClusterDetails) GetClusterDns() (string, error) {
ipFamily, err := GetCIDRIpFamily(details.CIDR)
if err != nil {
return "", err
}
switch ipFamily {
case IPFamilyIPv4:
dnsAddress := fmt.Sprintf("%s.10", details.CIDR[:strings.LastIndex(details.CIDR, ".")])
return dnsAddress, nil
case IPFamilyIPv6:
dnsAddress := fmt.Sprintf("%sa", strings.Split(details.CIDR, "/")[0])
return dnsAddress, nil
default:
return "", fmt.Errorf("%s was not a valid IP family", ipFamily)
}
}
func GetCIDRIpFamily(cidr string) (IPFamily, error) {
ip, _, err := net.ParseCIDR(cidr)
if err != nil {
return "", fmt.Errorf("%s is not a valid IP Address. error: %v", cidr, err)
}
if ip.To4() != nil {
return IPFamilyIPv4, nil
} else {
return IPFamilyIPv6, nil
}
}