Skip to content

Commit

Permalink
Create local kubeconfig also for kubectl
Browse files Browse the repository at this point in the history
Signed-off-by: David Kornel <[email protected]>
  • Loading branch information
kornys committed Feb 27, 2024
1 parent 3cf3a14 commit 494e008
Showing 1 changed file with 72 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.openshift.client.OpenShiftClient;
import io.skodjob.testframe.TestFrameConstants;
import io.skodjob.testframe.TestFrameEnv;
import io.skodjob.testframe.executor.Exec;

Expand Down Expand Up @@ -43,7 +44,7 @@ private Config getConfig() {
if (TestFrameEnv.KUBE_USERNAME != null
&& TestFrameEnv.KUBE_PASSWORD != null
&& TestFrameEnv.KUBE_URL != null) {
kubeconfigPath = createLocalKubeconfig(TestFrameEnv.KUBE_USERNAME, TestFrameEnv.KUBE_PASSWORD, TestFrameEnv.KUBE_URL);
kubeconfigPath = createLocalKubeconfig();
return new ConfigBuilder()
.withUsername(TestFrameEnv.KUBE_USERNAME)
.withPassword(TestFrameEnv.KUBE_PASSWORD)
Expand All @@ -53,7 +54,7 @@ private Config getConfig() {
.build();
} else if (TestFrameEnv.KUBE_URL != null
&& TestFrameEnv.KUBE_TOKEN != null) {
kubeconfigPath = createLocalKubeconfig(TestFrameEnv.KUBE_TOKEN, TestFrameEnv.KUBE_URL);
kubeconfigPath = createLocalKubeconfig();
return new ConfigBuilder()
.withOauthToken(TestFrameEnv.KUBE_TOKEN)
.withMasterUrl(TestFrameEnv.KUBE_URL)
Expand All @@ -66,28 +67,81 @@ private Config getConfig() {
}
}

private String createLocalKubeconfig(String username, String password, String apiUrl) {
private String createLocalKubeconfig() {
try {
Exec.exec(null, Arrays.asList("oc", "login", "-u", username,
"-p", password,
"--insecure-skip-tls-verify",
"--kubeconfig", TestFrameEnv.USER_PATH + "/test.kubeconfig",
apiUrl), 0, false, true);
if (TestFrameEnv.CLIENT_TYPE.equals(TestFrameConstants.OPENSHIFT_CLIENT)) {
if (TestFrameEnv.KUBE_URL != null && TestFrameEnv.KUBE_TOKEN != null) {
createLocalOcKubeconfig(TestFrameEnv.KUBE_TOKEN, TestFrameEnv.KUBE_URL);
} else {
createLocalOcKubeconfig(TestFrameEnv.KUBE_USERNAME, TestFrameEnv.KUBE_PASSWORD, TestFrameEnv.KUBE_URL);
}
} else {
if (TestFrameEnv.KUBE_URL != null && TestFrameEnv.KUBE_TOKEN != null) {
createLocalKubectlContext(TestFrameEnv.KUBE_TOKEN, TestFrameEnv.KUBE_URL);
} else {
createLocalKubectlContext(TestFrameEnv.KUBE_USERNAME, TestFrameEnv.KUBE_PASSWORD, TestFrameEnv.KUBE_URL);
}
}
return TestFrameEnv.USER_PATH + "/test.kubeconfig";
} catch (Exception ex) {
return null;
}
}

private String createLocalKubeconfig(String token, String apiUrl) {
try {
Exec.exec(null, Arrays.asList("oc", "login", "--token", token,
"--insecure-skip-tls-verify",
"--kubeconfig", TestFrameEnv.USER_PATH + "/test.kubeconfig",
apiUrl), 0, false, true);
return TestFrameEnv.USER_PATH + "/test.kubeconfig";
} catch (Exception ex) {
return null;
}
private void createLocalOcKubeconfig(String username, String password, String apiUrl) {
Exec.exec(null, Arrays.asList("oc", "login",
"-u", username,
"-p", password,
"--insecure-skip-tls-verify",
"--kubeconfig", TestFrameEnv.USER_PATH + "/test.kubeconfig",
apiUrl), 0, false, true);
}

private void createLocalOcKubeconfig(String token, String apiUrl) {
Exec.exec(null, Arrays.asList("oc", "login", "--token", token,
"--insecure-skip-tls-verify",
"--kubeconfig", TestFrameEnv.USER_PATH + "/test.kubeconfig",
apiUrl), 0, false, true);
}

private void createLocalKubectlContext(String username, String password, String apiUrl) {

Exec.exec(null, Arrays.asList("kubectl", "config",
"set-credentials", "test-user",
"--username", username,
"--password", password,
"--kubeconfig", TestFrameEnv.USER_PATH + "/test.kubeconfig"),
0, false, true);
buildKubectlContext(apiUrl);

}

private void createLocalKubectlContext(String token, String apiUrl) {
Exec.exec(null, Arrays.asList("kubectl", "config",
"set-credentials", "test-user",
"--token", token,
"--kubeconfig", TestFrameEnv.USER_PATH + "/test.kubeconfig"),
0, false, true);
buildKubectlContext(apiUrl);

}

private void buildKubectlContext(String apiUrl) {
Exec.exec(null, Arrays.asList("kubectl", "config",
"set-cluster", "test-cluster",
"--insecure-skip-tls-verify=true", "--server", apiUrl,
"--kubeconfig", TestFrameEnv.USER_PATH + "/test.kubeconfig"),
0, false, true);
Exec.exec(null, Arrays.asList("kubectl", "config",
"set-context", "test-context",
"--user", "test-user",
"--cluster", "test-cluster",
"--namespace", "default",
"--kubeconfig", TestFrameEnv.USER_PATH + "/test.kubeconfig"),
0, false, true);
Exec.exec(null, Arrays.asList("kubectl", "config",
"use-context", "test-context",
"--kubeconfig", TestFrameEnv.USER_PATH + "/test.kubeconfig"),
0, false, true);
}
}

0 comments on commit 494e008

Please sign in to comment.