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

@ -65,14 +65,14 @@ type KubernetesMetaConfiguration struct {
NodeReadinessLabel map[string]string `json:"node_readiness_label,omitempty"`
CustomPodAnnotations map[string]string `json:"custom_pod_annotations,omitempty"`
// TODO: use a proper toleration structure?
PodToleration map[string]string `json:"toleration,omitempty"`
PodEnvironmentConfigMap spec.NamespacedName `json:"pod_environment_configmap,omitempty"`
PodPriorityClassName string `json:"pod_priority_class_name,omitempty"`
MasterPodMoveTimeout Duration `json:"master_pod_move_timeout,omitempty"`
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"`
PodToleration map[string]string `json:"toleration,omitempty"`
PodEnvironmentConfigMap spec.NamespacedName `json:"pod_environment_configmap,omitempty"`
PodPriorityClassName string `json:"pod_priority_class_name,omitempty"`
MasterPodMoveTimeout Duration `json:"master_pod_move_timeout,omitempty"`
EnablePodAntiAffinity bool `json:"enable_pod_antiaffinity,omitempty"`
PodAntiAffinityTopologyKey string `json:"pod_antiaffinity_topology_key,omitempty"`
PodManagementPolicy string `json:"pod_management_policy,omitempty"`
StatefulsetPropagateAnnotations []string `json:"statefulset_propagate_annotations,omitempty"`
}
// PostgresPodResourcesDefaults defines the spec of default resources
@ -182,31 +182,31 @@ type OperatorLogicalBackupConfiguration struct {
// OperatorConfigurationData defines the operation config
type OperatorConfigurationData struct {
EnableCRDValidation *bool `json:"enable_crd_validation,omitempty"`
EtcdHost string `json:"etcd_host,omitempty"`
KubernetesUseConfigMaps bool `json:"kubernetes_use_configmaps,omitempty"`
DockerImage string `json:"docker_image,omitempty"`
Workers uint32 `json:"workers,omitempty"`
MinInstances int32 `json:"min_instances,omitempty"`
MaxInstances int32 `json:"max_instances,omitempty"`
ResyncPeriod Duration `json:"resync_period,omitempty"`
RepairPeriod Duration `json:"repair_period,omitempty"`
SetMemoryRequestToLimit bool `json:"set_memory_request_to_limit,omitempty"`
ShmVolume *bool `json:"enable_shm_volume,omitempty"`
Sidecars map[string]string `json:"sidecar_docker_images,omitempty"`
PostgresUsersConfiguration PostgresUsersConfiguration `json:"users"`
Kubernetes KubernetesMetaConfiguration `json:"kubernetes"`
PostgresPodResources PostgresPodResourcesDefaults `json:"postgres_pod_resources"`
Timeouts OperatorTimeouts `json:"timeouts"`
LoadBalancer LoadBalancerConfiguration `json:"load_balancer"`
AWSGCP AWSGCPConfiguration `json:"aws_or_gcp"`
OperatorDebug OperatorDebugConfiguration `json:"debug"`
TeamsAPI TeamsAPIConfiguration `json:"teams_api"`
LoggingRESTAPI LoggingRESTAPIConfiguration `json:"logging_rest_api"`
Scalyr ScalyrConfiguration `json:"scalyr"`
LogicalBackup OperatorLogicalBackupConfiguration `json:"logical_backup"`
ConnectionPooler ConnectionPoolerConfiguration `json:"connection_pooler"`
StatefulsetPropAnnotations []string `json:"statefulset_propagate_annotations,omitempty"`
EnableCRDValidation *bool `json:"enable_crd_validation,omitempty"`
EtcdHost string `json:"etcd_host,omitempty"`
KubernetesUseConfigMaps bool `json:"kubernetes_use_configmaps,omitempty"`
DockerImage string `json:"docker_image,omitempty"`
Workers uint32 `json:"workers,omitempty"`
MinInstances int32 `json:"min_instances,omitempty"`
MaxInstances int32 `json:"max_instances,omitempty"`
ResyncPeriod Duration `json:"resync_period,omitempty"`
RepairPeriod Duration `json:"repair_period,omitempty"`
SetMemoryRequestToLimit bool `json:"set_memory_request_to_limit,omitempty"`
ShmVolume *bool `json:"enable_shm_volume,omitempty"`
Sidecars map[string]string `json:"sidecar_docker_images,omitempty"`
PostgresUsersConfiguration PostgresUsersConfiguration `json:"users"`
Kubernetes KubernetesMetaConfiguration `json:"kubernetes"`
PostgresPodResources PostgresPodResourcesDefaults `json:"postgres_pod_resources"`
Timeouts OperatorTimeouts `json:"timeouts"`
LoadBalancer LoadBalancerConfiguration `json:"load_balancer"`
AWSGCP AWSGCPConfiguration `json:"aws_or_gcp"`
OperatorDebug OperatorDebugConfiguration `json:"debug"`
TeamsAPI TeamsAPIConfiguration `json:"teams_api"`
LoggingRESTAPI LoggingRESTAPIConfiguration `json:"logging_rest_api"`
Scalyr ScalyrConfiguration `json:"scalyr"`
LogicalBackup OperatorLogicalBackupConfiguration `json:"logical_backup"`
ConnectionPooler ConnectionPoolerConfiguration `json:"connection_pooler"`
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,21 +331,8 @@ 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
}
}
}
c.updateStatefulSetAnnotations(annotations)
}
annotations := c.PropagateAnnotationsToStatefulsets(c.Statefulset.Annotations)
c.updateStatefulSetAnnotations(annotations)
}
// Apply special PostgreSQL parameters that can only be set via the Patroni API.
@ -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