Skip to content

Commit 747a577

Browse files
committed
Refactor watcher initialization to use goroutines and wait for completion
1 parent 7566b4b commit 747a577

File tree

3 files changed

+62
-29
lines changed

3 files changed

+62
-29
lines changed

pkg/mutation/mutation_deploy.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,18 @@ func deleteConfigMap(name, namespace string) error {
192192
return nil
193193
}
194194

195-
func createDeployPatch(target *appsv1.Deployment, original *appsv1.Deployment) ([]byte, error) {
196-
targetPod, err := json.Marshal(target)
197-
originalPod, err := json.Marshal(original)
195+
func createDeployPatch(target, original *appsv1.Deployment) ([]byte, error) {
196+
targetJSON, err := json.Marshal(target)
198197
if err != nil {
199198
return nil, err
200199
}
201-
p, err := jsonpatch.CreatePatch(originalPod, targetPod)
200+
originalJSON, err := json.Marshal(original)
202201
if err != nil {
203202
return nil, err
204203
}
205-
return json.Marshal(p)
204+
patch, err := jsonpatch.CreatePatch(originalJSON, targetJSON)
205+
if err != nil {
206+
return nil, err
207+
}
208+
return json.Marshal(patch)
206209
}

pkg/mutation/mutation_pod.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,15 +367,18 @@ func addPodVolume(targetPod *corev1.Pod, deploymentName string) []corev1.Volume
367367
return append(volumes, agentVolumes...)
368368
}
369369

370-
func createPodPatch(target *corev1.Pod, original *corev1.Pod) ([]byte, error) {
371-
targetPod, err := json.Marshal(target)
372-
originalPod, err := json.Marshal(original)
370+
func createPodPatch(target, original *corev1.Pod) ([]byte, error) {
371+
targetJSON, err := json.Marshal(target)
373372
if err != nil {
374373
return nil, err
375374
}
376-
p, err := jsonpatch.CreatePatch(originalPod, targetPod)
375+
originalJSON, err := json.Marshal(original)
377376
if err != nil {
378377
return nil, err
379378
}
380-
return json.Marshal(p)
379+
patch, err := jsonpatch.CreatePatch(originalJSON, targetJSON)
380+
if err != nil {
381+
return nil, err
382+
}
383+
return json.Marshal(patch)
381384
}

pkg/watcher/init.go

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,59 @@
11
package watcher
22

33
import (
4+
"sync"
5+
46
"github.com/jd-opensource/joylive-injector/pkg/config"
57
"github.com/jd-opensource/joylive-injector/pkg/log"
68
"github.com/jd-opensource/joylive-injector/pkg/resource"
79
"go.uber.org/zap"
810
)
911

1012
func init() {
11-
// Start the ConfigMap listener and initialize the content
12-
cmWatcher := NewConfigMapWatcher(resource.GetResource().ClientSet)
13-
err := cmWatcher.Start()
14-
if err != nil {
15-
log.Fatal("start cmWatcher error", zap.Error(err))
16-
}
17-
err = cmWatcher.InitConfigMap(config.GetNamespace())
18-
if err != nil {
19-
log.Fatal("init cm error", zap.Error(err))
20-
}
13+
var wg sync.WaitGroup
14+
var fatalErr error
15+
var once sync.Once
2116

22-
// Start the AgentVersion listener and initialize the content
23-
avWatcher := NewAgentVersionWatcher(resource.GetResource().RestConfig)
24-
err = avWatcher.Start()
25-
if err != nil {
26-
log.Fatal("start avWatcher error", zap.Error(err))
27-
}
28-
err = avWatcher.InitAgentVersion(config.GetNamespace())
29-
if err != nil {
30-
log.Fatal("init agentVersion error", zap.Error(err))
17+
wg.Add(2)
18+
19+
go func() {
20+
defer wg.Done()
21+
cmWatcher := NewConfigMapWatcher(resource.GetResource().ClientSet)
22+
err := cmWatcher.Start()
23+
if err != nil {
24+
once.Do(func() {
25+
fatalErr = err
26+
})
27+
return
28+
}
29+
err = cmWatcher.InitConfigMap(config.GetNamespace())
30+
if err != nil {
31+
once.Do(func() {
32+
fatalErr = err
33+
})
34+
}
35+
}()
36+
37+
go func() {
38+
defer wg.Done()
39+
avWatcher := NewAgentVersionWatcher(resource.GetResource().RestConfig)
40+
err := avWatcher.Start()
41+
if err != nil {
42+
once.Do(func() {
43+
fatalErr = err
44+
})
45+
return
46+
}
47+
err = avWatcher.InitAgentVersion(config.GetNamespace())
48+
if err != nil {
49+
once.Do(func() {
50+
fatalErr = err
51+
})
52+
}
53+
}()
54+
55+
wg.Wait()
56+
if fatalErr != nil {
57+
log.Fatal("watcher init error", zap.Error(fatalErr))
3158
}
3259
}

0 commit comments

Comments
 (0)