Skip to content

Commit

Permalink
Feat/telemetry platform (#5217)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjngx authored Mar 12, 2024
1 parent 18b5092 commit 3e459f7
Show file tree
Hide file tree
Showing 6 changed files with 760 additions and 147 deletions.
5 changes: 3 additions & 2 deletions internal/configs/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1479,8 +1479,9 @@ func (cnf *Configurator) GetIngressCounts() map[string]int {

// GetVirtualServerCounts returns the total count of
// VirtualServer and VirtualServerRoute resources that are handled by the Ingress Controller
func (cnf *Configurator) GetVirtualServerCounts() (vsCount int, vsrCount int) {
vsCount = len(cnf.virtualServers)
func (cnf *Configurator) GetVirtualServerCounts() (int, int) {
vsCount := len(cnf.virtualServers)
vsrCount := 0
for _, vs := range cnf.virtualServers {
vsrCount += len(vs.VirtualServerRoutes)
}
Expand Down
39 changes: 37 additions & 2 deletions internal/telemetry/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package telemetry

import (
"context"
"errors"
"strings"

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) (int64, error) {
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 int64(len(nodes.Items)), nil
return len(nodes.Items), nil
}

// ClusterID returns the UID of the kube-system namespace representing cluster id.
Expand All @@ -35,3 +37,36 @@ func (c *Collector) ClusterVersion() (string, error) {
}
return sv.String(), nil
}

// Platform returns a string representing platform name.
func (c *Collector) Platform(ctx context.Context) (string, error) {
nodes, err := c.Config.K8sClientReader.CoreV1().Nodes().List(ctx, metaV1.ListOptions{})
if err != nil {
return "", err
}
if len(nodes.Items) == 0 {
return "", errors.New("no nodes in the cluster, cannot determine platform name")
}
return lookupPlatform(nodes.Items[0].Spec.ProviderID), nil
}

// lookupPlatform takes a string representing a K8s PlatformID
// retrieved from a cluster node and returns a string
// representing the platform name.
func lookupPlatform(providerID string) string {
provider := strings.TrimSpace(providerID)
if provider == "" {
return "other"
}

provider = strings.ToLower(providerID)

p := strings.Split(provider, ":")
if len(p) == 0 {
return "other"
}
if p[0] == "" {
return "other"
}
return p[0]
}
Loading

0 comments on commit 3e459f7

Please sign in to comment.