Repository navigation
[SPARK-48382] Add reconciler to spark-operator module
#12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
spark-operator/src/main/java/org/apache/spark/k8s/operator/context/BaseContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * 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.context; | ||
|
|
||
| import io.fabric8.kubernetes.client.KubernetesClient; | ||
|
|
||
| import org.apache.spark.k8s.operator.BaseResource; | ||
|
|
||
| public abstract class BaseContext<CR extends BaseResource<?, ?, ?, ?, ?>> { | ||
| public abstract CR getResource(); | ||
|
|
||
| public abstract KubernetesClient getClient(); | ||
| } |
112 changes: 112 additions & 0 deletions
112
spark-operator/src/main/java/org/apache/spark/k8s/operator/context/SparkAppContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /* | ||
| * 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.context; | ||
|
|
||
| import static org.apache.spark.k8s.operator.utils.Utils.driverLabels; | ||
| import static org.apache.spark.k8s.operator.utils.Utils.executorLabels; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import io.fabric8.kubernetes.api.model.HasMetadata; | ||
| import io.fabric8.kubernetes.api.model.Pod; | ||
| import io.fabric8.kubernetes.client.KubernetesClient; | ||
| import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| import org.apache.spark.k8s.operator.SparkAppResourceSpec; | ||
| import org.apache.spark.k8s.operator.SparkAppSubmissionWorker; | ||
| import org.apache.spark.k8s.operator.SparkApplication; | ||
| import org.apache.spark.k8s.operator.reconciler.SparkAppResourceSpecFactory; | ||
|
|
||
| /** | ||
| * Context for {@link org.apache.spark.k8s.operator.SparkApplication} resource, including secondary | ||
| * resource(s) and desired secondary resource spec | ||
| */ | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class SparkAppContext extends BaseContext<SparkApplication> { | ||
| private final SparkApplication sparkApplication; | ||
| private final Context<?> josdkContext; | ||
| private final SparkAppSubmissionWorker submissionWorker; | ||
|
|
||
| /** secondaryResourceSpec is initialized in a lazy fashion - built upon the first attempt */ | ||
| private SparkAppResourceSpec secondaryResourceSpec; | ||
|
|
||
| public Optional<Pod> getDriverPod() { | ||
| return josdkContext | ||
| .getSecondaryResourcesAsStream(Pod.class) | ||
| .filter( | ||
| p -> | ||
| p.getMetadata() | ||
| .getLabels() | ||
| .entrySet() | ||
| .containsAll(driverLabels(sparkApplication).entrySet())) | ||
| .findAny(); | ||
| } | ||
|
|
||
| public Set<Pod> getExecutorsForApplication() { | ||
| return josdkContext | ||
| .getSecondaryResourcesAsStream(Pod.class) | ||
| .filter( | ||
| p -> | ||
| p.getMetadata() | ||
| .getLabels() | ||
| .entrySet() | ||
| .containsAll(executorLabels(sparkApplication).entrySet())) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| private SparkAppResourceSpec getSecondaryResourceSpec() { | ||
| synchronized (this) { | ||
| if (secondaryResourceSpec == null) { | ||
| secondaryResourceSpec = | ||
| SparkAppResourceSpecFactory.buildResourceSpec( | ||
| sparkApplication, josdkContext.getClient(), submissionWorker); | ||
| } | ||
| return secondaryResourceSpec; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public SparkApplication getResource() { | ||
| return sparkApplication; | ||
| } | ||
|
|
||
| @Override | ||
| public KubernetesClient getClient() { | ||
| return josdkContext.getClient(); | ||
| } | ||
|
|
||
| public List<HasMetadata> getDriverPreResourcesSpec() { | ||
| return getSecondaryResourceSpec().getDriverPreResources(); | ||
| } | ||
|
|
||
| public Pod getDriverPodSpec() { | ||
| return getSecondaryResourceSpec().getConfiguredPod(); | ||
| } | ||
|
|
||
| public List<HasMetadata> getDriverResourcesSpec() { | ||
| return getSecondaryResourceSpec().getDriverResources(); | ||
| } | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
spark-operator/src/main/java/org/apache/spark/k8s/operator/decorators/DriverDecorator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * 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.decorators; | ||
|
|
||
| import static org.apache.spark.k8s.operator.utils.ModelUtils.buildOwnerReferenceTo; | ||
| import static org.apache.spark.k8s.operator.utils.Utils.sparkAppResourceLabels; | ||
|
|
||
| import io.fabric8.kubernetes.api.model.HasMetadata; | ||
| import io.fabric8.kubernetes.api.model.ObjectMeta; | ||
| import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| import org.apache.spark.k8s.operator.SparkApplication; | ||
|
|
||
| /** | ||
| * Decorates driver (pod) to make sure its metadata matches event source Also adds owner reference | ||
| * to the owner SparkApplication for garbage collection | ||
| */ | ||
| @RequiredArgsConstructor | ||
| public class DriverDecorator implements ResourceDecorator { | ||
|
|
||
| private final SparkApplication app; | ||
|
|
||
| /** Add labels and owner references to the app for all secondary resources */ | ||
| @Override | ||
| public <T extends HasMetadata> T decorate(T resource) { | ||
| ObjectMeta metaData = | ||
| new ObjectMetaBuilder(resource.getMetadata()) | ||
| .addToOwnerReferences(buildOwnerReferenceTo(app)) | ||
| .addToLabels(sparkAppResourceLabels(app)) | ||
| .withNamespace(app.getMetadata().getNamespace()) | ||
| .build(); | ||
| resource.setMetadata(metaData); | ||
| return resource; | ||
| } | ||
| } |
73 changes: 73 additions & 0 deletions
73
...rator/src/main/java/org/apache/spark/k8s/operator/decorators/DriverResourceDecorator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * 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.decorators; | ||
|
|
||
| import static org.apache.spark.k8s.operator.utils.ModelUtils.buildOwnerReferenceTo; | ||
|
|
||
| import io.fabric8.kubernetes.api.model.HasMetadata; | ||
| import io.fabric8.kubernetes.api.model.ObjectMeta; | ||
| import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
| import io.fabric8.kubernetes.api.model.OwnerReference; | ||
| import io.fabric8.kubernetes.api.model.Pod; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| /** | ||
| * Decorates Driver resources (except the pod). This makes sure all resources have owner reference | ||
|
jiangzho marked this conversation as resolved.
|
||
| * to the driver pod, so they can be garbage collected upon termination. Secondary resources would | ||
| * be garbage-collected if ALL owners are deleted. Therefore, operator makes only driver pod has | ||
| * owned by the SparkApplication while all other secondary resources are owned by the driver. In | ||
| * this way, after driver pod is deleted at the end of each attempt, all other resources would be | ||
| * garbage collected automatically. If given secondary resource already has owner reference to | ||
| * additional resources, it's reference to driver pod would be added to the list. Note - this is | ||
| * uncommon as additional owner reference might impact the garbage collection at the end of each | ||
| * attempt | ||
| */ | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class DriverResourceDecorator implements ResourceDecorator { | ||
| private final Pod driverPod; | ||
|
|
||
| @Override | ||
| public <T extends HasMetadata> T decorate(T resource) { | ||
| boolean ownerReferenceExists = false; | ||
| if (resource.getMetadata().getOwnerReferences() != null | ||
| && !resource.getMetadata().getOwnerReferences().isEmpty()) { | ||
| for (OwnerReference o : resource.getMetadata().getOwnerReferences()) { | ||
| if (driverPod.getKind().equals(o.getKind()) | ||
| && driverPod.getMetadata().getName().equals(o.getName()) | ||
| && driverPod.getMetadata().getUid().equals(o.getUid())) { | ||
| ownerReferenceExists = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| if (!ownerReferenceExists) { | ||
|
dongjoon-hyun marked this conversation as resolved.
|
||
| log.debug("Adding OwnerReference to driver for secondary resource"); | ||
| ObjectMeta metaData = | ||
| new ObjectMetaBuilder(resource.getMetadata()) | ||
| .addToOwnerReferences(buildOwnerReferenceTo(driverPod)) | ||
| .addToLabels(driverPod.getMetadata().getLabels()) | ||
| .build(); | ||
| resource.setMetadata(metaData); | ||
| } | ||
| return resource; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.