diff --git a/pkg/apis/pingcap/v1alpha1/validation/validation.go b/pkg/apis/pingcap/v1alpha1/validation/validation.go index 7e47317799..0af83133b6 100644 --- a/pkg/apis/pingcap/v1alpha1/validation/validation.go +++ b/pkg/apis/pingcap/v1alpha1/validation/validation.go @@ -199,6 +199,14 @@ func validateTiFlashSpec(spec *v1alpha1.TiFlashSpec, fldPath *field.Path) field. spec.StorageClaims, "storageClaims should be configured at least one item.")) } allErrs = append(allErrs, validateScalePolicy(&spec.ScalePolicy, fldPath.Child("scalePolicy"))...) + + // fix storageClaim + for _, storageClaim := range spec.StorageClaims { + if _, ok := storageClaim.Resources.Requests["storage"]; !ok { + allErrs = append(allErrs, field.Invalid(fldPath.Child("spec.StorageClaims.Resources.Requests"), + spec.StorageClaims, "spec.tiflash.storageClaims.resources[storage]: Required value.")) + } + } return allErrs } diff --git a/pkg/apis/pingcap/v1alpha1/validation/validation_test.go b/pkg/apis/pingcap/v1alpha1/validation/validation_test.go index a40b6d5df1..27009b71e3 100644 --- a/pkg/apis/pingcap/v1alpha1/validation/validation_test.go +++ b/pkg/apis/pingcap/v1alpha1/validation/validation_test.go @@ -375,6 +375,62 @@ func TestValidatePumpSpec(t *testing.T) { } } +func TestValidateTiFlashSpec(t *testing.T) { + g := NewGomegaWithT(t) + tests := []struct { + name string + replicas int32 + expectedErrors int + storageClaims []v1alpha1.StorageClaim + }{ + { + name: "has storage request", + replicas: 1, + expectedErrors: 0, + storageClaims: []v1alpha1.StorageClaim{ + { + Resources: corev1.ResourceRequirements{ + Requests: corev1.ResourceList{ + corev1.ResourceStorage: resource.MustParse("10G"), + }, + }, + }, + }, + }, + { + name: "no storage request", + replicas: 1, + expectedErrors: 3, + storageClaims: []v1alpha1.StorageClaim{ + { + Resources: corev1.ResourceRequirements{ + Requests: corev1.ResourceList{}, + }, + }, + { + Resources: corev1.ResourceRequirements{ + Requests: corev1.ResourceList{}, + }, + }, + { + Resources: corev1.ResourceRequirements{ + Requests: corev1.ResourceList{}, + }, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tc := newTidbClusterWithTiflash() + tc.Spec.TiFlash.StorageClaims = tt.storageClaims + err := validateTiFlashSpec(tc.Spec.TiFlash, field.NewPath("spec", "tiflash")) + r := len(err) + g.Expect(r).Should(Equal(tt.expectedErrors)) + }) + } +} + func TestValidateRequestsStorage(t *testing.T) { g := NewGomegaWithT(t) tests := []struct { @@ -542,6 +598,14 @@ func newTidbCluster() *v1alpha1.TidbCluster { return tc } +func newTidbClusterWithTiflash() *v1alpha1.TidbCluster { + tc := newTidbCluster() + tc.Spec.TiFlash = &v1alpha1.TiFlashSpec{ + Replicas: 1, + } + return tc +} + func newService() *v1alpha1.ServiceSpec { svc := &v1alpha1.ServiceSpec{} return svc