Skip to content

Commit

Permalink
Merge pull request #59 from st-tech/patch/k8s-requestconfig
Browse files Browse the repository at this point in the history
patch k8s requestconfig
  • Loading branch information
hnarimiya authored Jan 19, 2022
2 parents f0db989 + 805d7c7 commit 46809cf
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,50 @@ public static KubernetesClientConfig create(final Optional<String> name,
final Config systemConfig,
final Config requestConfig)
{
if (requestConfig.has("kubernetes")) {
// from task request config
return KubernetesClientConfig.createFromTaskRequestConfig(name, requestConfig.getNested("kubernetes"));
}
else {
// from system config
return KubernetesClientConfig.createFromSystemConfig(name, systemConfig);

String clusterName = null;
if (name.isPresent()) {
clusterName = name.get();
}
}

private static KubernetesClientConfig createFromTaskRequestConfig(final Optional<String> name,
final Config config)
{
// TODO
// We'd better to customize cluster config by task request config??
throw new UnsupportedOperationException("Not support yet");
}
Config extractedSystemConfig = null;
if (systemConfig != null && systemConfig.get("agent.command_executor.type", String.class, "").equals("kubernetes")) {
if (clusterName == null) {
clusterName = systemConfig.get(KUBERNETES_CLIENT_PARAMS_PREFIX + "name", String.class); // ConfigException
}
final String keyPrefix = KUBERNETES_CLIENT_PARAMS_PREFIX + clusterName + ".";
extractedSystemConfig = StorageManager.extractKeyPrefix(systemConfig, keyPrefix);
}

@VisibleForTesting
static KubernetesClientConfig createFromSystemConfig(final Optional<String> name,
final io.digdag.client.config.Config systemConfig)
{
final String clusterName;
if (!name.isPresent()) {
if (!systemConfig.get("agent.command_executor.type", String.class, "").equals("kubernetes")) {
throw new ConfigException("agent.command_executor.type: is not 'kubernetes'");
Config extractedRequestConfig = null;
if (requestConfig != null && requestConfig.has("kubernetes")) {
if (clusterName == null) {
clusterName = requestConfig.get("name", String.class); // ConfigException
}
clusterName = systemConfig.get(KUBERNETES_CLIENT_PARAMS_PREFIX + "name", String.class);
extractedRequestConfig = requestConfig.getNested("kubernetes");
}
else {
clusterName = name.get();

// Create a config that merges RequestConfig with SystemConfig
final Config config;
if (extractedSystemConfig != null && extractedRequestConfig != null) {
config = extractedSystemConfig.merge(extractedRequestConfig);

} else if (extractedRequestConfig != null) {
config = extractedRequestConfig;

} else if (extractedSystemConfig != null) {
config = extractedSystemConfig;

} else {
throw new ConfigException("systemConfig and requestConfig does not exist");
}
final String keyPrefix = KUBERNETES_CLIENT_PARAMS_PREFIX + clusterName + ".";
final Config extracted = StorageManager.extractKeyPrefix(systemConfig, keyPrefix);
if (extracted.has("kube_config_path")) {
String kubeConfigPath = extracted.get("kube_config_path", String.class);

return KubernetesClientConfig.createKubeConfig(clusterName, config);
}

private static KubernetesClientConfig createKubeConfig(final String clusterName, final Config config){
if (config.has("kube_config_path")) {
String kubeConfigPath = config.get("kube_config_path", String.class);
io.fabric8.kubernetes.client.Config validatedKubeConfig;
validatedKubeConfig = validateKubeConfig(getKubeConfigFromPath(kubeConfigPath));
return create(clusterName,
Expand All @@ -63,7 +71,7 @@ static KubernetesClientConfig createFromSystemConfig(final Optional<String> name
validatedKubeConfig.getOauthToken(),
validatedKubeConfig.getNamespace());
} else {
final Config validatedConfig = validateConfig(extracted);
final Config validatedConfig = validateConfig(config);
return create(clusterName,
validatedConfig.get("master", String.class),
validatedConfig.get("certs_ca_data", String.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ public void testCreateFromSystemConfig()
.set(KUBERNETES_CLIENT_PARAMS_PREFIX+"test.oauth_token", "test=")
.set(KUBERNETES_CLIENT_PARAMS_PREFIX+"test.namespace", "default");

KubernetesClientConfig kubernetesClientConfig = KubernetesClientConfig.createFromSystemConfig(clusterName, systemConfig);
final Config kubernetesConfig = cf.create()
.set("test.master", "https://127.0.0.1")
.set("test.certs_ca_data", "test=")
.set("test.oauth_token", "test=")
.set("test.namespace", "default");

final Config requestConfig = cf.create()
.setNested("kubernetes", kubernetesConfig);

KubernetesClientConfig kubernetesClientConfig = KubernetesClientConfig.create(clusterName, systemConfig, null);

String masterUrl = "https://127.0.0.1";
String namespace = "default";
Expand All @@ -79,7 +88,32 @@ public void testCreateFromSystemConfigWithKubeConfig()
.set("agent.command_executor.type", "kubernetes")
.set(KUBERNETES_CLIENT_PARAMS_PREFIX+"test.kube_config_path", kubeConfigPath);

KubernetesClientConfig kubernetesClientConfig = KubernetesClientConfig.createFromSystemConfig(clusterName, systemConfig);
KubernetesClientConfig kubernetesClientConfig = KubernetesClientConfig.create(clusterName, systemConfig, null);

String masterUrl = "https://127.0.0.1";
String namespace = "default";
String caCertData = "test=";
String oauthToken = "test=";
assertThat(masterUrl, is(kubernetesClientConfig.getMaster()));
assertThat(caCertData, is(kubernetesClientConfig.getCertsCaData()));
assertThat(oauthToken, is(kubernetesClientConfig.getOauthToken()));
assertThat(namespace, is(kubernetesClientConfig.getNamespace()));
}

@Test
public void testCreateFromRequestConfig()
throws Exception
{
final Config kubernetesConfig = cf.create()
.set("master", "https://127.0.0.1")
.set("certs_ca_data", "test=")
.set("oauth_token", "test=")
.set("namespace", "default");

final Config requestConfig = cf.create()
.setNested("kubernetes", kubernetesConfig);

KubernetesClientConfig kubernetesClientConfig = KubernetesClientConfig.create(clusterName, null, requestConfig);

String masterUrl = "https://127.0.0.1";
String namespace = "default";
Expand All @@ -90,4 +124,38 @@ public void testCreateFromSystemConfigWithKubeConfig()
assertThat(oauthToken, is(kubernetesClientConfig.getOauthToken()));
assertThat(namespace, is(kubernetesClientConfig.getNamespace()));
}

@Test
public void testCreateFromRequestConfigAndSystemConfigMerge()
throws Exception
{

final Config systemConfig = cf.create()
.set("agent.command_executor.type", "kubernetes")
.set(KUBERNETES_CLIENT_PARAMS_PREFIX+"test.master", "https://127.0.0.1")
.set(KUBERNETES_CLIENT_PARAMS_PREFIX+"test.certs_ca_data", "test=")
.set(KUBERNETES_CLIENT_PARAMS_PREFIX+"test.oauth_token", "test=")
.set(KUBERNETES_CLIENT_PARAMS_PREFIX+"test.namespace", "default");

final Config kubernetesConfig = cf.create()
.set("master", "https://localhost")
// .set("certs_ca_data", "test=")
// .set("oauth_token", "test=")
.set("namespace", "request");

final Config requestConfig = cf.create()
.setNested("kubernetes", kubernetesConfig);

KubernetesClientConfig kubernetesClientConfig = KubernetesClientConfig.create(clusterName, systemConfig, requestConfig);

String masterUrl = "https://localhost";
String namespace = "request";
String caCertData = "test=";
String oauthToken = "test=";
assertThat(masterUrl, is(kubernetesClientConfig.getMaster()));
assertThat(caCertData, is(kubernetesClientConfig.getCertsCaData()));
assertThat(oauthToken, is(kubernetesClientConfig.getOauthToken()));
assertThat(namespace, is(kubernetesClientConfig.getNamespace()));
}

}

0 comments on commit 46809cf

Please sign in to comment.