You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@submarine.apache.org by pi...@apache.org on 2022/02/13 23:41:39 UTC
[submarine] branch master updated: SUBMARINE-1184. Get namespace via Downward API
This is an automated email from the ASF dual-hosted git repository.
pingsutw pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/submarine.git
The following commit(s) were added to refs/heads/master by this push:
new a0a9ed0 SUBMARINE-1184. Get namespace via Downward API
a0a9ed0 is described below
commit a0a9ed0743b0d42f9ae1ac4ffada6cdba013573b
Author: MortalHappiness <b0...@ntu.edu.tw>
AuthorDate: Tue Jan 25 18:16:02 2022 +0800
SUBMARINE-1184. Get namespace via Downward API
### What is this PR for?
Get the current namespace with the downward API.
In the beginning, I tried to get the current namespace by reading the file `/var/run/secrets/kubernetes.io/serviceaccount/namespace`. However, not all Kubernetes environments have this file. Therefore I decided to use the downward API to get the current namespace.
References:
+ https://stackoverflow.com/questions/31557932/how-to-get-the-namespace-from-inside-a-pod-in-openshift
+ https://github.com/kubernetes/kubernetes/blob/release-1.0/docs/user-guide/downward-api.md
### What type of PR is it?
[Improvement]
### Todos
### What is the Jira issue?
https://issues.apache.org/jira/projects/SUBMARINE/issues/SUBMARINE-1184
### How should this be tested?
### Screenshots (if appropriate)
### Questions:
* Do the license files need updating? No
* Are there breaking changes for older versions? No
* Does this need new documentation? No
Author: MortalHappiness <b0...@ntu.edu.tw>
Signed-off-by: Kevin <pi...@apache.org>
Closes #875 from MortalHappiness/SUBMARINE-1184 and squashes the following commits:
ccd1b353 [MortalHappiness] Use downward API instead of reading /var/run/secrets/kubernetes.io/serviceaccount/namespace to get the namespace
7e1b778b [MortalHappiness] SUBMARINE-1184. Get namespace via /var/run/secrets/kubernetes.io/serviceaccount/namespace
---
.../pkg/controller/submarine_server.go | 8 +++-
submarine-server/server-api/pom.xml | 5 +++
.../submarine/server/api/spec/ExperimentMeta.java | 12 ++---
.../k8s-utils}/pom.xml | 52 +++++++++-------------
.../submarine/server/k8s/utils/K8sUtils.java | 50 +++++++++++++++++++++
.../k8s-utils/src/main/resources/log4j.properties | 25 +++++++++++
submarine-server/server-submitter/pom.xml | 3 +-
.../server-submitter/submitter-k8s/pom.xml | 6 +++
.../server/submitter/k8s/K8sSubmitter.java | 10 +----
9 files changed, 122 insertions(+), 49 deletions(-)
diff --git a/submarine-cloud-v2/pkg/controller/submarine_server.go b/submarine-cloud-v2/pkg/controller/submarine_server.go
index 702acdf..e71ee0b 100644
--- a/submarine-cloud-v2/pkg/controller/submarine_server.go
+++ b/submarine-cloud-v2/pkg/controller/submarine_server.go
@@ -65,8 +65,12 @@ func newSubmarineServerDeployment(submarine *v1alpha1.Submarine) *appsv1.Deploym
Value: serverName + "." + submarine.Namespace,
},
{
- Name: "ENV_NAMESPACE",
- Value: submarine.Namespace,
+ Name: "ENV_NAMESPACE",
+ ValueFrom: &corev1.EnvVarSource{
+ FieldRef: &corev1.ObjectFieldSelector{
+ FieldPath: "metadata.namespace",
+ },
+ },
},
{
Name: "SUBMARINE_APIVERSION",
diff --git a/submarine-server/server-api/pom.xml b/submarine-server/server-api/pom.xml
index 2ab3511..3bd6c32 100644
--- a/submarine-server/server-api/pom.xml
+++ b/submarine-server/server-api/pom.xml
@@ -37,6 +37,11 @@
<artifactId>submarine-commons-utils</artifactId>
</dependency>
<dependency>
+ <groupId>org.apache.submarine</groupId>
+ <artifactId>submarine-k8s-utils</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf-java.version}</version>
diff --git a/submarine-server/server-api/src/main/java/org/apache/submarine/server/api/spec/ExperimentMeta.java b/submarine-server/server-api/src/main/java/org/apache/submarine/server/api/spec/ExperimentMeta.java
index 1d6003a..c8dfd86 100644
--- a/submarine-server/server-api/src/main/java/org/apache/submarine/server/api/spec/ExperimentMeta.java
+++ b/submarine-server/server-api/src/main/java/org/apache/submarine/server/api/spec/ExperimentMeta.java
@@ -24,6 +24,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import org.apache.submarine.server.k8s.utils.K8sUtils;
+
/**
* ExperimentMeta is metadata that all experiments must have.
*/
@@ -33,20 +35,14 @@ public class ExperimentMeta {
private String experimentId;
private String name;
- private String namespace;
+ private final String namespace;
private String framework;
private String cmd;
private Map<String, String> envVars = new HashMap<>();
private List<String> tags = new ArrayList<>();
public ExperimentMeta() {
- namespace = "default";
- /* The environment variable "ENV_NAMESPACE" will be set by submarine-operator. Hence,
- * if the user creates Submarine with Helm, the variable "namespace" will always be "default".
- */
- if (System.getenv("ENV_NAMESPACE") != null) {
- namespace = System.getenv("ENV_NAMESPACE");
- }
+ namespace = K8sUtils.getNamespace();
}
/**
diff --git a/submarine-server/server-api/pom.xml b/submarine-server/server-submitter/k8s-utils/pom.xml
similarity index 62%
copy from submarine-server/server-api/pom.xml
copy to submarine-server/server-submitter/k8s-utils/pom.xml
index 2ab3511..be64202 100644
--- a/submarine-server/server-api/pom.xml
+++ b/submarine-server/server-submitter/k8s-utils/pom.xml
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
-<!--
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <!--
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
@@ -17,46 +20,35 @@
specific language governing permissions and limitations
under the License.
-->
-
-<project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
- <artifactId>submarine-server</artifactId>
<groupId>org.apache.submarine</groupId>
+ <artifactId>submarine-server-submitter</artifactId>
<version>0.7.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
- <artifactId>submarine-server-api</artifactId>
- <name>Submarine: Server API</name>
+ <artifactId>submarine-k8s-utils</artifactId>
+ <name>Submarine: K8S Utils</name>
<dependencies>
<dependency>
- <groupId>org.apache.submarine</groupId>
- <artifactId>submarine-commons-utils</artifactId>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>${slf4j.version}</version>
</dependency>
+
<dependency>
- <groupId>com.google.protobuf</groupId>
- <artifactId>protobuf-java</artifactId>
- <version>${protobuf-java.version}</version>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <version>${slf4j.version}</version>
</dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-checkstyle-plugin</artifactId>
- <configuration>
- <excludes>**/proto/**</excludes>
- <skip>false</skip>
- </configuration>
- </plugin>
- <plugin>
- <artifactId>maven-enforcer-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
+ <!-- Test libraries -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>${junit.version}</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
</project>
diff --git a/submarine-server/server-submitter/k8s-utils/src/main/java/org/apache/submarine/server/k8s/utils/K8sUtils.java b/submarine-server/server-submitter/k8s-utils/src/main/java/org/apache/submarine/server/k8s/utils/K8sUtils.java
new file mode 100644
index 0000000..a21f20a
--- /dev/null
+++ b/submarine-server/server-submitter/k8s-utils/src/main/java/org/apache/submarine/server/k8s/utils/K8sUtils.java
@@ -0,0 +1,50 @@
+/*
+ * 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.submarine.server.k8s.utils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility methods for common k8s operations.
+ *
+ * @author Chi-Sheng Liu
+ * @since 0.7.0-SNAPSHOT
+ */
+public abstract class K8sUtils {
+
+ private static final Logger LOG = LoggerFactory.getLogger(K8sUtils.class);
+ private static String namespace = null;
+
+ /**
+ * Get the current Kubernetes namespace.
+ * @return The current Kubernetes namespace.
+ */
+ public static String getNamespace() {
+ if (namespace == null) {
+ namespace = System.getenv("ENV_NAMESPACE");
+ if (namespace == null) {
+ namespace = "default";
+ }
+ LOG.info("Namespace: {}", namespace);
+ }
+ return namespace;
+ }
+}
diff --git a/submarine-server/server-submitter/k8s-utils/src/main/resources/log4j.properties b/submarine-server/server-submitter/k8s-utils/src/main/resources/log4j.properties
new file mode 100644
index 0000000..fdd0239
--- /dev/null
+++ b/submarine-server/server-submitter/k8s-utils/src/main/resources/log4j.properties
@@ -0,0 +1,25 @@
+#
+# 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.
+#
+
+log4j.rootLogger = info, stdout
+
+log4j.appender.stdout = org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.Target = System.out
+log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
diff --git a/submarine-server/server-submitter/pom.xml b/submarine-server/server-submitter/pom.xml
index 18bb3ee..c479a9a 100644
--- a/submarine-server/server-submitter/pom.xml
+++ b/submarine-server/server-submitter/pom.xml
@@ -32,6 +32,7 @@
<modules>
<module>submitter-k8s</module>
<module>submarine-k8s-agent</module>
+ <module>k8s-utils</module>
</modules>
-</project>
\ No newline at end of file
+</project>
diff --git a/submarine-server/server-submitter/submitter-k8s/pom.xml b/submarine-server/server-submitter/submitter-k8s/pom.xml
index a0e7e4d..cbb6282 100644
--- a/submarine-server/server-submitter/submitter-k8s/pom.xml
+++ b/submarine-server/server-submitter/submitter-k8s/pom.xml
@@ -53,6 +53,12 @@
<dependency>
<groupId>org.apache.submarine</groupId>
+ <artifactId>submarine-k8s-utils</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.submarine</groupId>
<artifactId>submarine-server-api</artifactId>
<version>${project.version}</version>
</dependency>
diff --git a/submarine-server/server-submitter/submitter-k8s/src/main/java/org/apache/submarine/server/submitter/k8s/K8sSubmitter.java b/submarine-server/server-submitter/submitter-k8s/src/main/java/org/apache/submarine/server/submitter/k8s/K8sSubmitter.java
index dc1c34f..94f7863 100644
--- a/submarine-server/server-submitter/submitter-k8s/src/main/java/org/apache/submarine/server/submitter/k8s/K8sSubmitter.java
+++ b/submarine-server/server-submitter/submitter-k8s/src/main/java/org/apache/submarine/server/submitter/k8s/K8sSubmitter.java
@@ -63,6 +63,7 @@ import org.apache.submarine.serve.istio.IstioVirtualService;
import org.apache.submarine.serve.pytorch.SeldonPytorchServing;
import org.apache.submarine.serve.seldon.SeldonDeployment;
import org.apache.submarine.serve.tensorflow.SeldonTFServing;
+import org.apache.submarine.server.k8s.utils.K8sUtils;
import org.apache.submarine.server.api.Submitter;
import org.apache.submarine.server.api.exception.InvalidSpecException;
import org.apache.submarine.server.api.experiment.Experiment;
@@ -105,9 +106,6 @@ public class K8sSubmitter implements Submitter {
private static final String TF_JOB_SELECTOR_KEY = "tf-job-name=";
private static final String PYTORCH_JOB_SELECTOR_KEY = "pytorch-job-name=";
- private static final String ENV_NAMESPACE = "ENV_NAMESPACE";
-
-
// Add an exception Consumer, handle the problem that delete operation does not have the resource
public static final Function<ApiException, Object> API_EXCEPTION_404_CONSUMER = e -> {
if (e.getCode() != 404) {
@@ -939,11 +937,7 @@ public class K8sSubmitter implements Submitter {
}
private String getServerNamespace() {
- String namespace = "default";
- if (System.getenv(ENV_NAMESPACE) != null) {
- namespace = System.getenv(ENV_NAMESPACE);
- }
- return namespace;
+ return K8sUtils.getNamespace();
}
private enum ParseOp {
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@submarine.apache.org
For additional commands, e-mail: dev-help@submarine.apache.org