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

Cofigure kubeconfig for kubeCmdClient #37

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ build/

### Mac OS ###
.DS_Store

## Suite specific files
**/*.kubeconfig
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,16 @@
</properties>
</profile>

<profile>
<id>smoke</id>
<properties>
<it.skip>false</it.skip>
<groups>
smoke
</groups>
</properties>
</profile>

<profile>
<id>upgrade</id>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/io/odh/test/platform/KubeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,13 +36,15 @@

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;

public class KubeClient {
protected final KubernetesClient client;
protected String namespace;
private String kubeconfigPath;

private static final Logger LOGGER = LoggerFactory.getLogger(KubeClient.class);

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -122,6 +136,10 @@ public boolean namespaceExists(String namespace) {
.toList().contains(namespace);
}

public String getKubeconfigPath() {
return kubeconfigPath;
}

// ==================================================
// ---------> Create/read multi-resoruces <---------
// ==================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -288,7 +288,8 @@ public String toString() {

@Override
public List<String> 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
Expand Down Expand Up @@ -388,7 +389,7 @@ private List<String> command(List<String> rest, boolean namespaced) {
@SuppressWarnings("unchecked")
public K process(Map<String, String> parameters, String file, Consumer<String> c) {
List<String> 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)
Expand Down
Loading