Skip to content

Commit

Permalink
feat(GcpNfsVolume): add API validators (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
dushanpantic authored Nov 14, 2024
1 parent f935e7d commit cc6a60a
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 3 deletions.
12 changes: 12 additions & 0 deletions api/cloud-resources/v1beta1/gcpnfsvolume_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ const (
)

// GcpNfsVolumeSpec defines the desired state of GcpNfsVolume
// +kubebuilder:validation:XValidation:rule=(self.tier != "REGIONAL" || self.tier == "REGIONAL" && (self.capacityGb >= 1024 && self.capacityGb <= 9984 && (self.capacityGb - 1024) % 256 == 0 || self.capacityGb >= 10240 && self.capacityGb <= 102400 && (self.capacityGb - 10240) % 2560 == 0)), message="REGIONAL tier capacityGb must be between 1024 and 9984, and it must be divisble by 256, or between 10240 and 102400, and divisible by 2560"
// +kubebuilder:validation:XValidation:rule=(self.tier != "REGIONAL" || self.tier == "REGIONAL" && size(self.fileShareName) <= 64), message="REGIONAL tier fileShareName length must be 64 or less characters"
// +kubebuilder:validation:XValidation:rule=(self.tier != "ZONAL" || self.tier == "ZONAL" && (self.capacityGb >= 1024 && self.capacityGb <= 9984 && (self.capacityGb - 1024) % 256 == 0 || self.capacityGb >= 10240 && self.capacityGb <= 102400 && (self.capacityGb - 10240) % 2560 == 0)), message="ZONAL tier capacityGb must be between 1024 and 9984, and it must be divisble by 256, or between 10240 and 102400, and divisible by 2560"
// +kubebuilder:validation:XValidation:rule=(self.tier != "ZONAL" || self.tier == "ZONAL" && size(self.fileShareName) <= 64), message="ZONAL tier fileShareName length must be 64 or less characters"
// +kubebuilder:validation:XValidation:rule=(self.tier != "BASIC_SSD" || self.tier == "BASIC_SSD" && self.capacityGb >= 2560 && self.capacityGb <= 65400), message="BASIC_SSD tier capacityGb must be between 2560 and 65400"
// +kubebuilder:validation:XValidation:rule=(self.tier != "BASIC_SSD" || self.tier == "BASIC_SSD" && size(self.fileShareName) <= 16), message="BASIC_SSD tier fileShareName length must be 16 or less characters"
// +kubebuilder:validation:XValidation:rule=(self.tier != "BASIC_HDD" || self.tier == "BASIC_HDD" && self.capacityGb >= 1024 && self.capacityGb <= 65400), message="BASIC_HDD tier capacityGb must be between 1024 and 65400"
// +kubebuilder:validation:XValidation:rule=(self.tier != "BASIC_HDD" || self.tier == "BASIC_HDD" && size(self.fileShareName) <= 16), message="BASIC_HDD tier fileShareName length must be 16 or less characters"
// +kubebuilder:validation:XValidation:rule=(self.tier != "HIGH_SCALE_SSD" || self.tier == "HIGH_SCALE_SSD" && self.capacityGb >= 10240 && self.capacityGb <= 102400), message="HIGH_SCALE_SSD tier capacityGb must be between 10240 and 102400"
// +kubebuilder:validation:XValidation:rule=(self.tier != "HIGH_SCALE_SSD" || self.tier == "HIGH_SCALE_SSD" && size(self.fileShareName) <= 64), message="HIGH_SCALE_SSD tier fileShareName length must be 64 or less characters"
// +kubebuilder:validation:XValidation:rule=(self.tier != "ENTERPRISE" || self.tier == "ENTERPRISE" && (self.capacityGb >= 1024 && self.capacityGb <= 10240 && (self.capacityGb - 1024) % 256 == 0)), message="ENTERPRISE tier capacityGb must be between 1024 and 10240, and it must be divisble by 256"
// +kubebuilder:validation:XValidation:rule=(self.tier != "ENTERPRISE" || self.tier == "ENTERPRISE" && size(self.fileShareName) <= 64), message="ENTERPRISE tier fileShareName length must be 64 or less characters"
type GcpNfsVolumeSpec struct {
// +optional
// +kubebuilder:validation:XValidation:rule=(self == oldSelf), message="IpRange is immutable."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
cloud-resources.kyma-project.io/version: v0.0.5
cloud-resources.kyma-project.io/version: v0.0.6
name: gcpnfsvolumes.cloud-resources.kyma-project.io
spec:
group: cloud-resources.kyma-project.io
Expand Down Expand Up @@ -142,6 +142,31 @@ spec:
- fileShareName
- tier
type: object
x-kubernetes-validations:
- message: REGIONAL tier capacityGb must be between 1024 and 9984, and it must be divisble by 256, or between 10240 and 102400, and divisible by 2560
rule: (self.tier != "REGIONAL" || self.tier == "REGIONAL" && (self.capacityGb >= 1024 && self.capacityGb <= 9984 && (self.capacityGb - 1024) % 256 == 0 || self.capacityGb >= 10240 && self.capacityGb <= 102400 && (self.capacityGb - 10240) % 2560 == 0))
- message: REGIONAL tier fileShareName length must be 64 or less characters
rule: (self.tier != "REGIONAL" || self.tier == "REGIONAL" && size(self.fileShareName) <= 64)
- message: ZONAL tier capacityGb must be between 1024 and 9984, and it must be divisble by 256, or between 10240 and 102400, and divisible by 2560
rule: (self.tier != "ZONAL" || self.tier == "ZONAL" && (self.capacityGb >= 1024 && self.capacityGb <= 9984 && (self.capacityGb - 1024) % 256 == 0 || self.capacityGb >= 10240 && self.capacityGb <= 102400 && (self.capacityGb - 10240) % 2560 == 0))
- message: ZONAL tier fileShareName length must be 64 or less characters
rule: (self.tier != "ZONAL" || self.tier == "ZONAL" && size(self.fileShareName) <= 64)
- message: BASIC_SSD tier capacityGb must be between 2560 and 65400
rule: (self.tier != "BASIC_SSD" || self.tier == "BASIC_SSD" && self.capacityGb >= 2560 && self.capacityGb <= 65400)
- message: BASIC_SSD tier fileShareName length must be 16 or less characters
rule: (self.tier != "BASIC_SSD" || self.tier == "BASIC_SSD" && size(self.fileShareName) <= 16)
- message: BASIC_HDD tier capacityGb must be between 1024 and 65400
rule: (self.tier != "BASIC_HDD" || self.tier == "BASIC_HDD" && self.capacityGb >= 1024 && self.capacityGb <= 65400)
- message: BASIC_HDD tier fileShareName length must be 16 or less characters
rule: (self.tier != "BASIC_HDD" || self.tier == "BASIC_HDD" && size(self.fileShareName) <= 16)
- message: HIGH_SCALE_SSD tier capacityGb must be between 10240 and 102400
rule: (self.tier != "HIGH_SCALE_SSD" || self.tier == "HIGH_SCALE_SSD" && self.capacityGb >= 10240 && self.capacityGb <= 102400)
- message: HIGH_SCALE_SSD tier fileShareName length must be 64 or less characters
rule: (self.tier != "HIGH_SCALE_SSD" || self.tier == "HIGH_SCALE_SSD" && size(self.fileShareName) <= 64)
- message: ENTERPRISE tier capacityGb must be between 1024 and 10240, and it must be divisble by 256
rule: (self.tier != "ENTERPRISE" || self.tier == "ENTERPRISE" && (self.capacityGb >= 1024 && self.capacityGb <= 10240 && (self.capacityGb - 1024) % 256 == 0))
- message: ENTERPRISE tier fileShareName length must be 64 or less characters
rule: (self.tier != "ENTERPRISE" || self.tier == "ENTERPRISE" && size(self.fileShareName) <= 64)
status:
description: GcpNfsVolumeStatus defines the observed state of GcpNfsVolume
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
cloud-resources.kyma-project.io/version: v0.0.5
cloud-resources.kyma-project.io/version: v0.0.6
name: gcpnfsvolumes.cloud-resources.kyma-project.io
spec:
group: cloud-resources.kyma-project.io
Expand Down Expand Up @@ -142,6 +142,31 @@ spec:
- fileShareName
- tier
type: object
x-kubernetes-validations:
- message: REGIONAL tier capacityGb must be between 1024 and 9984, and it must be divisble by 256, or between 10240 and 102400, and divisible by 2560
rule: (self.tier != "REGIONAL" || self.tier == "REGIONAL" && (self.capacityGb >= 1024 && self.capacityGb <= 9984 && (self.capacityGb - 1024) % 256 == 0 || self.capacityGb >= 10240 && self.capacityGb <= 102400 && (self.capacityGb - 10240) % 2560 == 0))
- message: REGIONAL tier fileShareName length must be 64 or less characters
rule: (self.tier != "REGIONAL" || self.tier == "REGIONAL" && size(self.fileShareName) <= 64)
- message: ZONAL tier capacityGb must be between 1024 and 9984, and it must be divisble by 256, or between 10240 and 102400, and divisible by 2560
rule: (self.tier != "ZONAL" || self.tier == "ZONAL" && (self.capacityGb >= 1024 && self.capacityGb <= 9984 && (self.capacityGb - 1024) % 256 == 0 || self.capacityGb >= 10240 && self.capacityGb <= 102400 && (self.capacityGb - 10240) % 2560 == 0))
- message: ZONAL tier fileShareName length must be 64 or less characters
rule: (self.tier != "ZONAL" || self.tier == "ZONAL" && size(self.fileShareName) <= 64)
- message: BASIC_SSD tier capacityGb must be between 2560 and 65400
rule: (self.tier != "BASIC_SSD" || self.tier == "BASIC_SSD" && self.capacityGb >= 2560 && self.capacityGb <= 65400)
- message: BASIC_SSD tier fileShareName length must be 16 or less characters
rule: (self.tier != "BASIC_SSD" || self.tier == "BASIC_SSD" && size(self.fileShareName) <= 16)
- message: BASIC_HDD tier capacityGb must be between 1024 and 65400
rule: (self.tier != "BASIC_HDD" || self.tier == "BASIC_HDD" && self.capacityGb >= 1024 && self.capacityGb <= 65400)
- message: BASIC_HDD tier fileShareName length must be 16 or less characters
rule: (self.tier != "BASIC_HDD" || self.tier == "BASIC_HDD" && size(self.fileShareName) <= 16)
- message: HIGH_SCALE_SSD tier capacityGb must be between 10240 and 102400
rule: (self.tier != "HIGH_SCALE_SSD" || self.tier == "HIGH_SCALE_SSD" && self.capacityGb >= 10240 && self.capacityGb <= 102400)
- message: HIGH_SCALE_SSD tier fileShareName length must be 64 or less characters
rule: (self.tier != "HIGH_SCALE_SSD" || self.tier == "HIGH_SCALE_SSD" && size(self.fileShareName) <= 64)
- message: ENTERPRISE tier capacityGb must be between 1024 and 10240, and it must be divisble by 256
rule: (self.tier != "ENTERPRISE" || self.tier == "ENTERPRISE" && (self.capacityGb >= 1024 && self.capacityGb <= 10240 && (self.capacityGb - 1024) % 256 == 0))
- message: ENTERPRISE tier fileShareName length must be 64 or less characters
rule: (self.tier != "ENTERPRISE" || self.tier == "ENTERPRISE" && size(self.fileShareName) <= 64)
status:
description: GcpNfsVolumeStatus defines the observed state of GcpNfsVolume
properties:
Expand Down
2 changes: 1 addition & 1 deletion config/patchAfterMakeManifests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.1.1
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.3"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_awsnfsvolumes.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.3"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_awsnfsvolumebackups.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.16"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_awsredisinstances.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.5"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_gcpnfsvolumes.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.6"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_gcpnfsvolumes.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.18"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_gcpredisinstances.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.2"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_azurevpcpeerings.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.53"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_azureredisinstances.yaml
Expand Down
157 changes: 157 additions & 0 deletions internal/api-tests/skr_gcpnfsvolume_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package api_tests

import (
"fmt"

cloudresourcesv1beta1 "github.com/kyma-project/cloud-manager/api/cloud-resources/v1beta1"
. "github.com/kyma-project/cloud-manager/pkg/testinfra/dsl"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
)

type testGcpNfsVolumeBuilder struct {
instance cloudresourcesv1beta1.GcpNfsVolume
}

func newTestGcpNfsVolumeBuilder() *testGcpNfsVolumeBuilder {
return &testGcpNfsVolumeBuilder{
instance: cloudresourcesv1beta1.GcpNfsVolume{
Spec: cloudresourcesv1beta1.GcpNfsVolumeSpec{},
},
}
}

func (b *testGcpNfsVolumeBuilder) Build() *cloudresourcesv1beta1.GcpNfsVolume {
return &b.instance
}

func (b *testGcpNfsVolumeBuilder) WithTier(tier cloudresourcesv1beta1.GcpFileTier) *testGcpNfsVolumeBuilder {
b.instance.Spec.Tier = tier
return b
}

func (b *testGcpNfsVolumeBuilder) WithCapacityGb(capacityGb int) *testGcpNfsVolumeBuilder {
b.instance.Spec.CapacityGb = capacityGb
return b
}

func (b *testGcpNfsVolumeBuilder) WithFileShareName(fileShareName string) *testGcpNfsVolumeBuilder {
b.instance.Spec.FileShareName = fileShareName
return b
}

func (b *testGcpNfsVolumeBuilder) WithValidFileShareName() *testGcpNfsVolumeBuilder {
b.instance.Spec.FileShareName = "foo"
return b
}

var _ = Describe("Feature: SKR GcpNfsVolume", Ordered, func() {

It("Given SKR default namespace exists", func() {
Eventually(CreateNamespace).
WithArguments(infra.Ctx(), infra.SKR().Client(), &corev1.Namespace{}).
Should(Succeed())
})

fileShareName17char := "bwjfjlecorewsakjikpj"
fileShareName65char := "tcteafkhhfhxkocrtvbvgrzqvysxpfxeeauvgwqnbassacgejobhcuvjvdlrgbkypkuxteaztzjxrdfipqfxdpercpogqdslhm"

// REGIONAL and ZONAL tiers have same constraints
for _, tier := range []cloudresourcesv1beta1.GcpFileTier{cloudresourcesv1beta1.REGIONAL, cloudresourcesv1beta1.ZONAL} {
for _, validCapacity := range []int{1024, 1280, 9984, 10240, 12800, 102400} {
canCreateSkr(
fmt.Sprintf("GcpNfsVolume %s tier instance can be created with valid capacity: %d", tier, validCapacity),
newTestGcpNfsVolumeBuilder().WithTier(tier).WithCapacityGb(validCapacity).WithValidFileShareName(),
)
}
for _, invalidCapacity := range []int{0, 1, 1023, 1025, 10496, 102401, 104960} {
canNotCreateSkr(
fmt.Sprintf("GcpNfsVolume %s tier instance can not be created with invalid capacity: %d", tier, invalidCapacity),
newTestGcpNfsVolumeBuilder().WithTier(tier).WithCapacityGb(invalidCapacity).WithValidFileShareName(),
fmt.Sprintf("%s tier capacityGb must be between 1024 and 9984, and it must be divisble by 256, or between 10240 and 102400, and divisible by 2560", tier),
)
}
canNotCreateSkr(
fmt.Sprintf("GcpNfsVolume %s tier instance can not be created with invalid fileShareName length", tier),
newTestGcpNfsVolumeBuilder().WithTier(tier).WithCapacityGb(1024).WithFileShareName(fileShareName65char),
fmt.Sprintf("%s tier fileShareName length must be 64 or less characters", tier),
)
}

for _, validCapacity := range []int{2560, 2561, 65399, 65400} {
canCreateSkr(
fmt.Sprintf("GcpNfsVolume BASIC_SSD tier instance can be created with valid capacity: %d", validCapacity),
newTestGcpNfsVolumeBuilder().WithTier(cloudresourcesv1beta1.BASIC_SSD).WithCapacityGb(validCapacity).WithValidFileShareName(),
)
}
for _, invalidCapacity := range []int{0, 1, 2559, 65401} {
canNotCreateSkr(
fmt.Sprintf("GcpNfsVolume BASIC_SSD tier instance can not be created with invalid capacity: %d", invalidCapacity),
newTestGcpNfsVolumeBuilder().WithTier(cloudresourcesv1beta1.BASIC_SSD).WithCapacityGb(invalidCapacity).WithValidFileShareName(),
"BASIC_SSD tier capacityGb must be between 2560 and 65400",
)
}
canNotCreateSkr(
"GcpNfsVolume BASIC_SSD tier instance can not be created with invalid fileShareName length",
newTestGcpNfsVolumeBuilder().WithTier(cloudresourcesv1beta1.BASIC_SSD).WithCapacityGb(1024).WithFileShareName(fileShareName17char),
"BASIC_SSD tier fileShareName length must be 16 or less characters",
)

for _, validCapacity := range []int{1024, 1025, 65399, 65400} {
canCreateSkr(
fmt.Sprintf("GcpNfsVolume BASIC_HDD tier instance can be created with valid capacity: %d", validCapacity),
newTestGcpNfsVolumeBuilder().WithTier(cloudresourcesv1beta1.BASIC_HDD).WithCapacityGb(validCapacity).WithValidFileShareName(),
)
}
for _, invalidCapacity := range []int{0, 1, 1023, 65401} {
canNotCreateSkr(
fmt.Sprintf("GcpNfsVolume BASIC_HDD tier instance can not be created with invalid capacity: %d", invalidCapacity),
newTestGcpNfsVolumeBuilder().WithTier(cloudresourcesv1beta1.BASIC_HDD).WithCapacityGb(invalidCapacity).WithValidFileShareName(),
"BASIC_HDD tier capacityGb must be between 1024 and 65400",
)
}
canNotCreateSkr(
"GcpNfsVolume BASIC_HDD tier instance can not be created with invalid fileShareName length",
newTestGcpNfsVolumeBuilder().WithTier(cloudresourcesv1beta1.BASIC_HDD).WithCapacityGb(1024).WithFileShareName(fileShareName17char),
"BASIC_HDD tier fileShareName length must be 16 or less characters",
)

for _, validCapacity := range []int{1024, 1280, 9984, 10240} {
canCreateSkr(
fmt.Sprintf("GcpNfsVolume ENTERPRISE tier instance can be created with valid capacity: %d", validCapacity),
newTestGcpNfsVolumeBuilder().WithTier(cloudresourcesv1beta1.ENTERPRISE).WithCapacityGb(validCapacity).WithValidFileShareName(),
)
}
for _, invalidCapacity := range []int{0, 1, 1023, 1025, 10239, 10241} {
canNotCreateSkr(
fmt.Sprintf("GcpNfsVolume ENTERPRISE tier instance can not be created with invalid capacity: %d", invalidCapacity),
newTestGcpNfsVolumeBuilder().WithTier(cloudresourcesv1beta1.ENTERPRISE).WithCapacityGb(invalidCapacity).WithValidFileShareName(),
"ENTERPRISE tier capacityGb must be between 1024 and 10240",
)
}
canNotCreateSkr(
"GcpNfsVolume ENTERPRISE tier instance can not be created with invalid fileShareName length",
newTestGcpNfsVolumeBuilder().WithTier(cloudresourcesv1beta1.ENTERPRISE).WithCapacityGb(1024).WithFileShareName(fileShareName65char),
"ENTERPRISE tier fileShareName length must be 64 or less characters",
)

for _, validCapacity := range []int{10240, 10241, 102399, 102400} {
canCreateSkr(
fmt.Sprintf("GcpNfsVolume HIGH_SCALE_SSD tier instance can be created with valid capacity: %d", validCapacity),
newTestGcpNfsVolumeBuilder().WithTier(cloudresourcesv1beta1.HIGH_SCALE_SSD).WithCapacityGb(validCapacity).WithValidFileShareName(),
)
}
for _, invalidCapacity := range []int{0, 1, 10239, 102401} {
canNotCreateSkr(
fmt.Sprintf("GcpNfsVolume HIGH_SCALE_SSD tier instance can not be created with invalid capacity: %d", invalidCapacity),
newTestGcpNfsVolumeBuilder().WithTier(cloudresourcesv1beta1.HIGH_SCALE_SSD).WithCapacityGb(invalidCapacity).WithValidFileShareName(),
"HIGH_SCALE_SSD tier capacityGb must be between 10240 and 102400",
)
}
canNotCreateSkr(
"GcpNfsVolume HIGH_SCALE_SSD tier instance can not be created with invalid fileShareName length",
newTestGcpNfsVolumeBuilder().WithTier(cloudresourcesv1beta1.HIGH_SCALE_SSD).WithCapacityGb(1024).WithFileShareName(fileShareName65char),
"HIGH_SCALE_SSD tier fileShareName length must be 64 or less characters",
)
})

0 comments on commit cc6a60a

Please sign in to comment.