Skip to content
This repository was archived by the owner on May 24, 2021. It is now read-only.

Commit 4ffd8f3

Browse files
committed
use metallb module
1 parent d77db75 commit 4ffd8f3

File tree

2 files changed

+50
-51
lines changed

2 files changed

+50
-51
lines changed

tests/loadbalancer/main.go

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ package main
1919
import (
2020
"fmt"
2121
"os"
22-
"os/exec"
2322

23+
"github.com/thekubeworld/k8devel/pkg/base64"
2424
"github.com/thekubeworld/k8devel/pkg/client"
2525
"github.com/thekubeworld/k8devel/pkg/curl"
2626
"github.com/thekubeworld/k8devel/pkg/diagram"
27+
"github.com/thekubeworld/k8devel/pkg/secret"
2728

2829
"github.com/thekubeworld/k8devel/pkg/kubeproxy"
30+
"github.com/thekubeworld/k8devel/pkg/metallb"
2931
"github.com/thekubeworld/k8devel/pkg/namespace"
3032
"github.com/thekubeworld/k8devel/pkg/pod"
3133
"github.com/thekubeworld/k8devel/pkg/service"
@@ -34,44 +36,13 @@ import (
3436

3537
func main() {
3638

37-
args := []string{"apply", "-f", "https://raw.githubusercontent.com/metallb/metallb/v0.9.6/manifests/namespace.yaml"}
38-
cmd := exec.Command("kubectl", args...)
39-
_, err := cmd.Output()
40-
if err != nil {
41-
fmt.Printf("cannot create namespace for metallb\n")
42-
os.Exit(1)
43-
}
44-
45-
args = []string{"apply", "-f", "https://raw.githubusercontent.com/metallb/metallb/v0.9.6/manifests/metallb.yaml"}
46-
cmd = exec.Command("kubectl", args...)
47-
_, err = cmd.Output()
48-
if err != nil {
49-
fmt.Printf("unable to apply metallb yaml\n")
50-
os.Exit(1)
51-
}
52-
53-
args = []string{"create", "secret", "generic", "-n", "metallb-system", "memberlist", "--from-literal=secretkey=\"$(openssl rand -base64 128)\""}
54-
cmd = exec.Command("kubectl", args...)
55-
_, err = cmd.Output()
56-
if err != nil {
57-
fmt.Printf("unable to create secret..\n")
58-
os.Exit(1)
59-
}
60-
6139
fmt.Printf("kube-proxy tests has started...\n\n")
6240

6341
fmt.Printf("Test #3) User's Traffic reach loadbalancer that will\n")
6442
fmt.Printf("route using kubeproxy/iptables to the right service \n")
6543
fmt.Printf("that has the backend pod \n")
6644
diagram.LoadBalancer()
6745

68-
// Initial set
69-
randStr, err := util.GenerateRandomString(6, "lower")
70-
if err != nil {
71-
fmt.Printf("%s\n", err)
72-
os.Exit(1)
73-
}
74-
7546
namespaceName := "kptesting"
7647
labelApp := "kptesting"
7748

@@ -84,6 +55,35 @@ func main() {
8455
// - os.Getenv("USERPROFILE") (Windows)
8556
c.Connect()
8657

58+
err := metallb.Deploy(&c, "v0.9.6")
59+
if err != nil {
60+
fmt.Printf("%s\n", err)
61+
os.Exit(1)
62+
}
63+
fmt.Printf("metallb deployed...\n")
64+
65+
base64Str, _ := base64.GenerateRandomString(128)
66+
metallbSecret := secret.Instance{
67+
Name: "memberlist",
68+
Namespace: "metallb-system",
69+
Type: "Opaque",
70+
Key: "secretkey",
71+
Value: base64Str,
72+
}
73+
err = metallb.CreateSecret(&c, &metallbSecret)
74+
if err != nil {
75+
fmt.Printf("%s\n", err)
76+
os.Exit(1)
77+
}
78+
fmt.Printf("metallb secret created...\n")
79+
80+
// Initial set
81+
randStr, err := util.GenerateRandomString(6, "lower")
82+
if err != nil {
83+
fmt.Printf("%s\n", err)
84+
os.Exit(1)
85+
}
86+
8787
// START: kube-proxy variables
8888
Namespace := c.Namespace + randStr
8989
NameService := "kproxysvc" + randStr
@@ -105,6 +105,7 @@ func main() {
105105
fmt.Printf("%s\n", err)
106106
os.Exit(1)
107107
}
108+
fmt.Printf("namespace created %s...\n", Namespace)
108109
// END: Namespace
109110

110111
// START: Service
@@ -127,6 +128,7 @@ func main() {
127128
fmt.Printf("exiting... failed to create: %s\n", err)
128129
os.Exit(1)
129130
}
131+
fmt.Printf("Created service Loadbalancer %s\n", s.Name)
130132

131133
// END: Service
132134

@@ -151,14 +153,21 @@ func main() {
151153
os.Remove(fwInitialState)
152154
os.Remove(fwAfterEndpointCreated)
153155

154-
// TODO: use library
155-
args = []string{"apply", "-f", "metallbcfg.yaml"}
156-
cmd = exec.Command("kubectl", args...)
157-
_, err = cmd.Output()
156+
// Metallb Config
157+
conf := metallb.InstanceConfig{
158+
Name: "config",
159+
Namespace: "metallb-system",
160+
ConfigName: "config",
161+
AddressPoolName: "default",
162+
AddressPoolProtocol: "layer2",
163+
AddressPoolAddresses: "172.17.255.1-172.17.255.250",
164+
}
165+
err = metallb.CreateConfig(&c, &conf)
158166
if err != nil {
159167
fmt.Println(err)
160168
os.Exit(1)
161169
}
170+
fmt.Printf("metallb created configmap %s namespace %s", conf.Name, conf.Namespace)
162171

163172
// START: Pod
164173
// Creating a POD Behind the service
@@ -175,8 +184,8 @@ func main() {
175184
fmt.Printf("%s\n", err)
176185
os.Exit(1)
177186
}
187+
fmt.Printf("Pod behind service created %s\n", p.Name)
178188
// END: Pod
179-
fmt.Printf("\n")
180189

181190
// Creating a POD outside the service (No labels)
182191
// So it will try to connect to pod behind the service
@@ -193,7 +202,9 @@ func main() {
193202
fmt.Printf("%s\n", err)
194203
os.Exit(1)
195204
}
205+
fmt.Printf("Pod outside service created %s\n", p.Name)
196206
// END: Pod
207+
197208
// START: Execute curl from the pod created to the new service
198209
ret, err := curl.ExecuteHTTPReqInsideContainer(
199210
&c,
@@ -208,6 +219,6 @@ func main() {
208219
fmt.Printf("PASSED\n")
209220
// END: Execute curl from the pod created to the new service
210221

211-
namespace.Delete(&c, namespaceName)
212-
namespace.Delete(&c, "metallb-system")
222+
//namespace.Delete(&c, namespaceName)
223+
//namespace.Delete(&c, "metallb-system")
213224
}

tests/loadbalancer/metallbcfg.yaml

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)