/
controlplane.go
157 lines (138 loc) · 3.75 KB
/
controlplane.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
149
150
151
152
153
154
155
156
157
package main
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"html/template"
"io"
"net"
"os"
"os/exec"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
)
func getKindNetworkSubnetCIDRs() ([]string, error) {
cmd := exec.Command(
"docker", "inspect", "kind",
"--format", `{{ range $i, $a := .IPAM.Config }}{{ println .Subnet }}{{ end }}`,
)
cmdOut := new(bytes.Buffer)
cmd.Stdout = cmdOut
err := cmd.Run()
if err != nil {
return nil, err
}
reader := bufio.NewReader(cmdOut)
cidrs := []string{}
for {
line, readErr := reader.ReadString('\n')
if readErr != nil && readErr != io.EOF {
return nil, fmt.Errorf("error finding subnet CIDRs in the Docker \"kind\" network, %s", err)
}
cidrs = append(cidrs, strings.TrimSpace(line))
if readErr == io.EOF {
break
}
}
return cidrs, nil
}
func generateIPv4VIP() (string, error) {
cidrs, err := getKindNetworkSubnetCIDRs()
if err != nil {
return "", err
}
for _, cidr := range cidrs {
ip, ipNet, parseErr := net.ParseCIDR(cidr)
if err != nil {
return "", parseErr
}
if ip.To4() != nil {
mask := binary.BigEndian.Uint32(ipNet.Mask)
start := binary.BigEndian.Uint32(ipNet.IP)
end := (start & mask) | (^mask)
chosenVIP := make([]byte, 4)
binary.BigEndian.PutUint32(chosenVIP, end-5)
return net.IP(chosenVIP).String(), nil
}
}
return "", fmt.Errorf("could not find any IPv4 CIDRs in the Docker \"kind\" network")
}
func generateIPv6VIP() (string, error) {
cidrs, err := getKindNetworkSubnetCIDRs()
if err != nil {
return "", err
}
for _, cidr := range cidrs {
ip, ipNet, parseErr := net.ParseCIDR(cidr)
if err != nil {
return "", parseErr
}
if ip.To4() == nil {
lowerMask := binary.BigEndian.Uint64(ipNet.Mask[8:])
lowerStart := binary.BigEndian.Uint64(ipNet.IP[8:])
lowerEnd := (lowerStart & lowerMask) | (^lowerMask)
chosenVIP := make([]byte, 16)
// Copy upper half into chosenVIP
copy(chosenVIP, ipNet.IP[0:8])
// Copy lower half into chosenVIP
binary.BigEndian.PutUint64(chosenVIP[8:], lowerEnd-5)
return net.IP(chosenVIP).String(), nil
}
}
return "", fmt.Errorf("could not find any IPv6 CIDRs in the Docker \"kind\" network")
}
func (config *testConfig) manifestGen() error {
curDir, err := os.Getwd()
if err != nil {
return err
}
templatePath := filepath.Join(curDir, "testing/e2e/kube-vip.yaml.tmpl")
kubeVIPManifestTemplate, err := template.New("kube-vip.yaml.tmpl").ParseFiles(templatePath)
if err != nil {
return err
}
tempDirPath, err := os.MkdirTemp("", "kube-vip-test")
if err != nil {
return err
}
var manifestFile *os.File
if config.IPv6 {
config.Name = fmt.Sprintf("%s-ipv6", filepath.Base(tempDirPath))
config.ManifestPath = filepath.Join(tempDirPath, "kube-vip-ipv6.yaml")
manifestFile, err = os.Create(config.ManifestPath)
if err != nil {
return err
}
defer manifestFile.Close()
config.ControlPlaneAddress, err = generateIPv6VIP()
if err != nil {
return err
}
} else {
config.Name = fmt.Sprintf("%s-ipv4", filepath.Base(tempDirPath))
config.ManifestPath = filepath.Join(tempDirPath, "kube-vip-ipv4.yaml")
manifestFile, err = os.Create(config.ManifestPath)
if err != nil {
return err
}
defer manifestFile.Close()
config.ControlPlaneAddress, err = generateIPv4VIP()
if err != nil {
return err
}
}
log.Infof("🗃️ Manifest path %s", config.ManifestPath)
err = kubeVIPManifestTemplate.Execute(manifestFile, kubevipManifestValues{
ControlPlaneVIP: config.ControlPlaneAddress,
ImagePath: config.ImagePath,
})
return err
}
// func (config *testConfig) startTest(ctx context.Context, clientset *kubernetes.Clientset) error {
// if config.ControlPlaneAddress == "" {
// log.Fatal("no control plane address exists")
// }
// return nil
// }