/
deploy.go
295 lines (237 loc) · 7.11 KB
/
deploy.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
// 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"
"log"
"os"
"strconv"
"time"
"github.com/siderolabs/go-retry/retry"
"github.com/siderolabs/talos/pkg/machinery/constants"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client"
"github.com/siderolabs/capi-utils/pkg/capi/infrastructure"
)
// DeployOption defines a single CAPI cluster creation option.
type DeployOption func(opts *DeployOptions) error
// DeployOptions cluster deployment options.
type DeployOptions struct {
providerOptions interface{}
Provider string
ProviderVersion string
ClusterName string
ClusterNamespace string
TalosVersion string
KubernetesVersion string
TemplateFile string
Template []byte
ControlPlaneNodes int64
WorkerNodes int64
}
// DefaultDeployOptions default deployment settings.
func DefaultDeployOptions() *DeployOptions {
return &DeployOptions{
ControlPlaneNodes: 1,
WorkerNodes: 1,
ClusterNamespace: "default",
TalosVersion: "v1.7",
KubernetesVersion: constants.DefaultKubernetesVersion,
}
}
// WithControlPlaneNodes creates a cluster with N control plane nodes.
func WithControlPlaneNodes(count int64) DeployOption {
return func(o *DeployOptions) error {
o.ControlPlaneNodes = count
return nil
}
}
// WithWorkerNodes creates a cluster with N worker nodes.
func WithWorkerNodes(count int64) DeployOption {
return func(o *DeployOptions) error {
o.WorkerNodes = count
return nil
}
}
// WithProvider sets cluster provider.
func WithProvider(name string) DeployOption {
return func(o *DeployOptions) error {
o.Provider = name
return nil
}
}
// WithProviderVersion picks provider with a specific version.
func WithProviderVersion(version string) DeployOption {
return func(o *DeployOptions) error {
o.ProviderVersion = version
return nil
}
}
// WithTalosVersion sets Talos version.
func WithTalosVersion(version string) DeployOption {
return func(o *DeployOptions) error {
o.TalosVersion = version
return nil
}
}
// WithKubernetesVersion sets Kubernetes version.
func WithKubernetesVersion(version string) DeployOption {
return func(o *DeployOptions) error {
o.KubernetesVersion = version
return nil
}
}
// WithTemplateFile load cluster template from the file.
func WithTemplateFile(path string) DeployOption {
return func(o *DeployOptions) error {
o.TemplateFile = path
return nil
}
}
// WithTemplate loads cluster template from memory.
func WithTemplate(data []byte) DeployOption {
return func(o *DeployOptions) error {
o.Template = data
return nil
}
}
// WithDeployOptions sets deploy options as a struct.
func WithDeployOptions(val *DeployOptions) DeployOption {
return func(o *DeployOptions) error {
*o = *val
return nil
}
}
// WithProviderOptions sets provider specific deploy options.
func WithProviderOptions(val interface{}) DeployOption {
return func(o *DeployOptions) error {
o.providerOptions = val
return nil
}
}
// WithClusterNamespace sets cluster namespace.
func WithClusterNamespace(val string) DeployOption {
return func(o *DeployOptions) error {
o.ClusterNamespace = val
return nil
}
}
// DeployCluster creates a new cluster.
//
//nolint:gocognit
func (clusterAPI *Manager) DeployCluster(ctx context.Context, clusterName string, setters ...DeployOption) (*Cluster, error) {
if len(clusterAPI.providers) == 0 {
return nil, fmt.Errorf("no infrastructure providers are installed")
}
options := DefaultDeployOptions()
for _, setter := range setters {
if err := setter(options); err != nil {
return nil, err
}
}
options.ClusterName = clusterName
var provider infrastructure.Provider
if options.Provider != "" {
for _, p := range clusterAPI.providers {
if p.Name() == options.Provider {
if options.ProviderVersion != "" && p.Version() != options.ProviderVersion {
continue
}
provider = p
break
}
}
if provider == nil {
return nil, fmt.Errorf("no provider with name %s is installed", options.Provider)
}
} else {
provider = clusterAPI.providers[0]
}
// set up env variables common for all providers
clusterAPI.patchConfig(infrastructure.Variables{
"TALOS_VERSION": options.TalosVersion,
"KUBERNETES_VERSION": options.KubernetesVersion,
"CLUSTER_NAME": options.ClusterName,
"CONTROL_PLANE_MACHINE_COUNT": strconv.FormatInt(options.ControlPlaneNodes, 10),
"WORKER_MACHINE_COUNT": strconv.FormatInt(options.WorkerNodes, 10),
})
templateOptions := client.GetClusterTemplateOptions{
Kubeconfig: clusterAPI.kubeconfig,
ClusterName: options.ClusterName,
ControlPlaneMachineCount: &options.ControlPlaneNodes,
WorkerMachineCount: &options.WorkerNodes,
}
if options.Template != nil {
file, err := os.CreateTemp("", "clusterTemplate")
if err != nil {
log.Fatal(err)
}
templateOptions.URLSource = &client.URLSourceOptions{
URL: file.Name(),
}
if _, err = file.Write(options.Template); err != nil {
return nil, err
}
defer file.Close() //nolint:errcheck
} else if options.TemplateFile != "" {
templateOptions.URLSource = &client.URLSourceOptions{
URL: options.TemplateFile,
}
}
vars, err := provider.ClusterVars(options.providerOptions)
if err != nil {
return nil, err
}
clusterAPI.patchConfig(vars)
template, err := provider.GetClusterTemplate(clusterAPI.client, templateOptions)
if err != nil {
return nil, err
}
for _, obj := range template.Objs() {
if err = clusterAPI.runtimeClient.Create(ctx, &obj); err != nil {
return nil, err
}
}
deployedCluster, err := clusterAPI.NewCluster(ctx, options.ClusterName, options.ClusterNamespace)
if err != nil {
return nil, err
}
if err = retry.Constant(30*time.Minute, retry.WithUnits(10*time.Second), retry.WithErrorLogging(true)).Retry(func() error {
return clusterAPI.CheckClusterReady(ctx, deployedCluster)
}); err != nil {
return nil, err
}
return deployedCluster, nil
}
// DestroyCluster deletes cluster.
func (clusterAPI *Manager) DestroyCluster(ctx context.Context, name, namespace string) error {
cluster := &unstructured.Unstructured{}
cluster.SetName(name)
cluster.SetNamespace(namespace)
cluster.SetGroupVersionKind(schema.GroupVersionKind{
Group: "cluster.x-k8s.io",
Kind: "Cluster",
Version: clusterAPI.version,
})
if err := clusterAPI.runtimeClient.Delete(ctx, cluster); err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
return retry.Constant(30*time.Minute, retry.WithUnits(10*time.Second), retry.WithErrorLogging(true)).Retry(func() error {
err := clusterAPI.runtimeClient.Get(ctx, types.NamespacedName{Name: name, Namespace: namespace}, cluster)
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
return retry.ExpectedError(fmt.Errorf("cluster is being deleted"))
})
}