Skip to content

Commit c3c3c55

Browse files
committed
update test
1 parent 0eb6142 commit c3c3c55

File tree

9 files changed

+40
-182
lines changed

9 files changed

+40
-182
lines changed

apis/parameters/v1alpha1/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ type ParametersInFile struct {
167167

168168
// ReloadPolicy defines the policy of reconfiguring.
169169
// +enum
170-
// +kubebuilder:validation:Enum={none,restart,rolling,asyncReload,syncReload,dynamicReloadBeginRestart}
170+
// +kubebuilder:validation:Enum={none,restart,asyncReload,syncReload,dynamicReloadBeginRestart}
171171
type ReloadPolicy string
172172

173173
const (
File renamed without changes.

controllers/parameters/reconfigure_controller.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (r *ReconfigureReconciler) sync(reqCtx intctrlutil.RequestCtx, configMap *c
214214
configPatch.UpdateConfig))
215215
}
216216

217-
tasks, err := genReconfigureActionTasks(configSpec, rctx, configPatch, forceRestart)
217+
tasks, err := buildReconfigureTasks(configSpec, rctx, configPatch, forceRestart)
218218
if err != nil {
219219
return intctrlutil.RequeueWithErrorAndRecordEvent(configMap, r.Recorder, err, reqCtx.Log)
220220
}
@@ -234,15 +234,15 @@ func (r *ReconfigureReconciler) updateConfigCMStatus(reqCtx intctrlutil.RequestC
234234
return intctrlutil.Reconciled()
235235
}
236236

