-
Notifications
You must be signed in to change notification settings - Fork 107
/
inspection.go
231 lines (210 loc) · 8.22 KB
/
inspection.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
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2023 Tencent. All Rights Reserved.
*
* 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
*
* https://opensource.org/licenses/Apache-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 OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package inspection
import (
"context"
"fmt"
"sync"
"time"
clientv3 "go.etcd.io/etcd/client/v3"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
kstonev1alpha2 "tkestack.io/kstone/pkg/apis/kstone/v1alpha2"
"tkestack.io/kstone/pkg/controllers/util"
"tkestack.io/kstone/pkg/etcd"
"tkestack.io/kstone/pkg/featureprovider"
featureutil "tkestack.io/kstone/pkg/featureprovider/util"
clientset "tkestack.io/kstone/pkg/generated/clientset/versioned"
platformscheme "tkestack.io/kstone/pkg/generated/clientset/versioned/scheme"
)
const (
DefaultInspectionInterval = 300 * time.Second
DefaultInspectionPath = ""
)
type Server struct {
cli *clientset.Clientset
kubeCli kubernetes.Interface
client map[string]*clientv3.Client
wchan map[string]clientv3.WatchChan
watcher map[string]clientv3.Watcher
eventCh map[string]chan *clientv3.Event
mux sync.Mutex
clientConfigGetter etcd.ClientConfigGetter
}
// NewInspectionServer generates the server of inspection
func NewInspectionServer(ctx *featureprovider.FeatureContext) (*Server, error) {
cli, err := clientset.NewForConfig(ctx.ClientBuilder.ConfigOrDie())
if err != nil {
klog.Errorf("failed to init etcdinspection client, err is %v", err)
return nil, err
}
return &Server{
kubeCli: ctx.ClientBuilder.ClientOrDie(),
cli: cli,
client: make(map[string]*clientv3.Client),
wchan: make(map[string]clientv3.WatchChan),
watcher: make(map[string]clientv3.Watcher),
eventCh: make(map[string]chan *clientv3.Event),
clientConfigGetter: ctx.ClientConfigGetter,
}, nil
}
// GetEtcdCluster gets etcdcluster
func (c *Server) GetEtcdCluster(namespace, name string) (*kstonev1alpha2.EtcdCluster, error) {
return c.cli.KstoneV1alpha2().EtcdClusters(namespace).Get(context.TODO(), name, metav1.GetOptions{})
}
// GetEtcdInspection gets etcdinspection
func (c *Server) GetEtcdInspection(namespace, name string) (*kstonev1alpha2.EtcdInspection, error) {
inspectionTask, err := c.cli.KstoneV1alpha2().EtcdInspections(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
klog.Errorf("failed to get etcdinspection, err: %v, namespace is %s, name is %s", err, namespace, name)
}
return nil, err
}
return inspectionTask, nil
}
// DeleteEtcdInspection deletes etcdinspection
func (c *Server) DeleteEtcdInspection(namespace, name string) error {
err := c.cli.KstoneV1alpha2().EtcdInspections(namespace).
Delete(context.TODO(), name, metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
klog.Errorf(
"failed to delete etcdinspection, namespace is %s, name is %s, err is %v",
namespace,
name,
err,
)
return err
}
return nil
}
// CreateEtcdInspection creates etcdinspection
func (c *Server) CreateEtcdInspection(inspection *kstonev1alpha2.EtcdInspection) (*kstonev1alpha2.EtcdInspection, error) {
newinspectionTask, err := c.cli.KstoneV1alpha2().EtcdInspections(inspection.Namespace).
Create(context.TODO(), inspection, metav1.CreateOptions{})
if err != nil && !apierrors.IsAlreadyExists(err) {
klog.Errorf(
"failed to create etcdinspection, namespace is %s, name is %s, err is %v",
inspection.Namespace,
inspection.Name,
err,
)
return newinspectionTask, err
}
return newinspectionTask, nil
}
func (c *Server) initInspectionTask(
cluster *kstonev1alpha2.EtcdCluster,
inspectionFeatureName kstonev1alpha2.KStoneFeature,
) (*kstonev1alpha2.EtcdInspection, error) {
name := cluster.Name + "-" + string(inspectionFeatureName)
inspectionTask := &kstonev1alpha2.EtcdInspection{}
inspectionTask.ObjectMeta = metav1.ObjectMeta{
Name: name,
Namespace: cluster.Namespace,
Labels: cluster.Labels,
}
inspectionTask.Spec = kstonev1alpha2.EtcdInspectionSpec{
InspectionType: string(inspectionFeatureName),
ClusterName: cluster.Name,
}
inspectionTask.Status = kstonev1alpha2.EtcdInspectionStatus{
LastUpdatedTime: metav1.Time{
Time: time.Now(),
},
}
err := controllerutil.SetOwnerReference(cluster, inspectionTask, platformscheme.Scheme)
if err != nil {
klog.Errorf("set inspection task's owner failed, err is %v", err)
return inspectionTask, err
}
return inspectionTask, nil
}
func (c *Server) GetEtcdClusterInfo(namespace, name string) (*kstonev1alpha2.EtcdCluster, *etcd.ClientConfig, error) {
cluster, err := c.GetEtcdCluster(namespace, name)
if err != nil {
klog.Errorf("failed to get cluster info, namespace is %s, name is %s, err is %v", namespace, name, err)
return nil, nil, err
}
annotations := cluster.ObjectMeta.Annotations
secretName := ""
if annotations != nil {
if _, found := annotations[util.ClusterTLSSecretName]; found {
secretName = annotations[util.ClusterTLSSecretName]
}
}
path := fmt.Sprintf("%s/%s", cluster.Namespace, cluster.Name)
clientConfig, err := c.clientConfigGetter.New(path, secretName)
if err != nil {
klog.Errorf("failed to get cluster, namespace is %s, name is %s, err is %v", namespace, name, err)
return nil, nil, err
}
return cluster, clientConfig, nil
}
func (c *Server) Equal(cluster *kstonev1alpha2.EtcdCluster, inspectionFeatureName kstonev1alpha2.KStoneFeature) bool {
if !featureutil.IsFeatureGateEnabled(cluster.ObjectMeta.Annotations, inspectionFeatureName) {
if cluster.Status.FeatureGatesStatus[inspectionFeatureName] != featureutil.FeatureStatusDisabled {
return c.checkEqualIfDisabled(cluster, inspectionFeatureName)
}
return true
}
return c.checkEqualIfEnabled(cluster, inspectionFeatureName)
}
func (c *Server) Sync(cluster *kstonev1alpha2.EtcdCluster, inspectionFeatureName kstonev1alpha2.KStoneFeature) error {
if !featureutil.IsFeatureGateEnabled(cluster.ObjectMeta.Annotations, inspectionFeatureName) {
return c.cleanInspectionTask(cluster, inspectionFeatureName)
}
return c.syncInspectionTask(cluster, inspectionFeatureName)
}
// CheckEqualIfDisabled Checks whether the inspection task has been deleted if the inspection feature is disabled.
func (c *Server) checkEqualIfDisabled(cluster *kstonev1alpha2.EtcdCluster, inspectionFeatureName kstonev1alpha2.KStoneFeature) bool {
name := cluster.Name + "-" + string(inspectionFeatureName)
if _, err := c.GetEtcdInspection(cluster.Namespace, name); apierrors.IsNotFound(err) {
return true
}
return false
}
// CheckEqualIfEnabled check whether the desired inspection task are consistent with the actual task,
// if the inspection feature is enabled.
func (c *Server) checkEqualIfEnabled(cluster *kstonev1alpha2.EtcdCluster, inspectionFeatureName kstonev1alpha2.KStoneFeature) bool {
name := cluster.Name + "-" + string(inspectionFeatureName)
if _, err := c.GetEtcdInspection(cluster.Namespace, name); err == nil {
return true
}
return false
}
// CleanInspectionTask cleans inspection task
func (c *Server) cleanInspectionTask(cluster *kstonev1alpha2.EtcdCluster, inspectionFeatureName kstonev1alpha2.KStoneFeature) error {
name := cluster.Name + "-" + string(inspectionFeatureName)
return c.DeleteEtcdInspection(cluster.Namespace, name)
}
// SyncInspectionTask syncs inspection task
func (c *Server) syncInspectionTask(cluster *kstonev1alpha2.EtcdCluster, inspectionFeatureName kstonev1alpha2.KStoneFeature) error {
task, err := c.initInspectionTask(cluster, inspectionFeatureName)
if err != nil {
return err
}
_, err = c.CreateEtcdInspection(task)
if err != nil {
return err
}
return nil
}