Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate annotations to the StatefulSet #932

Merged
merged 26 commits into from May 4, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 21 additions & 13 deletions pkg/apis/acid.zalan.do/v1/crds.go
Expand Up @@ -21,48 +21,48 @@ const (

// PostgresCRDResourceColumns definition of AdditionalPrinterColumns for postgresql CRD
var PostgresCRDResourceColumns = []apiextv1beta1.CustomResourceColumnDefinition{
apiextv1beta1.CustomResourceColumnDefinition{
{
Name: "Team",
Type: "string",
Description: "Team responsible for Postgres cluster",
JSONPath: ".spec.teamId",
},
apiextv1beta1.CustomResourceColumnDefinition{
{
Name: "Version",
Type: "string",
Description: "PostgreSQL version",
JSONPath: ".spec.postgresql.version",
},
apiextv1beta1.CustomResourceColumnDefinition{
{
Name: "Pods",
Type: "integer",
Description: "Number of Pods per Postgres cluster",
JSONPath: ".spec.numberOfInstances",
},
apiextv1beta1.CustomResourceColumnDefinition{
{
Name: "Volume",
Type: "string",
Description: "Size of the bound volume",
JSONPath: ".spec.volume.size",
},
apiextv1beta1.CustomResourceColumnDefinition{
{
Name: "CPU-Request",
Type: "string",
Description: "Requested CPU for Postgres containers",
JSONPath: ".spec.resources.requests.cpu",
},
apiextv1beta1.CustomResourceColumnDefinition{
{
Name: "Memory-Request",
Type: "string",
Description: "Requested memory for Postgres containers",
JSONPath: ".spec.resources.requests.memory",
},
apiextv1beta1.CustomResourceColumnDefinition{
{
Name: "Age",
Type: "date",
JSONPath: ".metadata.creationTimestamp",
},
apiextv1beta1.CustomResourceColumnDefinition{
{
Name: "Status",
Type: "string",
Description: "Current sync status of postgresql resource",
Expand All @@ -72,31 +72,31 @@ var PostgresCRDResourceColumns = []apiextv1beta1.CustomResourceColumnDefinition{

// OperatorConfigCRDResourceColumns definition of AdditionalPrinterColumns for OperatorConfiguration CRD
var OperatorConfigCRDResourceColumns = []apiextv1beta1.CustomResourceColumnDefinition{
apiextv1beta1.CustomResourceColumnDefinition{
{
Name: "Image",
Type: "string",
Description: "Spilo image to be used for Pods",
JSONPath: ".configuration.docker_image",
},
apiextv1beta1.CustomResourceColumnDefinition{
{
Name: "Cluster-Label",
Type: "string",
Description: "Label for K8s resources created by operator",
JSONPath: ".configuration.kubernetes.cluster_name_label",
},
apiextv1beta1.CustomResourceColumnDefinition{
{
Name: "Service-Account",
Type: "string",
Description: "Name of service account to be used",
JSONPath: ".configuration.kubernetes.pod_service_account_name",
},
apiextv1beta1.CustomResourceColumnDefinition{
{
Name: "Min-Instances",
Type: "integer",
Description: "Minimum number of instances per Postgres cluster",
JSONPath: ".configuration.min_instances",
},
apiextv1beta1.CustomResourceColumnDefinition{
{
Name: "Age",
Type: "date",
JSONPath: ".metadata.creationTimestamp",
Expand Down Expand Up @@ -837,6 +837,14 @@ var OperatorConfigCRDResourceValidation = apiextv1beta1.CustomResourceValidation
},
},
},
"statefulset_propagate_annotations": {
Type: "array",
Items: &apiextv1beta1.JSONSchemaPropsOrArray{
Schema: &apiextv1beta1.JSONSchemaProps{
Type: "string",
},
},
},
"enable_init_containers": {
Type: "boolean",
},
Expand Down
64 changes: 33 additions & 31 deletions pkg/apis/acid.zalan.do/v1/operator_configuration_type.go
Expand Up @@ -65,13 +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"`
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
Expand Down Expand Up @@ -181,30 +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"`
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"`
FxKu marked this conversation as resolved.
Show resolved Hide resolved
}

//Duration shortens this frequently used name
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/acid.zalan.do/v1/zz_generated.deepcopy.go

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

8 changes: 6 additions & 2 deletions pkg/cluster/k8sres.go
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"path"
"sort"
"strconv"

"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -1148,12 +1149,15 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
return nil, fmt.Errorf("could not set the pod management policy to the unknown value: %v", c.OpConfig.PodManagementPolicy)
}

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: map[string]string{rollingUpdateStatefulsetAnnotationKey: "false"},
Annotations: c.PropagateAnnotationsToStatefulsets(annotations),
},
Spec: appsv1.StatefulSetSpec{
Replicas: &numberOfInstances,
Expand Down Expand Up @@ -1803,7 +1807,7 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1beta1.CronJob, error) {
c.OpConfig.AdditionalSecretMount,
c.OpConfig.AdditionalSecretMountPath,
[]acidv1.AdditionalVolume{}); err != nil {
return nil, fmt.Errorf("could not generate pod template for logical backup pod: %v", err)
return nil, fmt.Errorf("could not generate pod template for logical backup pod: %v", err)
}

// overwrite specific params of logical backups pods
Expand Down
23 changes: 23 additions & 0 deletions pkg/cluster/sync.go
Expand Up @@ -3,6 +3,7 @@ package cluster
import (
"context"
"fmt"
"regexp"

batchv1beta1 "k8s.io/api/batch/v1beta1"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -330,6 +331,8 @@ func (c *Cluster) syncStatefulSet() error {
}
}
}
annotations := c.PropagateAnnotationsToStatefulsets(c.Statefulset.Annotations)
c.updateStatefulSetAnnotations(annotations)
}

// Apply special PostgreSQL parameters that can only be set via the Patroni API.
Expand All @@ -354,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
FxKu marked this conversation as resolved.
Show resolved Hide resolved
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 {
RafiaSabih marked this conversation as resolved.
Show resolved Hide resolved
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 {
Expand Down
2 changes: 2 additions & 0 deletions pkg/controller/operator_config.go
Expand Up @@ -46,6 +46,8 @@ func (c *Controller) importConfigurationFromCRD(fromCRD *acidv1.OperatorConfigur
result.ShmVolume = fromCRD.ShmVolume
result.Sidecars = fromCRD.Sidecars

result.StatefulsetPropagateAnnotations = fromCRD.StatefulsetPropagateAnnotations

// user config
result.SuperUsername = fromCRD.PostgresUsersConfiguration.SuperUsername
result.ReplicationUsername = fromCRD.PostgresUsersConfiguration.ReplicationUsername
Expand Down
1 change: 1 addition & 0 deletions pkg/util/config/config.go
Expand Up @@ -134,6 +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"`
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
Expand Down