Skip to content

Commit

Permalink
Merge pull request #234 from anchore/custom-annotations-labels
Browse files Browse the repository at this point in the history
feat: custom annotations/labels
  • Loading branch information
bradleyjones authored Jun 5, 2024
2 parents de9bf7b + d57f327 commit 30c1070
Show file tree
Hide file tree
Showing 10 changed files with 462 additions and 76 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,16 +419,16 @@ Include only a subset of annotations/labels for each resource type or disable me
```yaml
metadata-collection:
nodes:
include-annotations: [] # List of annotations to include (explicit or regex)
include-labels: [] # List of labels to include (explicit or regex)
annotations: [] # List of annotations to include (explicit or regex)
labels: [] # List of labels to include (explicit or regex)
disable: false # Remove all optional node metadata from the inventory report
namespaces:
include-annotations: [] # List of annotations to include (explicit or regex)
include-labels: [] # List of labels to include (explicit or regex)
annotations: [] # List of annotations to include (explicit or regex)
labels: [] # List of labels to include (explicit or regex)
disable: false # Remove all optional namespace metadata from the inventory report
pods:
include-annotations: [] # List of annotations to include (explicit or regex)
include-labels: [] # List of labels to include (explicit or regex)
annotations: [] # List of annotations to include (explicit or regex)
labels: [] # List of labels to include (explicit or regex)
disable: false # Remove all optional pod metadata from the inventory report
```

