Skip to content

Commit

Permalink
feat(GcpRedisInstance): replace tier and memorySizeGb with redisTier (k…
Browse files Browse the repository at this point in the history
  • Loading branch information
dushanpantic authored Nov 5, 2024
1 parent 0c3f89d commit e1cec07
Show file tree
Hide file tree
Showing 23 changed files with 445 additions and 137 deletions.
1 change: 0 additions & 1 deletion api/cloud-control/v1beta1/redisinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ type RedisInstanceGcp struct {
// +kubebuilder:default=0
// +kubebuilder:validation:Minimum=0
// +kubebuilder:validation:Maximum=5
// +kubebuilder:validation:XValidation:rule=(self == oldSelf), message="ReplicaCount is immutable."
ReplicaCount int32 `json:"replicaCount"`
}

Expand Down
39 changes: 27 additions & 12 deletions api/cloud-resources/v1beta1/gcpredisinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

// +kubebuilder:validation:Enum=S1;S2;S3;S4;S5;S6;S7;S8;P1;P2;P3;P4;P5;P6;P7
type GcpRedisTier string

const (
GcpRedisTierS1 GcpRedisTier = "S1"
GcpRedisTierS2 GcpRedisTier = "S2"
GcpRedisTierS3 GcpRedisTier = "S3"
GcpRedisTierS4 GcpRedisTier = "S4"
GcpRedisTierS5 GcpRedisTier = "S5"
GcpRedisTierS6 GcpRedisTier = "S6"
GcpRedisTierS7 GcpRedisTier = "S7"
GcpRedisTierS8 GcpRedisTier = "S8"

GcpRedisTierP1 GcpRedisTier = "P1"
GcpRedisTierP2 GcpRedisTier = "P2"
GcpRedisTierP3 GcpRedisTier = "P3"
GcpRedisTierP4 GcpRedisTier = "P4"
GcpRedisTierP5 GcpRedisTier = "P5"
GcpRedisTierP6 GcpRedisTier = "P6"
GcpRedisTierP7 GcpRedisTier = "P7"
)

type AuthSecretSpec struct {
Name string `json:"name,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Expand Down Expand Up @@ -59,23 +81,17 @@ type MaintenancePolicy struct {
}

// GcpRedisInstanceSpec defines the desired state of GcpRedisInstance
// +kubebuilder:validation:XValidation:rule=(self.tier == "BASIC" && self.replicaCount == 0 || self.tier == "STANDARD_HA"), message="replicaCount must be zero for BASIC tier"
// +kubebuilder:validation:XValidation:rule=(self.tier == "STANDARD_HA" && self.replicaCount > 0 || self.tier == "BASIC"), message="replicaCount must be defined with value between 1 and 5 for STANDARD_HA tier"
// +kubebuilder:validation:XValidation:rule=(self.tier == "STANDARD_HA" && self.memorySizeGb >= 5 || self.tier == "BASIC"), message="memorySizeGb must be at least 5 GiB for STANDARD_HA tier"
// +kubebuilder:validation:XValidation:rule=(self.redisTier.startsWith('S') && self.replicaCount == 0 || self.redisTier.startsWith('P')), message="replicaCount must be zero for Standard service tier"
// +kubebuilder:validation:XValidation:rule=(self.redisTier.startsWith('P') && self.replicaCount > 0 || self.redisTier.startsWith('S')), message="replicaCount must be defined with value between 1 and 5 for Premium service tier"
type GcpRedisInstanceSpec struct {

// +optional
IpRange IpRangeRef `json:"ipRange"`

// The service tier of the instance.
// +kubebuilder:default=BASIC
// +kubebuilder:validation:XValidation:rule=(self == oldSelf), message="Tier is immutable."
// +kubebuilder:validation:Enum=BASIC;STANDARD_HA
Tier string `json:"tier"`

// Redis memory size in GiB.
// Defines Service Tier and Capacity Tier. RedisTiers starting with 'S' are Standard service tier. RedisTiers starting with 'P' are premium servicetier. Number next to service tier represents capacity tier.
// +kubebuilder:validation:Required
MemorySizeGb int32 `json:"memorySizeGb"`
// +kubebuilder:validation:XValidation:rule=(self.startsWith('S') && oldSelf.startsWith('S') || self.startsWith('P') && oldSelf.startsWith('P')), message="Service tier cannot be changed within redisTier. Only capacity tier can be changed."
RedisTier GcpRedisTier `json:"redisTier"`

// The version of Redis software.
// +optional
Expand Down Expand Up @@ -107,7 +123,6 @@ type GcpRedisInstanceSpec struct {
// +kubebuilder:default=0
// +kubebuilder:validation:Minimum=0
// +kubebuilder:validation:Maximum=5
// +kubebuilder:validation:XValidation:rule=(self == oldSelf), message="ReplicaCount is immutable."
ReplicaCount int32 `json:"replicaCount"`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,6 @@ spec:
maximum: 5
minimum: 0
type: integer
x-kubernetes-validations:
- message: ReplicaCount is immutable.
rule: (self == oldSelf)
tier:
default: BASIC
description: The service tier of the instance.
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.16
cloud-resources.kyma-project.io/version: v0.0.17
name: gcpredisinstances.cloud-resources.kyma-project.io
spec:
group: cloud-resources.kyma-project.io
Expand Down Expand Up @@ -116,17 +116,35 @@ spec:
- startTime
type: object
type: object
memorySizeGb:
description: Redis memory size in GiB.
format: int32
type: integer
redisConfigs:
additionalProperties:
type: string
description: |-
Redis configuration parameters, according to http://redis.io/topics/config.
See docs for the list of the supported parameters
type: object
redisTier:
description: Defines Service Tier and Capacity Tier. RedisTiers starting with 'S' are Standard service tier. RedisTiers starting with 'P' are premium servicetier. Number next to service tier represents capacity tier.
enum:
- S1
- S2
- S3
- S4
- S5
- S6
- S7
- S8
- P1
- P2
- P3
- P4
- P5
- P6
- P7
type: string
x-kubernetes-validations:
- message: Service tier cannot be changed within redisTier. Only capacity tier can be changed.
rule: (self.startsWith('S') && oldSelf.startsWith('S') || self.startsWith('P') && oldSelf.startsWith('P'))
redisVersion:
default: REDIS_7_0
description: The version of Redis software.
Expand All @@ -144,30 +162,14 @@ spec:
maximum: 5
minimum: 0
type: integer
x-kubernetes-validations:
- message: ReplicaCount is immutable.
rule: (self == oldSelf)
tier:
default: BASIC
description: The service tier of the instance.
enum:
- BASIC
- STANDARD_HA
type: string
x-kubernetes-validations:
- message: Tier is immutable.
rule: (self == oldSelf)
required:
- memorySizeGb
- tier
- redisTier
type: object
x-kubernetes-validations:
- message: replicaCount must be zero for BASIC tier
rule: (self.tier == "BASIC" && self.replicaCount == 0 || self.tier == "STANDARD_HA")
- message: replicaCount must be defined with value between 1 and 5 for STANDARD_HA tier
rule: (self.tier == "STANDARD_HA" && self.replicaCount > 0 || self.tier == "BASIC")
- message: memorySizeGb must be at least 5 GiB for STANDARD_HA tier
rule: (self.tier == "STANDARD_HA" && self.memorySizeGb >= 5 || self.tier == "BASIC")
- message: replicaCount must be zero for Standard service tier
rule: (self.redisTier.startsWith('S') && self.replicaCount == 0 || self.redisTier.startsWith('P'))
- message: replicaCount must be defined with value between 1 and 5 for Premium service tier
rule: (self.redisTier.startsWith('P') && self.replicaCount > 0 || self.redisTier.startsWith('S'))
status:
description: GcpRedisInstanceStatus defines the observed state of GcpRedisInstance
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,6 @@ spec:
maximum: 5
minimum: 0
type: integer
x-kubernetes-validations:
- message: ReplicaCount is immutable.
rule: (self == oldSelf)
tier:
default: BASIC
description: The service tier of the instance.
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.16
cloud-resources.kyma-project.io/version: v0.0.17
name: gcpredisinstances.cloud-resources.kyma-project.io
spec:
group: cloud-resources.kyma-project.io
Expand Down Expand Up @@ -116,17 +116,35 @@ spec:
- startTime
type: object
type: object
memorySizeGb:
description: Redis memory size in GiB.
format: int32
type: integer
redisConfigs:
additionalProperties:
type: string
description: |-
Redis configuration parameters, according to http://redis.io/topics/config.
See docs for the list of the supported parameters
type: object
redisTier:
description: Defines Service Tier and Capacity Tier. RedisTiers starting with 'S' are Standard service tier. RedisTiers starting with 'P' are premium servicetier. Number next to service tier represents capacity tier.
enum:
- S1
- S2
- S3
- S4
- S5
- S6
- S7
- S8
- P1
- P2
- P3
- P4
- P5
- P6
- P7
type: string
x-kubernetes-validations:
- message: Service tier cannot be changed within redisTier. Only capacity tier can be changed.
rule: (self.startsWith('S') && oldSelf.startsWith('S') || self.startsWith('P') && oldSelf.startsWith('P'))
redisVersion:
default: REDIS_7_0
description: The version of Redis software.
Expand All @@ -144,30 +162,14 @@ spec:
maximum: 5
minimum: 0
type: integer
x-kubernetes-validations:
- message: ReplicaCount is immutable.
rule: (self == oldSelf)
tier:
default: BASIC
description: The service tier of the instance.
enum:
- BASIC
- STANDARD_HA
type: string
x-kubernetes-validations:
- message: Tier is immutable.
rule: (self == oldSelf)
required:
- memorySizeGb
- tier
- redisTier
type: object
x-kubernetes-validations:
- message: replicaCount must be zero for BASIC tier
rule: (self.tier == "BASIC" && self.replicaCount == 0 || self.tier == "STANDARD_HA")
- message: replicaCount must be defined with value between 1 and 5 for STANDARD_HA tier
rule: (self.tier == "STANDARD_HA" && self.replicaCount > 0 || self.tier == "BASIC")
- message: memorySizeGb must be at least 5 GiB for STANDARD_HA tier
rule: (self.tier == "STANDARD_HA" && self.memorySizeGb >= 5 || self.tier == "BASIC")
- message: replicaCount must be zero for Standard service tier
rule: (self.redisTier.startsWith('S') && self.replicaCount == 0 || self.redisTier.startsWith('P'))
- message: replicaCount must be defined with value between 1 and 5 for Premium service tier
rule: (self.redisTier.startsWith('P') && self.replicaCount > 0 || self.redisTier.startsWith('S'))
status:
description: GcpRedisInstanceStatus defines the observed state of GcpRedisInstance
properties:
Expand Down
2 changes: 1 addition & 1 deletion config/patchAfterMakeManifests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.3
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.15"' $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.16"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_gcpredisinstances.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.17"' $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.51"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_azureredisinstances.yaml
yq -i '.metadata.annotations."cloud-resources.kyma-project.io/version" = "v0.0.4"' $SCRIPT_DIR/crd/bases/cloud-resources.kyma-project.io_gcpnfsvolumebackups.yaml
Expand Down
4 changes: 2 additions & 2 deletions config/samples/cloud-resources_v1beta1_gcpredisinstance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ metadata:
name: gcpredisinstance-sample
spec:
# required fields
memorySizeGb: 1
tier: "BASIC"
redisTier: "P1"
replicaCount: 2

# optional fields
redisVersion: REDIS_7_0
Expand Down
44 changes: 30 additions & 14 deletions internal/api-tests/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ type Builder[T client.Object] interface {
Build() T
}

func createScenario[T client.Object](title string, b Builder[T], ok bool, errMsg string, focus bool) {
func createScenario[T client.Object](clientFn func() client.Client, title string, b Builder[T], ok bool, errMsg string, focus bool) {
handler := func() {
obj := b.Build()
obj.SetName(uuid.NewString())
dsl.SetDefaultNamespace(obj)

err := infra.KCP().Client().Create(infra.Ctx(), obj)
err := clientFn().Create(infra.Ctx(), obj)
if ok {
Expect(err).NotTo(HaveOccurred(), title)
_ = infra.KCP().Client().Delete(infra.Ctx(), obj)
_ = clientFn().Delete(infra.Ctx(), obj)
} else {
Expect(err).To(HaveOccurred(), title)
if errMsg != "" {
Expand All @@ -36,20 +36,20 @@ func createScenario[T client.Object](title string, b Builder[T], ok bool, errMsg
}
}

func updateScenario[T client.Object](title string, b Builder[T], cb func(b Builder[T]), ok bool, errMsg string, focus bool) {
func updateScenario[T client.Object](clientFn func() client.Client, title string, b Builder[T], cb func(b Builder[T]), ok bool, errMsg string, focus bool) {
handler := func() {
obj := b.Build()
obj.SetName(uuid.NewString())
dsl.SetDefaultNamespace(obj)
err := infra.KCP().Client().Create(infra.Ctx(), obj)
err := clientFn().Create(infra.Ctx(), obj)
Expect(err).NotTo(HaveOccurred())
err = infra.KCP().Client().Get(infra.Ctx(), client.ObjectKeyFromObject(obj), obj)
err = clientFn().Get(infra.Ctx(), client.ObjectKeyFromObject(obj), obj)
Expect(err).NotTo(HaveOccurred())
defer func() {
_ = infra.KCP().Client().Delete(infra.Ctx(), obj)
_ = clientFn().Delete(infra.Ctx(), obj)
}()
cb(b)
err = infra.KCP().Client().Update(infra.Ctx(), obj)
err = clientFn().Update(infra.Ctx(), obj)
if ok {
Expect(err).NotTo(HaveOccurred(), title)
} else {
Expand All @@ -66,12 +66,28 @@ func updateScenario[T client.Object](title string, b Builder[T], cb func(b Build
}
}

func canCreate[T client.Object](title string, b Builder[T]) {
createScenario(title, b, true, "", false)
func canCreateKcp[T client.Object](title string, b Builder[T]) {
createScenario(func() client.Client { return infra.KCP().Client() },
title, b, true, "", false)
}
func canNotCreate[T client.Object](title string, b Builder[T], errMsg string) {
createScenario(title, b, false, errMsg, false)
func canNotCreateKcp[T client.Object](title string, b Builder[T], errMsg string) {
createScenario(func() client.Client { return infra.KCP().Client() },
title, b, false, errMsg, false)
}
func canNotChange[T client.Object](title string, b Builder[T], cb func(b Builder[T]), errMsg string) {
updateScenario(title, b, cb, false, errMsg, false)
func canNotChangeKcp[T client.Object](title string, b Builder[T], cb func(b Builder[T]), errMsg string) {
updateScenario(func() client.Client { return infra.KCP().Client() },
title, b, cb, false, errMsg, false)
}

func canCreateSkr[T client.Object](title string, b Builder[T]) {
createScenario(func() client.Client { return infra.SKR().Client() }, title, b, true, "", false)
}
func canNotCreateSkr[T client.Object](title string, b Builder[T], errMsg string) {
createScenario(func() client.Client { return infra.SKR().Client() }, title, b, false, errMsg, false)
}
func canChangeSkr[T client.Object](title string, b Builder[T], cb func(b Builder[T])) {
updateScenario(func() client.Client { return infra.SKR().Client() }, title, b, cb, true, "", false)
}
func canNotChangeSkr[T client.Object](title string, b Builder[T], cb func(b Builder[T]), errMsg string) {
updateScenario(func() client.Client { return infra.SKR().Client() }, title, b, cb, false, errMsg, false)
}
12 changes: 6 additions & 6 deletions internal/api-tests/kcp_redisinstance_gcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func (b *redisInstanceBuilderGcp) Build() *cloudcontrolv1beta1.RedisInstance {

var _ = Describe("Feature: KCP RedisInstance GCP", Ordered, func() {

canCreate("RedisInstance GCP can be created with zero replicas if tier is BASIC", &redisInstanceBuilderGcp{tier: "BASIC", replicaCount: 0, memorySizeGb: 16})
canCreate("RedisInstance GCP can be created with under memory size under 5GiB if tier is BASIC", &redisInstanceBuilderGcp{tier: "BASIC", replicaCount: 0, memorySizeGb: 1})
canNotCreate("RedisInstance GCP can not be created with non-zero replicas if tier is BASIC", &redisInstanceBuilderGcp{tier: "BASIC", replicaCount: 1, memorySizeGb: 16}, "")
canCreateKcp("RedisInstance GCP can be created with zero replicas if tier is BASIC", &redisInstanceBuilderGcp{tier: "BASIC", replicaCount: 0, memorySizeGb: 16})
canCreateKcp("RedisInstance GCP can be created with under memory size under 5GiB if tier is BASIC", &redisInstanceBuilderGcp{tier: "BASIC", replicaCount: 0, memorySizeGb: 1})
canNotCreateKcp("RedisInstance GCP can not be created with non-zero replicas if tier is BASIC", &redisInstanceBuilderGcp{tier: "BASIC", replicaCount: 1, memorySizeGb: 16}, "")

canCreate("RedisInstance GCP can be created with non-zero replicas if tier is STANDARD_HA", &redisInstanceBuilderGcp{tier: "STANDARD_HA", replicaCount: 1, memorySizeGb: 16})
canNotCreate("RedisInstance GCP can not be created with zero replicas if tier is STANDARD_HA", &redisInstanceBuilderGcp{tier: "STANDARD_HA", replicaCount: 0, memorySizeGb: 16}, "")
canNotCreate("RedisInstance GCP can not be created with memory size under 5GiB if tier is STANDARD_HA", &redisInstanceBuilderGcp{tier: "STANDARD_HA", replicaCount: 0, memorySizeGb: 1}, "")
canCreateKcp("RedisInstance GCP can be created with non-zero replicas if tier is STANDARD_HA", &redisInstanceBuilderGcp{tier: "STANDARD_HA", replicaCount: 1, memorySizeGb: 16})
canNotCreateKcp("RedisInstance GCP can not be created with zero replicas if tier is STANDARD_HA", &redisInstanceBuilderGcp{tier: "STANDARD_HA", replicaCount: 0, memorySizeGb: 16}, "")
canNotCreateKcp("RedisInstance GCP can not be created with memory size under 5GiB if tier is STANDARD_HA", &redisInstanceBuilderGcp{tier: "STANDARD_HA", replicaCount: 0, memorySizeGb: 1}, "")
})
Loading

0 comments on commit e1cec07

Please sign in to comment.