-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
90 lines (79 loc) · 2.68 KB
/
main.go
File metadata and controls
90 lines (79 loc) · 2.68 KB
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
package main
import (
"context"
"flag"
"fmt"
"path/filepath"
"sync"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
func main() {
fmt.Printf(" 🛡️ NIGHT-WATCH: Monitoring in progress...\n")
home := homedir.HomeDir()
defaultKubeconfig := ""
if home != "" {
defaultKubeconfig = filepath.Join(home, ".kube", "config")
}
kubeconfig := flag.String("kubeconfig", defaultKubeconfig, "(optional) absolute path to the kubeconfig file")
mode := flag.String("mode", "sleep", "Mode of operation: sleep or wake")
flag.Parse()
fmt.Printf("🔍 Using kubeconfig file: %s\n", *kubeconfig)
var replicas int = 0
if *mode == "sleep" {
replicas = 1
fmt.Printf("💤 NIGHT-WATCH is in SLEEP mode. Monitoring paused.\n")
} else if *mode == "wake" {
replicas = 3
fmt.Printf("🌅 NIGHT-WATCH is in WAKE mode. Monitoring active!\n")
} else {
fmt.Printf("❌ Invalid mode specified. Use 'sleep' or 'wake'.\n")
return
}
namespace := "sre-mart"
fmt.Printf("📂 Monitoring namespace: %s\n", namespace)
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
fmt.Printf("❌ LỖI KẾT NỐI KUBERNETES: %v\n", err)
return
} else {
fmt.Printf("✅ Kết nối Kubernetes thành công!\n")
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
fmt.Printf("❌ LỖI TẠO CLIENTSET: %v\n", err)
return
}
Deployments, err := clientset.AppsV1().Deployments(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
fmt.Printf("❌ LỖI LẤY DEPLOYMENTS: %v\n", err)
return
}
var wg sync.WaitGroup
for _, deployment := range Deployments.Items {
fmt.Printf("📦 Deployment: %s - Setting replicas to %d\n", deployment.Name, replicas)
wg.Add(1)
go deployReplicas(clientset, namespace, replicas, deployment.Name, &wg)
}
wg.Wait()
fmt.Printf("✅ NIGHT-WATCH operation completed.\n")
}
func deployReplicas(clientset *kubernetes.Clientset, namespace string, replicas int, name string, wg *sync.WaitGroup) {
defer wg.Done()
DeploymentsClient, err := clientset.AppsV1().Deployments(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
fmt.Printf("❌ LỖI LẤY DEPLOYMENT %s: %v\n", name, err)
return
}
deployment := DeploymentsClient
replicas32 := int32(replicas)
deployment.Spec.Replicas = &replicas32
_, err = clientset.AppsV1().Deployments(namespace).Update(context.TODO(), deployment, metav1.UpdateOptions{})
if err != nil {
fmt.Printf("❌ LỖI CẬP NHẬT DEPLOYMENT %s: %v\n", name, err)
} else {
fmt.Printf("✅ Deployment %s updated to %d replicas successfully.\n", name, replicas)
}
}