davidradl commented on code in PR #969: URL: https://github.com/apache/flink-kubernetes-operator/pull/969#discussion_r2037077568
########## flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/controller/FlinkBlueGreenDeploymentController.java: ########## @@ -0,0 +1,609 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.kubernetes.operator.controller; + +import org.apache.flink.api.common.JobID; +import org.apache.flink.api.common.JobStatus; +import org.apache.flink.kubernetes.operator.api.FlinkBlueGreenDeployment; +import org.apache.flink.kubernetes.operator.api.FlinkDeployment; +import org.apache.flink.kubernetes.operator.api.bluegreen.DeploymentType; +import org.apache.flink.kubernetes.operator.api.lifecycle.ResourceLifecycleState; +import org.apache.flink.kubernetes.operator.api.spec.FlinkBlueGreenDeploymentSpec; +import org.apache.flink.kubernetes.operator.api.status.FlinkBlueGreenDeploymentState; +import org.apache.flink.kubernetes.operator.api.status.FlinkBlueGreenDeploymentStatus; +import org.apache.flink.kubernetes.operator.api.status.Savepoint; +import org.apache.flink.kubernetes.operator.api.utils.SpecUtils; +import org.apache.flink.kubernetes.operator.service.FlinkResourceContextFactory; +import org.apache.flink.util.Preconditions; + +import org.apache.flink.shaded.guava31.com.google.common.collect.ImmutableSet; + +import com.fasterxml.jackson.core.JsonProcessingException; +import io.fabric8.kubernetes.api.model.Event; +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.kubernetes.api.model.OwnerReference; +import io.fabric8.kubernetes.api.model.StatusDetails; +import io.fabric8.kubernetes.client.dsl.PodResource; +import io.fabric8.kubernetes.client.dsl.Resource; +import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration; +import io.javaoperatorsdk.operator.api.reconciler.Context; +import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration; +import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext; +import io.javaoperatorsdk.operator.api.reconciler.EventSourceInitializer; +import io.javaoperatorsdk.operator.api.reconciler.Reconciler; +import io.javaoperatorsdk.operator.api.reconciler.UpdateControl; +import io.javaoperatorsdk.operator.processing.event.source.EventSource; +import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource; +import io.javaoperatorsdk.operator.processing.event.source.informer.Mappers; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.time.Instant; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** Controller that runs the main reconcile loop for Flink Blue/Green deployments. */ +@ControllerConfiguration +public class FlinkBlueGreenDeploymentController + implements Reconciler<FlinkBlueGreenDeployment>, + EventSourceInitializer<FlinkBlueGreenDeployment> { + + private static final Logger LOG = LoggerFactory.getLogger(FlinkDeploymentController.class); + private static final int DEFAULT_MAX_NUM_RETRIES = 5; + private static final int DEFAULT_RECONCILIATION_RESCHEDULING_INTERVAL_MS = 15000; + + private final FlinkResourceContextFactory ctxFactory; + + public FlinkBlueGreenDeploymentController(FlinkResourceContextFactory ctxFactory) { + this.ctxFactory = ctxFactory; + } + + @Override + public Map<String, EventSource> prepareEventSources( + EventSourceContext<FlinkBlueGreenDeployment> eventSourceContext) { + InformerConfiguration<FlinkDeployment> flinkDeploymentInformerConfig = + InformerConfiguration.from(FlinkDeployment.class, eventSourceContext) + .withSecondaryToPrimaryMapper(Mappers.fromOwnerReference()) + .withNamespacesInheritedFromController(eventSourceContext) + .followNamespaceChanges(true) + .build(); + + return EventSourceInitializer.nameEventSources( + new InformerEventSource<>(flinkDeploymentInformerConfig, eventSourceContext)); + } + + @Override + public UpdateControl<FlinkBlueGreenDeployment> reconcile( + FlinkBlueGreenDeployment flinkBlueGreenDeployment, + Context<FlinkBlueGreenDeployment> josdkContext) + throws Exception { + + FlinkBlueGreenDeploymentStatus deploymentStatus = flinkBlueGreenDeployment.getStatus(); + + if (deploymentStatus == null) { + deploymentStatus = new FlinkBlueGreenDeploymentStatus(); + deploymentStatus.setLastReconciledSpec( + SpecUtils.serializeObject(flinkBlueGreenDeployment.getSpec(), "spec")); + return initiateDeployment( + flinkBlueGreenDeployment, + deploymentStatus, + DeploymentType.BLUE, + FlinkBlueGreenDeploymentState.TRANSITIONING_TO_BLUE, + null, + josdkContext, + true); + } else { + FlinkBlueGreenDeployments deployments = + FlinkBlueGreenDeployments.fromSecondaryResources(josdkContext); + + // TODO: if a new deployment request comes while in the middle of a transition it's + // currently ignored, but the new spec remains changed, should we roll it back? + // TODO: if we choose to leave a previously failed deployment 'running' for debug + // purposes, + // we should flag it somehow as 'ROLLED_BACK' to signal that it can be overriden by a + // new deployment attempt. + switch (deploymentStatus.getBlueGreenState()) { + case ACTIVE_BLUE: + return checkAndInitiateDeployment( + flinkBlueGreenDeployment, + deployments, + deploymentStatus, + DeploymentType.BLUE, + josdkContext); + case ACTIVE_GREEN: + return checkAndInitiateDeployment( + flinkBlueGreenDeployment, + deployments, + deploymentStatus, + DeploymentType.GREEN, + josdkContext); + case TRANSITIONING_TO_BLUE: + return monitorTransition( + flinkBlueGreenDeployment, + deployments, + deploymentStatus, + DeploymentType.GREEN, + josdkContext); + case TRANSITIONING_TO_GREEN: + return monitorTransition( + flinkBlueGreenDeployment, + deployments, + deploymentStatus, + DeploymentType.BLUE, + josdkContext); + default: + return UpdateControl.noUpdate(); + } + } + } + + private UpdateControl<FlinkBlueGreenDeployment> monitorTransition( + FlinkBlueGreenDeployment bgDeployment, + FlinkBlueGreenDeployments deployments, + FlinkBlueGreenDeploymentStatus deploymentStatus, + DeploymentType currentDeploymentType, + Context<FlinkBlueGreenDeployment> josdkContext) { + + var nextState = FlinkBlueGreenDeploymentState.ACTIVE_BLUE; + FlinkDeployment currentDeployment; + FlinkDeployment nextDeployment; + + if (DeploymentType.BLUE == currentDeploymentType) { + nextState = FlinkBlueGreenDeploymentState.ACTIVE_GREEN; + currentDeployment = deployments.getFlinkDeploymentBlue(); + nextDeployment = deployments.getFlinkDeploymentGreen(); + } else { + currentDeployment = deployments.getFlinkDeploymentGreen(); + nextDeployment = deployments.getFlinkDeploymentBlue(); + } + + Preconditions.checkNotNull( + nextDeployment, + "Target Dependent Deployment resource not found. Blue/Green deployment name: " + + bgDeployment.getMetadata().getName() + + ", current deployment type: " + + currentDeploymentType); + + if (isDeploymentReady(nextDeployment, josdkContext, deploymentStatus)) { + return deleteAndFinalize( + bgDeployment, + deploymentStatus, + currentDeploymentType, + josdkContext, + currentDeployment, + nextState); + } else { + // This phase requires rescheduling the reconciliation because the pod initialization + // could get stuck + // (e.g. waiting for resources) + // TODO: figure out the course of action for error/failure cases Review Comment: It seems risky to put in a capability without error failure cases being dealt with, is there a way to put this under a feature flag , until it is mature enough and handles errors and failures? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org