use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec in project stackgres by ongres.
the class PostgresVersionValidatorTest method givenValidlatestPostgresVersion_shouldNotFail.
@Test
void givenValidlatestPostgresVersion_shouldNotFail() throws ValidationFailed {
final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/valid_creation.json", StackGresClusterReview.class);
StackGresClusterSpec spec = review.getRequest().getObject().getSpec();
spec.getPostgres().setVersion(StackGresComponent.LATEST);
String postgresProfile = spec.getConfiguration().getPostgresConfig();
String namespace = review.getRequest().getObject().getMetadata().getNamespace();
when(configFinder.findByNameAndNamespace(eq(postgresProfile), eq(namespace))).thenReturn(Optional.of(postgresConfig));
final String randomVersion = getRandomPostgresVersion();
spec.getPostgres().setVersion(randomVersion);
postgresConfig.getSpec().setPostgresVersion(getMajorPostgresVersion(randomVersion));
validator.validate(review);
verify(configFinder).findByNameAndNamespace(eq(postgresProfile), eq(namespace));
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec in project stackgres by ongres.
the class PostgresVersionValidatorTest method givenNoPostgresConfigReference_shouldFail.
@Test
void givenNoPostgresConfigReference_shouldFail() {
final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/valid_creation.json", StackGresClusterReview.class);
StackGresClusterSpec spec = review.getRequest().getObject().getSpec();
spec.getConfiguration().setPostgresConfig(null);
ValidationFailed exception = assertThrows(ValidationFailed.class, () -> {
validator.validate(review);
});
String resultMessage = exception.getResult().getMessage();
assertEquals("sgPostgresConfig must be provided", resultMessage);
verify(configFinder, never()).findByNameAndNamespace(anyString(), anyString());
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec in project stackgres by ongres.
the class PostgresVersionValidatorTest method givenPostgresConfigUpdate_shouldFail.
@Test
void givenPostgresConfigUpdate_shouldFail() throws ValidationFailed {
final StackGresClusterReview review = JsonUtil.readFromJson("cluster_allow_requests/postgres_config_update.json", StackGresClusterReview.class);
StackGresClusterSpec spec = review.getRequest().getObject().getSpec();
String postgresProfile = spec.getConfiguration().getPostgresConfig();
String namespace = review.getRequest().getObject().getMetadata().getNamespace();
review.getRequest().getObject().getSpec().getPostgres().setVersion(getRandomPostgresVersion());
when(configFinder.findByNameAndNamespace(eq(postgresProfile), eq(namespace))).thenReturn(Optional.empty());
ValidationFailed exception = assertThrows(ValidationFailed.class, () -> {
validator.validate(review);
});
String resultMessage = exception.getResult().getMessage();
assertEquals("Invalid sgPostgresConfig value " + postgresProfile, resultMessage);
verify(configFinder).findByNameAndNamespace(eq(postgresProfile), eq(namespace));
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec in project stackgres by ongres.
the class AbstractEnvoy method buildSource.
@NotNull
public HasMetadata buildSource(StackGresClusterContext context) {
final StackGresCluster stackGresCluster = context.getSource();
boolean disablePgBouncer = Optional.ofNullable(stackGresCluster.getSpec()).map(StackGresClusterSpec::getPod).map(StackGresClusterPod::getDisableConnectionPooling).orElse(false);
final String envoyConfPath = getEnvoyConfigPath(stackGresCluster, disablePgBouncer);
YAMLMapper yamlMapper = yamlMapperProvider.yamlMapper();
final ObjectNode envoyConfig;
try {
envoyConfig = (ObjectNode) yamlMapper.readTree(Envoy.class.getResource(envoyConfPath));
} catch (Exception ex) {
throw new IllegalStateException("couldn't read envoy config file", ex);
}
Seq.seq(envoyConfig.get("static_resources").get("listeners")).map(listener -> listener.get("address").get("socket_address")).cast(ObjectNode.class).forEach(socketAddress -> socketAddress.put("port_value", LISTEN_SOCKET_ADDRESS_PORT_MAPPING.get(socketAddress.get("port_value").asText())));
Seq.seq(envoyConfig.get("static_resources").get("clusters")).flatMap(cluster -> Seq.seq(cluster.get("load_assignment").get("endpoints").elements())).flatMap(endpoint -> Seq.seq(endpoint.get("lb_endpoints").elements())).map(endpoint -> endpoint.get("endpoint").get("address").get("socket_address")).cast(ObjectNode.class).forEach(socketAddress -> socketAddress.put("port_value", CLUSTER_SOCKET_ADDRESS_PORT_MAPPING.get(socketAddress.get("port_value").asText())));
final Map<String, String> data;
try {
data = ImmutableMap.of("default_envoy.yaml", yamlMapper.writeValueAsString(envoyConfig));
} catch (Exception ex) {
throw new IllegalStateException("couldn't parse envoy config file", ex);
}
String namespace = stackGresCluster.getMetadata().getNamespace();
String configMapName = AbstractEnvoy.configName(context);
return new ConfigMapBuilder().withNewMetadata().withNamespace(namespace).withName(configMapName).withLabels(labelFactory.clusterLabels(stackGresCluster)).endMetadata().withData(data).build();
}
use of io.stackgres.common.crd.sgcluster.StackGresClusterSpec in project stackgres by ongres.
the class ClusterResourceMockedTest method checkDto.
@Override
protected void checkDto(ClusterDto dto, StackGresCluster resource) {
if (resource.getMetadata() != null) {
assertNotNull(dto.getMetadata());
assertEquals(resource.getMetadata().getNamespace(), dto.getMetadata().getNamespace());
assertEquals(resource.getMetadata().getName(), dto.getMetadata().getName());
assertEquals(resource.getMetadata().getUid(), dto.getMetadata().getUid());
} else {
assertNull(dto.getMetadata());
}
final StackGresClusterSpec resourceSpec = resource.getSpec();
final ClusterSpec dtoSpec = dto.getSpec();
if (resourceSpec != null) {
assertNotNull(dtoSpec);
assertEquals(resourceSpec.getInstances(), dtoSpec.getInstances());
assertEquals(resourceSpec.getPostgres().getVersion(), dtoSpec.getPostgres().getVersion());
assertEquals(resourceSpec.getPrometheusAutobind(), dtoSpec.getPrometheusAutobind());
assertEquals(resourceSpec.getResourceProfile(), dtoSpec.getSgInstanceProfile());
final ClusterConfiguration dtoClusterConfigurations = dtoSpec.getConfigurations();
final StackGresClusterConfiguration resourceClusterConfiguration = resourceSpec.getConfiguration();
if (resourceClusterConfiguration != null) {
assertNotNull(dtoClusterConfigurations);
assertEquals(resourceClusterConfiguration.getBackupConfig(), dtoClusterConfigurations.getSgBackupConfig());
assertEquals(resourceClusterConfiguration.getConnectionPoolingConfig(), dtoClusterConfigurations.getSgPoolingConfig());
assertEquals(resourceClusterConfiguration.getPostgresConfig(), dtoClusterConfigurations.getSgPostgresConfig());
} else {
assertNull(dtoClusterConfigurations);
}
final ClusterPod dtoSpecPods = dtoSpec.getPods();
final StackGresClusterPod resourcePod = resourceSpec.getPod();
if (resourcePod != null) {
assertNotNull(dtoSpecPods);
assertEquals(resourcePod.getDisableConnectionPooling(), dtoSpecPods.getDisableConnectionPooling());
assertEquals(resourcePod.getDisableMetricsExporter(), dtoSpecPods.getDisableMetricsExporter());
assertEquals(resourcePod.getDisablePostgresUtil(), dtoSpecPods.getDisablePostgresUtil());
final ClusterPodPersistentVolume resourcePV = dtoSpecPods.getPersistentVolume();
final StackGresPodPersistentVolume dtoPV = resourcePod.getPersistentVolume();
if (dtoPV != null) {
assertNotNull(resourcePV);
assertEquals(dtoPV.getSize(), resourcePV.getSize());
assertEquals(dtoPV.getStorageClass(), resourcePV.getStorageClass());
} else {
assertNull(resourcePV);
}
if (resourceSpec.getMetadata() != null) {
assertNotNull(dtoSpec.getMetadata());
assertEquals(resourceSpec.getMetadata().getLabels().getClusterPods(), dtoSpec.getMetadata().getLabels().getClusterPods());
} else {
assertNull(dtoSpec.getMetadata());
}
if (resourcePod.getScheduling() != null) {
assertNotNull(dtoSpecPods.getScheduling());
assertEquals(resourcePod.getScheduling().getNodeSelector(), dtoSpecPods.getScheduling().getNodeSelector());
assertEquals(resourcePod.getScheduling().getNodeAffinity(), dtoSpecPods.getScheduling().getNodeAffinity());
} else {
assertNull(dtoSpecPods.getScheduling());
}
} else {
assertNull(dtoSpecPods);
}
if (resourceSpec.getDistributedLogs() != null) {
assertNotNull(dtoSpec.getDistributedLogs());
assertEquals(resourceSpec.getDistributedLogs().getDistributedLogs(), dtoSpec.getDistributedLogs().getDistributedLogs());
} else {
assertNull(dtoSpec.getDistributedLogs());
}
final StackGresClusterInitData resourceInitData = resourceSpec.getInitData();
if (resourceInitData != null) {
final ClusterInitData dtoInitData = dtoSpec.getInitData();
assertNotNull(dtoInitData);
if (resourceInitData.getRestore() != null) {
assertNotNull(dtoInitData.getRestore());
assertEquals(resourceInitData.getRestore().getFromBackup().getUid(), dtoInitData.getRestore().getFromBackup().getUid());
assertEquals(resourceInitData.getRestore().getDownloadDiskConcurrency(), dtoInitData.getRestore().getDownloadDiskConcurrency());
} else {
assertNull(dtoInitData.getRestore());
}
if (resourceInitData.getScripts() != null) {
assertNotNull(dtoInitData.getScripts());
Seq.zip(resourceInitData.getScripts(), dtoInitData.getScripts()).forEach(tuple -> {
assertEquals(tuple.v1.getDatabase(), tuple.v2.getDatabase());
assertEquals(tuple.v2.getName(), tuple.v2.getName());
assertEquals(tuple.v2.getScript(), tuple.v2.getScript());
final StackGresClusterScriptFrom resourceScriptFrom = tuple.v1.getScriptFrom();
final ClusterScriptFrom dtoScriptFrom = tuple.v2.getScriptFrom();
if (resourceScriptFrom != null) {
assertNotNull(dtoScriptFrom);
if (resourceScriptFrom.getSecretKeyRef() != null) {
assertNotNull(dtoScriptFrom.getSecretKeyRef());
assertEquals(resourceScriptFrom.getSecretKeyRef().getKey(), dtoScriptFrom.getSecretKeyRef().getKey());
assertEquals(resourceScriptFrom.getSecretKeyRef().getName(), dtoScriptFrom.getSecretKeyRef().getName());
} else {
assertNull(dtoScriptFrom.getSecretKeyRef());
}
if (resourceScriptFrom.getConfigMapKeyRef() != null) {
assertNotNull(dtoScriptFrom.getConfigMapKeyRef());
assertEquals(resourceScriptFrom.getConfigMapKeyRef().getKey(), dtoScriptFrom.getConfigMapKeyRef().getKey());
assertEquals(resourceScriptFrom.getConfigMapKeyRef().getName(), dtoScriptFrom.getConfigMapKeyRef().getName());
assertEquals(configMap.getData().get(resourceScriptFrom.getConfigMapKeyRef().getKey()), dtoScriptFrom.getConfigMapScript());
} else {
assertNull(dtoScriptFrom.getConfigMapKeyRef());
}
} else {
assertNull(dtoScriptFrom);
}
});
}
}
} else {
assertNull(dtoSpec);
}
if (dto.getPods() != null) {
assertEquals(1, dto.getPodsReady());
assertEquals(2, dto.getPods().size());
assertEquals(4, dto.getPods().get(0).getContainers());
assertEquals(4, dto.getPods().get(0).getContainersReady());
assertEquals("10.244.3.23", dto.getPods().get(0).getIp());
assertEquals("stackgres-0", dto.getPods().get(0).getName());
assertEquals("stackgres", dto.getPods().get(0).getNamespace());
assertEquals("primary", dto.getPods().get(0).getRole());
assertEquals("Active", dto.getPods().get(0).getStatus());
assertEquals(4, dto.getPods().get(0).getComponentVersions().size());
assertEquals("12.2", dto.getPods().get(0).getComponentVersions().get("postgresql"));
assertEquals("1.6.4", dto.getPods().get(0).getComponentVersions().get("patroni"));
assertEquals("1.13.1", dto.getPods().get(0).getComponentVersions().get("envoy"));
assertEquals("0.8", dto.getPods().get(0).getComponentVersions().get("prometheus-postgres-exporter"));
assertEquals(4, dto.getPods().get(1).getContainers());
assertEquals(0, dto.getPods().get(1).getContainersReady());
assertNull(dto.getPods().get(1).getIp());
assertEquals("stackgres-1", dto.getPods().get(1).getName());
assertEquals("stackgres", dto.getPods().get(1).getNamespace());
assertNull(dto.getPods().get(1).getRole());
assertEquals("Pending", dto.getPods().get(1).getStatus());
assertEquals(4, dto.getPods().get(1).getComponentVersions().size());
assertEquals("12.2", dto.getPods().get(1).getComponentVersions().get("postgresql"));
assertEquals("1.6.4", dto.getPods().get(1).getComponentVersions().get("patroni"));
assertEquals("1.13.1", dto.getPods().get(1).getComponentVersions().get("envoy"));
assertEquals("0.8", dto.getPods().get(1).getComponentVersions().get("prometheus-postgres-exporter"));
}
if (dto.getInfo() != null) {
String appendDns = "." + resource.getMetadata().getNamespace() + ".svc.cluster.local";
String expectedPrimaryDns = PatroniUtil.readWriteName(resource.getMetadata().getName()) + appendDns;
String expectedReplicasDns = "f4611c56942064ed5a468d8ce0a894ec.us-east-1.elb.amazonaws.com";
assertEquals(expectedPrimaryDns, dto.getInfo().getPrimaryDns());
assertEquals(expectedReplicasDns, dto.getInfo().getReplicasDns());
assertEquals("postgres", dto.getInfo().getSuperuserUsername());
assertEquals("superuser-password", dto.getInfo().getSuperuserPasswordKey());
assertEquals(resource.getMetadata().getName(), dto.getInfo().getSuperuserSecretName());
}
}
Aggregations