Search in sources :

Example 1 with Context

use of io.javaoperatorsdk.operator.api.reconciler.Context in project java-operator-sdk by java-operator-sdk.

the class ReconciliationDispatcherTest method propagatesRetryInfoToContextIfFinalizerSet.

@Test
void propagatesRetryInfoToContextIfFinalizerSet() {
    testCustomResource.addFinalizer(DEFAULT_FINALIZER);
    reconciliationDispatcher.handleExecution(new ExecutionScope(testCustomResource, new RetryInfo() {

        @Override
        public int getAttemptCount() {
            return 2;
        }

        @Override
        public boolean isLastAttempt() {
            return true;
        }
    }));
    ArgumentCaptor<Context> contextArgumentCaptor = ArgumentCaptor.forClass(Context.class);
    verify(reconciler, times(1)).reconcile(any(), contextArgumentCaptor.capture());
    Context context = contextArgumentCaptor.getValue();
    final var retryInfo = context.getRetryInfo().get();
    assertThat(retryInfo.getAttemptCount()).isEqualTo(2);
    assertThat(retryInfo.isLastAttempt()).isEqualTo(true);
}
Also used : Context(io.javaoperatorsdk.operator.api.reconciler.Context) RetryInfo(io.javaoperatorsdk.operator.api.reconciler.RetryInfo) Test(org.junit.jupiter.api.Test)

Example 2 with Context

use of io.javaoperatorsdk.operator.api.reconciler.Context in project java-operator-sdk by java-operator-sdk.

the class ReconciliationDispatcher method handleDispatch.

private PostExecutionControl<R> handleDispatch(ExecutionScope<R> executionScope) {
    R resource = executionScope.getResource();
    log.debug("Handling dispatch for resource {}", getName(resource));
    final var markedForDeletion = resource.isMarkedForDeletion();
    if (markedForDeletion && shouldNotDispatchToDelete(resource)) {
        log.debug("Skipping delete of resource {} because finalizer(s) {} don't allow processing yet", getName(resource), resource.getMetadata().getFinalizers());
        return PostExecutionControl.defaultDispatch();
    }
    Context context = new DefaultContext<>(executionScope.getRetryInfo(), controller, resource);
    if (markedForDeletion) {
        return handleCleanup(resource, context);
    } else {
        return handleReconcile(executionScope, resource, context);
    }
}
Also used : DefaultContext(io.javaoperatorsdk.operator.api.reconciler.DefaultContext) Context(io.javaoperatorsdk.operator.api.reconciler.Context) DefaultContext(io.javaoperatorsdk.operator.api.reconciler.DefaultContext)

Example 3 with Context

use of io.javaoperatorsdk.operator.api.reconciler.Context in project flink-kubernetes-operator by apache.

the class JobReconcilerTest method triggerSavepoint.

@Test
public void triggerSavepoint() throws Exception {
    Context context = TestUtils.createContextWithReadyJobManagerDeployment();
    TestingFlinkService flinkService = new TestingFlinkService();
    JobReconciler reconciler = new JobReconciler(null, flinkService, operatorConfiguration);
    FlinkDeployment deployment = TestUtils.buildApplicationCluster();
    Configuration config = FlinkUtils.getEffectiveConfig(deployment, new Configuration());
    reconciler.reconcile("test", deployment, context, config);
    List<Tuple2<String, JobStatusMessage>> runningJobs = flinkService.listJobs();
    verifyAndSetRunningJobsToStatus(deployment, runningJobs);
    assertNull(deployment.getStatus().getJobStatus().getSavepointInfo().getTriggerId());
    // trigger savepoint
    FlinkDeployment spDeployment = ReconciliationUtils.clone(deployment);
    long oldValue = spDeployment.getSpec().getJob().getSavepointTriggerNonce();
    spDeployment.getSpec().getJob().setSavepointTriggerNonce(oldValue + 1);
    reconciler.reconcile("test", spDeployment, context, config);
    assertEquals("trigger_0", spDeployment.getStatus().getJobStatus().getSavepointInfo().getTriggerId());
}
Also used : Context(io.javaoperatorsdk.operator.api.reconciler.Context) FlinkDeployment(org.apache.flink.kubernetes.operator.crd.FlinkDeployment) Configuration(org.apache.flink.configuration.Configuration) FlinkOperatorConfiguration(org.apache.flink.kubernetes.operator.config.FlinkOperatorConfiguration) TestingFlinkService(org.apache.flink.kubernetes.operator.TestingFlinkService) Tuple2(org.apache.flink.api.java.tuple.Tuple2) Test(org.junit.jupiter.api.Test)

Example 4 with Context

use of io.javaoperatorsdk.operator.api.reconciler.Context in project flink-kubernetes-operator by apache.

the class TestUtils method createContextWithReadyJobManagerDeployment.