237-
func (r *ReconfigureReconciler) performUpgrade(rctx *ReconcileContext, reloadTasks []ReloadAction) (ctrl.Result, error) {
237+
func (r *ReconfigureReconciler) performUpgrade(rctx *ReconcileContext, tasks []reconfigureTask) (ctrl.Result, error) {
238238
var (
239239
err error
240240
status returnedStatus
241241
reloadType string
242242
)
243-
for _, task := range reloadTasks {
244-
reloadType = task.ReloadType()
245-
status, err = task.ExecReload()
243+
for _, task := range tasks {
244+
reloadType = string(task.policy)
245+
status, err = task.reconfigure()
246246
if err != nil || status.Status != ESNone {
247247
break
248248
}

controllers/parameters/policy_util.go renamed to controllers/parameters/reconfigure_controller_util.go

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,19 @@ import (
2828
"github.com/apecloud/kubeblocks/pkg/parameters/core"
2929
)
3030

31-
type ReloadAction interface {
32-
ExecReload() (returnedStatus, error)
33-
ReloadType() string
34-
}
35-
3631
type reconfigureTask struct {
37-
parametersv1alpha1.ReloadPolicy
32+
policy parametersv1alpha1.ReloadPolicy
3833
taskCtx reconfigureContext
3934
}
4035

41-
func (r reconfigureTask) ReloadType() string {
42-
return string(r.ReloadPolicy)
43-
}
44-
45-
func (r reconfigureTask) ExecReload() (returnedStatus, error) {
46-
if executor, ok := upgradePolicyMap[r.ReloadPolicy]; ok {
36+
func (r reconfigureTask) reconfigure() (returnedStatus, error) {
37+
if executor, ok := upgradePolicyMap[r.policy]; ok {
4738
return executor.Upgrade(r.taskCtx)
4839
}
49-
return returnedStatus{}, fmt.Errorf("not support reload action[%s]", r.ReloadPolicy)
40+
return returnedStatus{}, fmt.Errorf("unknown reconfigure policy: %s", r.policy)
5041
}
5142

52-
func resolveReloadActionPolicy(jsonPatch string,
53-
format *parametersv1alpha1.FileFormatConfig,
43+
func resolveReconfigurePolicy(jsonPatch string, format *parametersv1alpha1.FileFormatConfig,
5444
pd *parametersv1alpha1.ParametersDefinitionSpec) (parametersv1alpha1.ReloadPolicy, error) {
5545
var policy = parametersv1alpha1.NonePolicy
5646
dynamicUpdate, err := core.CheckUpdateDynamicParameters(format, pd, jsonPatch)
@@ -74,14 +64,12 @@ func resolveReloadActionPolicy(jsonPatch string,
7464
return policy, nil
7565
}
7666

77-
// genReconfigureActionTasks generates a list of reconfiguration tasks based on the provided templateSpec,
78-
// reconfiguration context, configuration patch, and a restart flag.
79-
func genReconfigureActionTasks(templateSpec *appsv1.ComponentFileTemplate, rctx *ReconcileContext, patch *core.ConfigPatchInfo, restart bool) ([]ReloadAction, error) {
80-
var tasks []ReloadAction
67+
func buildReconfigureTasks(templateSpec *appsv1.ComponentFileTemplate, rctx *ReconcileContext, patch *core.ConfigPatchInfo, restart bool) ([]reconfigureTask, error) {
68+
var tasks []reconfigureTask
8169

8270
// If the patch or ConfigRender is nil, return a single restart task.
8371
if patch == nil || rctx.ConfigRender == nil {
84-
return []ReloadAction{buildRestartTask(templateSpec, rctx)}, nil
72+
return []reconfigureTask{buildRestartTask(templateSpec, rctx)}, nil
8573
}
8674

8775
// needReloadAction determines if a reload action is needed based on the ParametersDefinition and ReloadPolicy.
@@ -100,27 +88,32 @@ func genReconfigureActionTasks(templateSpec *appsv1.ComponentFileTemplate, rctx
10088
continue
10189
}
10290
// Determine the appropriate ReloadPolicy.
103-
policy, err := resolveReloadActionPolicy(string(jsonPatch), configFormat.FileFormatConfig, &pd.Spec)
91+
policy, err := resolveReconfigurePolicy(string(jsonPatch), configFormat.FileFormatConfig, &pd.Spec)
10492
if err != nil {
10593
return nil, err
10694
}
10795
// If a reload action is needed, append a new reload action task to the tasks slice.
10896
if needReloadAction(pd, policy) {
109-
tasks = append(tasks, buildReloadActionTask(policy, templateSpec, rctx, pd, configFormat, patch))
97+
tasks = append(tasks, buildReloadTask(policy, templateSpec, rctx, pd, configFormat, patch))
11098
}
11199
}
112100

113101
// If no tasks were added, return a single restart task.
114102
if len(tasks) == 0 {
115-
return []ReloadAction{buildRestartTask(templateSpec, rctx)}, nil
103+
return []reconfigureTask{buildRestartTask(templateSpec, rctx)}, nil
116104
}
117105

118106
return tasks, nil
119107
}
120108

121-
func buildReloadActionTask(reloadPolicy parametersv1alpha1.ReloadPolicy, templateSpec *appsv1.ComponentFileTemplate, rctx *ReconcileContext, pd *parametersv1alpha1.ParametersDefinition, configDescription *parametersv1alpha1.ComponentConfigDescription, patch *core.ConfigPatchInfo) reconfigureTask {
109+
func buildReloadTask(policy parametersv1alpha1.ReloadPolicy,
110+
templateSpec *appsv1.ComponentFileTemplate,
111+
rctx *ReconcileContext,
112+
pd *parametersv1alpha1.ParametersDefinition,
113+
configDescription *parametersv1alpha1.ComponentConfigDescription,
114+
patch *core.ConfigPatchInfo) reconfigureTask {
122115
return reconfigureTask{
123-
ReloadPolicy: reloadPolicy,
116+
policy: policy,
124117
taskCtx: reconfigureContext{
125118
RequestCtx: rctx.RequestCtx,
126119
Client: rctx.Client,
@@ -139,7 +132,7 @@ func buildReloadActionTask(reloadPolicy parametersv1alpha1.ReloadPolicy, templat
139132

140133
func buildRestartTask(configTemplate *appsv1.ComponentFileTemplate, rctx *ReconcileContext) reconfigureTask {
141134
return reconfigureTask{
142-
ReloadPolicy: parametersv1alpha1.RestartPolicy,
135+
policy: parametersv1alpha1.RestartPolicy,
143136
taskCtx: reconfigureContext{
144137
RequestCtx: rctx.RequestCtx,
145138
Client: rctx.Client,

controllers/parameters/policy_util_test.go renamed to controllers/parameters/reconfigure_controller_util_test.go

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
package parameters
2121

2222
import (
23-
"fmt"
2423
"testing"
2524

2625
"github.com/stretchr/testify/assert"
2726
corev1 "k8s.io/api/core/v1"
2827
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29-
"k8s.io/apimachinery/pkg/runtime"
3028
"k8s.io/client-go/tools/record"
3129
"k8s.io/utils/pointer"
3230
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -45,14 +43,6 @@ var (
4543
// itsSchemaKind = workloads.GroupVersion.WithKind(workloads.InstanceSetKind)
4644
)
4745

48-
func newMockRunningComponent() *appsv1.Component {
49-
return &appsv1.Component{
50-
Status: appsv1.ComponentStatus{
51-
Phase: appsv1.RunningComponentPhase,
52-
},
53-
}
54-
}
55-
5646
type paramsOps func(params *reconfigureContext)
5747

5848
func withClusterComponent(replicas int) paramsOps {
@@ -163,62 +153,6 @@ func newMockReconfigureParams(testName string, cli client.Client, paramOps ...pa
163153
return params
164154
}
165155

166-
func newMockPodsWitheContext(ctx reconfigureContext, replicas int, options ...podOptions) []corev1.Pod {
167-
pods := make([]corev1.Pod, replicas)
168-
for i := 0; i < replicas; i++ {
169-
pods[i] = newMockPod(ctx.SynthesizedComponent.Name+"-"+fmt.Sprint(i), ctx.SynthesizedComponent.PodSpec)
170-
// pods[i].OwnerReferences = []metav1.OwnerReference{newControllerRef(its, itsSchemaKind)}
171-
pods[i].Status.PodIP = "1.1.1.1"
172-
}
173-
for _, customFn := range options {
174-
for i := range pods {
175-
pod := &pods[i]
176-
customFn(pod, i)
177-
}
178-
}
179-
return pods
180-
}
181-
182-
func withReadyPod(rMin, rMax int) podOptions {
183-
return func(pod *corev1.Pod, index int) {
184-
if index < rMin || index >= rMax {
185-
return
186-
}
187-
188-
if pod.Status.Conditions == nil {
189-
pod.Status.Conditions = make([]corev1.PodCondition, 0)
190-
}
191-
192-
pod.Status.Conditions = append(pod.Status.Conditions, corev1.PodCondition{
193-
Type: corev1.PodReady,
194-
Status: corev1.ConditionTrue,
195-
})
196-
197-
pod.Status.Phase = corev1.PodRunning
198-
}
199-
}
200-
201-
func fromPodObjectList(pods []corev1.Pod) []runtime.Object {
202-
objs := make([]runtime.Object, len(pods))
203-
for i := 0; i < len(pods); i++ {
204-
objs[i] = &pods[i]
205-
}
206-
return objs
207-
}
208-
209-
type podOptions func(pod *corev1.Pod, index int)
210-
211-
func newMockPod(podName string, podSpec *corev1.PodSpec) corev1.Pod {
212-
pod := corev1.Pod{
213-
ObjectMeta: metav1.ObjectMeta{
214-
Name: podName,
215-
Namespace: defaultNamespace,
216-
},
217-
}
218-
pod.Spec = *podSpec.DeepCopy()
219-
return pod
220-
}
221-
222156
func Test_resolveReloadActionPolicy(t *testing.T) {
223157
type args struct {
224158
jsonPatch string
@@ -334,7 +268,7 @@ func Test_resolveReloadActionPolicy(t *testing.T) {
334268
}}
335269
for _, tt := range tests {
336270
t.Run(tt.name, func(t *testing.T) {
337-
got, err := resolveReloadActionPolicy(tt.args.jsonPatch, tt.args.format, tt.args.pd)
271+
got, err := resolveReconfigurePolicy(tt.args.jsonPatch, tt.args.format, tt.args.pd)
338272
if (err != nil) != tt.wantErr {
339273
t.Errorf("resolveReloadActionPolicy(%v, %v, %v)", tt.args.jsonPatch, tt.args.format, tt.args.pd)
340274
}

controllers/parameters/restart_policy.go

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
package parameters
2121

2222
import (
23-
"fmt"
24-
25-
"sigs.k8s.io/controller-runtime/pkg/client"
26-
27-
appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
2823
parametersv1alpha1 "github.com/apecloud/kubeblocks/apis/parameters/v1alpha1"
2924
"github.com/apecloud/kubeblocks/pkg/constant"
30-
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
3125
"github.com/apecloud/kubeblocks/pkg/parameters/core"
3226
)
3327

@@ -42,63 +36,21 @@ type restartPolicy struct{}
4236
func (s *restartPolicy) Upgrade(rctx reconfigureContext) (returnedStatus, error) {
4337
rctx.Log.V(1).Info("simple policy begin....")
4438

45-
var (
46-
newVersion = rctx.getTargetVersionHash()
47-
configKey = rctx.generateConfigIdentifier()
48-
)
39+
s.restart(rctx)
4940

50-
if err := s.restart(rctx.Client, rctx.RequestCtx, configKey, newVersion, rctx.Cluster, rctx.ClusterComponent.Name); err != nil {
51-
return makeReturnedStatus(ESFailedAndRetry), err
52-
}
5341
return syncLatestConfigStatus(rctx), nil
5442
}
5543

56-
func (s *restartPolicy) restart(cli client.Client, ctx intctrlutil.RequestCtx, configKey string, newVersion string, cluster *appsv1.Cluster, compName string) error {
57-
cfgAnnotationKey := core.GenerateUniqKeyWithConfig(constant.UpgradeRestartAnnotationKey, configKey)
58-
59-
compSpec, err := s.getComponentSpecPtrByName(cli, ctx, cluster, compName)
60-
if err != nil {
61-
return err
62-
}
63-
64-
if compSpec.Annotations == nil {
65-
compSpec.Annotations = map[string]string{}
66-
}
67-
68-
if compSpec.Annotations[cfgAnnotationKey] == newVersion {
69-
return nil
70-
}
71-
72-
compSpec.Annotations[cfgAnnotationKey] = newVersion
73-
74-
return cli.Update(ctx.Ctx, cluster)
75-
}
76-
77-
func (s *restartPolicy) getComponentSpecPtrByName(cli client.Client, ctx intctrlutil.RequestCtx, cluster *appsv1.Cluster, compName string) (*appsv1.ClusterComponentSpec, error) {
78-
for i := range cluster.Spec.ComponentSpecs {
79-
componentSpec := &cluster.Spec.ComponentSpecs[i]
80-
if componentSpec.Name == compName {
81-
return componentSpec, nil
82-
}
83-
}
84-
// check if the component is a sharding component
85-
compObjList := &appsv1.ComponentList{}
86-
if err := cli.List(ctx.Ctx, compObjList, client.MatchingLabels{
87-
constant.AppInstanceLabelKey: cluster.Name,
88-
constant.KBAppComponentLabelKey: compName,
89-
}); err != nil {
90-
return nil, err
44+
func (s *restartPolicy) restart(rctx reconfigureContext) {
45+
var (
46+
configKey = rctx.generateConfigIdentifier()
47+
newVersion = rctx.getTargetVersionHash()
48+
cfgAnnotationKey = core.GenerateUniqKeyWithConfig(constant.UpgradeRestartAnnotationKey, configKey)
49+
)
50+
if rctx.ClusterComponent.Annotations == nil {
51+
rctx.ClusterComponent.Annotations = map[string]string{}
9152
}
92-
if len(compObjList.Items) > 0 {
93-
shardingName := compObjList.Items[0].Labels[constant.KBAppShardingNameLabelKey]
94-
if shardingName != "" {
95-
for i := range cluster.Spec.Shardings {
96-
shardSpec := &cluster.Spec.Shardings[i]
97-
if shardSpec.Name == shardingName {
98-
return &shardSpec.Template, nil
99-
}
100-
}
101-
}
53+
if rctx.ClusterComponent.Annotations[cfgAnnotationKey] != newVersion {
54+
rctx.ClusterComponent.Annotations[cfgAnnotationKey] = newVersion
10255
}
103-
return nil, fmt.Errorf("component %s not found", compName)
10456
}

controllers/parameters/restart_policy_test.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,51 +20,30 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
package parameters
2121

2222
import (
23-
"fmt"
24-
2523
. "github.com/onsi/ginkgo/v2"
2624
. "github.com/onsi/gomega"
2725

2826
parametersv1alpha1 "github.com/apecloud/kubeblocks/apis/parameters/v1alpha1"
2927
workloads "github.com/apecloud/kubeblocks/apis/workloads/v1"
30-
testutil "github.com/apecloud/kubeblocks/pkg/testutil/k8s"
3128
)
3229

3330
var _ = Describe("Reconfigure restartPolicy", func() {
3431
var (
35-
k8sMockClient *testutil.K8sClientMockHelper
36-
policy = upgradePolicyMap[parametersv1alpha1.RestartPolicy]
32+
policy = upgradePolicyMap[parametersv1alpha1.RestartPolicy]
3733
)
3834

39-
BeforeEach(func() {
40-
k8sMockClient = testutil.NewK8sMockClient()
41-
})
42-
43-
AfterEach(func() {
44-
k8sMockClient.Finish()
45-
})
46-
4735
Context("restart policy test", func() {
4836
It("should success without error", func() {
49-
mockParam := newMockReconfigureParams("restartPolicy", k8sMockClient.Client(),
37+
mockParam := newMockReconfigureParams("restartPolicy", k8sClient,
5038
withConfigSpec("test", map[string]string{
5139
"key": "value",
5240
}),
5341
withClusterComponent(2),
5442
withWorkload())
5543

56-
updateErr := fmt.Errorf("mock error")
57-
k8sMockClient.MockUpdateMethod(
58-
testutil.WithFailed(updateErr, testutil.WithTimes(1)),
59-
testutil.WithSucceed(testutil.WithAnyTimes()))
60-
61-
status, err := policy.Upgrade(mockParam)
62-
Expect(err).Should(BeEquivalentTo(updateErr))
63-
Expect(status.Status).Should(BeEquivalentTo(ESFailedAndRetry))
64-
6544
// first upgrade, not pod is ready
6645
mockParam.its.Status.InstanceStatus = []workloads.InstanceStatus{}
67-
status, err = policy.Upgrade(mockParam)
46+
status, err := policy.Upgrade(mockParam)
6847
Expect(err).Should(Succeed())
6948
Expect(status.Status).Should(BeEquivalentTo(ESRetry))
7049
Expect(status.SucceedCount).Should(BeEquivalentTo(int32(0)))
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)