@@ -28,6 +28,7 @@ import (
2828 cloudcommon "github.com/deepflowio/deepflow/server/controller/cloud/common"
2929 "github.com/deepflowio/deepflow/server/controller/cloud/config"
3030 "github.com/deepflowio/deepflow/server/controller/cloud/kubernetes_gather/model"
31+ cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model"
3132 "github.com/deepflowio/deepflow/server/controller/common"
3233 "github.com/deepflowio/deepflow/server/controller/db/metadb"
3334 metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model"
@@ -68,6 +69,7 @@ type KubernetesGather struct {
6869 serviceLcuuidToIngressLcuuid map [string ]string
6970 k8sInfo map [string ][]string
7071 pgLcuuidToPSLcuuids map [string ][]string
72+ configMapToLcuuid map [[2 ]string ]string
7173 podLcuuidToPGInfo map [string ][2 ]string
7274 nsLabelToGroupLcuuids map [string ]mapset.Set
7375 pgLcuuidTopodTargetPorts map [string ]map [string ]int
@@ -207,6 +209,7 @@ func NewKubernetesGather(db *metadb.DB, domain *metadbmodel.Domain, subDomain *m
207209 serviceLcuuidToIngressLcuuid : map [string ]string {},
208210 k8sInfo : map [string ][]string {},
209211 pgLcuuidToPSLcuuids : map [string ][]string {},
212+ configMapToLcuuid : map [[2 ]string ]string {},
210213 podLcuuidToPGInfo : map [string ][2 ]string {},
211214 nsLabelToGroupLcuuids : map [string ]mapset.Set {},
212215 pgLcuuidTopodTargetPorts : map [string ]map [string ]int {},
@@ -249,6 +252,76 @@ func (k *KubernetesGather) GetLabel(labelMap map[string]interface{}) string {
249252 return strings .Join (labelSlice , ", " )
250253}
251254
255+ func (k * KubernetesGather ) simpleJsonMarshal (json * simplejson.Json ) string {
256+ bytes , err := json .MarshalJSON ()
257+ if err != nil {
258+ log .Infof ("simplejson (%s) marshal failed: %s" , json , err .Error (), logger .NewORGPrefix (k .orgID ))
259+ return ""
260+ }
261+ return string (bytes )
262+ }
263+
264+ func (k * KubernetesGather ) pgSpecGenerateConnections (nsName , pgName , pgLcuuid string , mainSpec * simplejson.Json ) []cloudmodel.PodGroupConfigMapConnection {
265+ var connections []cloudmodel.PodGroupConfigMapConnection
266+
267+ existSet := map [string ]bool {}
268+ spec := mainSpec .GetPath ("template" , "spec" )
269+ containers := spec .Get ("containers" )
270+ for c := range containers .MustArray () {
271+ envs := containers .GetIndex (c ).Get ("env" )
272+ for e := range envs .MustArray () {
273+ env := envs .GetIndex (e )
274+ ref , ok := env .Get ("valueFrom" ).CheckGet ("configMapKeyRef" )
275+ if ! ok {
276+ continue
277+ }
278+ cmName := ref .Get ("Name" ).MustString ()
279+ cmLcuuid , ok := k .configMapToLcuuid [[2 ]string {nsName , cmName }]
280+ if ! ok {
281+ log .Infof ("pod group (%s) imported env config map (%s) not found" , pgName , cmName , logger .NewORGPrefix (k .orgID ))
282+ continue
283+ }
284+ if _ , ok := existSet [pgLcuuid + cmLcuuid ]; ok {
285+ log .Debugf ("env pod group (%s) and config map (%s) connections already exists" , pgName , cmName , logger .NewORGPrefix (k .orgID ))
286+ continue
287+ }
288+ connections = append (connections , cloudmodel.PodGroupConfigMapConnection {
289+ Lcuuid : common .GetUUIDByOrgID (k .orgID , pgLcuuid + cmLcuuid ),
290+ PodGroupLcuuid : pgLcuuid ,
291+ ConfigMapLcuuid : cmLcuuid ,
292+ })
293+ existSet [pgLcuuid + cmLcuuid ] = false
294+ }
295+ }
296+
297+ volumes := spec .Get ("volumes" )
298+ for v := range volumes .MustArray () {
299+ volume := volumes .GetIndex (v )
300+ cm , ok := volume .CheckGet ("configMap" )
301+ if ! ok {
302+ continue
303+ }
304+ cmName := cm .Get ("name" ).MustString ()
305+ cmLcuuid , ok := k .configMapToLcuuid [[2 ]string {nsName , cmName }]
306+ if ! ok {
307+ log .Infof ("pod group (%s) imported volumes config map (%s) not found" , pgName , cmName , logger .NewORGPrefix (k .orgID ))
308+ continue
309+ }
310+ if _ , ok := existSet [pgLcuuid + cmLcuuid ]; ok {
311+ log .Debugf ("volumes pod group (%s) and config map (%s) connections already exists" , pgName , cmName , logger .NewORGPrefix (k .orgID ))
312+ continue
313+ }
314+ connections = append (connections , cloudmodel.PodGroupConfigMapConnection {
315+ Lcuuid : common .GetUUIDByOrgID (k .orgID , pgLcuuid + cmLcuuid ),
316+ PodGroupLcuuid : pgLcuuid ,
317+ ConfigMapLcuuid : cmLcuuid ,
318+ })
319+ existSet [pgLcuuid + cmLcuuid ] = false
320+ }
321+
322+ return connections
323+ }
324+
252325func (k * KubernetesGather ) GetKubernetesGatherData () (model.KubernetesGatherResource , error ) {
253326 // 任务循环的是同一个实例,所以这里要对关联关系进行初始化
254327 k .azLcuuid = ""
@@ -263,6 +336,7 @@ func (k *KubernetesGather) GetKubernetesGatherData() (model.KubernetesGatherReso
263336 k .serviceLcuuidToIngressLcuuid = map [string ]string {}
264337 k .nsLabelToGroupLcuuids = map [string ]mapset.Set {}
265338 k .pgLcuuidToPSLcuuids = map [string ][]string {}
339+ k .configMapToLcuuid = map [[2 ]string ]string {}
266340 k .podLcuuidToPGInfo = map [string ][2 ]string {}
267341 k .pgLcuuidTopodTargetPorts = map [string ]map [string ]int {}
268342 k .namespaceToExLabels = map [string ]map [string ]interface {}{}
@@ -309,24 +383,31 @@ func (k *KubernetesGather) GetKubernetesGatherData() (model.KubernetesGatherReso
309383 return model.KubernetesGatherResource {}, err
310384 }
311385
312- podGroups , err := k .getPodGroups ()
386+ configMaps , err := k .getConfigMaps ()
387+ if err != nil {
388+ return model.KubernetesGatherResource {}, err
389+ }
390+
391+ podGroups , podGroupConfigMapConnections , err := k .getPodGroups ()
313392 if err != nil {
314393 return model.KubernetesGatherResource {}, err
315394 }
316395
317- podRCs , err := k .getPodReplicationControllers ()
396+ podRCs , podRCsConfigMapConnections , err := k .getPodReplicationControllers ()
318397 if err != nil {
319398 return model.KubernetesGatherResource {}, err
320399 }
321400
322401 podGroups = append (podGroups , podRCs ... )
402+ podGroupConfigMapConnections = append (podGroupConfigMapConnections , podRCsConfigMapConnections ... )
323403
324- replicaSets , podRSCs , err := k .getReplicaSetsAndReplicaSetControllers ()
404+ replicaSets , podRSCs , podRSCsConfigMapConnections , err := k .getReplicaSetsAndReplicaSetControllers ()
325405 if err != nil {
326406 return model.KubernetesGatherResource {}, err
327407 }
328408
329409 podGroups = append (podGroups , podRSCs ... )
410+ podGroupConfigMapConnections = append (podGroupConfigMapConnections , podRSCsConfigMapConnections ... )
330411
331412 podServices , servicePorts , podGroupPorts , serviceNetworks , serviceSubnets , serviceVinterfaces , serviceIPs , err := k .getPodServices ()
332413 if err != nil {
@@ -354,33 +435,35 @@ func (k *KubernetesGather) GetKubernetesGatherData() (model.KubernetesGatherReso
354435 }
355436
356437 resource := model.KubernetesGatherResource {
357- Region : region ,
358- AZ : az ,
359- VPC : vpc ,
360- PodNodes : podNodes ,
361- PodCluster : podCluster ,
362- PodServices : podServices ,
363- PodNamespaces : podNamespaces ,
364- PodNetwork : podNetwork ,
365- PodSubnets : podSubnets ,
366- PodVInterfaces : podVInterfaces ,
367- PodIPs : podIPs ,
368- PodNodeNetwork : nodeNetwork ,
369- PodNodeSubnets : nodeSubnets ,
370- PodNodeVInterfaces : nodeVInterfaces ,
371- PodNodeIPs : nodeIPs ,
372- PodServiceNetwork : serviceNetworks ,
373- PodServiceSubnets : serviceSubnets ,
374- PodServiceVInterfaces : serviceVinterfaces ,
375- PodServiceIPs : serviceIPs ,
376- PodServicePorts : servicePorts ,
377- PodGroupPorts : podGroupPorts ,
378- PodIngresses : ingresses ,
379- PodIngressRules : ingressRules ,
380- PodIngressRuleBackends : ingressRuleBackends ,
381- PodReplicaSets : replicaSets ,
382- PodGroups : podGroups ,
383- Pods : pods ,
438+ Region : region ,
439+ AZ : az ,
440+ VPC : vpc ,
441+ PodNodes : podNodes ,
442+ PodCluster : podCluster ,
443+ PodServices : podServices ,
444+ PodNamespaces : podNamespaces ,
445+ PodNetwork : podNetwork ,
446+ PodSubnets : podSubnets ,
447+ PodVInterfaces : podVInterfaces ,
448+ PodIPs : podIPs ,
449+ PodNodeNetwork : nodeNetwork ,
450+ PodNodeSubnets : nodeSubnets ,
451+ PodNodeVInterfaces : nodeVInterfaces ,
452+ PodNodeIPs : nodeIPs ,
453+ PodServiceNetwork : serviceNetworks ,
454+ PodServiceSubnets : serviceSubnets ,
455+ PodServiceVInterfaces : serviceVinterfaces ,
456+ PodServiceIPs : serviceIPs ,
457+ PodServicePorts : servicePorts ,
458+ PodGroupPorts : podGroupPorts ,
459+ PodGroupConfigMapConnections : podGroupConfigMapConnections ,
460+ PodIngresses : ingresses ,
461+ PodIngressRules : ingressRules ,
462+ PodIngressRuleBackends : ingressRuleBackends ,
463+ PodReplicaSets : replicaSets ,
464+ PodGroups : podGroups ,
465+ ConfigMaps : configMaps ,
466+ Pods : pods ,
384467 }
385468
386469 k .cloudStatsd .ResCount = statsd .GetResCount (resource )
0 commit comments