Skip to content

Commit

Permalink
Add ability to configure plugin via environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Phoenix616 committed Mar 12, 2024
1 parent fe164ed commit 1bbaf29
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ redis:
password: "password"
```
Alternatively you can use environment variables to configure Kuvel. The environment variable will override
the config.yml and are `KUVEL_NAMESPACE`, `KUVEL_REDIS_GROUPNAME`, `KUVEL_REDIS_CONNECTION_HOSTNAME`,
`KUVEL_REDIS_CONNECTION_PORT`, `KUVEL_REDIS_CONNECTION_USERNAME`, and `KUVEL_REDIS_CONNECTION_PASSWORD`.

In order for Kuvel to monitor the server, you must request permission from Kubernetes to allow
Velocity pods discovery Minecraft servers. For Velocity pods, please allow get/list/watch to Pods
and ReplicaSets.
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/net/azisaba/kuvel/config/KuvelConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Nullable;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand All @@ -25,12 +26,23 @@ public void load() throws IOException {
VelocityConfigLoader conf = VelocityConfigLoader.load(new File(plugin.getDataDirectory(), CONFIG_FILE_NAME));
conf.saveDefaultConfig();

namespace = conf.getString("namespace", null);
Map<String, String> env = System.getenv();

String hostname = conf.getString("redis.connection.hostname");
namespace = env.getOrDefault("KUVEL_NAMESPACE", conf.getString("namespace", null));

String hostname = env.getOrDefault("KUVEL_REDIS_CONNECTION_HOSTNAME", conf.getString("redis.connection.hostname"));
int port = conf.getInt("redis.connection.port", -1);
String username = conf.getString("redis.connection.username");
String password = conf.getString("redis.connection.password");
if (env.containsKey("KUVEL_REDIS_CONNECTION_PORT")) {
try {
port = Integer.parseInt(env.get("KUVEL_REDIS_CONNECTION_PORT"));
} catch (NumberFormatException e) {
plugin
.getLogger()
.warning("Invalid port number for Redis connection specified in KUVEL_REDIS_CONNECTION_PORT environment variable. Using port " + port + " from config.yml.");
}
}
String username = env.getOrDefault("KUVEL_REDIS_CONNECTION_USERNAME", conf.getString("redis.connection.username"));
String password = env.getOrDefault("KUVEL_REDIS_CONNECTION_PASSWORD", conf.getString("redis.connection.password"));

if (hostname == null || port <= 0) {
redisEnabled = false;
Expand All @@ -42,6 +54,6 @@ public void load() throws IOException {
redisConnectionData = new RedisConnectionData(hostname, port, username, password);
}

proxyGroupName = conf.getString("redis.group-name", null);
proxyGroupName = env.getOrDefault("KUVEL_REDIS_GROUPNAME", conf.getString("redis.group-name", null));
}
}

0 comments on commit 1bbaf29

Please sign in to comment.