-
Notifications
You must be signed in to change notification settings - Fork 155
/
restart.go
133 lines (114 loc) · 5.09 KB
/
restart.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
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package operations
import (
"fmt"
"reflect"
"time"
appv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
workloads "github.com/apecloud/kubeblocks/apis/workloads/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
)
type restartOpsHandler struct{}
var _ OpsHandler = restartOpsHandler{}
func init() {
restartBehaviour := OpsBehaviour{
// if cluster is Abnormal or Failed, new opsRequest may repair it.
// TODO: we should add "force" flag for these opsRequest.
FromClusterPhases: appsv1alpha1.GetClusterUpRunningPhases(),
ToClusterPhase: appsv1alpha1.UpdatingClusterPhase,
OpsHandler: restartOpsHandler{},
ProcessingReasonInClusterCondition: ProcessingReasonRestarting,
}
opsMgr := GetOpsManager()
opsMgr.RegisterOps(appsv1alpha1.RestartType, restartBehaviour)
}
// ActionStartedCondition the started condition when handle the restart request.
func (r restartOpsHandler) ActionStartedCondition(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) (*metav1.Condition, error) {
return appsv1alpha1.NewRestartingCondition(opsRes.OpsRequest), nil
}
// Action restarts components by updating StatefulSet.
func (r restartOpsHandler) Action(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) error {
if opsRes.OpsRequest.Status.StartTimestamp.IsZero() {
return fmt.Errorf("status.startTimestamp can not be null")
}
componentNameMap := opsRes.OpsRequest.Spec.GetRestartComponentNameSet()
componentKindList := []client.ObjectList{
&appv1.DeploymentList{},
&appv1.StatefulSetList{},
&workloads.ReplicatedStateMachineList{},
}
for _, objectList := range componentKindList {
if err := restartComponent(reqCtx, cli, opsRes, componentNameMap, objectList); err != nil {
return err
}
}
return nil
}
// ReconcileAction will be performed when action is done and loops till OpsRequest.status.phase is Succeed/Failed.
// the Reconcile function for restart opsRequest.
func (r restartOpsHandler) ReconcileAction(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) (appsv1alpha1.OpsPhase, time.Duration, error) {
return reconcileActionWithComponentOps(reqCtx, cli, opsRes, "restart", handleComponentStatusProgress)
}
// SaveLastConfiguration this operation only restart the pods of the component, no changes for Cluster.spec.
// empty implementation here.
func (r restartOpsHandler) SaveLastConfiguration(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) error {
return nil
}
// restartStatefulSet restarts statefulSet workload
func restartComponent(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource, componentNameMap map[string]struct{}, objList client.ObjectList) error {
if err := cli.List(reqCtx.Ctx, objList,
client.InNamespace(opsRes.Cluster.Namespace),
client.MatchingLabels{constant.AppInstanceLabelKey: opsRes.Cluster.Name}); err != nil {
return err
}
items := reflect.ValueOf(objList).Elem().FieldByName("Items")
l := items.Len()
for i := 0; i < l; i++ {
// get the underlying object
object := items.Index(i).Addr().Interface().(client.Object)
template := items.Index(i).FieldByName("Spec").FieldByName("Template").Addr().Interface().(*corev1.PodTemplateSpec)
if isRestarted(opsRes, object, componentNameMap, template) {
continue
}
if err := cli.Update(reqCtx.Ctx, object); err != nil {
return err
}
}
return nil
}
// isRestarted checks whether the component has been restarted
func isRestarted(opsRes *OpsResource, object client.Object, componentNameMap map[string]struct{}, podTemplate *corev1.PodTemplateSpec) bool {
cName := object.GetLabels()[constant.KBAppComponentLabelKey]
if _, ok := componentNameMap[cName]; !ok {
return true
}
if podTemplate.Annotations == nil {
podTemplate.Annotations = map[string]string{}
}
hasRestarted := true
startTimestamp := opsRes.OpsRequest.Status.StartTimestamp
stsRestartTimeStamp := podTemplate.Annotations[constant.RestartAnnotationKey]
if res, _ := time.Parse(time.RFC3339, stsRestartTimeStamp); startTimestamp.After(res) {
podTemplate.Annotations[constant.RestartAnnotationKey] = startTimestamp.Format(time.RFC3339)
hasRestarted = false
}
return hasRestarted
}