/
helm_prober.go
228 lines (209 loc) · 7.46 KB
/
helm_prober.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
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2019 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 helm
import (
"context"
"fmt"
"sync"
"time"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
clientset "tkestack.io/tke/api/client/clientset/versioned"
lister "tkestack.io/tke/api/client/listers/platform/v1"
v1 "tkestack.io/tke/api/platform/v1"
controllerUtil "tkestack.io/tke/pkg/controller"
"tkestack.io/tke/pkg/util/log"
)
const (
checkingHcInterval = 5 * time.Second
runningHcInterval = 2 * time.Minute
unhealthyHcInternal = 20 * time.Second
failedHcInternal = 5 * time.Minute
checkingTimeout = 5 * time.Minute
unhealthyTimeout = 5 * time.Minute
)
// Prober is used for probing the instance status
type Prober interface {
// Run means starts to run prober
Run(ch <-chan struct{})
// Exist means whether key is in the prober
Exist(key string) bool
// ExistByPhase means whether key is in the prober for the given Addon
ExistByPhase(key string, phase v1.AddonPhase) bool
// Set means sets a prober for the given Addon
Set(key string, phase v1.AddonPhase)
// Del means remove a prober of the given key
Del(key string)
}
type prober struct {
checking sync.Map
running sync.Map
unhealthy sync.Map
failed sync.Map
lister lister.HelmLister
client clientset.Interface
}
// NewHealthProber returns a prober
func NewHealthProber(lister lister.HelmLister, client clientset.Interface) Prober {
prober := &prober{
lister: lister,
client: client,
}
return prober
}
func (p *prober) Exist(key string) bool {
if p.ExistByPhase(key, v1.AddonPhaseChecking) || p.ExistByPhase(key, v1.AddonPhaseRunning) ||
p.ExistByPhase(key, v1.AddonPhaseFailed) || p.ExistByPhase(key, v1.AddonPhaseUnhealthy) {
return true
}
return false
}
// ExistByPhase tells whether key is in the prober according to the phase
func (p *prober) ExistByPhase(key string, phase v1.AddonPhase) bool {
if _, ok := p.fetchSyncMap(phase).Load(key); !ok {
return false
}
return true
}
func (p *prober) Set(key string, phase v1.AddonPhase) {
p.fetchSyncMap(phase).Store(key, true)
}
func (p *prober) Del(key string) {
p.delByPhase(key, v1.AddonPhaseChecking)
p.delByPhase(key, v1.AddonPhaseRunning)
p.delByPhase(key, v1.AddonPhaseFailed)
p.delByPhase(key, v1.AddonPhaseUnhealthy)
}
func (p *prober) delByPhase(key string, phase v1.AddonPhase) {
p.fetchSyncMap(phase).Delete(key)
}
func (p *prober) Run(ch <-chan struct{}) {
go p.runProbe(v1.AddonPhaseChecking, checkingHcInterval, ch)
go p.runProbe(v1.AddonPhaseRunning, runningHcInterval, ch)
go p.runProbe(v1.AddonPhaseUnhealthy, unhealthyHcInternal, ch)
go p.runProbe(v1.AddonPhaseFailed, failedHcInternal, ch)
}
func (p *prober) runProbe(phase v1.AddonPhase, duration time.Duration, ch <-chan struct{}) {
log.Info(fmt.Sprintf("Helm %s prober begin running", phase))
defer controllerUtil.CatchPanic(fmt.Sprintf("%s prober", phase), "Helm")
p.periodHealthCheck(context.Background(), phase, duration, ch)
}
func (p *prober) periodHealthCheck(ctx context.Context, phase v1.AddonPhase, duration time.Duration, ch <-chan struct{}) {
wait.Until(func() {
p.fetchSyncMap(phase).Range(
func(k, v interface{}) bool {
key := k.(string)
log.Info("Probe check", log.String("helm", key), log.String("phase", string(phase)))
curPhase, err := p.checkAddonHealthz(ctx, key, phase)
if curPhase == "" || (curPhase != phase && err == nil) {
p.fetchSyncMap(phase).Delete(key)
return true
}
return true
})
}, duration, ch)
}
func (p *prober) fetchSyncMap(phase v1.AddonPhase) *sync.Map {
switch phase {
case v1.AddonPhaseRunning:
return &p.running
case v1.AddonPhaseChecking:
return &p.checking
case v1.AddonPhaseUnhealthy:
return &p.unhealthy
case v1.AddonPhaseFailed:
return &p.failed
default:
log.Info(fmt.Sprintf("set unrecognized phase %v", phase))
}
return &sync.Map{}
}
func (p *prober) checkAddonHealthz(ctx context.Context, key string, oldPhase v1.AddonPhase) (v1.AddonPhase, error) {
// 判断 key 没有被删除
obj, err := p.lister.Get(key)
if err != nil {
// delete key from map
return "", err
}
// 创建 provisioner 与用户集群交互
var provisioner Provisioner
if provisioner, err = createProvisioner(ctx, obj, p.client); err != nil {
if errors.IsNotFound(err) {
uptErr := updateHelmStatus(ctx, getUpdateObj(obj, v1.AddonPhaseFailed, err.Error()), p.client)
return v1.AddonPhaseFailed, uptErr
}
if isCheckOrUnhealthyTimeout(obj.Status.LastReInitializingTimestamp.Time, oldPhase) {
if err.Error() == "" {
uptErr := updateHelmStatus(ctx, getUpdateObj(obj, v1.AddonPhaseFailed, string(oldPhase)+" timeout: fail to connect to tiller"), p.client)
return v1.AddonPhaseFailed, uptErr
}
uptErr := updateHelmStatus(ctx, getUpdateObj(obj, v1.AddonPhaseFailed, err.Error()), p.client)
return v1.AddonPhaseFailed, uptErr
}
if oldPhase == v1.AddonPhaseUnhealthy && obj.Status.Reason != err.Error() {
uptErr := updateHelmStatus(ctx, getUpdateObj(obj, v1.AddonPhaseUnhealthy, err.Error()), p.client)
return oldPhase, uptErr
}
return v1.AddonPhaseUnhealthy, nil
}
// 获取 addon 状态
err = provisioner.GetStatus(ctx)
if err != nil && errors.IsNotFound(err) {
uptErr := updateHelmStatus(ctx, getUpdateObj(obj, v1.AddonPhaseFailed, err.Error()), p.client)
return v1.AddonPhaseFailed, uptErr
}
if err != nil {
// checking timeout or unhealthy timeout
if isCheckOrUnhealthyTimeout(obj.Status.LastReInitializingTimestamp.Time, oldPhase) {
if err.Error() == "" {
uptErr := updateHelmStatus(ctx, getUpdateObj(obj, v1.AddonPhaseFailed, string(oldPhase)+" timeout: fail to connect to tiller"), p.client)
return v1.AddonPhaseFailed, uptErr
}
uptErr := updateHelmStatus(ctx, getUpdateObj(obj, v1.AddonPhaseFailed, err.Error()), p.client)
return v1.AddonPhaseFailed, uptErr
}
// running -> unhealthy
if oldPhase == v1.AddonPhaseRunning {
newObj := getUpdateObj(obj, v1.AddonPhaseUnhealthy, err.Error())
newObj.Status.LastReInitializingTimestamp = metav1.Now()
uptErr := updateHelmStatus(ctx, newObj, p.client)
return v1.AddonPhaseUnhealthy, uptErr
}
// change reason, stay old phase
if obj.Status.Reason != err.Error() {
uptErr := updateHelmStatus(ctx, getUpdateObj(obj, oldPhase, err.Error()), p.client)
return oldPhase, uptErr
}
return oldPhase, nil
}
// other phases -> running
if oldPhase != v1.AddonPhaseRunning {
uptErr := updateHelmStatus(ctx, getUpdateObj(obj, v1.AddonPhaseRunning, ""), p.client)
return v1.AddonPhaseRunning, uptErr
}
// stay running
return v1.AddonPhaseRunning, nil
}
func isCheckOrUnhealthyTimeout(recordTime time.Time, oldPhase v1.AddonPhase) bool {
if (oldPhase == v1.AddonPhaseChecking && time.Since(recordTime) >= checkingTimeout) ||
(oldPhase == v1.AddonPhaseUnhealthy && time.Since(recordTime) >= unhealthyTimeout) {
return true
}
return false
}