-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
164 lines (137 loc) · 4.22 KB
/
main.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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-redis/redis/v7"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/cache"
toolsWatch "k8s.io/client-go/tools/watch"
ctrl "sigs.k8s.io/controller-runtime"
)
func main() {
// Define flags for configuration
redisAddr := flag.String("redis-addr", "localhost:16379", "Redis server address")
redisDB := flag.Int("redis-db", 1, "Redis database number")
argocdNamespace := flag.String("argocd-namespace", "argocd", "ArgoCD namespace")
// Parse command-line flags
flag.Parse()
namespace := *argocdNamespace
// Initialize Redis client
rdb := redis.NewClient(&redis.Options{
Addr: *redisAddr,
DB: *redisDB,
DialTimeout: 5 * time.Second,
})
// Test connection
pong, err := rdb.Ping().Result()
if err != nil {
log.Fatalf("Failed to connect to Redis: %v", err)
}
fmt.Printf("Connected to Redis: %s\n", pong)
config := ctrl.GetConfigOrDie()
dynamicClient := dynamic.NewForConfigOrDie(config)
resource := schema.GroupVersionResource{
Group: "argoproj.io",
Version: "v1alpha1",
Resource: "applications",
}
appList, err := dynamicClient.Resource(resource).Namespace(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
fmt.Println(err)
}
for _, item := range appList.Items {
// Remove the metadata.managedFields field
unstructured.RemoveNestedField(item.Object, "metadata", "managedFields")
specProject, _, err := unstructured.NestedString(item.Object, "spec", "project")
if err != nil {
fmt.Printf("Error getting spec.project: %v\n", err)
return
}
// Set the key-value pair
key := fmt.Sprintf("%s|%s", specProject, item.GetName())
val, _ := json.Marshal(item.Object)
err = rdb.Set(key, val, time.Hour).Err()
if err != nil {
log.Fatalf("Failed to set key: %v", err)
}
}
fmt.Println("Starting watcher...")
initRV := appList.GetResourceVersion()
retryWatcher, err := toolsWatch.NewRetryWatcher(initRV, &cache.ListWatch{
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
options.ResourceVersion = initRV
options.Watch = true
return dynamicClient.Resource(resource).Namespace(namespace).Watch(context.Background(), options)
},
})
if err != nil {
fmt.Printf("Failed to create retry watcher: %v\n", err)
panic(err)
}
defer retryWatcher.Stop()
// SIGTERM handler
ctx, cancel := context.WithCancel(context.Background())
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGTERM, syscall.SIGHUP)
for {
select {
case event := <-retryWatcher.ResultChan():
obj, ok := event.Object.(*unstructured.Unstructured)
if !ok {
fmt.Println("Failed to cast event object to Unstructured")
continue
}
switch event.Type {
case watch.Added, watch.Modified:
fmt.Println("Application added/modified:", event.Object)
// Remove the metadata.managedFields field
unstructured.RemoveNestedField(obj.Object, "metadata", "managedFields")
// Print spec.project
specProject, _, err := unstructured.NestedString(obj.Object, "spec", "project")
if err != nil {
fmt.Printf("Error getting spec.project: %v\n", err)
return
}
// Set and Get a key-value pair
key := fmt.Sprintf("%s|%s", specProject, obj.GetName())
val, _ := json.Marshal(event.Object)
err = rdb.Set(key, val, time.Hour).Err()
if err != nil {
log.Fatalf("Failed to set key: %v", err)
}
case watch.Deleted:
fmt.Println("Application deleted:", event.Object)
specProject, _, err := unstructured.NestedString(obj.Object, "spec", "project")
if err != nil {
fmt.Printf("Error getting spec.project: %v\n", err)
return
}
fmt.Printf("spec.project: %s\n", specProject)
// Set and Get a key-value pair
key := fmt.Sprintf("%s|%s", specProject, obj.GetName())
err = rdb.Del(key).Err()
if err != nil {
log.Fatalf("Failed to set key: %v", err)
}
case watch.Bookmark, watch.Error:
default:
}
case <-sig:
cancel()
return
case <-ctx.Done():
return
}
}
}