Skip to content

Commit

Permalink
Retrieve default namespace from file /var/run/secrets/kubernetes.io/s…
Browse files Browse the repository at this point in the history
…erviceaccount/namespace
  • Loading branch information
liuchong committed Dec 29, 2022
1 parent 4e8aeaf commit 298f5e5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 37 deletions.
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,44 @@ 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);
if (token.isEmpty()) {
return Future.succeededFuture(Buffer.buffer("{}"));
}

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);
});
});
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";

}

0 comments on commit 298f5e5

Please sign in to comment.