@@ -18,25 +18,23 @@ package informer
18
18
19
19
import (
20
20
"github.com/devtron-labs/common-lib/utils/k8s"
21
- "sync"
22
- "time"
23
-
24
21
"github.com/devtron-labs/devtron/api/bean"
25
22
"go.uber.org/zap"
26
23
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
24
kubeinformers "k8s.io/client-go/informers"
28
25
"k8s.io/client-go/tools/cache"
26
+ "sync"
27
+ "time"
29
28
)
30
29
31
- func NewGlobalMapClusterNamespace () map [ string ] map [ string ] bool {
32
- globalMapClusterNamespace := make ( map [ string ] map [ string ] bool )
30
+ func NewGlobalMapClusterNamespace () sync. Map {
31
+ var globalMapClusterNamespace sync. Map
33
32
return globalMapClusterNamespace
34
33
}
35
34
36
35
type K8sInformerFactoryImpl struct {
37
36
logger * zap.SugaredLogger
38
- globalMapClusterNamespace map [string ]map [string ]bool // {"cluster1":{"ns1":true","ns2":true"}}
39
- mutex sync.Mutex
37
+ globalMapClusterNamespace sync.Map // {"cluster1":{"ns1":true","ns2":true"}}
40
38
informerStopper map [string ]chan struct {}
41
39
k8sUtil * k8s.K8sServiceImpl
42
40
}
@@ -47,7 +45,7 @@ type K8sInformerFactory interface {
47
45
CleanNamespaceInformer (clusterName string )
48
46
}
49
47
50
- func NewK8sInformerFactoryImpl (logger * zap.SugaredLogger , globalMapClusterNamespace map [ string ] map [ string ] bool , k8sUtil * k8s.K8sServiceImpl ) * K8sInformerFactoryImpl {
48
+ func NewK8sInformerFactoryImpl (logger * zap.SugaredLogger , globalMapClusterNamespace sync. Map , k8sUtil * k8s.K8sServiceImpl ) * K8sInformerFactoryImpl {
51
49
informerFactory := & K8sInformerFactoryImpl {
52
50
logger : logger ,
53
51
globalMapClusterNamespace : globalMapClusterNamespace ,
@@ -59,19 +57,17 @@ func NewK8sInformerFactoryImpl(logger *zap.SugaredLogger, globalMapClusterNamesp
59
57
60
58
func (impl * K8sInformerFactoryImpl ) GetLatestNamespaceListGroupByCLuster () map [string ]map [string ]bool {
61
59
copiedClusterNamespaces := make (map [string ]map [string ]bool )
62
- for key , value := range impl .globalMapClusterNamespace {
63
- for namespace , v := range value {
64
- if _ , ok := copiedClusterNamespaces [key ]; ! ok {
65
- allNamespaces := make (map [string ]bool )
66
- allNamespaces [namespace ] = v
67
- copiedClusterNamespaces [key ] = allNamespaces
68
- } else {
69
- allNamespaces := copiedClusterNamespaces [key ]
70
- allNamespaces [namespace ] = v
71
- copiedClusterNamespaces [key ] = allNamespaces
72
- }
73
- }
74
- }
60
+ impl .globalMapClusterNamespace .Range (func (key , value interface {}) bool {
61
+ clusterName := key .(string )
62
+ allNamespaces := value .(* sync.Map )
63
+ namespaceMap := make (map [string ]bool )
64
+ allNamespaces .Range (func (nsKey , nsValue interface {}) bool {
65
+ namespaceMap [nsKey .(string )] = nsValue .(bool )
66
+ return true
67
+ })
68
+ copiedClusterNamespaces [clusterName ] = namespaceMap
69
+ return true
70
+ })
75
71
return copiedClusterNamespaces
76
72
}
77
73
@@ -86,14 +82,14 @@ func (impl *K8sInformerFactoryImpl) BuildInformer(clusterInfo []*bean.ClusterInf
86
82
CertData : info .CertData ,
87
83
CAData : info .CAData ,
88
84
}
89
- impl .buildInformerAndNamespaceList (info .ClusterName , clusterConfig , & impl . mutex )
85
+ impl .buildInformerAndNamespaceList (info .ClusterName , clusterConfig )
90
86
}
91
87
return
92
88
}
93
89
94
- func (impl * K8sInformerFactoryImpl ) buildInformerAndNamespaceList (clusterName string , clusterConfig * k8s.ClusterConfig , mutex * sync.Mutex ) map [ string ] map [ string ] bool {
95
- allNamespaces := make ( map [ string ] bool )
96
- impl .globalMapClusterNamespace [ clusterName ] = allNamespaces
90
+ func (impl * K8sInformerFactoryImpl ) buildInformerAndNamespaceList (clusterName string , clusterConfig * k8s.ClusterConfig ) sync.Map {
91
+ allNamespaces := sync. Map {}
92
+ impl .globalMapClusterNamespace . Store ( clusterName , & allNamespaces )
97
93
_ , _ , clusterClient , err := impl .k8sUtil .GetK8sConfigAndClients (clusterConfig )
98
94
if err != nil {
99
95
impl .logger .Errorw ("error in getting k8s clientset" , "err" , err , "clusterName" , clusterConfig .ClusterName )
@@ -105,30 +101,16 @@ func (impl *K8sInformerFactoryImpl) buildInformerAndNamespaceList(clusterName st
105
101
nsInformer .Informer ().AddEventHandler (cache.ResourceEventHandlerFuncs {
106
102
AddFunc : func (obj interface {}) {
107
103
if mobject , ok := obj .(metav1.Object ); ok {
108
- mutex .Lock ()
109
- defer mutex .Unlock ()
110
- if _ , ok := impl .globalMapClusterNamespace [clusterName ]; ! ok {
111
- allNamespaces := make (map [string ]bool )
112
- allNamespaces [mobject .GetName ()] = true
113
- impl .globalMapClusterNamespace [clusterName ] = allNamespaces
114
- } else {
115
- allNamespaces := impl .globalMapClusterNamespace [clusterName ]
116
- allNamespaces [mobject .GetName ()] = true
117
- impl .globalMapClusterNamespace [clusterName ] = allNamespaces
118
- }
119
- //mutex.Unlock()
104
+ value , _ := impl .globalMapClusterNamespace .Load (clusterName )
105
+ allNamespaces := value .(* sync.Map )
106
+ allNamespaces .Store (mobject .GetName (), true )
120
107
}
121
108
},
122
109
DeleteFunc : func (obj interface {}) {
123
110
if object , ok := obj .(metav1.Object ); ok {
124
- mutex .Lock ()
125
- defer mutex .Unlock ()
126
- if _ , ok := impl .globalMapClusterNamespace [clusterName ]; ok {
127
- allNamespaces := impl .globalMapClusterNamespace [clusterName ]
128
- delete (allNamespaces , object .GetName ())
129
- impl .globalMapClusterNamespace [clusterName ] = allNamespaces
130
- //mutex.Unlock()
131
- }
111
+ value , _ := impl .globalMapClusterNamespace .Load (clusterName )
112
+ allNamespaces := value .(* sync.Map )
113
+ allNamespaces .Delete (object .GetName ())
132
114
}
133
115
},
134
116
})
0 commit comments