Skip to content

Commit

Permalink
Add default resource limit (#650)
Browse files Browse the repository at this point in the history
Signed-off-by: Mohammad Fahim Abrar <[email protected]>
Co-authored-by: Tamal Saha <[email protected]>
  • Loading branch information
Mohammad Fahim Abrar and tamalsaha authored Nov 11, 2020
1 parent b5fa4a1 commit 592d5b4
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apis/kubedb/v1alpha2/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const (

DBCustomConfigName = "custom-config"

DefaultCPULimit = ".25"
DefaultMemoryLimit = "512Mi"

// =========================== Database key Constants ============================
PostgresKey = ResourceSingularPostgres + "." + kubedb.GroupName
ElasticsearchKey = ResourceSingularElasticsearch + "." + kubedb.GroupName
Expand Down
5 changes: 5 additions & 0 deletions apis/kubedb/v1alpha2/elasticsearch_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,21 @@ func (e *Elasticsearch) SetDefaults(esVersion *v1alpha1.ElasticsearchVersion, to
if e.Spec.Topology.Ingest.Prefix == "" {
e.Spec.Topology.Ingest.Prefix = ElasticsearchIngestNodePrefix
}
setDefaultResourceLimits(&e.Spec.Topology.Ingest.Resources)

// Default to "data"
if e.Spec.Topology.Data.Prefix == "" {
e.Spec.Topology.Data.Prefix = ElasticsearchDataNodePrefix
}
setDefaultResourceLimits(&e.Spec.Topology.Data.Resources)

// Default to "master"
if e.Spec.Topology.Master.Prefix == "" {
e.Spec.Topology.Master.Prefix = ElasticsearchMasterNodePrefix
}
setDefaultResourceLimits(&e.Spec.Topology.Master.Resources)
} else {
setDefaultResourceLimits(&e.Spec.PodTemplate.Spec.Resources)
}

e.setDefaultAffinity(&e.Spec.PodTemplate, e.OffshootSelectors(), topology)
Expand Down
1 change: 1 addition & 0 deletions apis/kubedb/v1alpha2/etcd_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func (e *Etcd) SetDefaults() {
}

e.Spec.Monitor.SetDefaults()
setDefaultResourceLimits(&e.Spec.PodTemplate.Spec.Resources)
}

func (e *EtcdSpec) GetPersistentSecrets() []string {
Expand Down
24 changes: 24 additions & 0 deletions apis/kubedb/v1alpha2/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package v1alpha2
import (
"fmt"

core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/labels"
appslister "k8s.io/client-go/listers/apps/v1"
apps_util "kmodules.xyz/client-go/apps/v1"
Expand Down Expand Up @@ -60,3 +62,25 @@ func GetServiceTemplate(templates []NamedServiceTemplateSpec, alias ServiceAlias
}
return ofst.ServiceTemplateSpec{}
}

func setDefaultResourceLimits(req *core.ResourceRequirements) {
fn := func(name core.ResourceName, defaultValue resource.Quantity) resource.Quantity {
if req.Limits != nil {
if v, ok := req.Limits[name]; ok {
return v
}
}
if req.Requests != nil {
if v, ok := req.Requests[name]; ok {
return v
}
}
return defaultValue
}

if req.Limits == nil {
req.Limits = core.ResourceList{}
}
req.Limits[core.ResourceCPU] = fn(core.ResourceCPU, resource.MustParse(DefaultCPULimit))
req.Limits[core.ResourceMemory] = fn(core.ResourceMemory, resource.MustParse(DefaultMemoryLimit))
}
1 change: 1 addition & 0 deletions apis/kubedb/v1alpha2/mariadb_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func (m *MariaDB) SetDefaults() {
}

m.Spec.Monitor.SetDefaults()
setDefaultResourceLimits(&m.Spec.PodTemplate.Spec.Resources)
}

func (m *MariaDBSpec) GetPersistentSecrets() []string {
Expand Down
1 change: 1 addition & 0 deletions apis/kubedb/v1alpha2/memcached_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (m *Memcached) SetDefaults() {
}

m.Spec.Monitor.SetDefaults()
setDefaultResourceLimits(&m.Spec.PodTemplate.Spec.Resources)
}

func (m *MemcachedSpec) GetPersistentSecrets() []string {
Expand Down
6 changes: 6 additions & 0 deletions apis/kubedb/v1alpha2/mongodb_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ func (m *MongoDB) SetDefaults(mgVersion *v1alpha1.MongoDBVersion, topology *core
}

if m.Spec.ShardTopology != nil {
setDefaultResourceLimits(&m.Spec.ShardTopology.Mongos.PodTemplate.Spec.Resources)
setDefaultResourceLimits(&m.Spec.ShardTopology.Shard.PodTemplate.Spec.Resources)
setDefaultResourceLimits(&m.Spec.ShardTopology.ConfigServer.PodTemplate.Spec.Resources)

if m.Spec.ShardTopology.Mongos.PodTemplate.Spec.Lifecycle == nil {
m.Spec.ShardTopology.Mongos.PodTemplate.Spec.Lifecycle = new(core.Lifecycle)
}
Expand Down Expand Up @@ -420,6 +424,8 @@ func (m *MongoDB) SetDefaults(mgVersion *v1alpha1.MongoDBVersion, topology *core
m.setDefaultProbes(m.Spec.PodTemplate, mgVersion)
// set default affinity (PodAntiAffinity)
m.setDefaultAffinity(m.Spec.PodTemplate, m.OffshootSelectors(), topology)

setDefaultResourceLimits(&m.Spec.PodTemplate.Spec.Resources)
}

m.SetTLSDefaults()
Expand Down
1 change: 1 addition & 0 deletions apis/kubedb/v1alpha2/mysql_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func (m *MySQL) SetDefaults() {
m.Spec.Monitor.SetDefaults()

m.SetTLSDefaults()
setDefaultResourceLimits(&m.Spec.PodTemplate.Spec.Resources)
}

func (m *MySQL) SetTLSDefaults() {
Expand Down
1 change: 1 addition & 0 deletions apis/kubedb/v1alpha2/perconaxtradb_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func (p *PerconaXtraDB) SetDefaults() {

p.Spec.setDefaultProbes()
p.Spec.Monitor.SetDefaults()
setDefaultResourceLimits(&p.Spec.PodTemplate.Spec.Resources)
}

// setDefaultProbes sets defaults only when probe fields are nil.
Expand Down
1 change: 1 addition & 0 deletions apis/kubedb/v1alpha2/pgbouncer_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (p *PgBouncer) SetDefaults() {
p.Spec.Monitor.SetDefaults()

p.SetTLSDefaults()
setDefaultResourceLimits(&p.Spec.PodTemplate.Spec.Resources)
}

func (p *PgBouncer) SetTLSDefaults() {
Expand Down
1 change: 1 addition & 0 deletions apis/kubedb/v1alpha2/postgres_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func (p *Postgres) SetDefaults() {
}

p.Spec.Monitor.SetDefaults()
setDefaultResourceLimits(&p.Spec.PodTemplate.Spec.Resources)
}

func (e *PostgresSpec) GetPersistentSecrets() []string {
Expand Down
1 change: 1 addition & 0 deletions apis/kubedb/v1alpha2/proxysql_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func (p *ProxySQL) SetDefaults() {
}

p.Spec.Monitor.SetDefaults()
setDefaultResourceLimits(&p.Spec.PodTemplate.Spec.Resources)
}

func (p *ProxySQLSpec) GetPersistentSecrets() []string {
Expand Down
1 change: 1 addition & 0 deletions apis/kubedb/v1alpha2/redis_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func (r *Redis) SetDefaults(topology *core_util.Topology) {
r.Spec.Monitor.SetDefaults()

r.SetTLSDefaults()
setDefaultResourceLimits(&r.Spec.PodTemplate.Spec.Resources)
}

func (r *Redis) SetTLSDefaults() {
Expand Down

0 comments on commit 592d5b4

Please sign in to comment.