/
cluster_info.go
266 lines (229 loc) · 8.3 KB
/
cluster_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package v1alpha1
import (
"context"
"fmt"
"net"
"strconv"
"github.com/tungstenfabric/tf-operator/pkg/k8s"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// KubernetesClusterConfig k8s cluster parameters
// +k8s:openapi-gen=true
type KubernetesClusterConfig struct {
ControlPlaneEndpoint string `json:"controlPlaneEndpoint,omitempty"`
ClusterName string `json:"clusterName,omitempty"`
Networking KubernetesClusterNetworking `json:"networking,omitempty"`
}
// KubernetesClusterNetworking k8s cluster networking parameters
// +k8s:openapi-gen=true
type KubernetesClusterNetworking struct {
DNSDomain string `json:"dnsDomain,omitempty"`
PodSubnet string `json:"podSubnet,omitempty"`
ServiceSubnet string `json:"serviceSubnet,omitempty"`
CNIConfig CNIConfig `json:"cniConfig,omitempty"`
}
// CNIConfig k8s cluster cni parameters
// +k8s:openapi-gen=true
type CNIConfig struct {
ConfigPath string `json:"configPath,omitempty"`
BinaryPath string `json:"binaryPath,omitempty"`
}
var clusterInfoLog = logf.Log.WithName("cluster_info")
// ClusterDNSDomain returns Cluster DNS domain set in kubeadm config
func ClusterDNSDomain(client client.Client) (string, error) {
cfg, err := ClusterParameters(client)
if err != nil {
return "", err
}
return cfg.Networking.DNSDomain, nil
}
func yamlToStruct(yamlString string, structPointer interface{}) error {
if err := k8s.YamlToStruct(yamlString, structPointer); err != nil {
clusterInfoLog.Error(err, "YamlToStruct failed", "yamlString", yamlString)
return err
}
return nil
}
var getConfigMapFromOtherNamespace = func(name string, namespace string) (*v1.ConfigMap, error) {
// getConfigMapFromOtherNamespace use requests to k8s api. Thats why we
// create it as a variable to have an ability to mock it in the unit tests.
configMap, err := k8s.GetCoreV1().ConfigMaps(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
if !errors.IsNotFound(err) {
clusterInfoLog.Error(err, fmt.Sprintf("CoreV1.ConfigMaps failed to get %v", name))
}
return nil, err
}
return configMap, nil
}
func (to *KubernetesClusterConfig) replaceFields(from KubernetesClusterConfig) *KubernetesClusterConfig {
if from.ClusterName != "" {
to.ClusterName = from.ClusterName
}
if from.Networking.DNSDomain != "" {
to.Networking.DNSDomain = from.Networking.DNSDomain
}
if from.Networking.PodSubnet != "" {
to.Networking.PodSubnet = from.Networking.PodSubnet
}
if from.Networking.ServiceSubnet != "" {
to.Networking.ServiceSubnet = from.Networking.ServiceSubnet
}
if from.Networking.CNIConfig.ConfigPath != "" {
to.Networking.CNIConfig.ConfigPath = from.Networking.CNIConfig.ConfigPath
}
if from.Networking.CNIConfig.BinaryPath != "" {
to.Networking.CNIConfig.BinaryPath = from.Networking.CNIConfig.BinaryPath
}
if from.ControlPlaneEndpoint != "" {
to.ControlPlaneEndpoint = from.ControlPlaneEndpoint
}
return to
}
func (c *KubernetesClusterConfig) fillWithDefaultValues() {
if !k8s.IsOpenshift() {
c.ClusterName = KubernetesClusterName
c.Networking.DNSDomain = KubernetesDNSDomainName
c.Networking.PodSubnet = KubernetesPodSubnet
c.Networking.ServiceSubnet = KubernetesServiceSubnet
c.Networking.CNIConfig.ConfigPath = CNIConfigPath
c.Networking.CNIConfig.BinaryPath = CNIBinaryPath
} else {
c.ClusterName = OpenShiftClusterName
c.Networking.DNSDomain = OpenShiftDNSDomain
c.Networking.PodSubnet = OpenShiftPodSubnet
c.Networking.ServiceSubnet = OpenShiftServiceSubnet
c.Networking.CNIConfig.ConfigPath = OpenShiftCNIConfigPath
c.Networking.CNIConfig.BinaryPath = OpenShiftCNIBinaryPath
}
}
func (c *KubernetesClusterConfig) fillWithKubeadmConfigMap() error {
kubeadmConfigMap, err := getConfigMapFromOtherNamespace("kubeadm-config", "kube-system")
if err != nil {
// Which config map is used for containing parameters depends on method of deployment.
// So, there is only one system config map at a time: kubeadm or cluster.
// Thats why it's ok to be not found.
if errors.IsNotFound(err) {
return nil
}
clusterInfoLog.Error(err, "Failed to fill with kubeadm config map.")
return err
}
yamlString := kubeadmConfigMap.Data["ClusterConfiguration"]
if err = yamlToStruct(yamlString, c); err != nil {
clusterInfoLog.Error(err, "Failed to yamlToStruct for kubeadmConfig ClusterConfiguration")
}
if c.ControlPlaneEndpoint != "" {
return nil
}
var clusterStatusStruct struct {
ApiEndpoints map[string]struct {
AdvertiseAddress string
BindPort *int
}
}
yamlString = kubeadmConfigMap.Data["ClusterStatus"]
if err = yamlToStruct(yamlString, &clusterStatusStruct); err != nil {
clusterInfoLog.Error(err, "Failed to yamlToStruct for kubeadmConfig CluserStatus")
return err
}
if clusterStatusStruct.ApiEndpoints == nil {
return nil
}
for _, val := range clusterStatusStruct.ApiEndpoints {
if val.AdvertiseAddress != "" && val.BindPort != nil {
c.ControlPlaneEndpoint = fmt.Sprintf("%v:%v", val.AdvertiseAddress, *val.BindPort)
return nil
}
}
return nil
}
func (c *KubernetesClusterConfig) fillWithClusterConfigMap() error {
clusterConfigMap, err := getConfigMapFromOtherNamespace("cluster-config-v1", "kube-system")
if err != nil {
// Which config map is used for containing parameters depends on method of deployment.
// So, there is only one system config map at a time: kubeadm or cluster.
// Thats why it's ok to be not found.
if errors.IsNotFound(err) {
return nil
}
clusterInfoLog.Error(err, "Failed to fill with cluster config map.")
return err
}
yamlString := clusterConfigMap.Data["install-config"]
var clusterConfigStruct struct {
BaseDomain string
Metadata struct {
Name string
}
Networking struct {
ClusterNetwork []struct {
Cidr string
HostPrefix string
}
ServiceNetwork []string
}
}
if err := yamlToStruct(yamlString, &clusterConfigStruct); err != nil {
return err
}
c.ClusterName = clusterConfigStruct.Metadata.Name
c.Networking.DNSDomain = clusterConfigStruct.BaseDomain
if len(clusterConfigStruct.Networking.ClusterNetwork) > 0 {
c.Networking.PodSubnet = clusterConfigStruct.Networking.ClusterNetwork[0].Cidr
}
if len(clusterConfigStruct.Networking.ServiceNetwork) > 0 {
c.Networking.ServiceSubnet = clusterConfigStruct.Networking.ServiceNetwork[0]
}
return nil
}
// ClusterParameters returns cluster configuration, merged from manager configuration and system configmaps
func ClusterParameters(client client.Client) (*KubernetesClusterConfig, error) {
var defaultConfig, kubeadmConfig, clusterConfig KubernetesClusterConfig
defaultConfig.fillWithDefaultValues()
if err := kubeadmConfig.fillWithKubeadmConfigMap(); err != nil {
return nil, err
}
if err := clusterConfig.fillWithClusterConfigMap(); err != nil {
return nil, err
}
resultConfig := defaultConfig.replaceFields(kubeadmConfig).
replaceFields(clusterConfig)
if resultConfig.ControlPlaneEndpoint == "" {
resultConfig.ControlPlaneEndpoint = fmt.Sprintf("api.%v.%v:%v",
resultConfig.ClusterName,
resultConfig.Networking.DNSDomain,
strconv.Itoa(KubernetesApiSSLPort),
)
}
return resultConfig, nil
}
// KubernetesAPISSLPort gathers SSL Port from Kubernetes Cluster via kubeadm-config ConfigMap
func (c *KubernetesClusterConfig) KubernetesAPISSLPort() (int, error) {
controlPlaneEndpoint := c.ControlPlaneEndpoint
_, kubernetesAPISSLPort, err := net.SplitHostPort(controlPlaneEndpoint)
if err != nil {
return 0, err
}
kubernetesAPISSLPortInt, err := strconv.Atoi(kubernetesAPISSLPort)
if err != nil {
return 0, err
}
return kubernetesAPISSLPortInt, nil
}
// KubernetesAPIServer gathers API Server from Kubernetes Cluster via kubeadm-config ConfigMap
func (c *KubernetesClusterConfig) KubernetesAPIServer() (string, error) {
controlPlaneEndpoint := c.ControlPlaneEndpoint
kubernetesAPIServer, _, err := net.SplitHostPort(controlPlaneEndpoint)
if err != nil {
return "", err
}
return kubernetesAPIServer, nil
}