...
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
20
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
38
39 type ServerInfo struct {
40 Address string
41 OpenShiftVersion string
42 KubernetesVersion string
43 }
44
45
46
47 func (c *Client) GetServerVersion(timeout time.Duration) (*ServerInfo, error) {
48 var info ServerInfo
49
50
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
58 if !isServerUp(config.Host, timeout) {
59 return nil, errors.New("unable to connect to OpenShift cluster, it may be down")
60 }
61
62
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
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
105 return update.Version, nil
106 }
107 }
108 }
109 return "", errors.New("unable to get OC version")
110 }
111
View as plain text