/
cluster.go
330 lines (266 loc) · 8.6 KB
/
cluster.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package capi
import (
"context"
"fmt"
"io"
"os"
"time"
"github.com/siderolabs/go-retry/retry"
talosclusterapi "github.com/siderolabs/talos/pkg/machinery/api/cluster"
talosclient "github.com/siderolabs/talos/pkg/machinery/client"
clientconfig "github.com/siderolabs/talos/pkg/machinery/client/config"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
clientcmd "k8s.io/client-go/tools/clientcmd"
capiclient "sigs.k8s.io/cluster-api/cmd/clusterctl/client"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
// Cluster attaches to the provisioned CAPI cluster and provides talos.Cluster.
type Cluster struct {
manager *Manager
client *talosclient.Client
clientConfig *clientconfig.Config
cluster unstructured.Unstructured
name string
namespace string
controlPlaneNodes []string
workerNodes []string
}
// NewCluster fetches cluster info from the CAPI state.
func (clusterAPI *Manager) NewCluster(ctx context.Context, name, namespace string) (*Cluster, error) {
res := &Cluster{
manager: clusterAPI,
name: name,
namespace: namespace,
}
if err := res.sync(ctx); err != nil {
return nil, err
}
return res, nil
}
// Sync updates nodes pool and recreates talos client.
//
//nolint:gocyclo,cyclop
func (cluster *Cluster) Sync(ctx context.Context) error {
var (
controlPlane unstructured.Unstructured
machines unstructured.UnstructuredList
controlPlaneNodes = []string{}
workerNodes = []string{}
configEndpoints = []string{}
)
machines.SetGroupVersionKind(
schema.GroupVersionKind{
Version: cluster.manager.version,
Group: "cluster.x-k8s.io",
Kind: "Machine",
},
)
var (
controlPlaneSelector string
found bool
err error
)
controlPlaneRef, err := getRef(cluster.cluster.Object, "spec", "controlPlaneRef")
if err != nil {
return err
}
controlPlane.SetGroupVersionKind(controlPlaneRef.gvk)
if err = cluster.manager.runtimeClient.Get(ctx, controlPlaneRef.NamespacedName, &controlPlane); err != nil {
return err
}
if controlPlaneSelector, found, err = unstructured.NestedString(controlPlane.Object, "status", "selector"); err != nil {
return err
} else if !found {
return fieldNotFound("status", "selector")
}
labelSelector, err := labels.Parse(controlPlaneSelector)
if err != nil {
return err
}
if err = cluster.manager.runtimeClient.List(ctx, &machines, runtimeclient.MatchingLabelsSelector{Selector: labelSelector}); err != nil {
return err
}
if len(machines.Items) < 1 {
return fmt.Errorf("not enough machines found")
}
var talosConfig v1.Secret
if err = cluster.manager.runtimeClient.Get(ctx, types.NamespacedName{
Namespace: cluster.Namespace(),
Name: cluster.Name() + "-talosconfig",
}, &talosConfig); err != nil {
return err
}
cluster.clientConfig, err = clientconfig.FromBytes(talosConfig.Data["talosconfig"])
if err != nil {
return err
}
kubeconfig, err := cluster.manager.GetKubeconfig(ctx)
if err != nil {
return err
}
options := capiclient.GetKubeconfigOptions{
Kubeconfig: kubeconfig,
WorkloadClusterName: cluster.name,
Namespace: cluster.namespace,
}
raw, err := cluster.manager.client.GetKubeconfig(ctx, options)
if err != nil {
return err
}
config, err := clientcmd.RESTConfigFromKubeConfig([]byte(raw))
if err != nil {
return err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return err
}
nodes, err := clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
for _, node := range nodes.Items {
_, controlPlane := node.Labels["node-role.kubernetes.io/control-plane"]
for _, address := range node.Status.Addresses {
switch {
case address.Type == v1.NodeExternalIP && controlPlane:
configEndpoints = append(configEndpoints, address.Address)
case address.Type == v1.NodeInternalIP && controlPlane:
controlPlaneNodes = append(controlPlaneNodes, address.Address)
case address.Type == v1.NodeInternalIP:
workerNodes = append(workerNodes, address.Address)
}
}
}
if len(configEndpoints) < 1 {
return fmt.Errorf("failed to find control plane nodes")
}
cluster.clientConfig.Contexts[cluster.clientConfig.Context].Endpoints = configEndpoints
var talosClient *talosclient.Client
talosClient, err = talosclient.New(ctx, talosclient.WithConfig(cluster.clientConfig))
if err != nil {
return err
}
cluster.client = talosClient
cluster.controlPlaneNodes = controlPlaneNodes
cluster.workerNodes = workerNodes
return nil
}
// TalosClient returns new talos client for the CAPI cluster.
func (cluster *Cluster) TalosClient(ctx context.Context) (*talosclient.Client, error) {
if cluster.client != nil {
return cluster.client, nil
}
if err := cluster.Sync(ctx); err != nil {
return nil, err
}
return cluster.client, nil
}
// TalosConfig returns talosconfig for the cluster.
func (cluster *Cluster) TalosConfig(ctx context.Context) (*clientconfig.Config, error) {
if cluster.clientConfig != nil {
return cluster.clientConfig, nil
}
if err := cluster.Sync(ctx); err != nil {
return nil, err
}
return cluster.clientConfig, nil
}
// Health runs the healthcheck for the cluster.
func (cluster *Cluster) Health(ctx context.Context) error {
return retry.Constant(5*time.Minute, retry.WithUnits(10*time.Second)).RetryWithContext(ctx, func(ctx context.Context) error {
// retry health checks as sometimes bootstrap bootkube issues break the check
return retry.ExpectedError(cluster.health(ctx))
})
}
func (cluster *Cluster) health(ctx context.Context) error {
client, err := cluster.TalosClient(ctx)
if err != nil {
return err
}
resp, err := client.ClusterHealthCheck(talosclient.WithNodes(ctx, cluster.controlPlaneNodes[0]), 3*time.Minute, &talosclusterapi.ClusterInfo{
ControlPlaneNodes: cluster.controlPlaneNodes,
WorkerNodes: cluster.workerNodes,
})
if err != nil {
return err
}
if err := resp.CloseSend(); err != nil {
return err
}
for {
msg, err := resp.Recv()
if err != nil {
if err == io.EOF || status.Code(err) == codes.Canceled { //nolint:errorlint
return nil
}
return err
}
if msg.GetMetadata().GetError() != "" {
return fmt.Errorf("healthcheck error: %s", msg.GetMetadata().GetError())
}
fmt.Fprintln(os.Stderr, msg.GetMessage())
}
}
// Name of the cluster.
func (cluster *Cluster) Name() string {
return cluster.name
}
// Namespace of the cluster.
func (cluster *Cluster) Namespace() string {
return cluster.namespace
}
// ControlPlanes gets controlplane object from the management cluster.
func (cluster *Cluster) ControlPlanes(ctx context.Context) (*unstructured.Unstructured, error) {
controlPlaneRef, err := getRef(cluster.cluster.Object, "spec", "controlPlaneRef")
if err != nil {
return nil, err
}
var controlPlane unstructured.Unstructured
controlPlane.SetGroupVersionKind(controlPlaneRef.gvk)
if err = cluster.manager.runtimeClient.Get(ctx, controlPlaneRef.NamespacedName, &controlPlane); err != nil {
return nil, err
}
return &controlPlane, nil
}
// Workers gets MachineDeployment list from the management cluster.
func (cluster *Cluster) Workers(ctx context.Context) (*unstructured.UnstructuredList, error) {
var machineDeployments unstructured.UnstructuredList
machineDeployments.SetGroupVersionKind(
schema.GroupVersionKind{
Version: cluster.manager.version,
Group: "cluster.x-k8s.io",
Kind: "MachineDeployment",
},
)
labelSelector, err := labels.Parse(fmt.Sprintf("cluster.x-k8s.io/cluster-name=%s", cluster.name))
if err != nil {
return nil, err
}
if err = cluster.manager.runtimeClient.List(ctx, &machineDeployments, runtimeclient.MatchingLabelsSelector{Selector: labelSelector}); err != nil {
return nil, err
}
return &machineDeployments, nil
}
func (cluster *Cluster) sync(ctx context.Context) error {
cluster.cluster.SetGroupVersionKind(
schema.GroupVersionKind{
Version: cluster.manager.version,
Group: "cluster.x-k8s.io",
Kind: "Cluster",
},
)
clusterRef := types.NamespacedName{Namespace: cluster.namespace, Name: cluster.name}
return cluster.manager.runtimeClient.Get(ctx, clusterRef, &cluster.cluster)
}