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

Collect Kubernetes distribution platform and version #1651

Merged
merged 24 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
79 changes: 55 additions & 24 deletions internal/mode/static/telemetry/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
k8sversion "k8s.io/apimachinery/pkg/util/version"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/config"
Expand Down Expand Up @@ -100,14 +101,11 @@ func NewDataCollectorImpl(
}
}

// notImplemented is a value for string field, for which collection is not implemented yet.
const notImplemented = "not-implemented"

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

graphResourceCount, err := collectGraphResourceCount(c.cfg.GraphGetter, c.cfg.ConfigurationGetter)
Expand All @@ -130,21 +128,16 @@ 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{
Data: tel.Data{
ProjectName: "NGF",
ProjectVersion: c.cfg.Version,
ProjectArchitecture: runtime.GOARCH,
ClusterID: clusterID,
ClusterVersion: notImplemented,
ClusterPlatform: notImplemented,
ClusterID: clusterInfo.ClusterID,
ClusterVersion: clusterInfo.Version,
ClusterPlatform: clusterInfo.Platform,
InstallationID: deploymentID,
ClusterNodeCount: int64(nodeCount),
ClusterNodeCount: int64(clusterInfo.NodeCount),
},
NGFResourceCounts: graphResourceCount,
ImageSource: c.cfg.ImageSource,
Expand All @@ -156,16 +149,6 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
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 collectGraphResourceCount(
graphGetter GraphGetter,
configurationGetter ConfigurationGetter,
Expand Down Expand Up @@ -275,3 +258,51 @@ func CollectClusterID(ctx context.Context, k8sClient client.Reader) (string, err
}
return string(kubeNamespace.GetUID()), nil
}

type clusterInformation struct {
bjee19 marked this conversation as resolved.
Show resolved Hide resolved
Platform string
Version string
ClusterID string
NodeCount int
}

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

var nodes v1.NodeList
if err := k8sClient.List(ctx, &nodes); err != nil {
return clusterInformation{}, fmt.Errorf("failed to get NodeList: %w", err)
}

nodeCount := len(nodes.Items)
if nodeCount == 0 {
return clusterInformation{}, errors.New("failed to collect cluster information: NodeList length is zero")
}
clusterInfo.NodeCount = nodeCount

node := nodes.Items[0]
bjee19 marked this conversation as resolved.
Show resolved Hide resolved

kubeletVersion := node.Status.NodeInfo.KubeletVersion
bjee19 marked this conversation as resolved.
Show resolved Hide resolved
version, err := k8sversion.ParseGeneric(kubeletVersion)
bjee19 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
clusterInfo.Version = "unknown"
} else {
clusterInfo.Version = version.String()
}

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

clusterInfo.Platform = getPlatform(node, namespaces)

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

return clusterInfo, nil
}
Loading
Loading