Search in sources :

Example 16 with StackGresDbOps

use of io.stackgres.common.crd.sgdbops.StackGresDbOps in project stackgres by ongres.

the class DbOpsMinorVersionUpgradeJob method createJob.

@Override
public Job createJob(StackGresDbOpsContext context) {
    StackGresDbOps dbOps = context.getSource();
    String namespace = dbOps.getMetadata().getNamespace();
    final Map<String, String> labels = dbOpsLabelFactory.dbOpsPodLabels(context.getSource());
    return new JobBuilder().withNewMetadata().withNamespace(namespace).withName(jobName(dbOps, "minor-version-upgrade")).withLabels(labels).endMetadata().withNewSpec().withBackoffLimit(0).withCompletions(1).withParallelism(1).withNewTemplate().withNewMetadata().withNamespace(namespace).withName(jobName(dbOps)).withLabels(labels).endMetadata().withNewSpec().withSecurityContext(podSecurityFactory.createResource(context)).withRestartPolicy("Never").withServiceAccountName(DbOpsRole.roleName(context)).withContainers(new ContainerBuilder().withName("minor-version-upgrade").withImagePullPolicy("IfNotPresent").withImage(String.format(IMAGE_NAME, StackGresProperty.OPERATOR_IMAGE_VERSION.getString())).addToEnv(new EnvVarBuilder().withName(OperatorProperty.OPERATOR_NAME.getEnvironmentVariableName()).withValue(OperatorProperty.OPERATOR_NAME.getString()).build(), new EnvVarBuilder().withName(OperatorProperty.OPERATOR_NAMESPACE.getEnvironmentVariableName()).withValue(OperatorProperty.OPERATOR_NAMESPACE.getString()).build(), new EnvVarBuilder().withName("JOB_NAMESPACE").withValue(namespace).build(), new EnvVarBuilder().withName(StackGresProperty.OPERATOR_VERSION.getEnvironmentVariableName()).withValue(StackGresProperty.OPERATOR_VERSION.getString()).build(), new EnvVarBuilder().withName("CRD_UPGRADE").withValue(Boolean.FALSE.toString()).build(), new EnvVarBuilder().withName("CONVERSION_WEBHOOKS").withValue(Boolean.FALSE.toString()).build(), new EnvVarBuilder().withName("DATABASE_OPERATION_JOB").withValue(Boolean.TRUE.toString()).build(), new EnvVarBuilder().withName("DATABASE_OPERATION_CR_NAME").withValue(dbOps.getMetadata().getName()).build(), new EnvVarBuilder().withName("SERVICE_ACCOUNT").withNewValueFrom().withNewFieldRef().withFieldPath("spec.serviceAccountName").endFieldRef().endValueFrom().build(), new EnvVarBuilder().withName("POD_NAME").withNewValueFrom().withNewFieldRef().withFieldPath("metadata.name").endFieldRef().endValueFrom().build(), new EnvVarBuilder().withName("APP_OPTS").withValue(System.getenv("APP_OPTS")).build(), new EnvVarBuilder().withName("JAVA_OPTS").withValue(System.getenv("JAVA_OPTS")).build(), new EnvVarBuilder().withName("DEBUG_JOBS").withValue(System.getenv("DEBUG_OPERATOR")).build(), new EnvVarBuilder().withName("DEBUG_JOBS_SUSPEND").withValue(System.getenv("DEBUG_OPERATOR_SUSPEND")).build(), new EnvVarBuilder().withName("DBOPS_LOCK_TIMEOUT").withValue(OperatorProperty.LOCK_TIMEOUT.getString()).build(), new EnvVarBuilder().withName("DBOPS_LOCK_POLL_INTERVAL").withValue(OperatorProperty.LOCK_POLL_INTERVAL.getString()).build()).build()).endSpec().endTemplate().endSpec().build();
}
Also used : ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) JobBuilder(io.fabric8.kubernetes.api.model.batch.v1.JobBuilder) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder)

Example 17 with StackGresDbOps

use of io.stackgres.common.crd.sgdbops.StackGresDbOps in project stackgres by ongres.

the class DatabaseOperationEventEmitterImplTest method assertInvolvedObject.

private void assertInvolvedObject(Event event) {
    StackGresDbOps dbOps = kubeDb.getDbOps(dbOpsName, namespace);
    final ObjectReference involvedObject = event.getInvolvedObject();
    final ObjectMeta metadata = dbOps.getMetadata();
    assertEquals(dbOps.getKind(), involvedObject.getKind());
    assertEquals(dbOps.getApiVersion(), involvedObject.getApiVersion());
    assertEquals(metadata.getName(), involvedObject.getName());
    assertEquals(metadata.getUid(), involvedObject.getUid());
    assertEquals(metadata.getNamespace(), involvedObject.getNamespace());
    assertEquals(metadata.getResourceVersion(), involvedObject.getResourceVersion());
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) ObjectReference(io.fabric8.kubernetes.api.model.ObjectReference) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps)

Example 18 with StackGresDbOps

use of io.stackgres.common.crd.sgdbops.StackGresDbOps in project stackgres by ongres.

the class DbOpLauncherImplTest method givenAValidDbOps_shouldSetRunningConditionsBeforeExecutingTheJob.

@Test
void givenAValidDbOps_shouldSetRunningConditionsBeforeExecutingTheJob() {
    ArgumentCaptor<StackGresDbOps> captor = ArgumentCaptor.forClass(StackGresDbOps.class);
    when(securityUpgradeJob.runJob(captor.capture(), any())).thenAnswer(invocation -> getClusterRestartStateUni());
    dbOpLauncher.launchDbOp(randomDbOpsName, namespace);
    StackGresDbOps captured = captor.getValue();
    assertNotNull(captured.getStatus().getOpStarted());
    assertTrue(Instant.parse(captured.getStatus().getOpStarted()).isBefore(Instant.now()));
    assertEquals(0, captured.getStatus().getOpRetries());
    var conditions = captured.getStatus().getConditions();
    assertNotNull(conditions);
    assertEquals(3, conditions.size());
    assertTrue(() -> conditions.stream().anyMatch(DbOpsStatusCondition.DB_OPS_RUNNING::isCondition));
    assertTrue(() -> conditions.stream().anyMatch(DbOpsStatusCondition.DB_OPS_FALSE_COMPLETED::isCondition));
    assertTrue(() -> conditions.stream().anyMatch(DbOpsStatusCondition.DB_OPS_FALSE_FAILED::isCondition));
}
Also used : StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) DbOpsStatusCondition(io.stackgres.common.crd.sgdbops.DbOpsStatusCondition) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 19 with StackGresDbOps

use of io.stackgres.common.crd.sgdbops.StackGresDbOps in project stackgres by ongres.

the class DbOpLauncherImplTest method givenAValidDbOpsRetry_shouldSetRunningConditionsBeforeExecutingTheJob.