public static Context createContextWithReadyJobManagerDeployment() {
    return new Context() {

        @Override
        public Optional<RetryInfo> getRetryInfo() {
            return Optional.empty();
        }

        @Override
        public <T> Optional<T> getSecondaryResource(Class<T> expectedType, String eventSourceName) {
            DeploymentStatus status = new DeploymentStatus();
            status.setAvailableReplicas(1);
            status.setReplicas(1);
            DeploymentSpec spec = new DeploymentSpec();
            spec.setReplicas(1);
            Deployment deployment = new Deployment();
            deployment.setSpec(spec);
            deployment.setStatus(status);
            return Optional.of((T) deployment);
        }
    };
}
Also used : Context(io.javaoperatorsdk.operator.api.reconciler.Context) FlinkDeploymentSpec(org.apache.flink.kubernetes.operator.crd.spec.FlinkDeploymentSpec) DeploymentSpec(io.fabric8.kubernetes.api.model.apps.DeploymentSpec) FlinkDeployment(org.apache.flink.kubernetes.operator.crd.FlinkDeployment) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) FlinkDeploymentStatus(org.apache.flink.kubernetes.operator.crd.status.FlinkDeploymentStatus) DeploymentStatus(io.fabric8.kubernetes.api.model.apps.DeploymentStatus) RetryInfo(io.javaoperatorsdk.operator.api.reconciler.RetryInfo)

Example 5 with Context

use of io.javaoperatorsdk.operator.api.reconciler.Context in project java-operator-sdk by java-operator-sdk.

the class DeploymentReconciler method reconcile.

@Override
public UpdateControl<Deployment> reconcile(Deployment resource, Context context) {
    log.info("Reconcile deployment: {}", resource);
    numberOfExecutions.incrementAndGet();
    if (resource.getStatus() == null) {
        resource.setStatus(new DeploymentStatus());
    }
    if (resource.getStatus().getConditions() == null) {
        resource.getStatus().setConditions(new ArrayList<>());
    }
    var conditions = resource.getStatus().getConditions();
    var condition = conditions.stream().filter(c -> c.getMessage().equals(STATUS_MESSAGE)).findFirst();
    if (condition.isEmpty()) {
        conditions.add(new DeploymentCondition(null, null, STATUS_MESSAGE, null, "unknown", "DeploymentReconciler"));
        return UpdateControl.updateStatus(resource);
    } else {
        return UpdateControl.noUpdate();
    }
}
Also used : Context(io.javaoperatorsdk.operator.api.reconciler.Context) ControllerConfiguration(io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration) TestExecutionInfoProvider(io.javaoperatorsdk.operator.support.TestExecutionInfoProvider) Reconciler(io.javaoperatorsdk.operator.api.reconciler.Reconciler) Logger(org.slf4j.Logger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LoggerFactory(org.slf4j.LoggerFactory) UpdateControl(io.javaoperatorsdk.operator.api.reconciler.UpdateControl) DeploymentStatus(io.fabric8.kubernetes.api.model.apps.DeploymentStatus) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ArrayList(java.util.ArrayList) DeploymentCondition(io.fabric8.kubernetes.api.model.apps.DeploymentCondition) DeploymentStatus(io.fabric8.kubernetes.api.model.apps.DeploymentStatus) DeploymentCondition(io.fabric8.kubernetes.api.model.apps.DeploymentCondition)

Aggregations

Context (io.javaoperatorsdk.operator.api.reconciler.Context)8 FlinkDeployment (org.apache.flink.kubernetes.operator.crd.FlinkDeployment)5 Test (org.junit.jupiter.api.Test)4 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)3 DeploymentStatus (io.fabric8.kubernetes.api.model.apps.DeploymentStatus)3 RetryInfo (io.javaoperatorsdk.operator.api.reconciler.RetryInfo)3 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)3 Configuration (org.apache.flink.configuration.Configuration)3 TestingFlinkService (org.apache.flink.kubernetes.operator.TestingFlinkService)3 FlinkOperatorConfiguration (org.apache.flink.kubernetes.operator.config.FlinkOperatorConfiguration)3 DeploymentCondition (io.fabric8.kubernetes.api.model.apps.DeploymentCondition)2 DeploymentSpec (io.fabric8.kubernetes.api.model.apps.DeploymentSpec)2 FlinkDeploymentSpec (org.apache.flink.kubernetes.operator.crd.spec.FlinkDeploymentSpec)2 FlinkDeploymentStatus (org.apache.flink.kubernetes.operator.crd.status.FlinkDeploymentStatus)2 ControllerConfiguration (io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration)1 DefaultContext (io.javaoperatorsdk.operator.api.reconciler.DefaultContext)1 Reconciler (io.javaoperatorsdk.operator.api.reconciler.Reconciler)1 UpdateControl (io.javaoperatorsdk.operator.api.reconciler.UpdateControl)1 TestExecutionInfoProvider (io.javaoperatorsdk.operator.support.TestExecutionInfoProvider)1 ArrayList (java.util.ArrayList)1