-
Notifications
You must be signed in to change notification settings - Fork 1
/
overlay.go
226 lines (204 loc) · 6.39 KB
/
overlay.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
The Overlay Network Test installs a DaemonSet in the target cluster
and send 2 ping to each node to check if the Overlay Network is in
a workable state. The state will be print out.
Requires a working .kube/config file or a param -kubeconfig with a
working kube-config file.
*/
package main
import (
"context"
"flag"
"fmt"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/client-go/util/homedir"
"net"
"os"
"path/filepath"
"time"
)
const appversion = "1.0.5"
func main() {
// install namespace and app name
var kubeconfig *string
namespace := "kube-system"
app := "overlaytest"
var graceperiod = int64(1)
var user = int64(1000)
var privledged = bool(true)
var readonly = bool(true)
image := "mtr.devops.telekom.de/mcsps/swiss-army-knife:latest"
// load kube-config file
if os.Getenv("KUBECONFIG") != "" {
kubeconfig = flag.String("kubeconfig", os.Getenv("KUBECONFIG"), "environment variable of KUBECONFIG")
} else if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
version := flag.Bool("version", false, "app version")
reuse := flag.Bool("reuse", false, "reuse existing deployment")
flag.Parse()
// print app version and exit
if *version {
fmt.Println("version", appversion)
os.Exit(0)
}
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// how this will work with env var?
// *argKubecfgFile = os.Getenv("KUBECONFIG")
// prepare the DaemonSet resource
daemonsetsClient := clientset.AppsV1().DaemonSets(namespace)
fmt.Println("Welcome to the overlaytest.\n\n")
daemonset := &apps.DaemonSet{
ObjectMeta: meta.ObjectMeta{
Name: app,
},
Spec: apps.DaemonSetSpec{
Selector: &meta.LabelSelector{
MatchLabels: map[string]string{
"app": app,
},
},
Template: core.PodTemplateSpec{
ObjectMeta: meta.ObjectMeta{
Labels: map[string]string{
"app": app,
},
},
Spec: core.PodSpec{
Containers: []core.Container{
{
Args: []string{"tail -f /dev/null"},
Command: []string{"sh", "-c"},
Name: app,
Image: image,
ImagePullPolicy: "IfNotPresent",
SecurityContext: &core.SecurityContext{
AllowPrivilegeEscalation: &privledged,
Privileged: &privledged,
ReadOnlyRootFilesystem: &readonly,
RunAsGroup: &user,
RunAsUser: &user,
},
},
},
TerminationGracePeriodSeconds: &graceperiod,
Tolerations: []core.Toleration{{
Operator: "Exists",
}},
SecurityContext: &core.PodSecurityContext{
FSGroup: &user,
},
},
},
},
}
if *reuse != true {
// create DaemonSet, delete it if exists
fmt.Println("Creating daemonset...")
result, err := daemonsetsClient.Create(context.TODO(), daemonset, meta.CreateOptions{})
if errors.IsAlreadyExists(err) {
fmt.Println("daemonset already exists, deleting ... & exit")
deletePolicy := meta.DeletePropagationForeground
if err := daemonsetsClient.Delete(context.TODO(), app, meta.DeleteOptions{
PropagationPolicy: &deletePolicy,
}); err != nil {
panic(err)
}
os.Exit(1)
} else if err != nil {
panic(err)
}
fmt.Printf("Created daemonset %q.\n", result.GetObjectMeta().GetName())
// wait here if all PODs are ready
for {
obj, err := clientset.AppsV1().DaemonSets(namespace).Get(context.TODO(), "overlaytest", meta.GetOptions{})
if err != nil {
fmt.Println("Error getting daemonset: %v", err)
panic(err.Error())
}
if obj.Status.NumberReady != 0 {
fmt.Println("all pods ready")
break
}
time.Sleep(2 * time.Second)
}
}
// load list of PODs
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), meta.ListOptions{LabelSelector: "app=overlaytest"})
if err != nil {
panic(err.Error())
}
fmt.Printf("There are %d nodes in the cluster\n", len(pods.Items))
// wait here again if all PODs become an ip-address
fmt.Println("checking pod network...")
for _, pod := range pods.Items {
for {
podi, err := clientset.CoreV1().Pods(namespace).Get(context.TODO(), pod.ObjectMeta.Name, meta.GetOptions{})
if err != nil {
panic(err.Error())
}
if net.ParseIP(podi.Status.PodIP) != nil {
fmt.Println(podi.ObjectMeta.Name, "ready", podi.Status.PodIP)
// fmt.Println(podi.ObjectMeta.Name, "ready")
break
}
}
}
fmt.Println("all pods have network\n")
// loop the pod list for each node for the network test
fmt.Println("=> Start network overlay test\n")
// refresh pod object list
pods, err = clientset.CoreV1().Pods(namespace).List(context.TODO(), meta.ListOptions{LabelSelector: "app=overlaytest"})
for _, upod := range pods.Items {
for _, pod := range pods.Items {
// fmt.Println("Podname: ", pod.ObjectMeta.Name)
// fmt.Println("PodIP: ", pod.Status.PodIP)
// fmt.Println("Nodename: ", pod.Spec.NodeName)
cmd := []string{
"sh",
"-c",
"ping -c 2 " + upod.Status.PodIP + " > /dev/null 2>&1",
}
req := clientset.CoreV1().RESTClient().Post().Resource("pods").Name(pod.ObjectMeta.Name).Namespace(namespace).SubResource("exec").VersionedParams(&core.PodExecOptions{
Command: cmd,
Stdin: true,
Stdout: true,
Stderr: true,
}, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
if err != nil {
fmt.Println("error while creating Executor: %v", err)
}
err = exec.Stream(remotecommand.StreamOptions{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Tty: false,
})
if err != nil {
fmt.Println(upod.Spec.NodeName, " can NOT reach ", pod.Spec.NodeName)
} else {
fmt.Println(upod.Spec.NodeName, " can reach ", pod.Spec.NodeName)
}
}
}
fmt.Println("=> End network overlay test\n")
fmt.Println("Call me again to remove installed cluster resources\n")
}