Skip to content

Commit 5934d44

Browse files
committed
update test
1 parent 0eb6142 commit 5934d44

File tree

4 files changed

+15
-150
lines changed

4 files changed

+15
-150
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 (

controllers/parameters/policy_util_test.go

Lines changed: 0 additions & 66 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

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)))

0 commit comments

Comments
 (0)