Skip to content
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

feat: "spark-submit jar on k8s" related label and resource expansion #4784

Merged
merged 15 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
public class FitterUtils {

public static boolean isOption(final String arg) {
return arg.matches("-[a-zA-Z-]+");
return arg.matches("-[0-9a-zA-Z-]+");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public class UniversalCmdTemplate extends AbstractCmdTemplate implements Cloneab
option(
CliKeys.JOB_LABEL,
CliKeys.JOB_LABEL_CLUSTER,
new String[] {"-yarnCluster"},
new String[] {"-yarnCluster", "-k8sCluster"},
"specify linkis yarn cluster for this job",
true,
"");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,31 @@
<version>${gson.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${kubernetes-client.version}</version>
<exclusions>
<exclusion>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-common</artifactId>
</exclusion>
<exclusion>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-common</artifactId>
<version>${kubernetes-client.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-core</artifactId>
<version>5.4.1</version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can use props value?

</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.linkis.manager.rm.external.kubernetes;

import org.apache.linkis.manager.common.entity.resource.ResourceType;
import org.apache.linkis.manager.rm.external.domain.ExternalResourceIdentifier;

public class KubernetesResourceIdentifier implements ExternalResourceIdentifier {
@Override
public ResourceType getResourceType() {
return ResourceType.Kubernetes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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.linkis.manager.rm.external.kubernetes;

import org.apache.linkis.manager.common.entity.resource.*;
import org.apache.linkis.manager.rm.external.domain.ExternalAppInfo;
import org.apache.linkis.manager.rm.external.domain.ExternalResourceIdentifier;
import org.apache.linkis.manager.rm.external.domain.ExternalResourceProvider;
import org.apache.linkis.manager.rm.external.request.ExternalResourceRequester;

import java.util.*;

import io.fabric8.kubernetes.api.model.Node;
import io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class KubernetesResourceRequester implements ExternalResourceRequester {
private static final Logger logger = LoggerFactory.getLogger(KubernetesResourceRequester.class);
private ExternalResourceProvider provider = null;
private String k8sMasterUrl;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to support different namespaces and different queues?

private String k8sClientCertData;
private String k8sClientKeyData;
private String k8sCaCertData;

@Override
public NodeResource requestResourceInfo(
ExternalResourceIdentifier identifier, ExternalResourceProvider provider) {
k8sMasterUrl = (String) provider.getConfigMap().get("k8sMasterUrl");
k8sClientCertData = (String) provider.getConfigMap().get("k8sClientCertData");
k8sClientKeyData = (String) provider.getConfigMap().get("k8sClientKeyData");
k8sCaCertData = (String) provider.getConfigMap().get("k8sCaCertData");
this.provider = provider;
DefaultKubernetesClient client =
new DefaultKubernetesClient(
new ConfigBuilder()
.withMasterUrl(k8sMasterUrl)
.withClientCertData(k8sClientCertData)
.withClientKeyData(k8sClientKeyData)
.withCaCertData(k8sCaCertData)
.build());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if DefaultKubernetesClient needs to be initialized every time? Does creating a client for each requestResourceInfo affect the K8s cluster?

long usedMemory = 0;
long allocatableMemory = 0;
long usedCPU = 0;
long allocatableCPU = 0;
for (NodeMetrics item : client.top().nodes().metrics().getItems()) {
usedMemory += Long.parseLong(item.getUsage().get("memory").getAmount());
usedCPU += Long.parseLong(item.getUsage().get("cpu").getAmount()) / 1000000L;
}
for (Node item : client.nodes().list().getItems()) {
allocatableMemory +=
Long.parseLong(item.getStatus().getAllocatable().get("memory").getAmount());
allocatableCPU += Long.parseLong(item.getStatus().getAllocatable().get("cpu").getAmount());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether the reference usedCPU is correct?

}
logger.info(
"usedMemory: {}, usedCPU: {}, allocatableMemory: {}, allocatableCPU: {}",
usedMemory,
usedCPU,
allocatableMemory,
allocatableCPU);
CommonNodeResource nodeResource = new CommonNodeResource();
nodeResource.setMaxResource(
new KubernetesResource(allocatableMemory * 1024L, allocatableCPU * 1000L));
nodeResource.setUsedResource(new KubernetesResource(usedMemory * 1024L, usedCPU));
client.close();
return nodeResource;
}

@Override
public List<ExternalAppInfo> requestAppInfo(
ExternalResourceIdentifier identifier, ExternalResourceProvider provider) {
// TODO
return null;
}

@Override
public ResourceType getResourceType() {
return ResourceType.Kubernetes;
}

@Override
public Boolean reloadExternalResourceAddress(ExternalResourceProvider provider) {
if (null == provider) {
k8sMasterUrl = null;
k8sClientCertData = null;
k8sClientKeyData = null;
k8sCaCertData = null;
} else {
k8sMasterUrl = (String) provider.getConfigMap().get("k8sMasterUrl");
k8sClientCertData = (String) provider.getConfigMap().get("k8sClientCertData");
k8sClientKeyData = (String) provider.getConfigMap().get("k8sClientKeyData");
k8sCaCertData = (String) provider.getConfigMap().get("k8sCaCertData");
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.linkis.manager.rm.external.parser;

import org.apache.linkis.manager.common.entity.resource.ResourceType;
import org.apache.linkis.manager.rm.external.domain.ExternalResourceIdentifier;
import org.apache.linkis.manager.rm.external.kubernetes.KubernetesResourceIdentifier;

import java.util.Map;

public class KubernetesResourceIdentifierParser implements ExternalResourceIdentifierParser {

@Override
public ExternalResourceIdentifier parse(Map<String, Object> identifierMap) {
return new KubernetesResourceIdentifier();
}

@Override
public ResourceType getResourceType() {
return ResourceType.Kubernetes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import org.apache.linkis.manager.rm.external.domain.ExternalAppInfo;
import org.apache.linkis.manager.rm.external.domain.ExternalResourceIdentifier;
import org.apache.linkis.manager.rm.external.domain.ExternalResourceProvider;
import org.apache.linkis.manager.rm.external.kubernetes.KubernetesResourceRequester;
import org.apache.linkis.manager.rm.external.parser.ExternalResourceIdentifierParser;
import org.apache.linkis.manager.rm.external.parser.KubernetesResourceIdentifierParser;
import org.apache.linkis.manager.rm.external.parser.YarnResourceIdentifierParser;
import org.apache.linkis.manager.rm.external.request.ExternalResourceRequester;
import org.apache.linkis.manager.rm.external.service.ExternalResourceService;
Expand Down Expand Up @@ -87,9 +89,15 @@ public List<ExternalResourceProvider> load(String resourceType) {

@Override
public void afterPropertiesSet() throws Exception {
resourceRequesters = new ExternalResourceRequester[] {new YarnResourceRequester()};

identifierParsers = new ExternalResourceIdentifierParser[] {new YarnResourceIdentifierParser()};
resourceRequesters =
new ExternalResourceRequester[] {
new YarnResourceRequester(), new KubernetesResourceRequester()
};

identifierParsers =
new ExternalResourceIdentifierParser[] {
new YarnResourceIdentifierParser(), new KubernetesResourceIdentifierParser()
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public void afterPropertiesSet() throws Exception {
requestResourceServices =
new RequestResourceService[] {
new DefaultReqResourceService(labelResourceService),
new DriverAndYarnReqResourceService(labelResourceService, externalResourceService)
new DriverAndYarnReqResourceService(labelResourceService, externalResourceService),
new DriverAndKubernetesReqResourceService(labelResourceService, externalResourceService)
};

// submit force release timeout lock job
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.linkis.manager.rm.service.impl;

import org.apache.linkis.manager.common.entity.resource.*;
import org.apache.linkis.manager.common.exception.RMWarnException;
import org.apache.linkis.manager.rm.domain.RMLabelContainer;
import org.apache.linkis.manager.rm.external.kubernetes.KubernetesResourceIdentifier;
import org.apache.linkis.manager.rm.external.service.ExternalResourceService;
import org.apache.linkis.manager.rm.service.LabelResourceService;
import org.apache.linkis.manager.rm.service.RequestResourceService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.linkis.manager.common.entity.resource.ResourceType.DriverAndKubernetes;

public class DriverAndKubernetesReqResourceService extends RequestResourceService {
private static final Logger logger =
LoggerFactory.getLogger(DriverAndKubernetesReqResourceService.class);
private LabelResourceService labelResourceService;
private ExternalResourceService externalResourceService;
private final ResourceType resourceType = DriverAndKubernetes;

public DriverAndKubernetesReqResourceService(
LabelResourceService labelResourceService, ExternalResourceService externalResourceService) {
super(labelResourceService);
this.labelResourceService = labelResourceService;
this.externalResourceService = externalResourceService;
}

@Override
public ResourceType resourceType() {
return this.resourceType;
}

@Override
public boolean canRequest(RMLabelContainer labelContainer, NodeResource resource)
throws RMWarnException {
// if (!super.canRequest(labelContainer, resource)) {
// return false;
// }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the configuration resource not validated?

DriverAndKubernetesResource requestedDriverAndKubernetesResource =
(DriverAndKubernetesResource) resource.getMaxResource();
KubernetesResource requestedKubernetesResource =
requestedDriverAndKubernetesResource.getKubernetesResource();

KubernetesResourceIdentifier kubernetesResourceIdentifier = new KubernetesResourceIdentifier();
NodeResource providedKubernetesResource =
externalResourceService.getResource(
ResourceType.Kubernetes, labelContainer, kubernetesResourceIdentifier);
Resource maxCapacity = providedKubernetesResource.getMaxResource();
Resource usedCapacity = providedKubernetesResource.getUsedResource();

logger.info(
"Kubernetes resource: used resource:" + usedCapacity + " and max resource: " + maxCapacity);
Resource leftResource = maxCapacity.minus(usedCapacity);
logger.info(
"Kubernetes resource: left "
+ leftResource
+ ", this request requires: "
+ requestedKubernetesResource);
if (leftResource.less(requestedKubernetesResource)) {
logger.info(
"user: "
+ labelContainer.getUserCreatorLabel().getUser()
+ " request kubernetes resource "
+ requestedKubernetesResource
+ " > left resource "
+ leftResource);
// Pair<Integer, String> notEnoughMessage =
// generateQueueNotEnoughMessage(requestedYarnResource, queueLeftResource,
// maxCapacity);
// throw new RMWarnException(notEnoughMessage.getKey(), notEnoughMessage.getValue());
return false;
} else {
return true;
}
}
}
Loading