@@ -18,6 +18,7 @@ package kubefedcluster
18
18
19
19
import (
20
20
"context"
21
+ "fmt"
21
22
"strings"
22
23
"time"
23
24
@@ -46,14 +47,16 @@ const (
46
47
LabelZoneRegion = "failure-domain.beta.kubernetes.io/region"
47
48
48
49
// Common ClusterConditions for KubeFedClusterStatus
49
- ClusterReady = "ClusterReady"
50
- HealthzOk = "/healthz responded with ok"
51
- ClusterNotReady = "ClusterNotReady"
52
- HealthzNotOk = "/healthz responded without ok"
53
- ClusterNotReachableReason = "ClusterNotReachable"
54
- ClusterNotReachableMsg = "cluster is not reachable"
55
- ClusterReachableReason = "ClusterReachable"
56
- ClusterReachableMsg = "cluster is reachable"
50
+ ClusterReady = "ClusterReady"
51
+ HealthzOk = "/healthz responded with ok"
52
+ ClusterNotReady = "ClusterNotReady"
53
+ HealthzNotOk = "/healthz responded without ok"
54
+ ClusterNotReachableReason = "ClusterNotReachable"
55
+ ClusterNotReachableMsg = "cluster is not reachable"
56
+ ClusterReachableReason = "ClusterReachable"
57
+ ClusterReachableMsg = "cluster is reachable"
58
+ ClusterConfigMalformedReason = "ClusterConfigMalformed"
59
+ ClusterConfigMalformedMsg = "cluster's configuration may be malformed"
57
60
)
58
61
59
62
// ClusterClient provides methods for determining the status and zones of a
@@ -67,21 +70,18 @@ type ClusterClient struct {
67
70
// The kubeClient is used to configure the ClusterClient's internal client
68
71
// with information from a kubeconfig stored in a kubernetes secret.
69
72
func NewClusterClientSet (c * fedv1b1.KubeFedCluster , client generic.Client , fedNamespace string , timeout time.Duration ) (* ClusterClient , error ) {
73
+ var clusterClientSet = ClusterClient {clusterName : c .Name }
70
74
clusterConfig , err := util .BuildClusterConfig (c , client , fedNamespace )
71
75
if err != nil {
72
- return nil , err
76
+ return & clusterClientSet , err
73
77
}
74
78
clusterConfig .Timeout = timeout
75
- var clusterClientSet = ClusterClient {clusterName : c .Name }
76
- clusterClientSet .kubeClient = kubeclientset .NewForConfigOrDie ((restclient .AddUserAgent (clusterConfig , UserAgentName )))
77
- if clusterClientSet .kubeClient == nil {
78
- return nil , nil
79
- }
80
- return & clusterClientSet , nil
79
+ clusterClientSet .kubeClient , err = kubeclientset .NewForConfig (restclient .AddUserAgent (clusterConfig , UserAgentName ))
80
+ return & clusterClientSet , err
81
81
}
82
82
83
- // GetClusterHealthStatus gets the kubernetes cluster health status by requesting "/healthz"
84
- func (c * ClusterClient ) GetClusterHealthStatus () (* fedv1b1.KubeFedClusterStatus , error ) {
83
+ // GetClusterStatus gets the kubernetes cluster's health and version status
84
+ func (c * ClusterClient ) GetClusterStatus () (* fedv1b1.KubeFedClusterStatus , error ) {
85
85
clusterStatus := fedv1b1.KubeFedClusterStatus {}
86
86
currentTime := metav1 .Now ()
87
87
clusterReady := ClusterReady
@@ -124,9 +124,26 @@ func (c *ClusterClient) GetClusterHealthStatus() (*fedv1b1.KubeFedClusterStatus,
124
124
LastProbeTime : currentTime ,
125
125
LastTransitionTime : & currentTime ,
126
126
}
127
+ clusterConfigMalformedReason := ClusterConfigMalformedReason
128
+ clusterConfigMalformedMsg := ClusterConfigMalformedMsg
129
+ newClusterConfigMalformedCondition := fedv1b1.ClusterCondition {
130
+ Type : fedcommon .ClusterConfigMalformed ,
131
+ Status : corev1 .ConditionTrue ,
132
+ Reason : & clusterConfigMalformedReason ,
133
+ Message : & clusterConfigMalformedMsg ,
134
+ LastProbeTime : currentTime ,
135
+ LastTransitionTime : & currentTime ,
136
+ }
137
+ if c .kubeClient == nil {
138
+ clusterStatus .Conditions = append (clusterStatus .Conditions , newClusterConfigMalformedCondition )
139
+ metrics .RegisterKubefedClusterTotal (metrics .ClusterNotReady , c .clusterName )
140
+ return & clusterStatus , nil
141
+ }
127
142
body , err := c .kubeClient .DiscoveryClient .RESTClient ().Get ().AbsPath ("/healthz" ).Do (context .Background ()).Raw ()
128
143
if err != nil {
129
144
runtime .HandleError (errors .Wrapf (err , "Failed to do cluster health check for cluster %q" , c .clusterName ))
145
+ msg := fmt .Sprintf ("%s: %v" , ClusterNotReachableMsg , err )
146
+ newClusterOfflineCondition .Message = & msg
130
147
clusterStatus .Conditions = append (clusterStatus .Conditions , newClusterOfflineCondition )
131
148
metrics .RegisterKubefedClusterTotal (metrics .ClusterOffline , c .clusterName )
132
149
} else {
@@ -136,6 +153,13 @@ func (c *ClusterClient) GetClusterHealthStatus() (*fedv1b1.KubeFedClusterStatus,
136
153
} else {
137
154
metrics .RegisterKubefedClusterTotal (metrics .ClusterReady , c .clusterName )
138
155
clusterStatus .Conditions = append (clusterStatus .Conditions , newClusterReadyCondition )
156
+
157
+ version , err := c .kubeClient .DiscoveryClient .ServerVersion ()
158
+ if err != nil {
159
+ runtime .HandleError (errors .Wrapf (err , "Failed to get Kubernetes version of cluster %q" , c .clusterName ))
160
+ } else {
161
+ clusterStatus .KubernetesVersion = version .GitVersion
162
+ }
139
163
}
140
164
}
141
165
0 commit comments