-
Notifications
You must be signed in to change notification settings - Fork 2
/
kube-carga.go
executable file
·113 lines (102 loc) · 2.63 KB
/
kube-carga.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
package main
import (
"context"
"flag"
"log"
"math/rand"
"os"
"os/signal"
"strconv"
"syscall"
"time"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
var kubeconfig string
flag.StringVar(&kubeconfig, "kubeconfig", "", "path to kubeconfig file")
flag.Parse()
// Check if kubeconfig path is provided, otherwise use KUBECONFIG environment variable
if kubeconfig == "" {
kubeconfig = os.Getenv("KUBECONFIG")
}
// Use the in-cluster config if kubeconfig path is not provided
var config *rest.Config
var err error
if kubeconfig == "" {
config, err = rest.InClusterConfig()
} else {
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
}
if err != nil {
log.Fatalf("Error building kubeconfig: %v", err)
}
// Create a new Kubernetes clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf("Error creating Kubernetes client: %v", err)
}
// Create a context for the test
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Channel to handle OS signals
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
// Start load testing
go func() {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
// Perform some workload here
// For example, create a deployment
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "test-deployment-" + strconv.Itoa(rand.Intn(100)),
Namespace: "default",
},
Spec: appsv1.DeploymentSpec{
Replicas: int32Ptr(1),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "test",
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": "test",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "nginx",
Image: "nginx",
},
},
},
},
},
}
_, err := clientset.AppsV1().Deployments("default").Create(ctx, deployment, metav1.CreateOptions{})
if err != nil {
log.Printf("Error creating deployment: %v\n", err)
} else {
log.Printf("Deployment %s created successfully\n", deployment.Name)
}
case <-ctx.Done():
return
}
}
}()
// Wait for termination signal
<-sigCh
log.Println("Received termination signal. Stopping test.")
}
func int32Ptr(i int32) *int32 { return &i }