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

Retrieve default namespace from file serviceaccount/namespace #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import io.vertx.config.spi.ConfigStore;
import io.vertx.config.spi.utils.JsonObjectHelper;
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
Expand All @@ -44,30 +45,21 @@ public class ConfigMapStore implements ConfigStore {
private static final Base64.Decoder DECODER = Base64.getDecoder();
private final VertxInternal vertx;
private final JsonObject configuration;
private final String namespace;
private final String name;
private final String key;
private final boolean secret;
private final boolean optional;

private final WebClient client;
private String namespace;
private String token;


public ConfigMapStore(Vertx vertx, JsonObject configuration) {
this.vertx = (VertxInternal) vertx;
this.configuration = configuration;

String ns = configuration.getString("namespace");
if (ns == null) {
if (KUBERNETES_NAMESPACE != null) {
ns = KUBERNETES_NAMESPACE;
} else {
ns = "default";
}
}
this.optional = configuration.getBoolean("optional", true);
this.namespace = ns;
this.name = configuration.getString("name");
this.key = configuration.getString("key");
this.secret = configuration.getBoolean("secret", false);
Expand Down Expand Up @@ -127,6 +119,30 @@ private Future<String> getToken() {
});
}

private Future<String> getNamespace() {
String namespace = configuration.getString("namespace");
if (namespace != null && !namespace.trim().isEmpty()) {
this.namespace = namespace;
return vertx.getOrCreateContext().succeededFuture(namespace);
}

if (KUBERNETES_NAMESPACE != null) {
this.namespace = KUBERNETES_NAMESPACE;
return vertx.getOrCreateContext().succeededFuture(namespace);
}

// Read from file
return vertx.fileSystem().readFile(KubernetesUtils.OPENSHIFT_KUBERNETES_NAMESPACE_FILE)
.recover(throwable -> optional ? Future.succeededFuture(Buffer.buffer()) : Future.failedFuture(throwable))
.map(Buffer::toString)
.onSuccess(ns -> {
this.namespace = ns;
})
.onFailure(t->{
this.namespace = "default";
});
}

@Override
public Future<Buffer> get() {
Future<String> retrieveToken;
Expand All @@ -136,34 +152,45 @@ public Future<Buffer> get() {
retrieveToken = vertx.getOrCreateContext().succeededFuture(token);
}

return retrieveToken.flatMap(token -> {
if (token.isEmpty()) {
return Future.succeededFuture(Buffer.buffer("{}"));
}
Future<String> retrieveNamespace;
if (namespace == null) {
retrieveNamespace = getNamespace();
} else {
retrieveNamespace = vertx.getOrCreateContext().succeededFuture(namespace);
}

String path = "/api/v1/namespaces/" + namespace;
if (secret) {
path += "/secrets/" + name;
} else {
path += "/configmaps/" + name;
}
return CompositeFuture.all(retrieveToken, retrieveNamespace).flatMap(compFut->{
String token = compFut.resultAt(0);
String namespace = compFut.resultAt(1);

return client.get(path)
.putHeader("Authorization", "Bearer " + token)
.send()
.flatMap(response -> {
if (response.statusCode() == 404) {
return handle404();
}
if (response.statusCode() == 403) {
return handle403();
}
if (response.statusCode() != 200) {
return handleOtherErrors(response);
}
return handle200(response);
});
});
if (token.isEmpty()) {
return Future.succeededFuture(Buffer.buffer("{}"));
}

String path = "/api/v1/namespaces/" + namespace;
if (secret) {
path += "/secrets/" + name;
} else {
path += "/configmaps/" + name;
}

return client.get(path)
.putHeader("Authorization", "Bearer " + token)
.send()
.flatMap(response -> {
if (response.statusCode() == 404) {
return handle404();
}
if (response.statusCode() == 403) {
return handle403();
}
if (response.statusCode() != 200) {
return handleOtherErrors(response);
}
return handle200(response);
});

});
}

private Future<Buffer> handle404() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ public class KubernetesUtils {

public static final String OPENSHIFT_KUBERNETES_TOKEN_FILE = "/var/run/secrets/kubernetes.io/serviceaccount/token";

}
public static final String OPENSHIFT_KUBERNETES_NAMESPACE_FILE = "/var/run/secrets/kubernetes.io/serviceaccount/namespace";

}