Skip to content

Commit

Permalink
Read user namespace from file
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <dakwon@redhat.com>
  • Loading branch information
dkwon17 committed May 25, 2022
1 parent 96c7102 commit 25fb30f
Showing 1 changed file with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
package org.eclipse.che.incubator.workspace.telemetry.finder;

import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import org.slf4j.Logger;

import javax.enterprise.context.ApplicationScoped;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;

import static org.slf4j.LoggerFactory.getLogger;
Expand All @@ -29,6 +34,8 @@ public class DevWorkspaceFinderImpl implements DevWorkspaceFinder {
private static final String GROUP = "workspace.devfile.io";
private static final String VERSION = "v1alpha2";
private static final String SCOPE = "Namespaced";
private static final String DEVWORKSPACE_NAMESPACE = "DEVWORKSPACE_NAMESPACE";
private static final String KUBERNETES_NAMESPACE_PATH = Config.KUBERNETES_NAMESPACE_PATH;

public GenericKubernetesResource findDevWorkspace(String devworkspaceId) {
CustomResourceDefinitionContext devworkspaceContext = new CustomResourceDefinitionContext.Builder()
Expand All @@ -39,22 +46,44 @@ public GenericKubernetesResource findDevWorkspace(String devworkspaceId) {
.build();

final AtomicReference<GenericKubernetesResource> devworkspace = new AtomicReference<>();
String clientNamespace = null;
String namespace = null;
try (KubernetesClient client = new DefaultKubernetesClient()) {
clientNamespace = client.getNamespace();
client.genericKubernetesResources(devworkspaceContext).inNamespace(clientNamespace).list().getItems().forEach(

namespace = System.getenv(DEVWORKSPACE_NAMESPACE);

if (namespace == null) {
namespace = getNamespaceFromServiceAccount();
}

client.genericKubernetesResources(devworkspaceContext).inNamespace(namespace).list().getItems().forEach(
dw -> {
String dwId = dw.get("status", "devworkspaceId");
if (devworkspaceId.equals(dwId)) {
devworkspace.set(dw);
}
}
);
} catch (IOException e) {
LOG.warn("Failed to read namespace in {}", KUBERNETES_NAMESPACE_PATH, e);
} catch (Exception e) {
LOG.warn("Failed to find devworkspace with id: {} in namespace: {}", devworkspaceId, clientNamespace, e);
LOG.warn("Failed to find devworkspace with id: {} in namespace: {}", devworkspaceId, namespace, e);
}

return devworkspace.get();
}

private String getNamespaceFromServiceAccount() throws IOException {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(KUBERNETES_NAMESPACE_PATH));
String namespace = br.readLine();
return namespace.strip();
} catch (IOException e) {
throw e;
} finally {
if (br != null) {
br.close();
}
}
}
}

0 comments on commit 25fb30f

Please sign in to comment.