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

Commit 048be01

Browse files
committed
simplify the example code
1 parent c68983c commit 048be01

File tree

1 file changed

+68
-118
lines changed

1 file changed

+68
-118
lines changed

tests/loadbalancer/main.go

Lines changed: 68 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -17,64 +17,62 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"fmt"
21+
"os"
2022
"os/exec"
2123

22-
"github.com/gookit/color"
23-
"github.com/sirupsen/logrus"
24-
2524
"github.com/thekubeworld/k8devel/pkg/client"
2625
"github.com/thekubeworld/k8devel/pkg/curl"
2726
"github.com/thekubeworld/k8devel/pkg/diagram"
2827

29-
// "github.com/thekubeworld/k8devel/pkg/iptables"
3028
"github.com/thekubeworld/k8devel/pkg/kubeproxy"
31-
"github.com/thekubeworld/k8devel/pkg/logschema"
3229
"github.com/thekubeworld/k8devel/pkg/namespace"
3330
"github.com/thekubeworld/k8devel/pkg/pod"
3431
"github.com/thekubeworld/k8devel/pkg/service"
3532
"github.com/thekubeworld/k8devel/pkg/util"
3633
)
3734

3835
func main() {
39-
logschema.SetLogrusLogging()
4036

4137
args := []string{"apply", "-f", "https://raw.githubusercontent.com/metallb/metallb/v0.9.6/manifests/namespace.yaml"}
4238
cmd := exec.Command("kubectl", args...)
4339
_, err := cmd.Output()
4440
if err != nil {
45-
logrus.Infof("%s", err)
41+
fmt.Printf("cannot create namespace for metallb\n")
42+
os.Exit(1)
4643
}
4744

4845
args = []string{"apply", "-f", "https://raw.githubusercontent.com/metallb/metallb/v0.9.6/manifests/metallb.yaml"}
4946
cmd = exec.Command("kubectl", args...)
5047
_, err = cmd.Output()
5148
if err != nil {
52-
logrus.Infof("%s", err)
49+
fmt.Printf("unable to apply metallb yaml\n")
50+
os.Exit(1)
5351
}
5452

5553
args = []string{"create", "secret", "generic", "-n", "metallb-system", "memberlist", "--from-literal=secretkey=\"$(openssl rand -base64 128)\""}
5654
cmd = exec.Command("kubectl", args...)
5755
_, err = cmd.Output()
5856
if err != nil {
59-
logrus.Infof("%s", err)
57+
fmt.Printf("unable to create secret..\n")
58+
os.Exit(1)
6059
}
6160

62-
logrus.Infof("kube-proxy tests has started...")
61+
fmt.Printf("kube-proxy tests has started...\n\n")
6362

64-
logrus.Infof("\n")
65-
logrus.Infof("\n")
66-
logrus.Infof("Test #3) User's Traffic reach loadbalancer that will")
67-
logrus.Infof("route using kubeproxy/iptables to the right service ")
68-
logrus.Infof("that has the backend pod ")
63+
fmt.Printf("Test #3) User's Traffic reach loadbalancer that will\n")
64+
fmt.Printf("route using kubeproxy/iptables to the right service \n")
65+
fmt.Printf("that has the backend pod \n")
6966
diagram.LoadBalancer()
7067

7168
// Initial set
7269
randStr, err := util.GenerateRandomString(6, "lower")
7370
if err != nil {
74-
logrus.Fatal(err)
71+
fmt.Printf("%s\n", err)
72+
os.Exit(1)
7573
}
7674

77-
namespaceName := "kptesting" + randStr
75+
namespaceName := "kptesting"
7876
labelApp := "kptesting"
7977

8078
c := client.Client{}
@@ -86,82 +84,36 @@ func main() {
8684
// - os.Getenv("USERPROFILE") (Windows)
8785
c.Connect()
8886

89-
// kube-proxy is a DaemonSet, it will replicate the pod
90-
// data to all nodes. Let's find one pod name to use and
91-
// collect data.
92-
KP := "kube-proxy"
93-
namespaceKP := "kube-system"
94-
kyPods, kyNumberPods := pod.FindPodsWithNameContains(&c,
95-
KP,
96-
namespaceKP)
97-
if kyNumberPods < 0 {
98-
logrus.Fatal("exiting... unable to find kube-proxy pod..")
99-
}
100-
logrus.Infof("Found the following kube-proxy pods:")
101-
logrus.Infof("\t\tNamespace: %s", namespaceKP)
102-
logrus.Infof("\t\t%s", kyPods)
103-
104-
// Detect Kube-proxy mode
105-
kpMode, err := kubeproxy.DetectKubeProxyMode(&c,
106-
KP,
107-
KP,
108-
namespaceKP)
109-
if err != nil {
110-
logrus.Fatal(err)
111-
}
112-
logrus.Infof("\n")
113-
logrus.Infof("Detected kube-proxy mode: %s", kpMode)
87+
// START: kube-proxy variables
88+
Namespace := c.Namespace + randStr
89+
NameService := "kproxysvc" + randStr
11490

115-
// Setting ContainerName and Namespace
116-
//KPTestContainerName := kyPods[0]
117-
KPTestNamespaceName := c.Namespace
118-
randStr, err = util.GenerateRandomString(6, "lower")
91+
// Saving current state of firewall in kube-proxy
92+
fwInitialState, err := kubeproxy.SaveCurrentFirewallState(
93+
&c,
94+
"kube-proxy",
95+
"kube-proxy",
96+
"kube-system")
11997
if err != nil {
120-
logrus.Fatal(err)
98+
fmt.Println(err)
99+
os.Exit(1)
121100
}
122101

123-
// TODO: Just load the iptables commands if kube-proxy
124-
// is IPTABLES or return error
125-
// Loading some iptables
126-
//iptablesCmd := iptables.LoadPreDefinedCommands()
127-
//if err != nil {
128-
// logrus.Fatal(err)
129-
//}
130-
131-
// iptables saving initial state
132-
//iptablesInitialState, err := iptables.Save(
133-
// &c,
134-
// &iptablesCmd,
135-
// KPTestContainerName,
136-
// "kube-system")
137-
//if err != nil {
138-
// logrus.Fatal(err)
139-
//}
140-
141102
// START: Namespace
142-
_, err = namespace.Exists(&c,
143-
KPTestNamespaceName)
103+
err = namespace.Create(&c, Namespace)
144104
if err != nil {
145-
err = namespace.Create(&c,
146-
KPTestNamespaceName)
147-
if err != nil {
148-
logrus.Fatal("exiting... failed to create: ", err)
149-
}
105+
fmt.Printf("%s\n", err)
106+
os.Exit(1)
150107
}
151108
// END: Namespace
152109

153-
// Setting Service Name
154-
KPTestServiceName := KPTestNamespaceName +
155-
"service" +
156-
randStr
157-
158110
// START: Service
159111
// nodePort - a static port assigned on each the node
160112
// port - port exposed internally in the cluster
161113
// targetPort - the container port to send requests to
162114
s := service.Instance{
163-
Name: KPTestServiceName,
164-
Namespace: KPTestNamespaceName,
115+
Name: NameService,
116+
Namespace: Namespace,
165117
LabelKey: "app",
166118
LabelValue: labelApp,
167119
SelectorKey: "app",
@@ -172,92 +124,90 @@ func main() {
172124
}
173125
err = service.CreateLoadBalancer(&c, &s)
174126
if err != nil {
175-
logrus.Fatal("exiting... failed to create: ", err)
127+
fmt.Printf("exiting... failed to create: %s\n", err)
128+
os.Exit(1)
176129
}
177130

178131
// END: Service
179132

180-
// START: iptables diff
181-
// iptablesStateAfterEndpointCreated, err := iptables.Save(
182-
// &c, &iptablesCmd, KPTestContainerName, "kube-system")
183-
//if err != nil {
184-
// logrus.Fatal(err)
185-
//}
186-
187-
// Make a diff between two states we collected from iptables
188-
//out, err = util.DiffCommand(iptablesInitialState.Name(),
189-
// iptablesStateAfterEndpointCreated.Name())
190-
//if err != nil {
191-
// logrus.Fatal(err)
192-
//}
133+
// Save firewall state after service, endpoint created
134+
fwAfterEndpointCreated, err := kubeproxy.SaveCurrentFirewallState(
135+
&c,
136+
"kube-proxy",
137+
"kube-proxy",
138+
"kube-system")
139+
if err != nil {
140+
fmt.Println(err)
141+
os.Exit(1)
142+
}
193143

194-
//if len(string(out)) > 0 {
195-
// logrus.Infof("%s", string(out))
196-
//}
197-
// END: iptables diff
144+
// See the difference with diff command
145+
out, err := util.DiffCommand(fwInitialState, fwAfterEndpointCreated)
146+
if err != nil {
147+
fmt.Println(err)
148+
os.Exit(1)
149+
}
150+
fmt.Printf("%s", string(out))
151+
os.Remove(fwInitialState)
152+
os.Remove(fwAfterEndpointCreated)
198153

199154
// TODO: use library
200155
args = []string{"apply", "-f", "metallbcfg.yaml"}
201156
cmd = exec.Command("kubectl", args...)
202157
_, err = cmd.Output()
203158
if err != nil {
204-
logrus.Infof("%s", err)
159+
fmt.Println(err)
160+
os.Exit(1)
205161
}
206162

207-
/*
208-
ExternalIPService, err := k8devel.GetExternalIPFromService(
209-
&c,
210-
KPTestServiceName,
211-
KPTestNamespaceName)
212-
if err != nil {
213-
logrus.Fatal("exiting... failed to create: ", err)
214-
}*/
215-
216163
// START: Pod
217-
218164
// Creating a POD Behind the service
219165
p := pod.Instance{
220166
Name: "kpnginxbehindservice",
221-
Namespace: KPTestNamespaceName,
167+
Namespace: Namespace,
222168
Image: "nginx:1.14.2",
223169
LabelKey: "app",
224170
LabelValue: labelApp,
225171
}
226-
logrus.Infof("\n")
172+
fmt.Printf("\n")
227173
err = pod.Create(&c, &p)
228174
if err != nil {
229-
logrus.Fatal(err)
175+
fmt.Printf("%s\n", err)
176+
os.Exit(1)
230177
}
231178
// END: Pod
232-
logrus.Info("\n")
179+
fmt.Printf("\n")
233180

234181
// Creating a POD outside the service (No labels)
235182
// So it will try to connect to pod behind the service
236183
containerName := "nginxtoconnecttoservice"
237184
p = pod.Instance{
238185
Name: containerName,
239-
Namespace: KPTestNamespaceName,
186+
Namespace: Namespace,
240187
Image: "nginx",
241188
LabelKey: "app",
242189
LabelValue: "foobar",
243190
}
244191
err = pod.Create(&c, &p)
245192
if err != nil {
246-
logrus.Fatal(err)
193+
fmt.Printf("%s\n", err)
194+
os.Exit(1)
247195
}
248196
// END: Pod
249197
// START: Execute curl from the pod created to the new service
250198
ret, err := curl.ExecuteHTTPReqInsideContainer(
251199
&c,
252200
containerName,
253-
KPTestNamespaceName,
201+
Namespace,
254202
"172.17.255.1")
255203
if err != nil {
256-
logrus.Fatal(err)
204+
fmt.Printf("%s\n", err)
205+
os.Exit(1)
257206
}
258-
logrus.Infof("%s", ret)
259-
color.Green.Println("[Test #2 PASSED]")
207+
fmt.Printf("%s\n", ret)
208+
fmt.Printf("PASSED\n")
260209
// END: Execute curl from the pod created to the new service
261210

262211
namespace.Delete(&c, namespaceName)
212+
namespace.Delete(&c, "metallb-system")
263213
}

0 commit comments

Comments
 (0)