Skip to content

Commit

Permalink
Add platform collection
Browse files Browse the repository at this point in the history
  • Loading branch information
bjee19 committed Mar 7, 2024
1 parent 7ae2781 commit ffbdafc
Show file tree
Hide file tree
Showing 4 changed files with 468 additions and 82 deletions.
79 changes: 52 additions & 27 deletions internal/mode/static/telemetry/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"runtime"
"strings"

appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/config"
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/dataplane"
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/graph"
"github.com/nginxinc/nginx-gateway-fabric/pkg/telemetry"
)

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . GraphGetter
Expand Down Expand Up @@ -57,9 +57,9 @@ type Data struct {
Arch string
DeploymentID string
ImageSource string
Flags config.Flags
K8sVersion string
K8sPlatform string
Flags config.Flags
NGFResourceCounts NGFResourceCounts
NodeCount int
NGFReplicaCount int
Expand Down Expand Up @@ -99,19 +99,12 @@ func NewDataCollectorImpl(

// Collect collects and returns telemetry Data.
func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
nodeCount, err := CollectNodeCount(ctx, c.cfg.K8sClientReader)
if err != nil {
return Data{}, fmt.Errorf("failed to collect node count: %w", err)
}

nodes, err := collectNodeList(ctx, c.cfg.K8sClientReader)
clusterInfo, err := collectClusterInformation(ctx, c.cfg.K8sClientReader)
if err != nil {
return Data{}, err
}

node := nodes.Items[0]
k8sVersion := node.Status.NodeInfo.KubeletVersion
k8sPlatform := strings.Split(node.Spec.ProviderID, "://")[0]
nodeCount := CollectNodeCount(clusterInfo.Nodes)

graphResourceCount, err := collectGraphResourceCount(c.cfg.GraphGetter, c.cfg.ConfigurationGetter)
if err != nil {
Expand All @@ -133,11 +126,6 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
return Data{}, fmt.Errorf("failed to get NGF deploymentID: %w", err)
}

var clusterID string
if clusterID, err = CollectClusterID(ctx, c.cfg.K8sClientReader); err != nil {
return Data{}, fmt.Errorf("failed to collect clusterID: %w", err)
}

data := Data{
NodeCount: nodeCount,
NGFResourceCounts: graphResourceCount,
Expand All @@ -146,26 +134,21 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
Version: c.cfg.Version,
},
NGFReplicaCount: replicaCount,
ClusterID: clusterID,
ClusterID: clusterInfo.ClusterID,
ImageSource: c.cfg.ImageSource,
Arch: runtime.GOARCH,
DeploymentID: deploymentID,
Flags: c.cfg.Flags,
K8sVersion: k8sVersion,
K8sPlatform: k8sPlatform,
K8sVersion: clusterInfo.Version,
K8sPlatform: clusterInfo.Platform,
}

return data, nil
}

// CollectNodeCount returns the number of nodes in the cluster.
func CollectNodeCount(ctx context.Context, k8sClient client.Reader) (int, error) {
var nodes v1.NodeList
if err := k8sClient.List(ctx, &nodes); err != nil {
return 0, fmt.Errorf("failed to get NodeList: %w", err)
}

return len(nodes.Items), nil
func CollectNodeCount(nodes v1.NodeList) int {
return len(nodes.Items)
}

func collectGraphResourceCount(
Expand Down Expand Up @@ -278,11 +261,53 @@ func CollectClusterID(ctx context.Context, k8sClient client.Reader) (string, err
return string(kubeNamespace.GetUID()), nil
}

func collectNodeList(ctx context.Context, k8sClient client.Reader) (v1.NodeList, error) {
func CollectNodeList(ctx context.Context, k8sClient client.Reader) (v1.NodeList, error) {
var nodes v1.NodeList
if err := k8sClient.List(ctx, &nodes); err != nil {
return nodes, fmt.Errorf("failed to get NodeList: %w", err)
}

return nodes, nil
}

type clusterInformation struct {
Platform string
Version string
ClusterID string
Nodes v1.NodeList
}

func collectClusterInformation(ctx context.Context, k8sClient client.Reader) (clusterInformation, error) {
var clusterInfo clusterInformation

nodes, err := CollectNodeList(ctx, k8sClient)
if err != nil {
return clusterInformation{}, fmt.Errorf("failed to collect cluster information: %w", err)
}
if len(nodes.Items) == 0 {
return clusterInformation{}, errors.New("failed to collect cluster information: NodeList length is zero")
}

clusterInfo.Nodes = nodes

var clusterID string
if clusterID, err = CollectClusterID(ctx, k8sClient); err != nil {
return clusterInformation{}, fmt.Errorf("failed to collect cluster information: %w", err)
}
clusterInfo.ClusterID = clusterID

node := nodes.Items[0]
clusterInfo.Version = node.Status.NodeInfo.KubeletVersion
if clusterInfo.Version == "" {
clusterInfo.Version = "unknown"
}

var namespaces v1.NamespaceList
if err = k8sClient.List(ctx, &namespaces); err != nil {
return clusterInformation{}, fmt.Errorf("failed to collect cluster information: %w", err)
}

clusterInfo.Platform = telemetry.CollectK8sPlatform(node, namespaces)

return clusterInfo, nil
}
Loading

0 comments on commit ffbdafc

Please sign in to comment.