Expand Down
9 changes: 6 additions & 3 deletions pkg/inventory/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func FetchNamespaces(
c client.Client,
batchSize, timeout int64,
excludes, includes []string,
// annotations, labels []string,
includeAnnotations, includeLabels []string,
disableMetadata bool,
) ([]Namespace, error) {
defer tracker.TrackFunctionTime(time.Now(), "Fetching namespaces")
Expand All @@ -98,11 +98,14 @@ func FetchNamespaces(
for _, n := range list.Items {
if !excludeNamespace(exclusionChecklist, n.ObjectMeta.Name) {
if !disableMetadata {
annotations := processAnnotationsOrLabels(n.Annotations, includeAnnotations)
labels := processAnnotationsOrLabels(n.Labels, includeLabels)

nsMap[n.ObjectMeta.Name] = Namespace{
Name: n.ObjectMeta.Name,
UID: string(n.UID),
Annotations: n.Annotations,
Labels: n.Labels,
Annotations: annotations,
Labels: labels,
}
} else {
nsMap[n.ObjectMeta.Name] = Namespace{
Expand Down
185 changes: 144 additions & 41 deletions pkg/inventory/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (

func Test_fetchNamespaces(t *testing.T) {
type args struct {
c client.Client
batchSize int64
timeout int64
excludes []string
includes []string
disableMetadata bool
c client.Client
batchSize int64
timeout int64
excludes []string
includes []string
includeAnnotations []string
includeLabels []string
disableMetadata bool
}
tests := []struct {
name string
Expand All @@ -43,11 +45,13 @@ func Test_fetchNamespaces(t *testing.T) {
},
}),
},
batchSize: 100,
timeout: 10,
excludes: []string{},
includes: []string{},
disableMetadata: false,
batchSize: 100,
timeout: 10,
excludes: []string{},
includes: []string{},
includeAnnotations: []string{},
includeLabels: []string{},
disableMetadata: false,
},
want: []Namespace{
{
Expand All @@ -64,11 +68,13 @@ func Test_fetchNamespaces(t *testing.T) {
c: client.Client{
Clientset: fake.NewSimpleClientset(),
},
batchSize: 100,
timeout: 10,
excludes: []string{},
includes: []string{},
disableMetadata: false,
batchSize: 100,
timeout: 10,
excludes: []string{},
includes: []string{},
includeAnnotations: []string{},
includeLabels: []string{},
disableMetadata: false,
},
want: nil,
},
Expand Down Expand Up @@ -102,11 +108,13 @@ func Test_fetchNamespaces(t *testing.T) {
},
}),
},
batchSize: 100,
timeout: 10,
excludes: []string{"excluded-namespace"},
includes: []string{},
disableMetadata: false,
batchSize: 100,
timeout: 10,
excludes: []string{"excluded-namespace"},
includes: []string{},
includeAnnotations: []string{},
includeLabels: []string{},
disableMetadata: false,
},
want: []Namespace{
{
Expand Down Expand Up @@ -159,11 +167,13 @@ func Test_fetchNamespaces(t *testing.T) {
},
}),
},
batchSize: 100,
timeout: 10,
excludes: []string{"excluded.*"},
includes: []string{},
disableMetadata: false,
batchSize: 100,
timeout: 10,
excludes: []string{"excluded.*"},
includes: []string{},
includeAnnotations: []string{},
includeLabels: []string{},
disableMetadata: false,
},
want: []Namespace{
{
Expand Down Expand Up @@ -216,11 +226,13 @@ func Test_fetchNamespaces(t *testing.T) {
},
}),
},
batchSize: 100,
timeout: 10,
excludes: []string{"exclude.*"},
includes: []string{"test-namespace"},
disableMetadata: false,
batchSize: 100,
timeout: 10,
excludes: []string{"exclude.*"},
includes: []string{"test-namespace"},
includeAnnotations: []string{},
includeLabels: []string{},
disableMetadata: false,
},
want: []Namespace{
{
Expand Down Expand Up @@ -273,11 +285,13 @@ func Test_fetchNamespaces(t *testing.T) {
},
}),
},
batchSize: 100,
timeout: 10,
excludes: []string{},
includes: []string{"test-namespace"},
disableMetadata: false,
batchSize: 100,
timeout: 10,
excludes: []string{},
includes: []string{"test-namespace"},
includeAnnotations: []string{},
includeLabels: []string{},
disableMetadata: false,
},
want: []Namespace{
{
Expand Down Expand Up @@ -330,11 +344,13 @@ func Test_fetchNamespaces(t *testing.T) {
},
}),
},
batchSize: 100,
timeout: 10,
excludes: []string{},
includes: []string{"test-namespace"},
disableMetadata: true,
batchSize: 100,
timeout: 10,
excludes: []string{},
includes: []string{"test-namespace"},
includeAnnotations: []string{},
includeLabels: []string{},
disableMetadata: true,
},
want: []Namespace{
{
Expand All @@ -343,6 +359,91 @@ func Test_fetchNamespaces(t *testing.T) {
},
},
},
{
name: "only includes specified namespace annotations and labels",
args: args{
c: client.Client{
Clientset: fake.NewSimpleClientset(
&v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-namespace",
UID: "test-uid",
Annotations: map[string]string{
"test-annotation": "test-value",
"test-annotation2": "test-value2",
"do-not-include": "do-not-include",
},
Labels: map[string]string{
"test-label": "test-value",
"do-not-include": "do-not-include",
},
},
}),
},
batchSize: 100,
timeout: 10,
excludes: []string{},
includes: []string{"test-namespace"},
includeAnnotations: []string{"test-annotation", "test-annotation2"},
includeLabels: []string{"test-label"},
disableMetadata: false,
},
want: []Namespace{
{
Name: "test-namespace",
UID: "test-uid",
Annotations: map[string]string{
"test-annotation": "test-value",
"test-annotation2": "test-value2",
},
Labels: map[string]string{
"test-label": "test-value",
},
},
},
},
{
name: "only includes specified namespace annotations and labels (regex)",
args: args{
c: client.Client{
Clientset: fake.NewSimpleClientset(
&v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-namespace",
UID: "test-uid",
Annotations: map[string]string{
"test-annotation": "test-value",
"test-annotation2": "test-value2",
"do-not-include": "do-not-include",
},
Labels: map[string]string{
"test-label": "test-value",
"do-not-include": "do-not-include",
},
},
}),
},
batchSize: 100,
timeout: 10,
excludes: []string{},
includes: []string{},
includeAnnotations: []string{".*-not-.*"},
includeLabels: []string{".*-not-.*"},
disableMetadata: false,
},
want: []Namespace{
{
Name: "test-namespace",
UID: "test-uid",
Annotations: map[string]string{
"do-not-include": "do-not-include",
},
Labels: map[string]string{
"do-not-include": "do-not-include",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -352,6 +453,8 @@ func Test_fetchNamespaces(t *testing.T) {
tt.args.timeout,
tt.args.excludes,
tt.args.includes,
tt.args.includeAnnotations,
tt.args.includeLabels,
tt.args.disableMetadata,
)
if (err != nil) != tt.wantErr {
Expand Down
8 changes: 5 additions & 3 deletions pkg/inventory/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func FetchNodes(c client.Client, batchSize, timeout int64, disableMetadata bool) (map[string]Node, error) {
func FetchNodes(c client.Client, batchSize, timeout int64, includeAnnotations, includeLabels []string, disableMetadata bool) (map[string]Node, error) {
nodes := make(map[string]Node)

cont := ""
Expand All @@ -32,16 +32,18 @@ func FetchNodes(c client.Client, batchSize, timeout int64, disableMetadata bool)

for _, n := range list.Items {
if !disableMetadata {
annotations := processAnnotationsOrLabels(n.Annotations, includeAnnotations)
labels := processAnnotationsOrLabels(n.Labels, includeLabels)
nodes[n.ObjectMeta.Name] = Node{
Name: n.ObjectMeta.Name,
UID: string(n.UID),
Annotations: n.Annotations,
Annotations: annotations,
Arch: n.Status.NodeInfo.Architecture,
ContainerRuntimeVersion: n.Status.NodeInfo.ContainerRuntimeVersion,
KernelVersion: n.Status.NodeInfo.KernelVersion,
KubeProxyVersion: n.Status.NodeInfo.KubeProxyVersion,
KubeletVersion: n.Status.NodeInfo.KubeletVersion,
Labels: n.Labels,
Labels: labels,
OperatingSystem: n.Status.NodeInfo.OperatingSystem,
}
} else {
Expand Down
Loading

0 comments on commit 30c1070

Please sign in to comment.