-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from anchore/next
feat: add node metadata collection
- Loading branch information
Showing
6 changed files
with
209 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package inventory | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/anchore/k8s-inventory/internal/log" | ||
"github.com/anchore/k8s-inventory/pkg/client" | ||
k8sErrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func FetchNodes(c client.Client, batchSize, timeout int64) (map[string]Node, error) { | ||
nodes := make(map[string]Node) | ||
|
||
cont := "" | ||
for { | ||
opts := metav1.ListOptions{ | ||
Limit: batchSize, | ||
Continue: cont, | ||
TimeoutSeconds: &timeout, | ||
} | ||
|
||
list, err := c.Clientset.CoreV1().Nodes().List(context.Background(), opts) | ||
if err != nil { | ||
if k8sErrors.IsForbidden(err) { | ||
log.Warnf("failed to list nodes: %w", err) | ||
return nil, nil | ||
} | ||
return nil, fmt.Errorf("failed to list nodes: %w", err) | ||
} | ||
|
||
for _, n := range list.Items { | ||
nodes[n.ObjectMeta.Name] = Node{ | ||
Name: n.ObjectMeta.Name, | ||
UID: string(n.UID), | ||
Annotations: n.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, | ||
OperatingSystem: n.Status.NodeInfo.OperatingSystem, | ||
} | ||
} | ||
|
||
cont = list.GetListMeta().GetContinue() | ||
if cont == "" { | ||
break | ||
} | ||
} | ||
|
||
return nodes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package inventory | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/anchore/k8s-inventory/pkg/client" | ||
"github.com/stretchr/testify/assert" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
func TestFetchNodes(t *testing.T) { | ||
type args struct { | ||
c client.Client | ||
batchSize int64 | ||
timeout int64 | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want map[string]Node | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "successfully returns nodes", | ||
args: args{ | ||
c: client.Client{ | ||
Clientset: fake.NewSimpleClientset(&v1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-node", | ||
UID: "test-uid", | ||
Annotations: map[string]string{ | ||
"test-annotation": "test-value", | ||
}, | ||
Labels: map[string]string{ | ||
"test-label": "test-value", | ||
}, | ||
}, | ||
Status: v1.NodeStatus{ | ||
NodeInfo: v1.NodeSystemInfo{ | ||
Architecture: "arm64", | ||
ContainerRuntimeVersion: "docker://20.10.23", | ||
KernelVersion: "5.15.49-linuxkit", | ||
KubeProxyVersion: "v1.26.1", | ||
KubeletVersion: "v1.26.1", | ||
OperatingSystem: "linux", | ||
}, | ||
}, | ||
}), | ||
}, | ||
batchSize: 100, | ||
timeout: 100, | ||
}, | ||
want: map[string]Node{ | ||
"test-node": { | ||
Name: "test-node", | ||
UID: "test-uid", | ||
Annotations: map[string]string{ | ||
"test-annotation": "test-value", | ||
}, | ||
Labels: map[string]string{ | ||
"test-label": "test-value", | ||
}, | ||
Arch: "arm64", | ||
ContainerRuntimeVersion: "docker://20.10.23", | ||
KernelVersion: "5.15.49-linuxkit", | ||
KubeProxyVersion: "v1.26.1", | ||
KubeletVersion: "v1.26.1", | ||
OperatingSystem: "linux", | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := FetchNodes(tt.args.c, tt.args.batchSize, tt.args.timeout) | ||
if (err != nil) != tt.wantErr { | ||
assert.Error(t, err) | ||
} | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters