github.com/percona/percona-xtradb-cluster-operator@v1.14.0/version/server.go (about)

     1  package version
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"sync"
     8  
     9  	k8sversion "k8s.io/apimachinery/pkg/version"
    10  	"k8s.io/client-go/rest"
    11  
    12  	"github.com/percona/percona-xtradb-cluster-operator/clientcmd"
    13  )
    14  
    15  var (
    16  	cVersion *ServerVersion
    17  	mx       sync.Mutex
    18  )
    19  
    20  const (
    21  	PlatformUndef      Platform = ""
    22  	PlatformKubernetes Platform = "kubernetes"
    23  	PlatformOpenshift  Platform = "openshift"
    24  )
    25  
    26  type Platform string
    27  
    28  // ServerVersion represents info about k8s / openshift server version
    29  type ServerVersion struct {
    30  	Platform Platform
    31  	Info     k8sversion.Info
    32  }
    33  
    34  // Server returns server version and platform (k8s|oc)
    35  // it performs API requests for the first invocation and then returns "cached" value
    36  func Server() (*ServerVersion, error) {
    37  	mx.Lock()
    38  	defer mx.Unlock()
    39  	if cVersion != nil {
    40  		return cVersion, nil
    41  	}
    42  
    43  	v, err := GetServer()
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	cVersion = v
    49  
    50  	return cVersion, nil
    51  }
    52  
    53  // GetServer make request to platform server and returns server version and platform (k8s|oc)
    54  func GetServer() (*ServerVersion, error) {
    55  	cl, err := clientcmd.NewClient()
    56  	if err != nil {
    57  		return nil, fmt.Errorf("create REST client: %v", err)
    58  	}
    59  	client := cl.REST()
    60  
    61  	version := &ServerVersion{}
    62  	// oc 3.9
    63  	version.Info, err = probeAPI("/version/openshift", client)
    64  	if err == nil {
    65  		version.Platform = PlatformOpenshift
    66  		return version, nil
    67  	}
    68  
    69  	// oc 3.11+
    70  	version.Info, err = probeAPI("/oapi/v1", client)
    71  	if err == nil {
    72  		version.Platform = PlatformOpenshift
    73  		version.Info.GitVersion = "undefined (v3.11+)"
    74  		return version, nil
    75  	}
    76  
    77  	// openshift 4.0
    78  	version.Info, err = probeAPI("/apis/quota.openshift.io", client)
    79  	if err == nil {
    80  		version.Platform = PlatformOpenshift
    81  		version.Info.GitVersion = "undefined (v4.0+)"
    82  		return version, nil
    83  	}
    84  
    85  	// k8s
    86  	version.Info, err = probeAPI("/version", client)
    87  	if err == nil {
    88  		version.Platform = PlatformKubernetes
    89  		return version, nil
    90  	}
    91  
    92  	return version, err
    93  }
    94  
    95  func probeAPI(path string, client rest.Interface) (k8sversion.Info, error) {
    96  	var vInfo k8sversion.Info
    97  	vBody, err := client.Get().AbsPath(path).Do(context.TODO()).Raw()
    98  	if err != nil {
    99  		return vInfo, err
   100  	}
   101  
   102  	err = json.Unmarshal(vBody, &vInfo)
   103  	if err != nil {
   104  		return vInfo, err
   105  	}
   106  
   107  	return vInfo, nil
   108  }