-
Notifications
You must be signed in to change notification settings - Fork 159
/
upgrade.go
125 lines (107 loc) · 5.09 KB
/
upgrade.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
/*
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 (
"context"
"reflect"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
)
type upgradeOpsHandler struct{}
var _ OpsHandler = upgradeOpsHandler{}
func init() {
upgradeBehaviour := OpsBehaviour{
// if cluster is Abnormal or Failed, new opsRequest may can repair it.
// TODO: we should add "force" flag for these opsRequest.
FromClusterPhases: appsv1alpha1.GetClusterUpRunningPhases(),
ToClusterPhase: appsv1alpha1.UpdatingClusterPhase,
OpsHandler: upgradeOpsHandler{},
ProcessingReasonInClusterCondition: ProcessingReasonVersionUpgrading,
}
opsMgr := GetOpsManager()
opsMgr.RegisterOps(appsv1alpha1.UpgradeType, upgradeBehaviour)
}
// ActionStartedCondition the started condition when handle the upgrade request.
func (u upgradeOpsHandler) ActionStartedCondition(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) (*metav1.Condition, error) {
return appsv1alpha1.NewHorizontalScalingCondition(opsRes.OpsRequest), nil
}
// Action modifies Cluster.spec.clusterVersionRef with opsRequest.spec.upgrade.clusterVersionRef
func (u upgradeOpsHandler) Action(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) error {
opsRes.Cluster.Spec.ClusterVersionRef = opsRes.OpsRequest.Spec.Upgrade.ClusterVersionRef
return cli.Update(reqCtx.Ctx, opsRes.Cluster)
}
// ReconcileAction will be performed when action is done and loops till OpsRequest.status.phase is Succeed/Failed.
// the Reconcile function for upgrade opsRequest.
func (u upgradeOpsHandler) ReconcileAction(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) (appsv1alpha1.OpsPhase, time.Duration, error) {
return reconcileActionWithComponentOps(reqCtx, cli, opsRes, "upgrade", handleComponentStatusProgress)
}
// SaveLastConfiguration records last configuration to the OpsRequest.status.lastConfiguration
func (u upgradeOpsHandler) SaveLastConfiguration(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) error {
compsStatus, err := u.getUpgradeComponentsStatus(reqCtx, cli, opsRes)
if err != nil {
return err
}
opsRes.OpsRequest.Status.LastConfiguration.ClusterVersionRef = opsRes.Cluster.Spec.ClusterVersionRef
opsRes.OpsRequest.Status.Components = compsStatus
return nil
}
// getUpgradeComponentsStatus compares the ClusterVersions before and after upgrade, and get the changed components map.
func (u upgradeOpsHandler) getUpgradeComponentsStatus(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) (map[string]appsv1alpha1.OpsRequestComponentStatus, error) {
lastComponents, err := u.getClusterComponentVersionMap(reqCtx.Ctx, cli,
opsRes.Cluster.Spec.ClusterVersionRef)
if err != nil {
return nil, err
}
components, err := u.getClusterComponentVersionMap(reqCtx.Ctx, cli,
opsRes.OpsRequest.Spec.Upgrade.ClusterVersionRef)
if err != nil {
return nil, err
}
// get the changed components map
changedComponentMap := map[string]struct{}{}
for k, v := range components {
lastComp := lastComponents[k]
if !reflect.DeepEqual(v, lastComp) {
changedComponentMap[k] = struct{}{}
}
}
// get the changed components name map, and record the components infos to OpsRequest.status.
compStatusMap := map[string]appsv1alpha1.OpsRequestComponentStatus{}
for _, comp := range opsRes.Cluster.Spec.ComponentSpecs {
if _, ok := changedComponentMap[comp.ComponentDefRef]; !ok {
continue
}
compStatusMap[comp.Name] = appsv1alpha1.OpsRequestComponentStatus{
Phase: appsv1alpha1.UpdatingClusterCompPhase,
}
}
return compStatusMap, nil
}
// getClusterComponentVersionMap gets the components of ClusterVersion and converts the component list to map.
func (u upgradeOpsHandler) getClusterComponentVersionMap(ctx context.Context,
cli client.Client, clusterVersionName string) (map[string]appsv1alpha1.ClusterComponentVersion, error) {
clusterVersion := &appsv1alpha1.ClusterVersion{}
if err := cli.Get(ctx, client.ObjectKey{Name: clusterVersionName}, clusterVersion); err != nil {
return nil, client.IgnoreNotFound(err)
}
components := map[string]appsv1alpha1.ClusterComponentVersion{}
for _, v := range clusterVersion.Spec.ComponentVersions {
components[v.ComponentDefRef] = v
}
return components, nil
}