InstancePod populates a PodTemplateSpec with the fields needed to run Patroni. The database container must already be in the template.
(ctx context.Context, inCluster *v1beta1.PostgresCluster, inClusterConfigMap *corev1.ConfigMap, inClusterPodService *corev1.Service, inPatroniLeaderService *corev1.Service, inInstanceSpec *v1beta1.PostgresInstanceSetSpec, inInstanceCertificates *corev1.Secret, inInstanceConfigMap *corev1.ConfigMap, outInstancePod *corev1.PodTemplateSpec, )
| 82 | // InstancePod populates a PodTemplateSpec with the fields needed to run Patroni. |
| 83 | // The database container must already be in the template. |
| 84 | func InstancePod(ctx context.Context, |
| 85 | inCluster *v1beta1.PostgresCluster, |
| 86 | inClusterConfigMap *corev1.ConfigMap, |
| 87 | inClusterPodService *corev1.Service, |
| 88 | inPatroniLeaderService *corev1.Service, |
| 89 | inInstanceSpec *v1beta1.PostgresInstanceSetSpec, |
| 90 | inInstanceCertificates *corev1.Secret, |
| 91 | inInstanceConfigMap *corev1.ConfigMap, |
| 92 | outInstancePod *corev1.PodTemplateSpec, |
| 93 | ) error { |
| 94 | initialize.Labels(outInstancePod) |
| 95 | |
| 96 | // When using Kubernetes for DCS, Patroni discovers members by listing Pods |
| 97 | // that have the "scope" label. See the "kubernetes.scope_label" and |
| 98 | // "kubernetes.labels" settings. |
| 99 | outInstancePod.Labels[naming.LabelPatroni] = naming.PatroniScope(inCluster) |
| 100 | |
| 101 | var container *corev1.Container |
| 102 | for i := range outInstancePod.Spec.Containers { |
| 103 | if outInstancePod.Spec.Containers[i].Name == naming.ContainerDatabase { |
| 104 | container = &outInstancePod.Spec.Containers[i] |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Start Patroni in an environment that prioritizes executables of the specified Postgres version. |
| 109 | // NOTE: Patroni ignores PATH when "postgresql.bin_dir" is set. |
| 110 | // |
| 111 | // https://patroni.readthedocs.io/en/latest/yaml_configuration.html#postgresql |
| 112 | container.Command = []string{ |
| 113 | "sh", "-c", "--", postgres.ShellPath(inCluster.Spec.PostgresVersion) + ` && exec "$@"`, "--", |
| 114 | "patroni", configDirectory, |
| 115 | } |
| 116 | |
| 117 | container.Env = append(container.Env, |
| 118 | instanceEnvironment(inCluster, inClusterPodService, inPatroniLeaderService, |
| 119 | outInstancePod.Spec.Containers)...) |
| 120 | |
| 121 | volume := corev1.Volume{Name: "patroni-config"} |
| 122 | volume.Projected = new(corev1.ProjectedVolumeSource) |
| 123 | |
| 124 | // Add our projections after those specified in the CR. Items later in the |
| 125 | // list take precedence over earlier items (that is, last write wins). |
| 126 | // - https://kubernetes.io/docs/concepts/storage/volumes/#projected |
| 127 | volume.Projected.Sources = append(append(volume.Projected.Sources, |
| 128 | instanceConfigFiles(inClusterConfigMap, inInstanceConfigMap)...), |
| 129 | instanceCertificates(inInstanceCertificates)...) |
| 130 | |
| 131 | outInstancePod.Spec.Volumes = append(outInstancePod.Spec.Volumes, volume) |
| 132 | |
| 133 | container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{ |
| 134 | Name: volume.Name, |
| 135 | MountPath: configDirectory, |
| 136 | ReadOnly: true, |
| 137 | }) |
| 138 | |
| 139 | instanceProbes(inCluster, container) |
| 140 | |
| 141 | return nil |