|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 OnGres, Inc. |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +package io.stackgres.operator.conciliation.factory.cluster.autoscaling; |
| 7 | + |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Optional; |
| 10 | +import java.util.stream.Stream; |
| 11 | + |
| 12 | +import io.fabric8.kubernetes.api.model.HasMetadata; |
| 13 | +import io.fabric8.kubernetes.api.model.SecretBuilder; |
| 14 | +import io.stackgres.common.EnvoyUtil; |
| 15 | +import io.stackgres.common.PatroniUtil; |
| 16 | +import io.stackgres.common.StackGresVersion; |
| 17 | +import io.stackgres.common.crd.external.keda.ScaledObjectBuilder; |
| 18 | +import io.stackgres.common.crd.external.keda.TriggerAuthenticationBuilder; |
| 19 | +import io.stackgres.common.crd.sgcluster.StackGresCluster; |
| 20 | +import io.stackgres.common.crd.sgcluster.StackGresClusterAutoscaling; |
| 21 | +import io.stackgres.common.crd.sgcluster.StackGresClusterAutoscalingHorizontal; |
| 22 | +import io.stackgres.common.crd.sgcluster.StackGresClusterPods; |
| 23 | +import io.stackgres.common.crd.sgcluster.StackGresClusterSpec; |
| 24 | +import io.stackgres.common.labels.LabelFactoryForCluster; |
| 25 | +import io.stackgres.operator.conciliation.OperatorVersionBinder; |
| 26 | +import io.stackgres.operator.conciliation.ResourceGenerator; |
| 27 | +import io.stackgres.operator.conciliation.cluster.StackGresClusterContext; |
| 28 | +import io.stackgres.operator.conciliation.factory.cluster.patroni.PatroniSecret; |
| 29 | +import io.stackgres.operatorframework.resource.ResourceUtil; |
| 30 | +import jakarta.inject.Inject; |
| 31 | +import jakarta.inject.Singleton; |
| 32 | + |
| 33 | +@Singleton |
| 34 | +@OperatorVersionBinder(stopAt = StackGresVersion.V_1_12) |
| 35 | +public class HorizontalAutoscalingV1M12 implements ResourceGenerator<StackGresClusterContext> { |
| 36 | + |
| 37 | + public static String name(StackGresCluster cluster) { |
| 38 | + return ResourceUtil.resourceName(cluster.getMetadata().getName()); |
| 39 | + } |
| 40 | + |
| 41 | + public static String secretName(StackGresCluster cluster) { |
| 42 | + return ResourceUtil.resourceName(cluster.getMetadata().getName() + "-autoscaling"); |
| 43 | + } |
| 44 | + |
| 45 | + private LabelFactoryForCluster<StackGresCluster> labelFactory; |
| 46 | + |
| 47 | + @Inject |
| 48 | + public HorizontalAutoscalingV1M12(LabelFactoryForCluster<StackGresCluster> labelFactory) { |
| 49 | + this.labelFactory = labelFactory; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Stream<HasMetadata> generateResource(StackGresClusterContext context) { |
| 54 | + StackGresCluster cluster = context.getCluster(); |
| 55 | + return Optional.of(cluster.getSpec()) |
| 56 | + .map(StackGresClusterSpec::getAutoscaling) |
| 57 | + .filter(StackGresClusterAutoscaling::isHorizontalPodAutoscalingEnabled) |
| 58 | + .stream() |
| 59 | + .flatMap(autoscaling -> Stream.of( |
| 60 | + new SecretBuilder() |
| 61 | + .withNewMetadata() |
| 62 | + .withLabels(labelFactory.genericLabels(cluster)) |
| 63 | + .withName(secretName(cluster)) |
| 64 | + .withNamespace(cluster.getMetadata().getNamespace()) |
| 65 | + .endMetadata() |
| 66 | + .withData(Optional.of(PatroniSecret.getSuperuserCredentials(context)) |
| 67 | + .map(credentials -> "postgresql://" |
| 68 | + + credentials.v1 + ":" + credentials.v2 |
| 69 | + + "@" + PatroniUtil.readOnlyName(cluster) |
| 70 | + + "." + cluster.getMetadata().getNamespace() |
| 71 | + + ":" + String.valueOf(EnvoyUtil.PG_PORT) |
| 72 | + + "/postgres?sslmode=prefer&default_query_exec_mode=exec") |
| 73 | + .map(connection -> Map.of("connection", connection)) |
| 74 | + .map(ResourceUtil::encodeSecret) |
| 75 | + .get()) |
| 76 | + .build(), |
| 77 | + new TriggerAuthenticationBuilder() |
| 78 | + .withNewMetadata() |
| 79 | + .withLabels(labelFactory.genericLabels(cluster)) |
| 80 | + .withName(name(cluster)) |
| 81 | + .withNamespace(cluster.getMetadata().getNamespace()) |
| 82 | + .endMetadata() |
| 83 | + .withNewSpec() |
| 84 | + .addNewSecretTargetRef() |
| 85 | + .withName(secretName(cluster)) |
| 86 | + .withKey("connection") |
| 87 | + .withParameter("connection") |
| 88 | + .endSecretTargetRef() |
| 89 | + .endSpec() |
| 90 | + .build(), |
| 91 | + new ScaledObjectBuilder() |
| 92 | + .withNewMetadata() |
| 93 | + .withLabels(labelFactory.genericLabels(cluster)) |
| 94 | + .withName(name(cluster)) |
| 95 | + .withNamespace(cluster.getMetadata().getNamespace()) |
| 96 | + .endMetadata() |
| 97 | + .withNewSpec() |
| 98 | + .withNewScaleTargetRef() |
| 99 | + .withApiVersion(HasMetadata.getApiVersion(StackGresCluster.class)) |
| 100 | + .withKind(HasMetadata.getKind(StackGresCluster.class)) |
| 101 | + .withName(cluster.getMetadata().getName()) |
| 102 | + .endScaleTargetRef() |
| 103 | + .withCooldownPeriod(Optional.ofNullable(autoscaling.getHorizontal()) |
| 104 | + .map(StackGresClusterAutoscalingHorizontal::getCooldownPeriod) |
| 105 | + .orElse(300)) |
| 106 | + .withPollingInterval(Optional.ofNullable(autoscaling.getHorizontal()) |
| 107 | + .map(StackGresClusterAutoscalingHorizontal::getPollingInterval) |
| 108 | + .orElse(30)) |
| 109 | + .withMinReplicaCount(autoscaling.getMinInstances()) |
| 110 | + .withMaxReplicaCount(autoscaling.getMaxInstances()) |
| 111 | + .addNewTrigger() |
| 112 | + .withNewAuthenticationRef() |
| 113 | + .withName(name(cluster)) |
| 114 | + .endAuthenticationRef() |
| 115 | + .withType("postgresql") |
| 116 | + .withName("connections-used") |
| 117 | + .withMetadata(Map.ofEntries( |
| 118 | + Map.entry( |
| 119 | + "targetQueryValue", |
| 120 | + Optional.ofNullable(autoscaling.getHorizontal()) |
| 121 | + .map(StackGresClusterAutoscalingHorizontal::getReplicasConnectionsUsageTarget) |
| 122 | + .orElse("0.8")), |
| 123 | + Map.entry( |
| 124 | + "query", |
| 125 | + Optional.of(cluster.getSpec().getPods()) |
| 126 | + .map(StackGresClusterPods::getDisableConnectionPooling) |
| 127 | + .orElse(false) |
| 128 | + ? |
| 129 | + """ |
| 130 | + WITH |
| 131 | + max_connections (size) AS ( |
| 132 | + SELECT setting::numeric |
| 133 | + FROM pg_settings |
| 134 | + WHERE name = 'max_connections'), |
| 135 | + active_connections (size) AS ( |
| 136 | + SELECT COUNT(*)::numeric |
| 137 | + FROM pg_stat_activity |
| 138 | + WHERE state = 'active' |
| 139 | + AND backend_type = 'client backend') |
| 140 | + SELECT active_connections.size / max_connections.size AS connection_usage |
| 141 | + FROM max_connections, active_connections; |
| 142 | + """ |
| 143 | + : |
| 144 | + """ |
| 145 | + WITH |
| 146 | + max_connections (size) AS ( |
| 147 | + SELECT SUM(_.pool_size)::numeric |
| 148 | + FROM dblink( |
| 149 | + 'host=/var/run/postgresql port=6432 dbname=pgbouncer user=pgbouncer', |
| 150 | + 'show databases'::text) |
| 151 | + AS _( |
| 152 | + name text, host text, port integer, database text, |
| 153 | + force_user text, pool_size integer, min_pool_size integer, |
| 154 | + reserve_pool integer, pool_mode text, max_connections integer, |
| 155 | + current_connections integer, paused boolean, disabled boolean)), |
| 156 | + active_connections (size) AS ( |
| 157 | + SELECT SUM(_.current_connections)::numeric |
| 158 | + FROM dblink( |
| 159 | + 'host=/var/run/postgresql port=6432 dbname=pgbouncer user=pgbouncer', |
| 160 | + 'show databases'::text) |
| 161 | + AS _( |
| 162 | + name text, host text, port integer, database text, |
| 163 | + force_user text, pool_size integer, min_pool_size integer, |
| 164 | + reserve_pool integer, pool_mode text, max_connections integer, |
| 165 | + current_connections integer, paused boolean, disabled boolean)) |
| 166 | + SELECT active_connections.size / max_connections.size AS connection_usage |
| 167 | + FROM max_connections, active_connections; |
| 168 | + """))) |
| 169 | + .withMetricType(Optional.ofNullable(autoscaling.getHorizontal()) |
| 170 | + .map(StackGresClusterAutoscalingHorizontal::getReplicasConnectionsUsageMetricType) |
| 171 | + .orElse("AverageValue")) |
| 172 | + .endTrigger() |
| 173 | + .endSpec() |
| 174 | + .build())); |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments