Files
kubescape/core/pkg/resourcehandler/queryableresource.go
Amir Malka 0c019819ff Scanning a single resource (#1300)
* add cmd

Signed-off-by: Daniel Grunberger <danielgrunberger@armosec.io>

* support single workload scan

Signed-off-by: Amir Malka <amirm@armosec.io>

* fix conflict

Signed-off-by: Amir Malka <amirm@armosec.io>

* added unit tests

Signed-off-by: Amir Malka <amirm@armosec.io>

* added unit tests

Signed-off-by: Amir Malka <amirm@armosec.io>

* more refactoring

Signed-off-by: Amir Malka <amirm@armosec.io>

* add scanned workload reference to opasessionobj

Signed-off-by: Amir Malka <amirm@armosec.io>

* fix GetWorkloadParentKind

Signed-off-by: Amir Malka <amirm@armosec.io>

* remove namespace argument from pullSingleResource, using field selector instead

Signed-off-by: Amir Malka <amirm@armosec.io>

* removed designators (unused) field from PolicyIdentifier, and designators argument from GetResources function

Signed-off-by: Amir Malka <amirm@armosec.io>

* fix tests

Signed-off-by: Amir Malka <amirm@armosec.io>

* use ScanObject instead of workload identifier

Signed-off-by: Amir Malka <amirm@armosec.io>

* refactor logic after CR

Signed-off-by: Amir Malka <amirm@armosec.io>

---------

Signed-off-by: Daniel Grunberger <danielgrunberger@armosec.io>
Signed-off-by: Amir Malka <amirm@armosec.io>
Co-authored-by: Daniel Grunberger <danielgrunberger@armosec.io>
2023-08-01 14:07:31 +03:00

57 lines
1.4 KiB
Go

package resourcehandler
import (
"fmt"
"github.com/kubescape/kubescape/v2/core/cautils"
)
type QueryableResources map[string]QueryableResource
// QueryableResource is a struct that holds a representation of a resource we would like to query (from the K8S API, or from other sources)
type QueryableResource struct {
// <api group/api version/resource>
GroupVersionResourceTriplet string
// metadata.name==<resource name>, metadata.namespace==<resource namespace> etc.
FieldSelectors string
}
func (qr *QueryableResource) String() string {
if qr.FieldSelectors == "" {
return qr.GroupVersionResourceTriplet
}
return fmt.Sprintf("%s/%s", qr.GroupVersionResourceTriplet, qr.FieldSelectors)
}
func (qr *QueryableResource) Copy() QueryableResource {
return QueryableResource{
GroupVersionResourceTriplet: qr.GroupVersionResourceTriplet,
FieldSelectors: qr.FieldSelectors,
}
}
func (qr *QueryableResource) AddFieldSelector(fieldSelector string) {
if fieldSelector == "" {
return
}
if qr.FieldSelectors == "" {
qr.FieldSelectors = fieldSelector
return
}
qr.FieldSelectors = combineFieldSelectors(qr.FieldSelectors, fieldSelector)
}
func (qrm QueryableResources) ToK8sResourceMap() cautils.K8SResources {
resources := make(cautils.K8SResources)
for _, qr := range qrm {
resources[qr.GroupVersionResourceTriplet] = nil
}
return resources
}
func (qrm QueryableResources) Add(qr QueryableResource) {
qrm[qr.String()] = qr
}