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

Add NodeCount to telemetry data #5147

Merged
merged 4 commits into from
Feb 22, 2024
Merged
Changes from 3 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
6 changes: 6 additions & 0 deletions deployments/rbac/rbac.yaml
Original file line number Diff line number Diff line change
@@ -54,6 +54,12 @@ rules:
- get
- list
- watch
- apiGroups:
- ""
resources:
- nodes
verbs:
- list
- apiGroups:
- ""
resources:
17 changes: 17 additions & 0 deletions internal/telemetry/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package telemetry

import (
"context"

metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// NodeCount returns the total number of nodes in the cluster.
// It returns an error if the underlying k8s API client errors.
func (c *Collector) NodeCount(ctx context.Context) (int, error) {
nodes, err := c.Config.K8sClientReader.CoreV1().Nodes().List(ctx, metaV1.ListOptions{})
if err != nil {
return 0, err
}
return len(nodes.Items), nil
}
94 changes: 94 additions & 0 deletions internal/telemetry/cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package telemetry_test

import (
"context"
"testing"

"github.com/nginxinc/kubernetes-ingress/internal/telemetry"
apiCoreV1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
testClient "k8s.io/client-go/kubernetes/fake"
)

func TestNodeCountInAClusterWithThreeNodes(t *testing.T) {
t.Parallel()

c := newTestCollectorForCluserWithNodes(t, node1, node2, node3)

got, err := c.NodeCount(context.Background())
if err != nil {
t.Fatal(err)
}
want := 3
if want != got {
t.Errorf("want %v, got %v", want, got)
}
}

func TestNodeCountInAClusterWithOneNode(t *testing.T) {
t.Parallel()

c := newTestCollectorForCluserWithNodes(t, node1)
got, err := c.NodeCount(context.Background())
if err != nil {
t.Fatal(err)
}
want := 1
if want != got {
t.Errorf("want %v, got %v", want, got)
}
}

// newTestCollectorForClusterWithNodes returns a telemetry collector configured
// to simulate collecting data on a cluser with provided nodes.
func newTestCollectorForCluserWithNodes(t *testing.T, nodes ...runtime.Object) *telemetry.Collector {
t.Helper()

c, err := telemetry.NewCollector(
telemetry.CollectorConfig{},
)
if err != nil {
t.Fatal(err)
}
c.Config.K8sClientReader = testClient.NewSimpleClientset(nodes...)
return c
}

var (
node1 = &apiCoreV1.Node{
TypeMeta: metaV1.TypeMeta{
Kind: "Node",
APIVersion: "v1",
},
ObjectMeta: metaV1.ObjectMeta{
Name: "test-node-1",
Namespace: "default",
},
Spec: apiCoreV1.NodeSpec{},
}

node2 = &apiCoreV1.Node{
TypeMeta: metaV1.TypeMeta{
Kind: "Node",
APIVersion: "v1",
},
ObjectMeta: metaV1.ObjectMeta{
Name: "test-node-2",
Namespace: "default",
},
Spec: apiCoreV1.NodeSpec{},
}

node3 = &apiCoreV1.Node{
TypeMeta: metaV1.TypeMeta{
Kind: "Node",
APIVersion: "v1",
},
ObjectMeta: metaV1.ObjectMeta{
Name: "test-node-3",
Namespace: "default",
},
Spec: apiCoreV1.NodeSpec{},
}
)
9 changes: 7 additions & 2 deletions internal/telemetry/collector.go
Original file line number Diff line number Diff line change
@@ -80,7 +80,7 @@ func (c *Collector) Start(ctx context.Context) {
func (c *Collector) Collect(ctx context.Context) {
glog.V(3).Info("Collecting telemetry data")
// TODO: Re-add ctx to BuildReport when collecting Node Count.
data, err := c.BuildReport()
data, err := c.BuildReport(ctx)
if err != nil {
glog.Errorf("Error collecting telemetry data: %v", err)
}
@@ -92,13 +92,18 @@ func (c *Collector) Collect(ctx context.Context) {
}

// BuildReport takes context and builds report from gathered telemetry data.
func (c *Collector) BuildReport() (Data, error) {
func (c *Collector) BuildReport(ctx context.Context) (Data, error) {
d := Data{}
var err error

if c.Config.Configurator != nil {
d.NICResourceCounts.VirtualServers, d.NICResourceCounts.VirtualServerRoutes = c.Config.Configurator.GetVirtualServerCounts()
d.NICResourceCounts.TransportServers = c.Config.Configurator.GetTransportServerCounts()
}
nc, err := c.NodeCount(ctx)
if err != nil {
glog.Errorf("Error collecting telemetry data: Nodes: %v", err)
}
d.NodeCount = nc
return d, err
}
88 changes: 81 additions & 7 deletions internal/telemetry/collector_test.go
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ import (
"github.com/nginxinc/kubernetes-ingress/internal/telemetry"
conf_v1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
testClient "k8s.io/client-go/kubernetes/fake"
)

func TestCreateNewCollectorWithCustomReportingPeriod(t *testing.T) {
@@ -46,7 +47,8 @@ func TestCreateNewCollectorWithCustomExporter(t *testing.T) {
td := telemetry.Data{}

cfg := telemetry.CollectorConfig{
Configurator: newConfigurator(t),
K8sClientReader: testClient.NewSimpleClientset(),
Configurator: newConfigurator(t),
}

c, err := telemetry.NewCollector(cfg, telemetry.WithExporter(exp))
@@ -62,6 +64,76 @@ func TestCreateNewCollectorWithCustomExporter(t *testing.T) {
}
}

func TestCollectNodeCountInClusterWithOneNode(t *testing.T) {
t.Parallel()

buf := &bytes.Buffer{}
exp := &telemetry.StdoutExporter{Endpoint: buf}
cfg := telemetry.CollectorConfig{
Configurator: newConfigurator(t),
K8sClientReader: testClient.NewSimpleClientset(node1),
}

c, err := telemetry.NewCollector(cfg, telemetry.WithExporter(exp))
if err != nil {
t.Fatal(err)
}
c.Collect(context.Background())

td := telemetry.Data{
ProjectMeta: telemetry.ProjectMeta{
Name: "",
Version: "",
},
NICResourceCounts: telemetry.NICResourceCounts{
VirtualServers: 0,
VirtualServerRoutes: 0,
TransportServers: 0,
},
NodeCount: 1,
}
want := fmt.Sprintf("%+v", td)
got := buf.String()
if !cmp.Equal(want, got) {
t.Error(cmp.Diff(want, got))
}
}

func TestCollectNodeCountInClusterWithThreeNodes(t *testing.T) {
t.Parallel()

buf := &bytes.Buffer{}
exp := &telemetry.StdoutExporter{Endpoint: buf}
cfg := telemetry.CollectorConfig{
Configurator: newConfigurator(t),
K8sClientReader: testClient.NewSimpleClientset(node1, node2, node3),
}

c, err := telemetry.NewCollector(cfg, telemetry.WithExporter(exp))
if err != nil {
t.Fatal(err)
}
c.Collect(context.Background())

td := telemetry.Data{
ProjectMeta: telemetry.ProjectMeta{
Name: "",
Version: "",
},
NICResourceCounts: telemetry.NICResourceCounts{
VirtualServers: 0,
VirtualServerRoutes: 0,
TransportServers: 0,
},
NodeCount: 3,
}
want := fmt.Sprintf("%+v", td)
got := buf.String()
if !cmp.Equal(want, got) {
t.Error(cmp.Diff(want, got))
}
}

func TestCountVirtualServers(t *testing.T) {
t.Parallel()

@@ -171,7 +243,8 @@ func TestCountVirtualServers(t *testing.T) {
configurator := newConfigurator(t)

c, err := telemetry.NewCollector(telemetry.CollectorConfig{
Configurator: configurator,
K8sClientReader: testClient.NewSimpleClientset(),
Configurator: configurator,
})
if err != nil {
t.Fatal(err)
@@ -184,7 +257,7 @@ func TestCountVirtualServers(t *testing.T) {
}
}

gotTraceDataOnAdd, err := c.BuildReport()
gotTraceDataOnAdd, err := c.BuildReport(context.Background())
if err != nil {
t.Fatal(err)
}
@@ -202,7 +275,7 @@ func TestCountVirtualServers(t *testing.T) {
}
}

gotTraceDataOnDelete, err := c.BuildReport()
gotTraceDataOnDelete, err := c.BuildReport(context.Background())
if err != nil {
t.Fatal(err)
}
@@ -342,7 +415,8 @@ func TestCountTransportServers(t *testing.T) {
configurator := newConfigurator(t)

c, err := telemetry.NewCollector(telemetry.CollectorConfig{
Configurator: configurator,
K8sClientReader: testClient.NewSimpleClientset(),
Configurator: configurator,
})
if err != nil {
t.Fatal(err)
@@ -355,7 +429,7 @@ func TestCountTransportServers(t *testing.T) {
}
}

gotTraceDataOnAdd, err := c.BuildReport()
gotTraceDataOnAdd, err := c.BuildReport(context.Background())
if err != nil {
t.Fatal(err)
}
@@ -373,7 +447,7 @@ func TestCountTransportServers(t *testing.T) {
}
}

gotTraceDataOnDelete, err := c.BuildReport()
gotTraceDataOnDelete, err := c.BuildReport(context.Background())
if err != nil {
t.Fatal(err)
}
1 change: 1 addition & 0 deletions internal/telemetry/exporter.go
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ func (e *StdoutExporter) Export(_ context.Context, data Data) error {
type Data struct {
ProjectMeta ProjectMeta
NICResourceCounts NICResourceCounts
NodeCount int
}

// ProjectMeta holds metadata for the project.