dongjoon-hyun commented on code in PR #12: URL: https://github.com/apache/spark-kubernetes-operator/pull/12#discussion_r1677218778
########## spark-operator/src/main/java/org/apache/spark/k8s/operator/SparkOperator.java: ########## @@ -0,0 +1,225 @@ +/* + * 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.spark.k8s.operator; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import io.fabric8.kubernetes.client.KubernetesClient; +import io.javaoperatorsdk.operator.Operator; +import io.javaoperatorsdk.operator.RegisteredController; +import io.javaoperatorsdk.operator.api.config.ConfigurationServiceOverrider; +import io.javaoperatorsdk.operator.api.config.ControllerConfigurationOverrider; +import lombok.extern.slf4j.Slf4j; +import okhttp3.Interceptor; +import org.apache.commons.collections.CollectionUtils; + +import org.apache.spark.k8s.operator.client.KubernetesClientFactory; +import org.apache.spark.k8s.operator.client.RetryInterceptor; +import org.apache.spark.k8s.operator.config.SparkOperatorConf; +import org.apache.spark.k8s.operator.config.SparkOperatorConfigMapReconciler; +import org.apache.spark.k8s.operator.listeners.SparkAppStatusListener; +import org.apache.spark.k8s.operator.metrics.MetricsService; +import org.apache.spark.k8s.operator.metrics.MetricsSystem; +import org.apache.spark.k8s.operator.metrics.MetricsSystemFactory; +import org.apache.spark.k8s.operator.metrics.healthcheck.SentinelManager; +import org.apache.spark.k8s.operator.metrics.source.KubernetesMetricsInterceptor; +import org.apache.spark.k8s.operator.metrics.source.OperatorJosdkMetrics; +import org.apache.spark.k8s.operator.probe.ProbeService; +import org.apache.spark.k8s.operator.reconciler.SparkAppReconciler; +import org.apache.spark.k8s.operator.reconciler.SparkReconcilerUtils; +import org.apache.spark.k8s.operator.utils.ClassLoadingUtils; +import org.apache.spark.k8s.operator.utils.SparkAppStatusRecorder; + +/** + * Entry point for Spark Operator. Sets up reconcilers for CustomResource and health check servers + */ +@Slf4j +public class SparkOperator { + private final List<Operator> operators; + private final KubernetesClient client; + private final SparkAppSubmissionWorker appSubmissionWorker; + private final SparkAppStatusRecorder sparkAppStatusRecorder; + protected Set<RegisteredController<?>> registeredSparkControllers; + protected Set<String> watchedNamespaces; + private final MetricsSystem metricsSystem; + private final SentinelManager<SparkApplication> sparkApplicationSentinelManager; + private final ProbeService probeService; + private final MetricsService metricsService; + private final ExecutorService metricsResourcesSingleThreadPool; + + public SparkOperator() { + this.metricsSystem = MetricsSystemFactory.createMetricsSystem(); + this.client = + KubernetesClientFactory.buildKubernetesClient(getClientInterceptors(metricsSystem)); + this.appSubmissionWorker = new SparkAppSubmissionWorker(); + this.sparkAppStatusRecorder = + new SparkAppStatusRecorder( + ClassLoadingUtils.getStatusListener( + SparkAppStatusListener.class, + SparkOperatorConf.SPARK_APP_STATUS_LISTENER_CLASS_NAMES.getValue())); + this.registeredSparkControllers = new HashSet<>(); + this.watchedNamespaces = SparkReconcilerUtils.getWatchedNamespaces(); + this.sparkApplicationSentinelManager = new SentinelManager<>(); + this.operators = new ArrayList<>(); + this.operators.add(createSparkOperator()); + if (SparkOperatorConf.DYNAMIC_CONFIG_ENABLED.getValue()) { + this.operators.add(createSparkOperatorConfMonitor()); + } + this.metricsResourcesSingleThreadPool = Executors.newSingleThreadExecutor(); + this.probeService = + new ProbeService( + operators, Collections.singletonList(sparkApplicationSentinelManager), null); + this.metricsService = new MetricsService(metricsSystem, metricsResourcesSingleThreadPool); + } + + protected Operator createSparkOperator() { + Operator op = new Operator(this::overrideOperatorConfigs); + registeredSparkControllers.add( + op.register( + new SparkAppReconciler( + appSubmissionWorker, sparkAppStatusRecorder, sparkApplicationSentinelManager), + this::overrideControllerConfigs)); + return op; + } + + protected Operator createSparkOperatorConfMonitor() { + Operator op = new Operator(this::overrideConfigMonitorConfigs); + String operatorNamespace = SparkOperatorConf.OPERATOR_NAMESPACE.getValue(); + String confSelector = SparkOperatorConf.DYNAMIC_CONFIG_SELECTOR_STR.getValue(); + log.info( + "Starting conf monitor in namespace: {}, with selector: {}", + operatorNamespace, + confSelector); + op.register( + new SparkOperatorConfigMapReconciler( + this::updateWatchingNamespaces, + SparkOperatorConf.OPERATOR_NAMESPACE.getValue(), + unused -> SparkReconcilerUtils.getWatchedNamespaces()), + c -> { + c.withRateLimiter(SparkOperatorConf.getOperatorRateLimiter()); + c.settingNamespaces(operatorNamespace); + c.withLabelSelector(confSelector); + }); + return op; + } + + protected boolean updateWatchingNamespaces(Set<String> namespaces) { + if (watchedNamespaces.equals(namespaces)) { + log.info("No watched namespace change detected"); + return false; + } + if (CollectionUtils.isEmpty(namespaces)) { + log.error("Cannot updating namespaces to empty"); + return false; + } + registeredSparkControllers.forEach( + c -> { + if (c.allowsNamespaceChanges()) { + log.info("Updating operator namespaces to {}", namespaces); + c.changeNamespaces(namespaces); + } else { + log.error("Controller does not allow namespace change, skipping namespace change."); + } + }); + this.watchedNamespaces = new HashSet<>(namespaces); + return true; + } + + protected void overrideOperatorConfigs(ConfigurationServiceOverrider overrider) { + overrider.withKubernetesClient(client); + overrider.withStopOnInformerErrorDuringStartup( + SparkOperatorConf.TERMINATE_ON_INFORMER_FAILURE.getValue()); + overrider.withTerminationTimeoutSeconds( + SparkOperatorConf.RECONCILER_TERMINATION_TIMEOUT_SECONDS.getValue()); + int parallelism = SparkOperatorConf.RECONCILER_PARALLELISM.getValue(); + if (parallelism > 0) { + log.info("Configuring operator with {} reconciliation threads.", parallelism); + overrider.withConcurrentReconciliationThreads(parallelism); + } else { + log.info("Configuring operator with unbounded reconciliation thread pool."); + overrider.withExecutorService(Executors.newCachedThreadPool()); + } + if (SparkOperatorConf.LEADER_ELECTION_ENABLED.getValue()) { + overrider.withLeaderElectionConfiguration(SparkOperatorConf.getLeaderElectionConfig()); + } + if (SparkOperatorConf.JOSDK_METRICS_ENABLED.getValue()) { + log.info("Adding OperatorJosdkMetrics."); + OperatorJosdkMetrics operatorJosdkMetrics = new OperatorJosdkMetrics(); + overrider.withMetrics(operatorJosdkMetrics); + metricsSystem.registerSource(operatorJosdkMetrics); + } + } + + protected void overrideConfigMonitorConfigs(ConfigurationServiceOverrider overrider) { + overrider.withKubernetesClient(client); + overrider.withConcurrentReconciliationThreads( + SparkOperatorConf.DYNAMIC_CONFIG_RECONCILER_PARALLELISM.getValue()); + overrider.withStopOnInformerErrorDuringStartup(true); + overrider.withCloseClientOnStop(false); + overrider.withInformerStoppedHandler( + (informer, ex) -> + log.error("Dynamic config informer stopped: operator will not accept config updates.")); + } + + protected void overrideControllerConfigs(ControllerConfigurationOverrider<?> overrider) { + if (watchedNamespaces.isEmpty()) { + log.info("Initializing operator watching at cluster level."); + } else { + log.info("Initializing with watched namespaces {}", watchedNamespaces); + } + overrider.settingNamespaces(watchedNamespaces); + overrider.withRateLimiter(SparkOperatorConf.getOperatorRateLimiter()); + overrider.withRetry(SparkOperatorConf.getOperatorRetry()); + } + + protected List<Interceptor> getClientInterceptors(MetricsSystem metricsSystem) { + List<Interceptor> clientInterceptors = new ArrayList<>(); + RetryInterceptor retryInterceptor = + RetryInterceptor.builder() + .maxRetryAfterInSecs(SparkOperatorConf.MAX_RETRY_ATTEMPT_AFTER_SECONDS.getValue()) + .maxAttemptCount(SparkOperatorConf.MAX_RETRY_ATTEMPT_ON_KUBE_SERVER_FAILURE.getValue()) + .defaultRetryAfterInSecs(SparkOperatorConf.RETRY_ATTEMPT_AFTER_SECONDS.getValue()) + .build(); + clientInterceptors.add(retryInterceptor); + if (SparkOperatorConf.KUBERNETES_CLIENT_METRICS_ENABLED.getValue()) { + KubernetesMetricsInterceptor metricsInterceptor = new KubernetesMetricsInterceptor(); + clientInterceptors.add(metricsInterceptor); + metricsSystem.registerSource(metricsInterceptor); + } + return clientInterceptors; + } + + public static void main(String[] args) { Review Comment: Could you add a comment about configurations because we will not use these `args`? I guess we can mention `SparkOperatorConfManager` and how the users can tune this `SparkOperator`. -- 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: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org