@Test
void givenAValidDbOpsRetry_shouldSetRunningConditionsBeforeExecutingTheJob() {
    ArgumentCaptor<StackGresDbOps> captor = ArgumentCaptor.forClass(StackGresDbOps.class);
    when(securityUpgradeJob.runJob(captor.capture(), any())).thenAnswer(invocation -> getClusterRestartStateUni());
    Instant previousOpStarted = Instant.now();
    dbOps.setStatus(new StackGresDbOpsStatus());
    dbOps.getStatus().setOpStarted(previousOpStarted.toString());
    dbOps.getStatus().setOpRetries(0);
    dbOps.getStatus().setConditions(Seq.of(DbOpsStatusCondition.DB_OPS_FALSE_RUNNING, DbOpsStatusCondition.DB_OPS_FALSE_COMPLETED, DbOpsStatusCondition.DB_OPS_FAILED).map(DbOpsStatusCondition::getCondition).peek(condition -> condition.setLastTransitionTime(previousOpStarted.toString())).toList());
    mockKubeDb.addOrReplaceDbOps(dbOps);
    dbOpLauncher.launchDbOp(randomDbOpsName, namespace);
    StackGresDbOps captured = captor.getValue();
    assertNotNull(captured.getStatus().getOpStarted());
    assertTrue(Instant.parse(captured.getStatus().getOpStarted()).isBefore(Instant.now()));
    assertEquals(1, captured.getStatus().getOpRetries());
    var conditions = captured.getStatus().getConditions();
    assertNotNull(conditions);
    assertEquals(3, conditions.size());
    assertTrue(() -> conditions.stream().anyMatch(DbOpsStatusCondition.DB_OPS_RUNNING::isCondition));
    assertTrue(() -> conditions.stream().anyMatch(DbOpsStatusCondition.DB_OPS_FALSE_COMPLETED::isCondition));
    assertTrue(() -> conditions.stream().anyMatch(DbOpsStatusCondition.DB_OPS_FALSE_FAILED::isCondition));
}
Also used : StackGresDbOpsStatus(io.stackgres.common.crd.sgdbops.StackGresDbOpsStatus) Instant(java.time.Instant) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) DbOpsStatusCondition(io.stackgres.common.crd.sgdbops.DbOpsStatusCondition) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 20 with StackGresDbOps

use of io.stackgres.common.crd.sgdbops.StackGresDbOps in project stackgres by ongres.

the class AbstractRestartStateHandler method buildClusterRestartState.

