Skip to content

Commit

Permalink
Update KubeDB api (#548)
Browse files Browse the repository at this point in the history
Signed-off-by: 1gtm <[email protected]>
  • Loading branch information
1gtm authored Nov 11, 2020
1 parent 3f4e0fd commit 08bce12
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
kmodules.xyz/custom-resources v0.0.0-20201105075444-3c6af51b4f79
kmodules.xyz/monitoring-agent-api v0.0.0-20201105074044-be7a1044891a
kmodules.xyz/objectstore-api v0.0.0-20201105133858-cbb2af88d50a
kubedb.dev/apimachinery v0.14.2-0.20201111085900-25e0e4af16bb
kubedb.dev/apimachinery v0.14.2-0.20201111182733-592d5b4729bb
stash.appscode.dev/apimachinery v0.11.6
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1394,8 +1394,8 @@ kmodules.xyz/openshift v0.0.0-20201105073146-0da509a7d39f/go.mod h1:vFwB/f5rVH5Q
kmodules.xyz/prober v0.0.0-20201105074402-a243b3a27fd8 h1:UJb5lHQVFKbmlgmRLq5IWJGtz3JqYYbyVG+dNdjC9Cc=
kmodules.xyz/prober v0.0.0-20201105074402-a243b3a27fd8/go.mod h1:2eN8X5Wq7/AAgE5AWMAX8T0lE51HZiYEldG2RQuouX4=
kmodules.xyz/webhook-runtime v0.0.0-20201105073856-2dc7382b88c6/go.mod h1:xLgewoOzwR5ZrVOHQ2SR0P4E7tgCyBWbYlUawEXgeF4=
kubedb.dev/apimachinery v0.14.2-0.20201111085900-25e0e4af16bb h1:bVqi4Xs1HVDch9f3r/oBCCSQhfkbnm8shGnp+dhDyI8=
kubedb.dev/apimachinery v0.14.2-0.20201111085900-25e0e4af16bb/go.mod h1:4H3J+V7lZy8dLgz0+z/Nzd13cEgZHnwbMaH729rxl/M=
kubedb.dev/apimachinery v0.14.2-0.20201111182733-592d5b4729bb h1:mWHMhfdd9VaXcuVSaawCBiwzh5Hc4+eicqEHmzJHh6Q=
kubedb.dev/apimachinery v0.14.2-0.20201111182733-592d5b4729bb/go.mod h1:4H3J+V7lZy8dLgz0+z/Nzd13cEgZHnwbMaH729rxl/M=
modernc.org/cc v1.0.0/go.mod h1:1Sk4//wdnYJiUIxnW8ddKpaOJCF37yAdqYnkxUpaYxw=
modernc.org/golex v1.0.0/go.mod h1:b/QX9oBD/LhixY6NDh+IdGv17hgB+51fET1i2kPSmvk=
modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03k=
Expand Down
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 All @@ -60,7 +63,6 @@ const (
ElasticsearchTransportPort = 9300
ElasticsearchTransportPortName = "transport"
ElasticsearchMetricsPort = 9600
ElasticsearchMetricsPortName = "metrics"
ElasticsearchIngestNodePrefix = "ingest"
ElasticsearchDataNodePrefix = "data"
ElasticsearchMasterNodePrefix = "master"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (e Elasticsearch) ServiceName() string {
return e.OffshootName()
}

func (e *Elasticsearch) MasterServiceName() string {
func (e *Elasticsearch) MasterDiscoveryServiceName() string {
return meta_util.NameWithSuffix(e.ServiceName(), "master")
}

Expand Down 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
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
47 changes: 47 additions & 0 deletions vendor/kubedb.dev/apimachinery/apis/kubedb/v1alpha2/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ 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"
ofst "kmodules.xyz/offshoot-api/api/v1"
)

func checkReplicas(lister appslister.StatefulSetNamespaceLister, selector labels.Selector, expectedItems int) (bool, string, error) {
Expand All @@ -37,3 +40,47 @@ func checkReplicas(lister appslister.StatefulSetNamespaceLister, selector labels
ready, msg := apps_util.StatefulSetsAreReady(items)
return ready, msg, nil
}

// HasServiceTemplate returns "true" if the desired serviceTemplate provided in "aliaS" is present in the serviceTemplate list.
// Otherwise, it returns "false".
func HasServiceTemplate(templates []NamedServiceTemplateSpec, alias ServiceAlias) bool {
for i := range templates {
if templates[i].Alias == alias {
return true
}
}
return false
}

// GetServiceTemplate returns a pointer to the desired serviceTemplate referred by "aliaS". Otherwise, it returns nil.
func GetServiceTemplate(templates []NamedServiceTemplateSpec, alias ServiceAlias) ofst.ServiceTemplateSpec {
for i := range templates {
c := templates[i]
if c.Alias == alias {
return c.ServiceTemplate
}
}
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))
}
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
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
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
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
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
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
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
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
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
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ kmodules.xyz/objectstore-api/api/v1
kmodules.xyz/offshoot-api/api/v1
# kmodules.xyz/prober v0.0.0-20201105074402-a243b3a27fd8
kmodules.xyz/prober/api/v1
# kubedb.dev/apimachinery v0.14.2-0.20201111085900-25e0e4af16bb
# kubedb.dev/apimachinery v0.14.2-0.20201111182733-592d5b4729bb
kubedb.dev/apimachinery/apis
kubedb.dev/apimachinery/apis/autoscaling
kubedb.dev/apimachinery/apis/autoscaling/v1alpha1
Expand Down

0 comments on commit 08bce12

Please sign in to comment.