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 all 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
7 changes: 7 additions & 0 deletions charts/nginx-ingress/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ rules:
- nodes
verbs:
- list
- apiGroups:
- "apps"
resources:
- replicasets
- daemonsets
verbs:
- get
- apiGroups:
- networking.k8s.io
resources:
Expand Down
7 changes: 7 additions & 0 deletions deployments/rbac/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ rules:
- get
- list
- watch
- apiGroups:
- "apps"
resources:
- replicasets
- daemonsets
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
30 changes: 30 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,35 @@ 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))
}

switch podRef[0].Kind {
case "ReplicaSet":
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
case "DaemonSet":
ds, err := c.Config.K8sClientReader.AppsV1().DaemonSets(c.Config.PodNSName.Namespace).Get(ctx, podRef[0].Name, metaV1.GetOptions{})
if err != nil {
return 0, err
}
return int(ds.Status.CurrentNumberScheduled), nil
default:
return 0, fmt.Errorf("expected pod owner reference to be ReplicaSet or DeamonSet, got %s", podRef[0].Kind)
}
}

// 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
179 changes: 166 additions & 13 deletions 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,37 @@ func TestPlatformLookupOnMalformedPartialPlatformIDField(t *testing.T) {
}
}

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

c := newTestCollectorForClusterWithNodes(t, node1, pod1, 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)
}
}

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

c := newTestCollectorForClusterWithNodes(t, node1, pod2, daemon)
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 +395,141 @@ 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 (
pod1 = &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,
},
}

pod2 = &apiCoreV1.Pod{
TypeMeta: metaV1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metaV1.ObjectMeta{
Name: "nginx-ingress",
Namespace: "nginx-ingress",
OwnerReferences: []metaV1.OwnerReference{
{
Kind: "DaemonSet",
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",
},
},
},
},
},
}

daemonNum int32 = 1
daemon = &appsV1.DaemonSet{
TypeMeta: metaV1.TypeMeta{
Kind: "DaemonSet",
APIVersion: "apps/v1",
},
ObjectMeta: metaV1.ObjectMeta{
Name: "nginx-ingress",
Namespace: "nginx-ingress",
Labels: map[string]string{"app": "nginx-ingress"},
},
Spec: appsV1.DaemonSetSpec{},
Status: appsV1.DaemonSetStatus{
CurrentNumberScheduled: daemonNum,
NumberReady: daemonNum,
NumberAvailable: daemonNum,
},
}
)

// Nodes for testing NIC namespaces.
var (
node1 = &apiCoreV1.Node{
TypeMeta: metaV1.TypeMeta{
Expand All @@ -373,7 +538,7 @@ var (
},
ObjectMeta: metaV1.ObjectMeta{
Name: "test-node-1",
Namespace: "default",
Namespace: "nginx-ingress",
},
Spec: apiCoreV1.NodeSpec{},
}
Expand Down Expand Up @@ -413,18 +578,6 @@ var (
},
Spec: apiCoreV1.NamespaceSpec{},
}

dummyKubeNS = &apiCoreV1.Namespace{
TypeMeta: metaV1.TypeMeta{
Kind: "Namespace",
APIVersion: "v1",
},
ObjectMeta: metaV1.ObjectMeta{
Name: "kube-system",
UID: "",
},
Spec: apiCoreV1.NamespaceSpec{},
}
)

// Cloud providers' nodes for testing ProviderID lookups.
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
Loading
Loading