Aitozi commented on a change in pull request #112: URL: https://github.com/apache/flink-kubernetes-operator/pull/112#discussion_r839428005
########## File path: flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/reconciler/sessionjob/FlinkSessionJobReconciler.java ########## @@ -0,0 +1,135 @@ +/* + * 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.reconciler.sessionjob; + +import org.apache.flink.api.common.JobID; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.kubernetes.operator.config.FlinkOperatorConfiguration; +import org.apache.flink.kubernetes.operator.crd.FlinkDeployment; +import org.apache.flink.kubernetes.operator.crd.FlinkSessionJob; +import org.apache.flink.kubernetes.operator.crd.spec.FlinkSessionJobSpec; +import org.apache.flink.kubernetes.operator.crd.status.JobManagerDeploymentStatus; +import org.apache.flink.kubernetes.operator.reconciler.Reconciler; +import org.apache.flink.kubernetes.operator.reconciler.ReconciliationUtils; +import org.apache.flink.kubernetes.operator.reconciler.deployment.ApplicationReconciler; +import org.apache.flink.kubernetes.operator.service.FlinkService; +import org.apache.flink.kubernetes.operator.utils.FlinkUtils; + +import io.fabric8.kubernetes.client.KubernetesClient; +import io.javaoperatorsdk.operator.api.reconciler.Context; +import io.javaoperatorsdk.operator.api.reconciler.DeleteControl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Optional; + +/** The reconciler for the {@link FlinkSessionJob}. */ +public class FlinkSessionJobReconciler implements Reconciler<FlinkSessionJob> { + + private static final Logger LOG = LoggerFactory.getLogger(ApplicationReconciler.class); + + private final FlinkOperatorConfiguration operatorConfiguration; + private final KubernetesClient kubernetesClient; + private final FlinkService flinkService; + + public FlinkSessionJobReconciler( + KubernetesClient kubernetesClient, + FlinkService flinkService, + FlinkOperatorConfiguration operatorConfiguration) { + this.kubernetesClient = kubernetesClient; + this.flinkService = flinkService; + this.operatorConfiguration = operatorConfiguration; + } + + @Override + public void reconcile( + FlinkSessionJob flinkSessionJob, Context context, Configuration defaultConfig) + throws Exception { + + FlinkSessionJobSpec lastReconciledSpec = + flinkSessionJob.getStatus().getReconciliationStatus().getLastReconciledSpec(); + + if (lastReconciledSpec == null) { + deployFlinkJob(flinkSessionJob, defaultConfig); + return; + } + + boolean specChanged = !flinkSessionJob.getSpec().equals(lastReconciledSpec); + + if (specChanged) { + // TODO reconcile other spec change. + LOG.info("Other spec change have not supported"); + } + } + + @Override + public DeleteControl cleanup(FlinkSessionJob sessionJob, Configuration defaultConfig) { + Optional<FlinkDeployment> flinkDepOptional = getFlinkDeployment(sessionJob); + + if (flinkDepOptional.isPresent()) { + Configuration effectiveConfig = + FlinkUtils.getEffectiveConfig(flinkDepOptional.get(), defaultConfig); + String jobID = sessionJob.getStatus().getJobStatus().getJobId(); + if (jobID != null) { + try { + flinkService.cancelSessionJob(JobID.fromHexString(jobID), effectiveConfig); + } catch (Exception e) { + LOG.error("Failed to cancel job.", e); + } + } + } else { + LOG.info("Session cluster deployment not available"); + } + return DeleteControl.defaultDelete(); + } + + private void deployFlinkJob(FlinkSessionJob sessionJob, Configuration defaultConfig) + throws Exception { + Optional<FlinkDeployment> flinkDepOptional = getFlinkDeployment(sessionJob); + if (flinkDepOptional.isPresent() + && (flinkDepOptional.get().getStatus().getJobManagerDeploymentStatus() + == JobManagerDeploymentStatus.READY)) { + Configuration effectiveConfig = + FlinkUtils.getEffectiveConfig(flinkDepOptional.get(), defaultConfig); + flinkService.submitJobToSessionCluster(sessionJob, effectiveConfig); + ReconciliationUtils.updateForSpecReconciliationSuccess(sessionJob); + } else { + LOG.info("Session cluster deployment is not in READY state"); + } + } + + private Optional<FlinkDeployment> getFlinkDeployment(FlinkSessionJob sessionJob) { Review comment: Register an InformerEventSource firstly need to create an informer and then tell the SDK how to mapping the secondaryResource to the custom resource. By this, when a secondaryResource changed, it can trigger an event for the custom resource (session job) -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
