Skip to content

Commit

Permalink
Add ClusterID to telemetry data (#5155)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjngx committed Feb 23, 2024
1 parent c154334 commit 19671a5
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 5 deletions.
10 changes: 10 additions & 0 deletions internal/telemetry/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ func (c *Collector) NodeCount(ctx context.Context) (int, error) {
}
return len(nodes.Items), nil
}

// ClusterID returns the UID of the kube-system namespace representing cluster id.
// It returns an error if the underlying k8s API client errors.
func (c *Collector) ClusterID(ctx context.Context) (string, error) {
cluster, err := c.Config.K8sClientReader.CoreV1().Namespaces().Get(ctx, "kube-system", metaV1.GetOptions{})
if err != nil {
return "", err
}
return string(cluster.UID), nil
}
56 changes: 53 additions & 3 deletions internal/telemetry/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestNodeCountInAClusterWithThreeNodes(t *testing.T) {
t.Parallel()

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

got, err := c.NodeCount(context.Background())
if err != nil {
Expand All @@ -29,7 +29,7 @@ func TestNodeCountInAClusterWithThreeNodes(t *testing.T) {
func TestNodeCountInAClusterWithOneNode(t *testing.T) {
t.Parallel()

c := newTestCollectorForCluserWithNodes(t, node1)
c := newTestCollectorForClusterWithNodes(t, node1)
got, err := c.NodeCount(context.Background())
if err != nil {
t.Fatal(err)
Expand All @@ -40,9 +40,35 @@ func TestNodeCountInAClusterWithOneNode(t *testing.T) {
}
}

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

c := newTestCollectorForClusterWithNodes(t, node1, kubeNS)

got, err := c.ClusterID(context.Background())
if err != nil {
t.Fatal(err)
}

want := "329766ff-5d78-4c9e-8736-7faad1f2e937"
if want != got {
t.Errorf("want %v, got %v", want, got)
}
}

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

c := newTestCollectorForClusterWithNodes(t, node1)
_, err := c.ClusterID(context.Background())
if err == nil {
t.Error("want error, got nil")
}
}

// 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 {
func newTestCollectorForClusterWithNodes(t *testing.T, nodes ...runtime.Object) *telemetry.Collector {
t.Helper()

c, err := telemetry.NewCollector(
Expand Down Expand Up @@ -91,4 +117,28 @@ var (
},
Spec: apiCoreV1.NodeSpec{},
}

kubeNS = &apiCoreV1.Namespace{
TypeMeta: metaV1.TypeMeta{
Kind: "Namespace",
APIVersion: "v1",
},
ObjectMeta: metaV1.ObjectMeta{
Name: "kube-system",
UID: "329766ff-5d78-4c9e-8736-7faad1f2e937",
},
Spec: apiCoreV1.NamespaceSpec{},
}

dummyKubeNS = &apiCoreV1.Namespace{
TypeMeta: metaV1.TypeMeta{
Kind: "Namespace",
APIVersion: "v1",
},
ObjectMeta: metaV1.ObjectMeta{
Name: "kube-system",
UID: "",
},
Spec: apiCoreV1.NamespaceSpec{},
}
)
6 changes: 6 additions & 0 deletions internal/telemetry/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,11 @@ func (c *Collector) BuildReport(ctx context.Context) (Data, error) {
glog.Errorf("Error collecting telemetry data: Nodes: %v", err)
}
d.NodeCount = nc

cID, err := c.ClusterID(ctx)
if err != nil {
glog.Errorf("Error collecting telemetry data: ClusterID: %v", err)
}
d.ClusterID = cID
return d, err
}
40 changes: 38 additions & 2 deletions internal/telemetry/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,42 @@ func TestCollectNodeCountInClusterWithThreeNodes(t *testing.T) {
}
}

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

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

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,
ClusterID: "329766ff-5d78-4c9e-8736-7faad1f2e937",
}
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()

Expand Down Expand Up @@ -243,7 +279,7 @@ func TestCountVirtualServers(t *testing.T) {
configurator := newConfigurator(t)

c, err := telemetry.NewCollector(telemetry.CollectorConfig{
K8sClientReader: testClient.NewSimpleClientset(),
K8sClientReader: testClient.NewSimpleClientset(dummyKubeNS),
Configurator: configurator,
})
if err != nil {
Expand Down Expand Up @@ -415,7 +451,7 @@ func TestCountTransportServers(t *testing.T) {
configurator := newConfigurator(t)

c, err := telemetry.NewCollector(telemetry.CollectorConfig{
K8sClientReader: testClient.NewSimpleClientset(),
K8sClientReader: testClient.NewSimpleClientset(dummyKubeNS),
Configurator: configurator,
})
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/telemetry/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Data struct {
ProjectMeta ProjectMeta
NICResourceCounts NICResourceCounts
NodeCount int
ClusterID string
}

// ProjectMeta holds metadata for the project.
Expand Down

0 comments on commit 19671a5

Please sign in to comment.