Skip to content

Commit

Permalink
Merge pull request #92 from anchore/remove-metadata-config
Browse files Browse the repository at this point in the history
feat: remove metadata config option
  • Loading branch information
bradleyjones authored May 9, 2023
2 parents 666e11d + a5d9322 commit 2fc0e98
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 135 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,6 @@ kubeconfig:
# enable/disable printing inventory reports to stdout
verbose-inventory-reports: false
# collect additional metadata about where a container is running (e.g. namespace labels, namespace annotations, etc.) [defaults to true]
metadata: true
```

### Namespace selection
Expand Down
2 changes: 0 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ type Application struct {
PollingIntervalSeconds int `mapstructure:"polling-interval-seconds"`
AnchoreDetails AnchoreInfo `mapstructure:"anchore"`
VerboseInventoryReports bool `mapstructure:"verbose-inventory-reports"`
Metadata bool `mapstructure:"metadata"` // if true, include runtime metadata in the inventory report
}

// MissingTagConf details the policy for handling missing tags when reporting images
Expand Down Expand Up @@ -129,7 +128,6 @@ func setNonCliDefaultValues(v *viper.Viper) {
v.SetDefault("namespaces", []string{})
v.SetDefault("namespace-selectors.include", []string{})
v.SetDefault("namespace-selectors.exclude", []string{})
v.SetDefault("metadata", true)
}

// Load the Application Configuration from the Viper specifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,3 @@ anchoredetails:
insecure: false
timeoutseconds: 10
verboseinventoryreports: false
metadata: true
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,3 @@ anchoredetails:
insecure: false
timeoutseconds: 0
verboseinventoryreports: false
metadata: false
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,3 @@ anchoredetails:
insecure: false
timeoutseconds: 10
verboseinventoryreports: false
metadata: true
18 changes: 5 additions & 13 deletions pkg/inventory/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func FetchNamespaces(
c client.Client,
batchSize, timeout int64,
excludes, includes []string,
metadata bool,
) ([]Namespace, error) {
defer tracker.TrackFunctionTime(time.Now(), "Fetching namespaces")
nsMap := make(map[string]Namespace)
Expand All @@ -96,18 +95,11 @@ func FetchNamespaces(
}
for _, n := range list.Items {
if !excludeNamespace(exclusionChecklist, n.ObjectMeta.Name) {
if metadata {
nsMap[n.ObjectMeta.Name] = Namespace{
Name: n.ObjectMeta.Name,
UID: string(n.UID),
Annotations: n.Annotations,
Labels: n.Labels,
}
} else {
nsMap[n.ObjectMeta.Name] = Namespace{
Name: n.ObjectMeta.Name,
UID: string(n.UID),
}
nsMap[n.ObjectMeta.Name] = Namespace{
Name: n.ObjectMeta.Name,
UID: string(n.UID),
Annotations: n.Annotations,
Labels: n.Labels,
}
}
}
Expand Down
37 changes: 0 additions & 37 deletions pkg/inventory/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func Test_fetchNamespaces(t *testing.T) {
timeout int64
excludes []string
includes []string
metadata bool
}
tests := []struct {
name string
Expand Down Expand Up @@ -47,7 +46,6 @@ func Test_fetchNamespaces(t *testing.T) {
timeout: 10,
excludes: []string{},
includes: []string{},
metadata: true,
},
want: []Namespace{
{
Expand Down Expand Up @@ -105,7 +103,6 @@ func Test_fetchNamespaces(t *testing.T) {
timeout: 10,
excludes: []string{"excluded-namespace"},
includes: []string{},
metadata: true,
},
want: []Namespace{
{
Expand Down Expand Up @@ -162,7 +159,6 @@ func Test_fetchNamespaces(t *testing.T) {
timeout: 10,
excludes: []string{"excluded.*"},
includes: []string{},
metadata: true,
},
want: []Namespace{
{
Expand Down Expand Up @@ -219,7 +215,6 @@ func Test_fetchNamespaces(t *testing.T) {
timeout: 10,
excludes: []string{"exclude.*"},
includes: []string{"test-namespace"},
metadata: true,
},
want: []Namespace{
{
Expand Down Expand Up @@ -276,7 +271,6 @@ func Test_fetchNamespaces(t *testing.T) {
timeout: 10,
excludes: []string{},
includes: []string{"test-namespace"},
metadata: true,
},
want: []Namespace{
{
Expand All @@ -287,36 +281,6 @@ func Test_fetchNamespaces(t *testing.T) {
},
},
},
{
name: "only returns minimal metadata",
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",
},
Labels: map[string]string{
"test-label": "test-value",
},
},
}),
},
batchSize: 100,
timeout: 10,
excludes: []string{},
includes: []string{},
metadata: false,
},
want: []Namespace{
{
Name: "test-namespace",
UID: "test-uid",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -326,7 +290,6 @@ func Test_fetchNamespaces(t *testing.T) {
tt.args.timeout,
tt.args.excludes,
tt.args.includes,
tt.args.metadata,
)
if (err != nil) != tt.wantErr {
assert.Error(t, err)
Expand Down
32 changes: 12 additions & 20 deletions pkg/inventory/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,22 @@ func FetchPodsInNamespace(c client.Client, batchSize, timeout int64, namespace s
return podList, nil
}

func ProcessPods(pods []v1.Pod, namespaceUID string, metadata bool, nodes map[string]Node) []Pod {
func ProcessPods(pods []v1.Pod, namespaceUID string, nodes map[string]Node) []Pod {
var podList []Pod

for _, p := range pods {
if metadata {
pod := Pod{
Name: p.ObjectMeta.Name,
UID: string(p.UID),
Annotations: p.Annotations,
Labels: p.Labels,
NamespaceUID: namespaceUID,
}
node, ok := nodes[p.Spec.NodeName]
if ok {
pod.NodeUID = node.UID
}
podList = append(podList, pod)
} else {
podList = append(podList, Pod{
Name: p.ObjectMeta.Name,
UID: string(p.UID),
NamespaceUID: namespaceUID,
})
pod := Pod{
Name: p.ObjectMeta.Name,
UID: string(p.UID),
Annotations: p.Annotations,
Labels: p.Labels,
NamespaceUID: namespaceUID,
}
node, ok := nodes[p.Spec.NodeName]
if ok {
pod.NodeUID = node.UID
}
podList = append(podList, pod)
}

return podList
Expand Down
36 changes: 1 addition & 35 deletions pkg/inventory/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func TestProcessPods(t *testing.T) {
type args struct {
pods []v1.Pod
namespaceUID string
metadata bool
nodes map[string]Node
}
tests := []struct {
Expand Down Expand Up @@ -105,7 +104,6 @@ func TestProcessPods(t *testing.T) {
},
},
namespaceUID: "namespace-uid-0000",
metadata: true,
nodes: map[string]Node{
"test-node": {
Name: "test-node",
Expand All @@ -128,42 +126,10 @@ func TestProcessPods(t *testing.T) {
},
},
},
{
name: "only return minimal metadata",
args: args{
pods: []v1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
UID: "test-uid",
Annotations: map[string]string{
"test-annotation": "test-value",
},
Labels: map[string]string{
"test-label": "test-value",
},
Namespace: "test-namespace",
},
Spec: v1.PodSpec{
NodeName: "test-node",
},
},
},
namespaceUID: "namespace-uid-0000",
metadata: false,
},
want: []Pod{
{
Name: "test-pod",
UID: "test-uid",
NamespaceUID: "namespace-uid-0000",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ProcessPods(tt.args.pods, tt.args.namespaceUID, tt.args.metadata, tt.args.nodes)
got := ProcessPods(tt.args.pods, tt.args.namespaceUID, tt.args.nodes)
assert.Equal(t, tt.want, got)
})
}
Expand Down
32 changes: 10 additions & 22 deletions pkg/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func GetInventoryReport(cfg *config.Application) (inventory.Report, error) {

namespaces, err := inventory.FetchNamespaces(client,
cfg.Kubernetes.RequestBatchSize, cfg.Kubernetes.RequestTimeoutSeconds,
cfg.NamespaceSelectors.Exclude, cfg.NamespaceSelectors.Include, cfg.Metadata)
cfg.NamespaceSelectors.Exclude, cfg.NamespaceSelectors.Include)
if err != nil {
return inventory.Report{}, err
}
Expand All @@ -152,15 +152,13 @@ func GetInventoryReport(cfg *config.Application) (inventory.Report, error) {
close(queue)

var nodeMap map[string]inventory.Node
if cfg.Metadata {
nodeMap, err = inventory.FetchNodes(
client,
cfg.Kubernetes.RequestBatchSize,
cfg.Kubernetes.RequestTimeoutSeconds,
)
if err != nil {
return inventory.Report{}, err
}
nodeMap, err = inventory.FetchNodes(
client,
cfg.Kubernetes.RequestBatchSize,
cfg.Kubernetes.RequestTimeoutSeconds,
)
if err != nil {
return inventory.Report{}, err
}

launchWorkerPool(cfg, kubeconfig, ch, queue, nodeMap) // get pods/containers from namespaces using a worker pool pattern
Expand Down Expand Up @@ -196,22 +194,12 @@ func GetInventoryReport(cfg *config.Application) (inventory.Report, error) {
}

log.Infof("Got Inventory Report with %d containers running across %d namespaces", len(containers), len(namespaces))
if cfg.Metadata {
return inventory.Report{
Timestamp: time.Now().UTC().Format(time.RFC3339),
Containers: containers,
Pods: pods,
Namespaces: namespaces,
Nodes: nodes,
ServerVersionMetadata: serverVersion,
ClusterName: cfg.KubeConfig.Cluster,
}, nil
}
return inventory.Report{
Timestamp: time.Now().UTC().Format(time.RFC3339),
Containers: containers,
Pods: pods,
Namespaces: namespaces,
Nodes: nodes,
ServerVersionMetadata: serverVersion,
ClusterName: cfg.KubeConfig.Cluster,
}, nil
Expand All @@ -235,7 +223,7 @@ func processNamespace(
return
}

pods := inventory.ProcessPods(v1pods, ns.UID, cfg.Metadata, nodes)
pods := inventory.ProcessPods(v1pods, ns.UID, nodes)
containers := inventory.GetContainersFromPods(
v1pods,
cfg.IgnoreNotRunning,
Expand Down

0 comments on commit 2fc0e98

Please sign in to comment.