diff --git a/core/cautils/datastructures.go b/core/cautils/datastructures.go index a01dc898..f164dada 100644 --- a/core/cautils/datastructures.go +++ b/core/cautils/datastructures.go @@ -29,6 +29,23 @@ type OPASessionObj struct { SessionID string // SessionID } +func (sessionObj *OPASessionObj) SetMapNamespaceToNumberOfResources(mapNamespaceToNumberOfResources map[string]int) { + if sessionObj.Metadata.ContextMetadata.ClusterContextMetadata == nil { + sessionObj.Metadata.ContextMetadata.ClusterContextMetadata = &reporthandlingv2.ClusterMetadata{} + } + if sessionObj.Metadata.ContextMetadata.ClusterContextMetadata.MapNamespaceToNumberOfResources == nil { + sessionObj.Metadata.ContextMetadata.ClusterContextMetadata.MapNamespaceToNumberOfResources = make(map[string]int) + } + sessionObj.Metadata.ContextMetadata.ClusterContextMetadata.MapNamespaceToNumberOfResources = mapNamespaceToNumberOfResources +} + +func (sessionObj *OPASessionObj) SetNumberOfWorkerNodes(n int) { + if sessionObj.Metadata.ContextMetadata.ClusterContextMetadata == nil { + sessionObj.Metadata.ContextMetadata.ClusterContextMetadata = &reporthandlingv2.ClusterMetadata{} + } + sessionObj.Metadata.ContextMetadata.ClusterContextMetadata.NumberOfWorkerNodes = n +} + func NewOPASessionObj(frameworks []reporthandling.Framework, k8sResources *K8SResources, scanInfo *ScanInfo) *OPASessionObj { return &OPASessionObj{ Report: &reporthandlingv2.PostureReport{}, diff --git a/core/pkg/resourcehandler/k8sresources.go b/core/pkg/resourcehandler/k8sresources.go index 49074bb5..6fb6671c 100644 --- a/core/pkg/resourcehandler/k8sresources.go +++ b/core/pkg/resourcehandler/k8sresources.go @@ -76,9 +76,7 @@ func (k8sHandler *K8sResourceHandler) GetResources(sessionObj *cautils.OPASessio if err != nil { logger.L().Debug("failed to collect worker nodes number", helpers.Error(err)) } else { - if sessionObj.Metadata != nil && sessionObj.Metadata.ContextMetadata.ClusterContextMetadata != nil { - sessionObj.Metadata.ContextMetadata.ClusterContextMetadata.NumberOfWorkerNodes = numberOfWorkerNodes - } + sessionObj.SetNumberOfWorkerNodes(numberOfWorkerNodes) } imgVulnResources := cautils.MapImageVulnResources(ksResourceMap) @@ -152,10 +150,7 @@ func (k8sHandler *K8sResourceHandler) GetClusterAPIServerInfo() *version.Info { // set namespaceToNumOfResources map in report func setMapNamespaceToNumOfResources(allResources map[string]workloadinterface.IMetadata, sessionObj *cautils.OPASessionObj) { - - if sessionObj.Metadata.ContextMetadata.ClusterContextMetadata.MapNamespaceToNumberOfResources == nil { - sessionObj.Metadata.ContextMetadata.ClusterContextMetadata.MapNamespaceToNumberOfResources = make(map[string]int) - } + mapNamespaceToNumberOfResources := make(map[string]int) for _, resource := range allResources { if obj := workloadinterface.NewWorkloadObj(resource.GetObject()); obj != nil { ownerReferences, err := obj.GetOwnerReferences() @@ -164,7 +159,7 @@ func setMapNamespaceToNumOfResources(allResources map[string]workloadinterface.I if len(ownerReferences) == 0 { if ns := resource.GetNamespace(); ns != "" { if obj.GetKind() != "Job" { - sessionObj.Metadata.ContextMetadata.ClusterContextMetadata.MapNamespaceToNumberOfResources[ns]++ + mapNamespaceToNumberOfResources[ns]++ } } } @@ -173,6 +168,7 @@ func setMapNamespaceToNumOfResources(allResources map[string]workloadinterface.I } } } + sessionObj.SetMapNamespaceToNumberOfResources(mapNamespaceToNumberOfResources) } func (k8sHandler *K8sResourceHandler) pullResources(k8sResources *cautils.K8SResources, allResources map[string]workloadinterface.IMetadata, namespace string, labels map[string]string) error {