Improvements

-handle rollingUpdate flag
-modularize code
-rename config parameter name
This commit is contained in:
Rafia Sabih 2020-04-28 13:20:07 +02:00
parent 8d718c4fc1
commit eec21453f8
6 changed files with 65 additions and 51 deletions

View File

@ -72,7 +72,7 @@ type KubernetesMetaConfiguration struct {
EnablePodAntiAffinity bool `json:"enable_pod_antiaffinity,omitempty"`
PodAntiAffinityTopologyKey string `json:"pod_antiaffinity_topology_key,omitempty"`
PodManagementPolicy string `json:"pod_management_policy,omitempty"`
StatefulsetPropAnnotations []string `json:"statefulset_propagate_annotations,omitempty"`
StatefulsetPropagateAnnotations []string `json:"statefulset_propagate_annotations,omitempty"`
}
// PostgresPodResourcesDefaults defines the spec of default resources
@ -206,7 +206,7 @@ type OperatorConfigurationData struct {
Scalyr ScalyrConfiguration `json:"scalyr"`
LogicalBackup OperatorLogicalBackupConfiguration `json:"logical_backup"`
ConnectionPooler ConnectionPoolerConfiguration `json:"connection_pooler"`
StatefulsetPropAnnotations []string `json:"statefulset_propagate_annotations,omitempty"`
StatefulsetPropagateAnnotations []string `json:"statefulset_propagate_annotations,omitempty"`
}
//Duration shortens this frequently used name

View File

@ -202,6 +202,11 @@ func (in *KubernetesMetaConfiguration) DeepCopyInto(out *KubernetesMetaConfigura
}
}
out.PodEnvironmentConfigMap = in.PodEnvironmentConfigMap
if in.StatefulsetPropagateAnnotations != nil {
in, out := &in.StatefulsetPropagateAnnotations, &out.StatefulsetPropagateAnnotations
*out = make([]string, len(*in))
copy(*out, *in)
}
return
}

View File

@ -6,6 +6,7 @@ import (
"fmt"
"path"
"sort"
"strconv"
"github.com/sirupsen/logrus"
@ -1149,13 +1150,14 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
}
annotations = make(map[string]string)
annotations[rollingUpdateStatefulsetAnnotationKey] = strconv.FormatBool(false)
statefulSet := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: c.statefulSetName(),
Namespace: c.Namespace,
Labels: c.labelsSet(true),
Annotations: annotations,
Annotations: c.PropagateAnnotationsToStatefulsets(annotations),
},
Spec: appsv1.StatefulSetSpec{
Replicas: &numberOfInstances,

View File

@ -331,22 +331,9 @@ func (c *Cluster) syncStatefulSet() error {
}
}
}
ToPropagateAnnotations := c.OpConfig.StatefulsetPropAnnotations
PgCRDAnnotations := c.Postgresql.ObjectMeta.GetAnnotations()
annotations := make(map[string]string)
if ToPropagateAnnotations != nil && PgCRDAnnotations != nil {
for _, anno := range ToPropagateAnnotations {
for k, v := range PgCRDAnnotations {
matched, err := regexp.MatchString(anno, k)
if err == nil && matched {
annotations[k] = v
}
}
}
annotations := c.PropagateAnnotationsToStatefulsets(c.Statefulset.Annotations)
c.updateStatefulSetAnnotations(annotations)
}
}
// Apply special PostgreSQL parameters that can only be set via the Patroni API.
// it is important to do it after the statefulset pods are there, but before the rolling update
@ -370,6 +357,26 @@ func (c *Cluster) syncStatefulSet() error {
return nil
}
// PropagateAnnotationsToStatefulsets updates annotations to statefulsets if required
// based on the annotations in postgres CRD
func (c *Cluster) PropagateAnnotationsToStatefulsets(annotations map[string]string) map[string]string {
ToPropagateAnnotations := c.OpConfig.StatefulsetPropagateAnnotations
PgCRDAnnotations := c.Postgresql.ObjectMeta.GetAnnotations()
if ToPropagateAnnotations != nil && PgCRDAnnotations != nil {
for _, anno := range ToPropagateAnnotations {
for k, v := range PgCRDAnnotations {
matched, err := regexp.MatchString(anno, k)
if err == nil && matched {
annotations[k] = v
}
}
}
}
return annotations
}
// checkAndSetGlobalPostgreSQLConfiguration checks whether cluster-wide API parameters
// (like max_connections) has changed and if necessary sets it via the Patroni API
func (c *Cluster) checkAndSetGlobalPostgreSQLConfiguration() error {

View File

@ -46,7 +46,7 @@ func (c *Controller) importConfigurationFromCRD(fromCRD *acidv1.OperatorConfigur
result.ShmVolume = fromCRD.ShmVolume
result.Sidecars = fromCRD.Sidecars
result.StatefulsetPropAnnotations = fromCRD.StatefulsetPropAnnotations
result.StatefulsetPropagateAnnotations = fromCRD.StatefulsetPropagateAnnotations
// user config
result.SuperUsername = fromCRD.PostgresUsersConfiguration.SuperUsername

View File

@ -134,7 +134,7 @@ type Config struct {
EnableReplicaLoadBalancer bool `name:"enable_replica_load_balancer" default:"false"`
CustomServiceAnnotations map[string]string `name:"custom_service_annotations"`
CustomPodAnnotations map[string]string `name:"custom_pod_annotations"`
StatefulsetPropAnnotations []string `name:"statefulset_propagate_annotations"`
StatefulsetPropagateAnnotations []string `name:"statefulset_propagate_annotations"`
EnablePodAntiAffinity bool `name:"enable_pod_antiaffinity" default:"false"`
PodAntiAffinityTopologyKey string `name:"pod_antiaffinity_topology_key" default:"kubernetes.io/hostname"`
// deprecated and kept for backward compatibility