From 601782c5eeabdd339666824640ddd15cb462cd24 Mon Sep 17 00:00:00 2001 From: David Kornel Date: Fri, 8 Dec 2023 11:50:36 +0100 Subject: [PATCH] Cofigure kubeconfig for kubeCmdClient (#37) --- .gitignore | 3 +++ pom.xml | 10 ++++++++++ .../framework/manager/ResourceManager.java | 2 +- .../java/io/odh/test/platform/KubeClient.java | 18 ++++++++++++++++++ .../platform/cmdClient/BaseCmdKubeClient.java | 7 ++++--- 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 50b7c0df..90f2f99e 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,6 @@ build/ ### Mac OS ### .DS_Store + +## Suite specific files +**/*.kubeconfig diff --git a/pom.xml b/pom.xml index a51c1bfe..0177966d 100644 --- a/pom.xml +++ b/pom.xml @@ -323,6 +323,16 @@ + + smoke + + false + + smoke + + + + upgrade diff --git a/src/main/java/io/odh/test/framework/manager/ResourceManager.java b/src/main/java/io/odh/test/framework/manager/ResourceManager.java index 5f340b78..52b2a105 100644 --- a/src/main/java/io/odh/test/framework/manager/ResourceManager.java +++ b/src/main/java/io/odh/test/framework/manager/ResourceManager.java @@ -48,7 +48,7 @@ public static synchronized ResourceManager getInstance() { if (instance == null) { instance = new ResourceManager(); client = new KubeClient(TestConstants.DEFAULT_NAMESPACE); - kubeCmdClient = new Oc(); + kubeCmdClient = new Oc(client.getKubeconfigPath()); } return instance; } diff --git a/src/main/java/io/odh/test/platform/KubeClient.java b/src/main/java/io/odh/test/platform/KubeClient.java index 80f26cde..93e54302 100644 --- a/src/main/java/io/odh/test/platform/KubeClient.java +++ b/src/main/java/io/odh/test/platform/KubeClient.java @@ -27,6 +27,7 @@ import io.fabric8.openshift.api.model.operatorhub.v1alpha1.InstallPlanBuilder; import io.fabric8.openshift.client.OpenShiftClient; import io.odh.test.Environment; +import io.odh.test.platform.executor.Exec; import io.opendatahub.datasciencecluster.v1.DataScienceCluster; import io.opendatahub.v1alpha.OdhDashboardConfig; import org.kubeflow.v1.Notebook; @@ -35,6 +36,7 @@ import java.io.IOException; import java.io.InputStream; +import java.util.Arrays; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; @@ -42,6 +44,7 @@ public class KubeClient { protected final KubernetesClient client; protected String namespace; + private String kubeconfigPath; private static final Logger LOGGER = LoggerFactory.getLogger(KubeClient.class); @@ -92,6 +95,12 @@ private Config getConfig() { if (Environment.KUBE_USERNAME != null && Environment.KUBE_PASSWORD != null && Environment.KUBE_URL != null) { + Exec.exec(Arrays.asList("oc", "login", "-u", Environment.KUBE_USERNAME, + "-p", Environment.KUBE_PASSWORD, + "--insecure-skip-tls-verify", + "--kubeconfig", Environment.USER_PATH + "/test.kubeconfig", + Environment.KUBE_URL)); + kubeconfigPath = Environment.USER_PATH + "/test.kubeconfig"; return new ConfigBuilder() .withUsername(Environment.KUBE_USERNAME) .withPassword(Environment.KUBE_PASSWORD) @@ -101,6 +110,11 @@ private Config getConfig() { .build(); } else if (Environment.KUBE_URL != null && Environment.KUBE_TOKEN != null) { + Exec.exec(Arrays.asList("oc", "login", "--token", Environment.KUBE_TOKEN, + "--insecure-skip-tls-verify", + "--kubeconfig", Environment.USER_PATH + "/test.kubeconfig", + Environment.KUBE_URL)); + kubeconfigPath = Environment.USER_PATH + "/test.kubeconfig"; return new ConfigBuilder() .withOauthToken(Environment.KUBE_TOKEN) .withMasterUrl(Environment.KUBE_URL) @@ -122,6 +136,10 @@ public boolean namespaceExists(String namespace) { .toList().contains(namespace); } + public String getKubeconfigPath() { + return kubeconfigPath; + } + // ================================================== // ---------> Create/read multi-resoruces <--------- // ================================================== diff --git a/src/main/java/io/odh/test/platform/cmdClient/BaseCmdKubeClient.java b/src/main/java/io/odh/test/platform/cmdClient/BaseCmdKubeClient.java index 456f74be..ab27754b 100644 --- a/src/main/java/io/odh/test/platform/cmdClient/BaseCmdKubeClient.java +++ b/src/main/java/io/odh/test/platform/cmdClient/BaseCmdKubeClient.java @@ -68,7 +68,7 @@ protected Context defaultContext() { } // Admin context is not implemented now, because it's not needed - // In case it will be neded in future, we should change the kubeconfig and apply it for both oc and kubectl + // In case it will be needed in the future, we should change the kubeconfig and apply it for both oc and kubectl protected Context adminContext() { return defaultContext(); } @@ -288,7 +288,8 @@ public String toString() { @Override public List list(String resourceType) { - return asList(Exec.exec(namespacedCommand("get", resourceType, "-o", "jsonpath={range .items[*]}{.metadata.name} ")).out().trim().split(" +")).stream().filter(s -> !s.trim().isEmpty()).collect(Collectors.toList()); + return Arrays.stream(Exec.exec(namespacedCommand("get", resourceType, "-o", "jsonpath={range .items[*]}{.metadata.name} ")).out().trim().split(" +")) + .filter(s -> !s.trim().isEmpty()).collect(Collectors.toList()); } @Override @@ -388,7 +389,7 @@ private List command(List rest, boolean namespaced) { @SuppressWarnings("unchecked") public K process(Map parameters, String file, Consumer c) { List command = command(asList(PROCESS, "-f", file), false); - command.addAll(parameters.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.toList())); + command.addAll(parameters.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).toList()); ExecResult exec = Exec.builder() .throwErrors(true) .withCommand(command)