MCPcopy Create free account
hub / github.com/CrunchyData/postgres-operator / instanceEnvironment

Function instanceEnvironment

internal/patroni/config.go:291–412  ·  view source on GitHub ↗

instanceEnvironment returns the environment variables needed by Patroni's instance container.

(
	cluster *v1beta1.PostgresCluster,
	clusterPodService *corev1.Service,
	leaderService *corev1.Service,
	podContainers []corev1.Container,
)

Source from the content-addressed store, hash-verified

289// instanceEnvironment returns the environment variables needed by Patroni's
290// instance container.
291func instanceEnvironment(
292 cluster *v1beta1.PostgresCluster,
293 clusterPodService *corev1.Service,
294 leaderService *corev1.Service,
295 podContainers []corev1.Container,
296) []corev1.EnvVar {
297 var (
298 patroniPort = *cluster.Spec.Patroni.Port
299 postgresPort = *cluster.Spec.Port
300 podSubdomain = clusterPodService.Name
301 )
302
303 // Gather Endpoint ports for any Container ports that match the leader
304 // Service definition.
305 ports := []corev1.EndpointPort{}
306 for _, sp := range leaderService.Spec.Ports {
307 for i := range podContainers {
308 for _, cp := range podContainers[i].Ports {
309 if sp.TargetPort.StrVal == cp.Name {
310 ports = append(ports, corev1.EndpointPort{
311 Name: sp.Name,
312 Port: cp.ContainerPort,
313 Protocol: cp.Protocol,
314 })
315 }
316 }
317 }
318 }
319 portsYAML, _ := yaml.Marshal(ports)
320
321 // NOTE(cbandy): Patroni consumes and then removes environment variables
322 // starting with "PATRONI_".
323 // - https://github.com/zalando/patroni/blob/v2.0.2/patroni/config.py#L247
324 // - https://github.com/zalando/patroni/blob/v2.0.2/patroni/postgresql/postmaster.py#L215-L216
325
326 variables := []corev1.EnvVar{
327 // Set "name" to the v1.Pod's name. Required when using Kubernetes for DCS.
328 // Patroni must be restarted when changing this value.
329 {
330 Name: "PATRONI_NAME",
331 ValueFrom: &corev1.EnvVarSource{FieldRef: &corev1.ObjectFieldSelector{
332 APIVersion: "v1",
333 FieldPath: "metadata.name",
334 }},
335 },
336
337 // Set "kubernetes.pod_ip" to the v1.Pod's primary IP address.
338 // Patroni must be restarted when changing this value.
339 {
340 Name: "PATRONI_KUBERNETES_POD_IP",
341 ValueFrom: &corev1.EnvVarSource{FieldRef: &corev1.ObjectFieldSelector{
342 APIVersion: "v1",
343 FieldPath: "status.podIP",
344 }},
345 },
346
347 // When using Endpoints for DCS, Patroni needs to replicate the leader
348 // ServicePort definitions. Set "kubernetes.ports" to the YAML of this

Callers 2

TestInstanceEnvironmentFunction · 0.85
InstancePodFunction · 0.85

Calls 2

ConfigDirectoryFunction · 0.92
DataDirectoryFunction · 0.92

Tested by 1

TestInstanceEnvironmentFunction · 0.68