Skip to content

Commit

Permalink
Move Redis mutator defaults to SetDefaults() (#1385)
Browse files Browse the repository at this point in the history
Signed-off-by: Neaj Morshad <neaj@appscode.com>
  • Loading branch information
Neaj-Morshad-101 authored Jan 16, 2025
1 parent ce659b3 commit bdae9dd
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
40 changes: 35 additions & 5 deletions apis/kubedb/v1/redis_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"kubedb.dev/apimachinery/apis/kubedb"
"kubedb.dev/apimachinery/crds"

"github.com/Masterminds/semver/v3"
"github.com/pkg/errors"
promapi "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"gomodules.xyz/pointer"
core "k8s.io/api/core/v1"
Expand Down Expand Up @@ -206,9 +208,31 @@ func (r Redis) StatsServiceLabels() map[string]string {
return r.ServiceLabels(StatsServiceAlias, map[string]string{kubedb.LabelRole: kubedb.RoleStats})
}

func (r *Redis) SetDefaults(rdVersion *catalog.RedisVersion) {
func (r *Redis) SetDefaults(rdVersion *catalog.RedisVersion) error {
if r == nil {
return
return nil
}

curVersion, err := semver.NewVersion(rdVersion.Spec.Version)
if err != nil {
return fmt.Errorf("can't get the semvar version from RedisVersion spec. err: %v", err)
}
if curVersion.Major() <= 4 {
r.Spec.DisableAuth = true
}

if r.Spec.Halted {
if r.Spec.DeletionPolicy == DeletionPolicyDoNotTerminate {
return errors.New(`Can't halt, since termination policy is 'DoNotTerminate'`)
}
r.Spec.DeletionPolicy = DeletionPolicyHalt
}
if r.Spec.DeletionPolicy == "" {
r.Spec.DeletionPolicy = DeletionPolicyDelete
}

if r.Spec.Replicas == nil && r.Spec.Mode != RedisModeCluster {
r.Spec.Replicas = pointer.Int32P(1)
}

// perform defaulting
Expand All @@ -224,13 +248,16 @@ func (r *Redis) SetDefaults(rdVersion *catalog.RedisVersion) {
if r.Spec.Cluster.Replicas == nil {
r.Spec.Cluster.Replicas = pointer.Int32P(2)
}
} else if r.Spec.Mode == RedisModeSentinel {
if r.Spec.SentinelRef != nil && r.Spec.SentinelRef.Namespace == "" {
r.Spec.SentinelRef.Namespace = r.Namespace
}
}

if r.Spec.StorageType == "" {
r.Spec.StorageType = StorageTypeDurable
}
if r.Spec.DeletionPolicy == "" {
r.Spec.DeletionPolicy = DeletionPolicyDelete
}

r.setDefaultContainerSecurityContext(rdVersion, &r.Spec.PodTemplate)
r.setDefaultContainerResourceLimits(&r.Spec.PodTemplate)

Expand All @@ -245,6 +272,7 @@ func (r *Redis) SetDefaults(rdVersion *catalog.RedisVersion) {

r.SetTLSDefaults()
r.SetHealthCheckerDefaults()

r.Spec.Monitor.SetDefaults()
if r.Spec.Monitor != nil && r.Spec.Monitor.Prometheus != nil {
if r.Spec.Monitor.Prometheus.Exporter.SecurityContext.RunAsUser == nil {
Expand All @@ -254,6 +282,8 @@ func (r *Redis) SetDefaults(rdVersion *catalog.RedisVersion) {
r.Spec.Monitor.Prometheus.Exporter.SecurityContext.RunAsGroup = rdVersion.Spec.SecurityContext.RunAsUser
}
}

return nil
}

func (r *Redis) SetHealthCheckerDefaults() {
Expand Down
30 changes: 26 additions & 4 deletions apis/kubedb/v1/redis_sentinel_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"kubedb.dev/apimachinery/apis/kubedb"
"kubedb.dev/apimachinery/crds"

"github.com/Masterminds/semver/v3"
"github.com/pkg/errors"
promapi "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"gomodules.xyz/pointer"
core "k8s.io/api/core/v1"
Expand Down Expand Up @@ -181,18 +183,36 @@ func (rs RedisSentinel) StatsServiceLabels() map[string]string {
return rs.ServiceLabels(StatsServiceAlias, map[string]string{kubedb.LabelRole: kubedb.RoleStats})
}

func (rs *RedisSentinel) SetDefaults(rdVersion *catalog.RedisVersion) {
func (rs *RedisSentinel) SetDefaults(rdVersion *catalog.RedisVersion) error {
if rs == nil {
return
return nil
}

if rs.Spec.StorageType == "" {
rs.Spec.StorageType = StorageTypeDurable
curVersion, err := semver.NewVersion(rdVersion.Spec.Version)
if err != nil {
return fmt.Errorf("can't get the semvar version from RedisVersion spec. err: %v", err)
}
if curVersion.Major() <= 4 {
rs.Spec.DisableAuth = true
}
if rs.Spec.Halted {
if rs.Spec.DeletionPolicy == DeletionPolicyDoNotTerminate {
return errors.New(`Can't halt, since termination policy is 'DoNotTerminate'`)
}
rs.Spec.DeletionPolicy = DeletionPolicyHalt
}
if rs.Spec.DeletionPolicy == "" {
rs.Spec.DeletionPolicy = DeletionPolicyDelete
}

if rs.Spec.Replicas == nil {
rs.Spec.Replicas = pointer.Int32P(1)
}

if rs.Spec.StorageType == "" {
rs.Spec.StorageType = StorageTypeDurable
}

rs.setDefaultContainerSecurityContext(rdVersion, &rs.Spec.PodTemplate)
rs.setDefaultContainerResourceLimits(&rs.Spec.PodTemplate)

Expand All @@ -211,6 +231,8 @@ func (rs *RedisSentinel) SetDefaults(rdVersion *catalog.RedisVersion) {
rs.Spec.Monitor.Prometheus.Exporter.SecurityContext.RunAsGroup = rdVersion.Spec.SecurityContext.RunAsUser
}
}

return nil
}

func (rs *RedisSentinel) SetHealthCheckerDefaults() {
Expand Down

0 comments on commit bdae9dd

Please sign in to comment.