Skip to content

Commit

Permalink
feat: supporting nosync and nofailover node tags
Browse files Browse the repository at this point in the history
  • Loading branch information
rkojedzinszky committed Mar 18, 2024
1 parent 83661e3 commit 51bc574
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
15 changes: 15 additions & 0 deletions api/v1alpha1/patronipostgres_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,28 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// NodeTags holds a subset of Patroni tags
// https://patroni.readthedocs.io/en/latest/yaml_configuration.html#tags
type NodeTags struct {
// NoSync If set to true the node will never be selected as a synchronous replica.
NoSync bool `json:"nosync,omitempty"`

// NoFailover controls whether this node is allowed to participate in the leader
// race and become a leader. Defaults to false, meaning this node _can_
// participate in leader races.
NoFailover bool `json:"nofailover,omitempty"`
}

// Node represents a PatroniPostgres node's configuration
type Node struct {
// StorageClassName references a storage class to allocate volume from
StorageClassName string `json:"storageClassName"`

// AccessMode allows for overriding implicit ReadWriteOnce accessmode
AccessMode corev1.PersistentVolumeAccessMode `json:"accessMode,omitempty"`

// Tags for Node
Tags NodeTags `json:"tags,omitempty"`
}

type VolumeStatus struct {
Expand Down
14 changes: 14 additions & 0 deletions config/crd/bases/kwebs.cloud_patronipostgres.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2191,6 +2191,20 @@ spec:
description: StorageClassName references a storage class to
allocate volume from
type: string
tags:
description: Tags for Node
properties:
nofailover:
description: NoFailover controls whether this node is allowed
to participate in the leader race and become a leader.
Defaults to false, meaning this node _can_ participate
in leader races.
type: boolean
nosync:
description: NoSync If set to true the node will never be
selected as a synchronous replica.
type: boolean
type: object
required:
- storageClassName
type: object
Expand Down
14 changes: 14 additions & 0 deletions config/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2474,6 +2474,20 @@ spec:
description: StorageClassName references a storage class to
allocate volume from
type: string
tags:
description: Tags for Node
properties:
nofailover:
description: NoFailover controls whether this node is allowed
to participate in the leader race and become a leader.
Defaults to false, meaning this node _can_ participate
in leader races.
type: boolean
nosync:
description: NoSync If set to true the node will never be
selected as a synchronous replica.
type: boolean
type: object
required:
- storageClassName
type: object
Expand Down
27 changes: 27 additions & 0 deletions private/controllers/statefulset/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package statefulset
import (
"encoding/json"
"fmt"
"strings"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -281,6 +282,8 @@ func ReconcileSts(ctx context.Context, p *v1alpha1.PatroniPostgres) (sts *appsv1
},
}

sts.Spec.Template.Spec.Containers[0].Env = append(sts.Spec.Template.Spec.Containers[0].Env, genNodeTagsEnvs(p)...)

sts.Spec.Template.Spec.Containers = append(sts.Spec.Template.Spec.Containers, p.Spec.ExtraContainers...)

if create {
Expand Down Expand Up @@ -314,3 +317,27 @@ func GetK8SStatefulSet(ctx context.Context, p *v1alpha1.PatroniPostgres) (sts *a
err = ctx.Get(ctx, types.NamespacedName{Namespace: p.Namespace, Name: p.Name}, sts)
return
}

func genNodeTagsEnvs(p *v1alpha1.PatroniPostgres) (envs []corev1.EnvVar) {
podPrefix := strings.ReplaceAll(p.Name, "-", "_")

for idx := range p.Spec.Nodes {
node := &p.Spec.Nodes[idx]

if node.Tags.NoSync {
envs = append(envs, corev1.EnvVar{
Name: fmt.Sprintf("PATRONI_NODE_%s_%d_TAG_%s", podPrefix, idx, "nosync"),
Value: "true",
})
}

if node.Tags.NoFailover {
envs = append(envs, corev1.EnvVar{
Name: fmt.Sprintf("PATRONI_NODE_%s_%d_TAG_%s", podPrefix, idx, "nofailover"),
Value: "true",
})
}
}

return
}

0 comments on commit 51bc574

Please sign in to comment.