Skip to content

Commit

Permalink
Add ReplicationSlot support for Postgres (#1142)
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <tamal@appscode.com>
Signed-off-by: souravbiswassanto <saurov@appscode.com>
Co-authored-by: souravbiswassanto <saurov@appscode.com>
  • Loading branch information
tamalsaha and souravbiswassanto authored Mar 15, 2024
1 parent 9c51f2d commit e156d7a
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 1 deletion.
46 changes: 45 additions & 1 deletion apis/kubedb/v1alpha2/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions apis/kubedb/v1alpha2/postgres_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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 @@ -261,6 +263,7 @@ func (p *Postgres) SetDefaults(postgresVersion *catalog.PostgresVersion, topolog
// So that /var/pv directory have the group permission for the RunAsGroup user GID.
// Otherwise, We will get write permission denied.
p.Spec.PodTemplate.Spec.SecurityContext.FSGroup = p.Spec.PodTemplate.Spec.ContainerSecurityContext.RunAsGroup
p.SetDefaultReplicationMode(postgresVersion)
p.SetArbiterDefault()
p.SetTLSDefaults()
p.SetHealthCheckerDefaults()
Expand All @@ -278,6 +281,39 @@ func (p *Postgres) SetDefaults(postgresVersion *catalog.PostgresVersion, topolog
}
}

func getMajorPgVersion(postgresVersion *catalog.PostgresVersion) (uint64, error) {
ver, err := semver.NewVersion(postgresVersion.Spec.Version)
if err != nil {
return 0, errors.Wrap(err, "Failed to get postgres major.")
}
return ver.Major(), nil
}

// SetDefaultReplicationMode set the default replication mode.
// Replication slot will be prioritized if no WalLimitPolicy is mentioned
func (p *Postgres) SetDefaultReplicationMode(postgresVersion *catalog.PostgresVersion) {
majorVersion, _ := getMajorPgVersion(postgresVersion)
if p.Spec.Replication == nil {
p.Spec.Replication = &PostgresReplication{}
}
if p.Spec.Replication.WALLimitPolicy == "" {
if majorVersion <= uint64(12) {
p.Spec.Replication.WALLimitPolicy = WALKeepSegment
} else {
p.Spec.Replication.WALLimitPolicy = WALKeepSize
}
}
if p.Spec.Replication.WALLimitPolicy == WALKeepSegment && p.Spec.Replication.WalKeepSegment == nil {
p.Spec.Replication.WalKeepSegment = pointer.Int32P(64)
}
if p.Spec.Replication.WALLimitPolicy == WALKeepSize && p.Spec.Replication.WalKeepSizeInMegaBytes == nil {
p.Spec.Replication.WalKeepSizeInMegaBytes = pointer.Int32P(1024)
}
if p.Spec.Replication.WALLimitPolicy == ReplicationSlot && p.Spec.Replication.MaxSlotWALKeepSizeInMegaBytes == nil {
p.Spec.Replication.MaxSlotWALKeepSizeInMegaBytes = pointer.Int32P(-1)
}
}

func (p *Postgres) SetArbiterDefault() {
if p.Spec.Arbiter == nil {
p.Spec.Arbiter = &ArbiterSpec{
Expand Down
22 changes: 22 additions & 0 deletions apis/kubedb/v1alpha2/postgres_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,28 @@ type PostgresSpec struct {
// Arbiter controls spec for arbiter pods
// +optional
Arbiter *ArbiterSpec `json:"arbiter,omitempty"`

// +optional
Replication *PostgresReplication `json:"replication,omitempty"`
}

type WALLimitPolicy string

const (
WALKeepSize WALLimitPolicy = "WALKeepSize"
ReplicationSlot WALLimitPolicy = "ReplicationSlot"
WALKeepSegment WALLimitPolicy = "WALKeepSegment"
)

type PostgresReplication struct {
WALLimitPolicy WALLimitPolicy `json:"walLimitPolicy"`

// +optional
WalKeepSizeInMegaBytes *int32 `json:"walKeepSize,omitempty"`
// +optional
WalKeepSegment *int32 `json:"walKeepSegment,omitempty"`
// +optional
MaxSlotWALKeepSizeInMegaBytes *int32 `json:"maxSlotWALKeepSize,omitempty"`
}

type ArbiterSpec struct {
Expand Down
36 changes: 36 additions & 0 deletions apis/kubedb/v1alpha2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions crds/kubedb.com_postgreses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4027,6 +4027,22 @@ spec:
replicas:
format: int32
type: integer
replication:
properties:
maxSlotWALKeepSize:
format: int32
type: integer
walKeepSegment:
format: int32
type: integer
walKeepSize:
format: int32
type: integer
walLimitPolicy:
type: string
required:
- walLimitPolicy
type: object
serviceTemplates:
items:
properties:
Expand Down
27 changes: 27 additions & 0 deletions openapi/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -25362,6 +25362,30 @@
}
]
},
"dev.kubedb.apimachinery.apis.kubedb.v1alpha2.PostgresReplication": {
"type": "object",
"required": [
"walLimitPolicy"
],
"properties": {
"maxSlotWALKeepSize": {
"type": "integer",
"format": "int32"
},
"walKeepSegment": {
"type": "integer",
"format": "int32"
},
"walKeepSize": {
"type": "integer",
"format": "int32"
},
"walLimitPolicy": {
"type": "string",
"default": ""
}
}
},
"dev.kubedb.apimachinery.apis.kubedb.v1alpha2.PostgresSpec": {
"type": "object",
"required": [
Expand Down Expand Up @@ -25444,6 +25468,9 @@
"type": "integer",
"format": "int32"
},
"replication": {
"$ref": "#/definitions/dev.kubedb.apimachinery.apis.kubedb.v1alpha2.PostgresReplication"
},
"serviceTemplates": {
"description": "ServiceTemplates is an optional configuration for services used to expose database",
"type": "array",
Expand Down

0 comments on commit e156d7a

Please sign in to comment.