Skip to content
Open
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
2 changes: 1 addition & 1 deletion cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func main() {
"starting manager",
"GitCommit", GitCommit,
"BuildTime", BuildTime,
"Platform", serverVersion.Platform,
"Platform", serverVersion.String(),
"Version", serverVersion.Info,
)
if err := mgr.Start(ctx); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/binlogserver/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (f *fakeExecClient) REST() restclient.Interface {
return nil
}

func (f *fakeExecClient) Host() string {
return ""
}

func newReadyBinlogServerPod(cr *apiv1.PerconaServerMySQL) *corev1.Pod {
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down
8 changes: 8 additions & 0 deletions pkg/clientcmd/clientcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type client struct {
type Client interface {
Exec(ctx context.Context, pod *corev1.Pod, containerName string, command []string, stdin io.Reader, stdout, stderr io.Writer, tty bool) error
REST() restclient.Interface
Host() string
}

func NewClient() (Client, error) {
Expand Down Expand Up @@ -92,3 +93,10 @@ func (c *client) Exec(
func (c *client) REST() restclient.Interface {
return c.client.RESTClient()
}

func (c *client) Host() string {
if c.restconfig == nil {
return ""
}
return c.restconfig.Host
}
4 changes: 4 additions & 0 deletions pkg/controller/ps/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,10 @@ func (c *fakeClient) REST() restclient.Interface {
return nil
}

func (c *fakeClient) Host() string {
return ""
}

// fakeClientScript is an object which contains an info about executed command.
// cmd, stdin values are compared with the corresponding values in the Exec method.
// stdin, stdout values are written to the corresponding streams in the Exec method.
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/ps/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,10 @@ func (f *fakeVersionClientCmd) REST() restclient.Interface {
return nil
}

func (f *fakeVersionClientCmd) Host() string {
return ""
}

func (vs *fakeVS) Apply(_ context.Context, req any) (any, error) {
if vs.unimplemented {
return nil, errors.New("unimplemented")
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/ps/volume_autoscaling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (m *mockExecClient) REST() restclient.Interface {
return nil
}

func (m *mockExecClient) Host() string {
return ""
}

func autoscalingCR(t *testing.T) *apiv1.PerconaServerMySQL {
t.Helper()

Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/psbackup/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,10 @@ func (f *fakeClientCmd) REST() restclient.Interface {
return nil
}

func (f *fakeClientCmd) Host() string {
return ""
}

func TestRenewDowntime(t *testing.T) {
ctx := context.Background()
scheme := runtime.NewScheme()
Expand Down
4 changes: 4 additions & 0 deletions pkg/mysql/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (m *mockClientCmd) REST() restclient.Interface {
return nil
}

func (m *mockClientCmd) Host() string {
return ""
}

func TestGetPVCUsage(t *testing.T) {
tests := map[string]struct {
pvcName string
Expand Down
82 changes: 73 additions & 9 deletions pkg/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ package platform
import (
"context"
"encoding/json"
"strings"
"sync"

k8sversion "k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/rest"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/percona/percona-server-mysql-operator/pkg/clientcmd"
)

var log = logf.Log.WithName("platform")

type Platform string

const (
Expand All @@ -19,18 +23,43 @@ const (
PlatformOpenshift Platform = "openshift"
)

type CloudProvider string

const (
CloudProviderUndef CloudProvider = ""
CloudProviderGKE CloudProvider = "gke"
CloudProviderEKS CloudProvider = "eks"
CloudProviderAKS CloudProvider = "aks"
CloudProviderDOKS CloudProvider = "doks"
CloudProviderOKE CloudProvider = "oke"
CloudProviderTanzu CloudProvider = "tanzu"
CloudProviderRancher CloudProvider = "rancher"
)

type ServerVersion struct {
Platform Platform
Info k8sversion.Info
Platform Platform
CloudProvider CloudProvider
Info k8sversion.Info
}

// String returns the platform identifier used in telemetry and version
// service reports. When a cloud provider is detected it is appended as
// a suffix (e.g. "kubernetes-gke", "kubernetes-doks").
func (s *ServerVersion) String() string {
if s == nil {
return ""
}
if s.CloudProvider == CloudProviderUndef {
return string(s.Platform)
}
return string(s.Platform) + "-" + string(s.CloudProvider)
}

var (
cVersion *ServerVersion
mx sync.Mutex
)

// GetServerVersion returns server version and platform (k8s|oc)
// it performs API requests for the first invocation and then returns "cached" value
func GetServerVersion(cliCmd clientcmd.Client) (*ServerVersion, error) {
mx.Lock()
defer mx.Unlock()
Expand All @@ -48,7 +77,6 @@ func GetServerVersion(cliCmd clientcmd.Client) (*ServerVersion, error) {
return cVersion, nil
}

// GetServer make request to platform server and returns server version and platform (k8s|oc)
func getServerVersion(cliCmd clientcmd.Client) (*ServerVersion, error) {
var err error
client := cliCmd.REST()
Expand All @@ -64,12 +92,48 @@ func getServerVersion(cliCmd clientcmd.Client) (*ServerVersion, error) {

// k8s
version.Info, err = probeAPI("/version", client)
if err == nil {
version.Platform = PlatformKubernetes
return version, nil
if err != nil {
return version, err
}
version.Platform = PlatformKubernetes
version.CloudProvider = detectCloudProvider(client, cliCmd.Host())

return version, err
return version, nil
}

func detectCloudProvider(client rest.Interface, host string) CloudProvider {
probes := []struct {
provider CloudProvider
apiGroups []string
hosts []string
}{
{provider: CloudProviderGKE, apiGroups: []string{"cloud.google.com"}},
{provider: CloudProviderEKS, apiGroups: []string{"vpcresources.k8s.aws"}},
{provider: CloudProviderDOKS, apiGroups: []string{"dataplane-operator.doks.digitalocean.com"}},
{provider: CloudProviderAKS, hosts: []string{".azmk8s.io"}},
{provider: CloudProviderOKE, hosts: []string{".oraclecloud.com"}},
{provider: CloudProviderTanzu, apiGroups: []string{"run.tanzu.vmware.com"}},
{provider: CloudProviderRancher, apiGroups: []string{"management.cattle.io"}},
}
for _, p := range probes {
for _, group := range p.apiGroups {
path := "/apis/" + group
if _, err := probeAPI(path, client); err == nil {
log.Info("cloud provider detected", "provider", p.provider, "signal", "apigroup:"+group)
return p.provider
} else {
log.V(1).Info("cloud provider probe miss", "provider", p.provider, "signal", "apigroup:"+group, "err", err.Error())
}
}
for _, h := range p.hosts {
if strings.Contains(host, h) {
log.Info("cloud provider detected", "provider", p.provider, "signal", "host:"+h)
return p.provider
}
}
}
log.Info("cloud provider not detected", "provider", "unknown")
return CloudProviderUndef
Comment on lines +134 to +136
}

func probeAPI(path string, client rest.Interface) (k8sversion.Info, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/telemetry/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func createReport(cr *apiv1.PerconaServerMySQL, serverVersion *platform.ServerVe
if serverVersion.Platform != "" {
metrics = append(metrics, &models.GenericReportMetric{
Key: metricPlatform,
Value: string(serverVersion.Platform),
Value: serverVersion.String(),
})
}
Comment thread
gkech marked this conversation as resolved.

Expand Down
2 changes: 1 addition & 1 deletion pkg/version/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func GetVersion(ctx context.Context, cr *apiv1.PerconaServerMySQL, endpoint stri

timeout := 10 * time.Second
crUID := string(cr.GetUID())
platformStr := string(serverVersion.Platform)
platformStr := serverVersion.String()
Comment thread
gkech marked this conversation as resolved.

Comment on lines 44 to 47
Comment on lines 44 to 47
apply := cr.Spec.UpgradeOptions.Apply
if apply == "" {
Expand Down
Loading