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();
}
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());
}
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));
}
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));
}
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();
}
Aggregations