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"` EnablePodAntiAffinity bool `json:"enable_pod_antiaffinity,omitempty"`
PodAntiAffinityTopologyKey string `json:"pod_antiaffinity_topology_key,omitempty"` PodAntiAffinityTopologyKey string `json:"pod_antiaffinity_topology_key,omitempty"`
PodManagementPolicy string `json:"pod_management_policy,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 // PostgresPodResourcesDefaults defines the spec of default resources
@ -206,7 +206,7 @@ type OperatorConfigurationData struct {
Scalyr ScalyrConfiguration `json:"scalyr"` Scalyr ScalyrConfiguration `json:"scalyr"`
LogicalBackup OperatorLogicalBackupConfiguration `json:"logical_backup"` LogicalBackup OperatorLogicalBackupConfiguration `json:"logical_backup"`
ConnectionPooler ConnectionPoolerConfiguration `json:"connection_pooler"` 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 //Duration shortens this frequently used name

View File

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

View File

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

View File

@ -331,22 +331,9 @@ func (c *Cluster) syncStatefulSet() error {
} }
} }
} }
ToPropagateAnnotations := c.OpConfig.StatefulsetPropAnnotations annotations := c.PropagateAnnotationsToStatefulsets(c.Statefulset.Annotations)
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
}
}
}
c.updateStatefulSetAnnotations(annotations) c.updateStatefulSetAnnotations(annotations)
} }
}
// Apply special PostgreSQL parameters that can only be set via the Patroni API. // 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 // 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 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 // checkAndSetGlobalPostgreSQLConfiguration checks whether cluster-wide API parameters
// (like max_connections) has changed and if necessary sets it via the Patroni API // (like max_connections) has changed and if necessary sets it via the Patroni API
func (c *Cluster) checkAndSetGlobalPostgreSQLConfiguration() error { func (c *Cluster) checkAndSetGlobalPostgreSQLConfiguration() error {

View File

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

View File

@ -134,7 +134,7 @@ type Config struct {
EnableReplicaLoadBalancer bool `name:"enable_replica_load_balancer" default:"false"` EnableReplicaLoadBalancer bool `name:"enable_replica_load_balancer" default:"false"`
CustomServiceAnnotations map[string]string `name:"custom_service_annotations"` CustomServiceAnnotations map[string]string `name:"custom_service_annotations"`
CustomPodAnnotations map[string]string `name:"custom_pod_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"` EnablePodAntiAffinity bool `name:"enable_pod_antiaffinity" default:"false"`
PodAntiAffinityTopologyKey string `name:"pod_antiaffinity_topology_key" default:"kubernetes.io/hostname"` PodAntiAffinityTopologyKey string `name:"pod_antiaffinity_topology_key" default:"kubernetes.io/hostname"`
// deprecated and kept for backward compatibility // deprecated and kept for backward compatibility