-
Notifications
You must be signed in to change notification settings - Fork 876
/
etcd.go
100 lines (81 loc) · 2.75 KB
/
etcd.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
/*
Copyright 2023 The Karmada Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tasks
import (
"errors"
"fmt"
"k8s.io/klog/v2"
"github.com/karmada-io/karmada/operator/pkg/controlplane/etcd"
"github.com/karmada-io/karmada/operator/pkg/util/apiclient"
"github.com/karmada-io/karmada/operator/pkg/workflow"
)
// NewEtcdTask init a etcd task to install etcd component
func NewEtcdTask() workflow.Task {
return workflow.Task{
Name: "Etcd",
Run: runEtcd,
RunSubTasks: true,
Tasks: []workflow.Task{
{
Name: "deploy-etcd",
Run: runDeployEtcd,
},
{
Name: "wait-etcd",
Run: runWaitEtcd,
},
},
}
}
func runEtcd(r workflow.RunData) error {
data, ok := r.(InitData)
if !ok {
return errors.New("etcd task invoked with an invalid data struct")
}
klog.V(4).InfoS("[etcd] Running etcd task", "karmada", klog.KObj(data))
return nil
}
func runDeployEtcd(r workflow.RunData) error {
data, ok := r.(InitData)
if !ok {
return errors.New("deploy-etcd task invoked with an invalid data struct")
}
cfg := data.Components()
if cfg.Etcd.External != nil {
klog.V(2).InfoS("[etcd] use external etcd, skip install etcd job", "karmada", data.GetName())
return nil
}
if cfg.Etcd.Local == nil {
return errors.New("unexpected empty etcd local configuration")
}
err := etcd.EnsureKarmadaEtcd(data.RemoteClient(), cfg.Etcd.Local, data.GetName(), data.GetNamespace())
if err != nil {
return fmt.Errorf("failed to install etcd component, err: %w", err)
}
klog.V(2).InfoS("[deploy-etcd] Successfully installed etcd component", "karmada", klog.KObj(data))
return nil
}
func runWaitEtcd(r workflow.RunData) error {
data, ok := r.(InitData)
if !ok {
return errors.New("wait-etcd task invoked with an invalid data struct")
}
waiter := apiclient.NewKarmadaWaiter(data.ControlplaneConfig(), data.RemoteClient(), componentBeReadyTimeout)
// wait etcd, karmada apiserver and aggregated apiserver to ready
// as long as a replica of pod is ready, we consider the service available.
if err := waiter.WaitForSomePods(etcdLabels.String(), data.GetNamespace(), 1); err != nil {
return fmt.Errorf("waiting for karmada-etcd to ready timeout, err: %w", err)
}
klog.V(2).InfoS("[wait-etcd] the etcd pods is ready", "karmada", klog.KObj(data))
return nil
}