From 1e4f046797275de4679461085ae985975eef8120 Mon Sep 17 00:00:00 2001 From: Oleksii Kliukin Date: Thu, 14 Jun 2018 22:20:24 +0200 Subject: [PATCH] Define sidecars in the operator configuration. Right now only the name and the docker image can be defined, but with the help of the pod_environment_configmap parameter arbitrary environment variables can be passed to the sidecars. --- docs/reference/operator_parameters.md | 5 ++++ pkg/cluster/k8sres.go | 36 ++++++++++++++++++++++++--- pkg/util/config/config.go | 7 +++--- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/docs/reference/operator_parameters.md b/docs/reference/operator_parameters.md index 115cb055e..c13da2317 100644 --- a/docs/reference/operator_parameters.md +++ b/docs/reference/operator_parameters.md @@ -15,6 +15,11 @@ words. your own Spilo image from the [github repository](https://github.com/zalando/spilo). +* **sidecars** + a map of sidecar names to docker images for the containers to run alongside + Spilo. In case of the name conflict with the definition in the cluster + manifest the cluster-specific one is preferred. + * **workers** number of working routines the operator spawns to process requests to create/update/delete/sync clusters concurrently. The default is `4`. diff --git a/pkg/cluster/k8sres.go b/pkg/cluster/k8sres.go index d6e249708..a6f40b77b 100644 --- a/pkg/cluster/k8sres.go +++ b/pkg/cluster/k8sres.go @@ -318,7 +318,7 @@ func (c *Cluster) generatePodTemplate( patroniParameters *spec.Patroni, cloneDescription *spec.CloneDescription, dockerImage *string, - sidecars *[]spec.Sidecar, + sidecars []spec.Sidecar, customPodEnvVars map[string]string, ) (*v1.PodTemplateSpec, error) { spiloConfiguration := c.generateSpiloJSONConfiguration(pgParameters, patroniParameters) @@ -526,8 +526,8 @@ func (c *Cluster) generatePodTemplate( ) } - if sidecars != nil && len(*sidecars) > 0 { - for index, sidecar := range *sidecars { + if sidecars != nil && len(sidecars) > 0 { + for index, sidecar := range sidecars { sc, err := c.getSidecarContainer(sidecar, index, volumeMounts) if err != nil { return nil, err @@ -659,7 +659,10 @@ func (c *Cluster) generateStatefulSet(spec *spec.PostgresSpec) (*v1beta1.Statefu customPodEnvVars = cm.Data } } - podTemplate, err := c.generatePodTemplate(c.Postgresql.GetUID(), resourceRequirements, resourceRequirementsScalyrSidecar, &spec.Tolerations, &spec.PostgresqlParam, &spec.Patroni, &spec.Clone, &spec.DockerImage, &spec.Sidecars, customPodEnvVars) + + mergedSidecars := c.mergeSidecars(spec.Sidecars) + + podTemplate, err := c.generatePodTemplate(c.Postgresql.GetUID(), resourceRequirements, resourceRequirementsScalyrSidecar, &spec.Tolerations, &spec.PostgresqlParam, &spec.Patroni, &spec.Clone, &spec.DockerImage, mergedSidecars, customPodEnvVars) if err != nil { return nil, fmt.Errorf("could not generate pod template: %v", err) } @@ -689,6 +692,31 @@ func (c *Cluster) generateStatefulSet(spec *spec.PostgresSpec) (*v1beta1.Statefu return statefulSet, nil } +// mergeSidecar merges globally-defined sidecars with those defined in the cluster manifest +func (c *Cluster) mergeSidecars(sidecars []spec.Sidecar) []spec.Sidecar { + globalSidecarsToSkip := map[string]bool{} + result := make([]spec.Sidecar, 0) + + for i, sidecar := range sidecars { + dockerImage, ok := c.OpConfig.Sidecars[sidecar.Name] + if ok { + if dockerImage != sidecar.DockerImage { + c.logger.Warningf("merging definitions for sidecar %q: "+ + "ignoring %q in the global scope in favor of %q defined in the cluster", + sidecar.Name, dockerImage, sidecar.DockerImage) + } + globalSidecarsToSkip[sidecar.Name] = true + } + result = append(result, sidecars[i]) + } + for name, dockerImage := range c.OpConfig.Sidecars { + if !globalSidecarsToSkip[name] { + result = append(result, spec.Sidecar{Name: name, DockerImage: dockerImage}) + } + } + return result +} + func (c *Cluster) getNumberOfInstances(spec *spec.PostgresSpec) (newcur int32) { min := c.OpConfig.MinInstances max := c.OpConfig.MaxInstances diff --git a/pkg/util/config/config.go b/pkg/util/config/config.go index 182bb077c..11ed77a60 100644 --- a/pkg/util/config/config.go +++ b/pkg/util/config/config.go @@ -68,9 +68,10 @@ type Config struct { Auth Scalyr - WatchedNamespace string `name:"watched_namespace"` // special values: "*" means 'watch all namespaces', the empty string "" means 'watch a namespace where operator is deployed to' - EtcdHost string `name:"etcd_host" default:""` // special values: the empty string "" means Patroni will use k8s as a DCS - DockerImage string `name:"docker_image" default:"registry.opensource.zalan.do/acid/spilo-cdp-10:1.4-p8"` + WatchedNamespace string `name:"watched_namespace"` // special values: "*" means 'watch all namespaces', the empty string "" means 'watch a namespace where operator is deployed to' + EtcdHost string `name:"etcd_host" default:""` // special values: the empty string "" means Patroni will use k8s as a DCS + DockerImage string `name:"docker_image" default:"registry.opensource.zalan.do/acid/spilo-cdp-10:1.4-p8"` + Sidecars map[string]string `name:"sidecars"` // default name `operator` enables backward compatibility with the older ServiceAccountName field PodServiceAccountName string `name:"pod_service_account_name" default:"operator"` // value of this string must be valid JSON or YAML; see initPodServiceAccount