protected ClusterRestartState buildClusterRestartState(StackGresDbOps dbOps, StackGresCluster cluster, Optional<StatefulSet> statefulSet, List<Pod> clusterPods) {
    DbOpsRestartStatus restartStatus = getDbOpRestartStatus(dbOps);
    Map<String, Pod> podsDict = clusterPods.stream().collect(Collectors.toMap(pod -> pod.getMetadata().getName(), Function.identity()));
    var initialInstances = Optional.ofNullable(restartStatus.getInitialInstances()).map(instances -> instances.stream().map(podsDict::get).collect(Collectors.toUnmodifiableList())).orElse(clusterPods);
    var restartedInstances = Optional.ofNullable(restartStatus.getRestartedInstances()).map(instances -> instances.stream().map(podsDict::get).collect(Collectors.toUnmodifiableList())).orElse(List.of());
    var podRestartReasonsMap = clusterPods.stream().collect(Collectors.toUnmodifiableMap(Function.identity(), pod -> getPodRestartReasons(cluster, statefulSet, pod)));
    final String method = getRestartMethod(dbOps).orElse(REDUCED_IMPACT_METHOD);
    final boolean onlyPendingRestart = Optional.of(dbOps.getSpec()).map(StackGresDbOpsSpec::getRestart).map(StackGresDbOpsRestart::getOnlyPendingRestart).orElse(false);
    return ImmutableClusterRestartState.builder().namespace(dbOps.getMetadata().getNamespace()).dbOpsName(dbOps.getMetadata().getName()).dbOpsOperation(dbOps.getSpec().getOp()).clusterName(cluster.getMetadata().getName()).restartMethod(method).isOnlyPendingRestart(onlyPendingRestart).primaryInstance(getPrimaryInstance(clusterPods)).isSwitchoverInitiated(restartStatus.getSwitchoverInitiated() != null).isSwitchoverFinalized(restartStatus.getSwitchoverFinalized() != null).initialInstances(initialInstances).restartedInstances(restartedInstances).totalInstances(clusterPods).podRestartReasonsMap(podRestartReasonsMap).build();
}
Also used : StackGresContext(io.stackgres.common.StackGresContext) StackGresDbOpsRestart(io.stackgres.common.crd.sgdbops.StackGresDbOpsRestart) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) LoggerFactory(org.slf4j.LoggerFactory) DbOpsRestartStatus(io.stackgres.common.crd.sgdbops.DbOpsRestartStatus) InvalidCluster(io.stackgres.jobs.dbops.clusterrestart.InvalidCluster) Function(java.util.function.Function) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) RestartEvent(io.stackgres.jobs.dbops.clusterrestart.RestartEvent) ArrayList(java.util.ArrayList) Uni(io.smallrye.mutiny.Uni) Inject(javax.inject.Inject) ImmutableClusterRestartState(io.stackgres.jobs.dbops.clusterrestart.ImmutableClusterRestartState) ImmutableList(com.google.common.collect.ImmutableList) Duration(java.time.Duration) Map(java.util.Map) ClusterDbOpsRestartStatus(io.stackgres.common.crd.sgcluster.ClusterDbOpsRestartStatus) CustomResourceFinder(io.stackgres.common.resource.CustomResourceFinder) StackGresDbOpsSpec(io.stackgres.common.crd.sgdbops.StackGresDbOpsSpec) StackGresClusterStatus(io.stackgres.common.crd.sgcluster.StackGresClusterStatus) REDUCED_IMPACT_METHOD(io.stackgres.jobs.dbops.clusterrestart.ClusterRestartImpl.REDUCED_IMPACT_METHOD) RestartReasons(io.stackgres.common.ClusterPendingRestartUtil.RestartReasons) Logger(org.slf4j.Logger) EventEmitter(io.stackgres.common.event.EventEmitter) ClusterRestart(io.stackgres.jobs.dbops.clusterrestart.ClusterRestart) ClusterRestartStateHandlerImpl(io.stackgres.jobs.dbops.clusterrestart.ClusterRestartStateHandlerImpl) LabelFactoryForCluster(io.stackgres.common.LabelFactoryForCluster) Pod(io.fabric8.kubernetes.api.model.Pod) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) ClusterRestartState(io.stackgres.jobs.dbops.clusterrestart.ClusterRestartState) List(java.util.List) CustomResourceScheduler(io.stackgres.common.resource.CustomResourceScheduler) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) ResourceScanner(io.stackgres.common.resource.ResourceScanner) Optional(java.util.Optional) ResourceFinder(io.stackgres.common.resource.ResourceFinder) NotNull(org.jetbrains.annotations.NotNull) ClusterPendingRestartUtil(io.stackgres.common.ClusterPendingRestartUtil) Pod(io.fabric8.kubernetes.api.model.Pod) StackGresDbOpsSpec(io.stackgres.common.crd.sgdbops.StackGresDbOpsSpec) DbOpsRestartStatus(io.stackgres.common.crd.sgdbops.DbOpsRestartStatus) ClusterDbOpsRestartStatus(io.stackgres.common.crd.sgcluster.ClusterDbOpsRestartStatus)

Aggregations

StackGresDbOps (io.stackgres.common.crd.sgdbops.StackGresDbOps)23 EnvVarBuilder (io.fabric8.kubernetes.api.model.EnvVarBuilder)8 List (java.util.List)8 Inject (javax.inject.Inject)8 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)7 Optional (java.util.Optional)6 ImmutableList (com.google.common.collect.ImmutableList)5 LabelFactoryForCluster (io.stackgres.common.LabelFactoryForCluster)5 LabelFactoryForDbOps (io.stackgres.common.LabelFactoryForDbOps)5 Collectors (java.util.stream.Collectors)5 JsonMapper (com.fasterxml.jackson.databind.json.JsonMapper)4 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)4 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)4 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)4 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)4 PodSecurityContext (io.fabric8.kubernetes.api.model.PodSecurityContext)4 JobBuilder (io.fabric8.kubernetes.api.model.batch.v1.JobBuilder)4 ClusterStatefulSetPath (io.stackgres.common.ClusterStatefulSetPath)4 DbOpsEnvironmentVariables (io.stackgres.operator.cluster.factory.DbOpsEnvironmentVariables)4 StackGresDbOpsContext (io.stackgres.operator.conciliation.dbops.StackGresDbOpsContext)4