Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add support for namespace and resource filtering during KBOM generation #135

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/cyclonexdx.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ func transformToCycloneDXBOM(kbom *model.KBOM) *cyclonedx.BOM { //nolint:funlen
Name: RADPrefix + "pkg:digest",
Value: img.Digest,
},
{
Name: RADPrefix + "pkg:namespace",
Value: img.Namespace,
},
},
}

Expand Down
26 changes: 20 additions & 6 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/golang-collections/collections/set"
"io"
"os"
"path"
Expand Down Expand Up @@ -31,10 +32,13 @@ const (
)

var (
short bool
output string
format string
outPath string
short bool
output string
format string
outPath string
namespaceFilter string

resourceFilter string

generatedAt = time.Now()
kbomID = uuid.New().String()
Expand All @@ -51,6 +55,8 @@ func init() {
GenerateCmd.Flags().StringVarP(&output, "output", "o", StdOutput, "Output (stdout, file)")
GenerateCmd.Flags().StringVarP(&format, "format", "f", JSONFormat.Name, fmt.Sprintf("Format (%s)", strings.Join(formatNames(), ", ")))
GenerateCmd.Flags().StringVarP(&outPath, "out-path", "p", ".", "Path to write KBOM file to. Works only with --output=file")
GenerateCmd.Flags().StringVarP(&namespaceFilter, "namespace", "n", "", "Filter only the given namespaces")
GenerateCmd.Flags().StringVarP(&resourceFilter, "resource", "r", "", "Filter only the given resources")

utils.BindFlags(GenerateCmd)
}
Expand All @@ -70,6 +76,9 @@ func generateKBOM(k8sClient kube.K8sClient) error {
return err
}

var kbomFilters model.Filters
kbomFilters = utils.GetFilterValue(namespaceFilter, resourceFilter)

ctx := context.Background()
k8sVersion, caCertDigest, err := k8sClient.Metadata(ctx)
if err != nil {
Expand All @@ -92,12 +101,17 @@ func generateKBOM(k8sClient kube.K8sClient) error {
return err
}

allImages, err := k8sClient.AllImages(ctx)
namespaceFilters := utils.ConvertListToSet(kbomFilters.Namespace)
allImages, err := k8sClient.AllImages(ctx, namespaceFilters)
if err != nil {
return err
}

resources, err := k8sClient.AllResources(ctx, full)
var targetKind *set.Set
if len(kbomFilters.Resources) > 0 {
targetKind = utils.ConvertListToSet(kbomFilters.Resources)
}
resources, err := k8sClient.AllResources(ctx, full, kbomFilters.Namespace, targetKind)
if err != nil {
return err
}
Expand Down
Loading
Loading