...

Source file src/github.com/redhat-developer/odo/pkg/kclient/oc_server.go

Documentation: github.com/redhat-developer/odo/pkg/kclient

     1  package kclient
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"errors"
     7  	"fmt"
     8  	"net"
     9  	"time"
    10  
    11  	dfutil "github.com/devfile/library/v2/pkg/util"
    12  	configv1 "github.com/openshift/api/config/v1"
    13  	kerrors "k8s.io/apimachinery/pkg/api/errors"
    14  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    15  	"k8s.io/apimachinery/pkg/version"
    16  	"k8s.io/klog"
    17  )
    18  
    19  // isServerUp returns true if server is up and running
    20  // server parameter has to be a valid url
    21  func isServerUp(server string, timeout time.Duration) bool {
    22  	address, err := dfutil.GetHostWithPort(server)
    23  	if err != nil {
    24  		klog.V(3).Infof("Unable to parse url %s (%s)", server, err)
    25  	}
    26  	klog.V(3).Infof("Trying to connect to server %s", address)
    27  	_, connectionError := net.DialTimeout("tcp", address, timeout)
    28  	if connectionError != nil {
    29  		klog.V(3).Info(fmt.Errorf("unable to connect to server: %w", connectionError))
    30  		return false
    31  	}
    32  
    33  	klog.V(3).Infof("Server %v is up", server)
    34  	return true
    35  }
    36  
    37  // ServerInfo contains the fields that contain the server's information like
    38  // address, OpenShift and Kubernetes versions
    39  type ServerInfo struct {
    40  	Address           string
    41  	OpenShiftVersion  string
    42  	KubernetesVersion string
    43  }
    44  
    45  // GetServerVersion will fetch the Server Host, OpenShift and Kubernetes Version
    46  // It will be shown on the execution of odo version command
    47  func (c *Client) GetServerVersion(timeout time.Duration) (*ServerInfo, error) {
    48  	var info ServerInfo
    49  
    50  	// This will fetch the information about Server Address
    51  	config, err := c.KubeConfig.ClientConfig()
    52  	if err != nil {
    53  		return nil, fmt.Errorf("unable to get server's address: %w", err)
    54  	}
    55  	info.Address = config.Host
    56  
    57  	// checking if the server is reachable
    58  	if !isServerUp(config.Host, timeout) {
    59  		return nil, errors.New("unable to connect to OpenShift cluster, it may be down")
    60  	}
    61  
    62  	// This will fetch the information about OpenShift Version
    63  	coreGet := c.GetClient().CoreV1().RESTClient().Get()
    64  	rawOpenShiftVersion, err := coreGet.AbsPath("/apis/config.openshift.io/v1/clusterversions/version").Do(context.TODO()).Raw()
    65  	if err != nil {
    66  		klog.V(3).Info("Unable to get OpenShift Version: ", err)
    67  	} else {
    68  		var openShiftVersion configv1.ClusterVersion
    69  		if e := json.Unmarshal(rawOpenShiftVersion, &openShiftVersion); e != nil {
    70  			return nil, fmt.Errorf("unable to unmarshal OpenShift version %v: %w", string(rawOpenShiftVersion), e)
    71  		}
    72  		info.OpenShiftVersion = openShiftVersion.Status.Desired.Version
    73  	}
    74  
    75  	// This will fetch the information about Kubernetes Version
    76  	rawKubernetesVersion, err := coreGet.AbsPath("/version").Do(context.TODO()).Raw()
    77  	if err != nil {
    78  		return nil, fmt.Errorf("unable to get Kubernetes Version: %w", err)
    79  	}
    80  	var kubernetesVersion version.Info
    81  	if err := json.Unmarshal(rawKubernetesVersion, &kubernetesVersion); err != nil {
    82  		return nil, fmt.Errorf("unable to unmarshal Kubernetes Version: %v: %w", string(rawKubernetesVersion), err)
    83  	}
    84  	info.KubernetesVersion = kubernetesVersion.GitVersion
    85  
    86  	return &info, nil
    87  }
    88  
    89  func (c *Client) GetOCVersion() (string, error) {
    90  	clusterVersion, err := c.configClient.ClusterVersions().Get(context.TODO(), "version", metav1.GetOptions{})
    91  	if err != nil {
    92  		return "", err
    93  	}
    94  	switch {
    95  	case kerrors.IsForbidden(err), kerrors.IsNotFound(err):
    96  		return "", err
    97  	}
    98  	if clusterVersion != nil {
    99  		if len(clusterVersion.Status.History) == 1 {
   100  			return clusterVersion.Status.History[0].Version, nil
   101  		}
   102  		for _, update := range clusterVersion.Status.History {
   103  			if update.State == configv1.CompletedUpdate {
   104  				// obtain the version from the last completed update
   105  				return update.Version, nil
   106  			}
   107  		}
   108  	}
   109  	return "", errors.New("unable to get OC version")
   110  }
   111  

View as plain text