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 number of NIC replicas to telemetry data #5245

Merged
merged 3 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 charts/nginx-ingress/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ rules:
- nodes
verbs:
- list
- apiGroups:
- "apps"
resources:
- replicasets
verbs:
- get
- apiGroups:
- networking.k8s.io
resources:
Expand Down
6 changes: 6 additions & 0 deletions deployments/rbac/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ rules:
- get
- list
- watch
- apiGroups:
- "apps"
resources:
- replicasets
verbs:
- get
- apiGroups:
- ""
resources:
Expand Down
13 changes: 9 additions & 4 deletions internal/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"net"
"os"
"strconv"
"strings"
"sync"
Expand All @@ -43,6 +44,7 @@ import (

"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -347,17 +349,20 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc

// NIC Telemetry Reporting
if input.EnableTelemetryReporting {
lbc.telemetryChan = make(chan struct{})

collectorConfig := telemetry.CollectorConfig{
K8sClientReader: input.KubeClient,
CustomK8sClientReader: input.ConfClient,
Period: 5 * time.Second,
Configurator: lbc.configurator,
Version: input.NICVersion,
PodNSName: types.NamespacedName{
Namespace: os.Getenv("POD_NAMESPACE"),
Name: os.Getenv("POD_NAME"),
},
}
lbc.telemetryChan = make(chan struct{})
collector, err := telemetry.NewCollector(
collectorConfig,
)
collector, err := telemetry.NewCollector(collectorConfig)
if err != nil {
glog.Fatalf("failed to initialize telemetry collector: %v", err)
}
Expand Down
21 changes: 21 additions & 0 deletions internal/telemetry/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package telemetry
import (
"context"
"errors"
"fmt"
"strings"

metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -18,6 +19,26 @@ func (c *Collector) NodeCount(ctx context.Context) (int, error) {
return len(nodes.Items), nil
}

// ReplicaCount returns a number of running NIC replicas.
func (c *Collector) ReplicaCount(ctx context.Context) (int, error) {
pod, err := c.Config.K8sClientReader.CoreV1().Pods(c.Config.PodNSName.Namespace).Get(ctx, c.Config.PodNSName.Name, metaV1.GetOptions{})
if err != nil {
return 0, err
}
podRef := pod.GetOwnerReferences()
if len(podRef) != 1 {
return 0, fmt.Errorf("expected pod owner reference to be 1, got %d", len(podRef))
}
if podRef[0].Kind != "ReplicaSet" {
return 0, fmt.Errorf("expected pod owner reference to be ReplicaSet, got %s", pod.OwnerReferences[0].Kind)
}
rs, err := c.Config.K8sClientReader.AppsV1().ReplicaSets(c.Config.PodNSName.Namespace).Get(ctx, podRef[0].Name, metaV1.GetOptions{})
if err != nil {
return 0, err
}
return int(*rs.Spec.Replicas), 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) {
Expand Down
94 changes: 93 additions & 1 deletion internal/telemetry/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"testing"

"github.com/nginxinc/kubernetes-ingress/internal/telemetry"
appsV1 "k8s.io/api/apps/v1"
apiCoreV1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
)

func TestNodeCountInAClusterWithThreeNodes(t *testing.T) {
Expand Down Expand Up @@ -350,6 +352,23 @@ func TestPlatformLookupOnMalformedPartialPlatformIDField(t *testing.T) {
}
}

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

c := newTestCollectorForClusterWithNodes(t, node1, pod, replica)

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

want := 1

if want != got {
t.Errorf("want %d, got %d", want, got)
}
}

// newTestCollectorForClusterWithNodes returns a telemetry collector configured
// to simulate collecting data on a cluser with provided nodes.
func newTestCollectorForClusterWithNodes(t *testing.T, nodes ...runtime.Object) *telemetry.Collector {
Expand All @@ -362,9 +381,82 @@ func newTestCollectorForClusterWithNodes(t *testing.T, nodes ...runtime.Object)
t.Fatal(err)
}
c.Config.K8sClientReader = newTestClientset(nodes...)
c.Config.PodNSName = types.NamespacedName{
Namespace: "nginx-ingress",
Name: "nginx-ingress",
}
return c
}

// Pod and ReplicaSet for testing NIC replica sets.
var (
pod = &apiCoreV1.Pod{
TypeMeta: metaV1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metaV1.ObjectMeta{
Name: "nginx-ingress",
Namespace: "nginx-ingress",
OwnerReferences: []metaV1.OwnerReference{
{
Kind: "ReplicaSet",
Name: "nginx-ingress",
},
},
Labels: map[string]string{
"app": "nginx-ingress",
"app.kubernetes.io/name": "nginx-ingress",
},
},
Spec: apiCoreV1.PodSpec{
Containers: []apiCoreV1.Container{
{
Name: "nginx-ingress",
Image: "nginx-ingress",
ImagePullPolicy: "Always",
Env: []apiCoreV1.EnvVar{
{
Name: "POD_NAMESPACE",
Value: "nginx-ingress",
},
{
Name: "POD_NAME",
Value: "nginx-ingress",
},
},
},
},
},
}

replicaNum int32 = 1
replica = &appsV1.ReplicaSet{
TypeMeta: metaV1.TypeMeta{
Kind: "ReplicaSet",
APIVersion: "apps/v1",
},
ObjectMeta: metaV1.ObjectMeta{
Name: "nginx-ingress",
Namespace: "nginx-ingress",
Labels: map[string]string{
"app": "nginx-ingress",
"app.kubernetes.io/name": "nginx-ingress",
},
},

Spec: appsV1.ReplicaSetSpec{
Replicas: &replicaNum,
},
Status: appsV1.ReplicaSetStatus{
Replicas: replicaNum,
ReadyReplicas: replicaNum,
AvailableReplicas: replicaNum,
},
}
)

// Nodes for testing NIC namespaces.
var (
node1 = &apiCoreV1.Node{
TypeMeta: metaV1.TypeMeta{
Expand All @@ -373,7 +465,7 @@ var (
},
ObjectMeta: metaV1.ObjectMeta{
Name: "test-node-1",
Namespace: "default",
Namespace: "nginx-ingress",
},
Spec: apiCoreV1.NodeSpec{},
}
Expand Down
12 changes: 12 additions & 0 deletions internal/telemetry/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/nginxinc/kubernetes-ingress/internal/configs"

k8s_nginx "github.com/nginxinc/kubernetes-ingress/pkg/client/clientset/versioned"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"

Expand Down Expand Up @@ -58,6 +59,9 @@ type CollectorConfig struct {

// Version represents NIC version.
Version string

// PodNSName represents NIC Pod's NamespacedName.
PodNSName types.NamespacedName
}

// NewCollector takes 0 or more options and creates a new TraceReporter.
Expand Down Expand Up @@ -104,6 +108,7 @@ func (c *Collector) Collect(ctx context.Context) {
VirtualServers: int64(report.VirtualServers),
VirtualServerRoutes: int64(report.VirtualServerRoutes),
TransportServers: int64(report.TransportServers),
Replicas: int64(report.NICReplicaCount),
},
}

Expand All @@ -125,6 +130,7 @@ type Report struct {
ClusterVersion string
ClusterPlatform string
ClusterNodeCount int
NICReplicaCount int
VirtualServers int
VirtualServerRoutes int
TransportServers int
Expand Down Expand Up @@ -161,6 +167,11 @@ func (c *Collector) BuildReport(ctx context.Context) (Report, error) {
glog.Errorf("Error collecting telemetry data: Platform: %v", err)
}

replicas, err := c.ReplicaCount(ctx)
if err != nil {
glog.Errorf("Error collecting telemetry data: Replicas: %v", err)
}

return Report{
Name: "NIC",
Version: c.Config.Version,
Expand All @@ -169,6 +180,7 @@ func (c *Collector) BuildReport(ctx context.Context) (Report, error) {
ClusterVersion: version,
ClusterPlatform: platform,
ClusterNodeCount: nodes,
NICReplicaCount: replicas,
VirtualServers: vsCount,
VirtualServerRoutes: vsrCount,
TransportServers: tsCount,
Expand Down
14 changes: 12 additions & 2 deletions internal/telemetry/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (
tel "github.com/nginxinc/telemetry-exporter/pkg/telemetry"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/version"
fakediscovery "k8s.io/client-go/discovery/fake"

testClient "k8s.io/client-go/kubernetes/fake"
)

Expand Down Expand Up @@ -342,13 +344,17 @@ func TestCountVirtualServers(t *testing.T) {
configurator := newConfigurator(t)

c, err := telemetry.NewCollector(telemetry.CollectorConfig{
K8sClientReader: newTestClientset(kubeNS, node1),
K8sClientReader: newTestClientset(kubeNS, node1, pod, replica),
Configurator: configurator,
Version: telemetryNICData.ProjectVersion,
})
if err != nil {
t.Fatal(err)
}
c.Config.PodNSName = types.NamespacedName{
Namespace: "nginx-ingress",
Name: "nginx-ingress",
}

for _, vs := range test.virtualServers {
_, err := configurator.AddOrUpdateVirtualServer(vs)
Expand Down Expand Up @@ -503,13 +509,17 @@ func TestCountTransportServers(t *testing.T) {
configurator := newConfigurator(t)

c, err := telemetry.NewCollector(telemetry.CollectorConfig{
K8sClientReader: newTestClientset(kubeNS, node1),
K8sClientReader: newTestClientset(kubeNS, node1, pod, replica),
Configurator: configurator,
Version: telemetryNICData.ProjectVersion,
})
if err != nil {
t.Fatal(err)
}
c.Config.PodNSName = types.NamespacedName{
Namespace: "nginx-ingress",
Name: "nginx-ingress",
}

for _, ts := range test.transportServers {
_, err := configurator.AddOrUpdateTransportServer(ts)
Expand Down
3 changes: 3 additions & 0 deletions internal/telemetry/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ type NICResourceCounts struct {
VirtualServerRoutes int64
// TransportServers is the number of TransportServers managed by the Ingress Controller.
TransportServers int64

// Replicas is the number of NIC replicas.
Replicas int64
}
Loading