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

Feat/telemetry platform #5217

Merged
merged 13 commits into from
Mar 12, 2024
80 changes: 80 additions & 0 deletions internal/telemetry/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package telemetry

import (
"context"
"errors"
"strings"

metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -35,3 +37,81 @@ func (c *Collector) K8sVersion() (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.
//
// Cloud providers identified by PlatformID (in K8s SIGs):
// https://github.com/orgs/kubernetes-sigs/repositories?q=cluster-api-provider
//
//gocyclo:ignore
func lookupPlatform(providerID string) string {
provider := strings.TrimSpace(providerID)
// The case when the ProviderID field not used by the cloud provider.
if provider == "" {
return "other"
}

provider = strings.ToLower(providerID)
jjngx marked this conversation as resolved.
Show resolved Hide resolved
if strings.HasPrefix(provider, "aws") {
return "aws"
}
if strings.HasPrefix(provider, "azure") {
return "azure"
}
if strings.HasPrefix(provider, "gce") {
return "gke"
}
if strings.HasPrefix(provider, "kind") {
return "kind"
}
if strings.HasPrefix(provider, "vsphere") {
return "vsphere"
}
if strings.HasPrefix(provider, "k3s") {
return "k3s"
}
if strings.HasPrefix(provider, "ibmcloud") {
return "ibmcloud"
}
if strings.HasPrefix(provider, "ibmpowervs") {
return "ibmpowervs"
}
if strings.HasPrefix(provider, "cloudstack") {
return "cloudstack"
}
if strings.HasPrefix(provider, "openstack") {
return "openstack"
}
if strings.HasPrefix(provider, "digitalocean") {
return "digitalocean"
}
if strings.HasPrefix(provider, "equinixmetal") {
return "equinixmetal"
}
if strings.HasPrefix(provider, "alicloud") {
return "alicloud"
}

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