Skip to content

Commit

Permalink
feat(config): autoConfigure supports multiple kubeconfig file paths
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa authored Nov 7, 2024
1 parent b520f0c commit cb63dc6
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand All @@ -63,7 +64,7 @@ public class Config {
private static final Logger LOGGER = LoggerFactory.getLogger(Config.class);

/**
* Disables auto-configuration based on opinionated defaults in a {@link Config} object in the all arguments constructor
* Disables autoconfiguration based on opinionated defaults in a {@link Config} object in the all arguments constructor
*/
public static final String KUBERNETES_DISABLE_AUTO_CONFIG_SYSTEM_PROPERTY = "kubernetes.disable.autoConfig";
public static final String KUBERNETES_MASTER_SYSTEM_PROPERTY = "kubernetes.master";
Expand Down Expand Up @@ -226,23 +227,6 @@ protected static boolean disableAutoConfig() {
return Utils.getSystemPropertyOrEnvVar(KUBERNETES_DISABLE_AUTO_CONFIG_SYSTEM_PROPERTY, false);
}

protected Config(boolean autoConfigure) {
this(null, null, null, null, null,
null, null, null, null, null,
null, null, null, null, null,
null, null, null, null, null,
null, null, null, null,
null,
null, null, null, null,
null, null, null,
null,
null, null, null, null, null,
null, null, null,
null, null, null,
null, null, null, null,
null, autoConfigure, true);
}

/**
* Create an empty {@link Config} class without any automatic configuration
* (i.e. reading system properties/environment variables to set values).
Expand All @@ -265,27 +249,30 @@ public static Config empty() {
}

/**
* Does auto detection with some opinionated defaults.
* Does auto-detection with some opinionated defaults.
*
* @param context if null will use current-context
* @return Config object
*/
public static Config autoConfigure(String context) {
Config config = new Config(false);
return autoConfigure(config, context);
final Config config = new Config(false);
autoConfigure(config, context);
return config;
}

private static Config autoConfigure(Config config, String context) {
final var kubeConfigFile = findKubeConfigFile();
if (kubeConfigFile != null) {
KubeConfigUtils.merge(config, context, KubeConfigUtils.parseConfig(kubeConfigFile));
private static void autoConfigure(Config config, String context) {
final var kubeConfigFiles = findKubeConfigFiles();
if (!kubeConfigFiles.isEmpty()) {
final var kubeconfigs = kubeConfigFiles.stream()
.map(KubeConfigUtils::parseConfig)
.toArray(io.fabric8.kubernetes.api.model.Config[]::new);
KubeConfigUtils.merge(config, context, kubeconfigs);
} else {
tryServiceAccount(config);
tryNamespaceFromPath(config);
}
postAutoConfigure(config);
config.autoConfigure = true;
return config;
}

private static void postAutoConfigure(Config config) {
Expand All @@ -310,6 +297,23 @@ private static String ensureHttps(String masterUrl, Config config) {
return masterUrl;
}

protected Config(boolean autoConfigure) {
this(null, null, null, null, null,
null, null, null, null, null,
null, null, null, null, null,
null, null, null, null, null,
null, null, null, null,
null,
null, null, null, null,
null, null, null,
null,
null, null, null, null, null,
null, null, null,
null, null, null,
null, null, null, null,
null, autoConfigure, true);
}

@JsonCreator
public Config(
@JsonProperty("masterUrl") String masterUrl,
Expand Down Expand Up @@ -821,6 +825,7 @@ public Config refresh() {
if (autoConfigure) {
return Config.autoConfigure(currentContextName);
}
// Only possible if the Config was created using Config.fromKubeconfig, otherwise autoConfigure would have been called
if (getFile() != null) {
if (loadKubeConfigContents(getFile()) == null) {
return this; // loadKubeConfigContents will have logged an exception
Expand All @@ -831,31 +836,26 @@ public Config refresh() {
}
return refreshedConfig;
}
// nothing to refresh - the kubeconfig was directly supplied
// nothing to refresh - the Config values were directly supplied
return this;
}

private static File findKubeConfigFile() {
private static Collection<File> findKubeConfigFiles() {
LOGGER.debug("Trying to configure client from Kubernetes config...");
if (!Utils.getSystemPropertyOrEnvVar(KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, true)) {
return null;
}
final var kubeConfigFilenames = getKubeconfigFilenames();
if (kubeConfigFilenames.size() > 1) {
LOGGER.warn(
"Found multiple Kubernetes config files [{}], using the first one: [{}]. If not desired file, please change it by doing `export KUBECONFIG=/path/to/kubeconfig` on Unix systems or `$Env:KUBECONFIG=/path/to/kubeconfig` on Windows.",
kubeConfigFilenames, kubeConfigFilenames.iterator().next());
}
final File kubeConfigFile = new File(kubeConfigFilenames.iterator().next());
if (!kubeConfigFile.isFile()) {
LOGGER.debug("Did not find Kubernetes config at: [{}]. Ignoring.", kubeConfigFile.getPath());
return null;
}
LOGGER.debug("Found for Kubernetes config at: [{}].", kubeConfigFile.getPath());
if (Utils.isNullOrEmpty(loadKubeConfigContents(kubeConfigFile))) {
return null;
}
return kubeConfigFile;
return Collections.emptyList();
}
return getKubeconfigFilenames().stream()
.map(File::new)
.filter(f -> {
if (!f.isFile()) {
LOGGER.debug("Did not find Kubernetes config at: [{}]. Ignoring.", f.getPath());
return false;
}
return true;
})
.filter(f -> Utils.isNotNullOrEmpty(loadKubeConfigContents(f)))
.collect(Collectors.toList());
}

public static Collection<String> getKubeconfigFilenames() {
Expand Down Expand Up @@ -1456,10 +1456,11 @@ public void setCurrentContext(NamedContext context) {
}

/**
* Returns the path to the file that contains the context from which this configuration was loaded from.
* <p>
* Returns {@code null} if no file was used.
*
* Returns the path to the file that this configuration was loaded from. Returns {@code null} if no file was used.
*
* @return the path to the kubeConfig file
* @return the path to the kubeconfig file.
*/
public File getFile() {
return KubeConfigUtils.getFileFromContext(getCurrentContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public static void persistKubeConfigIntoFile(Config kubeconfig, File kubeConfigP
.filter(ctx -> ctx.getAdditionalProperties() != null)
.forEach(ctx -> ctx.getAdditionalProperties().remove(KUBERNETES_CONFIG_FILE_KEY));
}
if (kubeconfig.getUsers() != null) {
kubeconfig.getUsers().stream()
.filter(u -> u.getAdditionalProperties() != null)
.forEach(u -> u.getAdditionalProperties().remove(KUBERNETES_CONFIG_FILE_KEY));
}
Files.writeString(kubeConfigPath.toPath(), Serialization.asYaml(kubeconfig));
}

Expand All @@ -105,6 +110,13 @@ public static File getFileFromContext(NamedContext namedContext) {
: null;
}

public static File getFileFromAuthInfo(NamedAuthInfo namedAuthInfo) {
return namedAuthInfo != null && namedAuthInfo.getAdditionalProperties() != null
&& namedAuthInfo.getAdditionalProperties().get(KUBERNETES_CONFIG_FILE_KEY) instanceof File
? (File) namedAuthInfo.getAdditionalProperties().get(KUBERNETES_CONFIG_FILE_KEY)
: null;
}

/**
* Merges the provided {@link Config} objects into the provided {@link io.fabric8.kubernetes.client.Config} object.
* <p>
Expand All @@ -131,15 +143,10 @@ public static void merge(io.fabric8.kubernetes.client.Config clientConfig, Strin
}
clientConfig.setCurrentContext(currentContext);
clientConfig.setNamespace(currentContext.getContext().getNamespace());
// If config was loaded using KubeConfigUtils#parseConfig, then the file is available in the additional properties
final File configFile;
if (currentContext.getAdditionalProperties().get(KUBERNETES_CONFIG_FILE_KEY) instanceof File) {
configFile = (File) currentContext.getAdditionalProperties().get(KUBERNETES_CONFIG_FILE_KEY);
} else {
configFile = null;
}
final var mergedClusters = mergeClusters(kubeconfigs);
if (mergedClusters.containsKey(currentContext.getContext().getCluster())) {
// If config was loaded using KubeConfigUtils#parseConfig, then the file is available in the additional properties
final File configFile = getFileFromContext(currentContext);
final var currentCluster = mergedClusters.get(currentContext.getContext().getCluster()).getCluster();
clientConfig.setMasterUrl(currentCluster.getServer());
clientConfig.setTrustCerts(Objects.equals(currentCluster.getInsecureSkipTlsVerify(), true));
Expand All @@ -163,7 +170,10 @@ public static void merge(io.fabric8.kubernetes.client.Config clientConfig, Strin
}
final var mergedUsers = mergeUsers(kubeconfigs);
if (mergedUsers.containsKey(currentContext.getContext().getUser())) {
final var currentAuthInfo = mergedUsers.get(currentContext.getContext().getUser()).getUser();
final var currentNamedAuthInfo = mergedUsers.get(currentContext.getContext().getUser());
// If config was loaded using KubeConfigUtils#parseConfig, then the file is available in the additional properties
final File configFile = getFileFromAuthInfo(currentNamedAuthInfo);
final var currentAuthInfo = currentNamedAuthInfo.getUser();
String clientCertFile = currentAuthInfo.getClientCertificate();
String clientKeyFile = currentAuthInfo.getClientKey();
if (configFile != null) {
Expand Down Expand Up @@ -230,6 +240,8 @@ private static Map<String, NamedAuthInfo> mergeUsers(Config... kubeconfigs) {
if (kubeconfigs[i].getUsers() != null) {
for (NamedAuthInfo user : kubeconfigs[i].getUsers()) {
if (user.getUser() != null) {
// Contains KUBERNETES_CONFIG_FILE_KEY if config was parsed using KubeConfigUtils#parseConfig
user.getAdditionalProperties().putAll(kubeconfigs[i].getAdditionalProperties());
mergedUsers.put(user.getName(), user);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

import static org.assertj.core.api.Assertions.assertThat;

class ConfigAutoConfigureTest {

@TempDir
private Path tempDir;

@AfterEach
void tearDown() {
System.clearProperty("kubeconfig");
}

@Nested
class FromKubeconfig {
@Test
void withNoKubeConfigFiles() {
System.setProperty("kubeconfig", "/dev/null");
final var result = new ConfigBuilder().withAutoConfigure().build();
assertThat(result)
.hasFieldOrPropertyWithValue("autoConfigure", true)
.returns(null, Config::getFile);
}

@Test
void withNonExistentConfigFile() {
System.setProperty("kubeconfig", tempDir.resolve("non-existent").toFile().getAbsolutePath());
final var result = new ConfigBuilder().withAutoConfigure().build();
assertThat(result)
.hasFieldOrPropertyWithValue("autoConfigure", true)
.returns(null, Config::getFile);
}

@Test
void withEmptyConfigFile() throws IOException {
final var emptyFile = Files.createFile(tempDir.resolve("empty"));
System.setProperty("kubeconfig", emptyFile.toFile().getAbsolutePath());
final var result = new ConfigBuilder().withAutoConfigure().build();
assertThat(result)
.hasFieldOrPropertyWithValue("autoConfigure", true)
.returns(null, Config::getFile);
}

@Test
void withSingleConfigFile() {
System.setProperty("kubeconfig", resolveFile("/config-auto-configure/config-2.yaml").getAbsolutePath());
final var result = new ConfigBuilder().withAutoConfigure().build();
assertThat(result)
.hasFieldOrPropertyWithValue("autoConfigure", true)
.returns(resolveFile("/config-auto-configure/config-2.yaml"), Config::getFile)
.hasFieldOrPropertyWithValue("masterUrl", "https://config-2.example.com/")
.hasFieldOrPropertyWithValue("currentContext.name", "context-in-all-configs");

}

@Test
void withMultipleConfigFiles() {
System.setProperty("kubeconfig",
resolveFile("/config-auto-configure/config-1.yaml").getAbsolutePath() + File.pathSeparator +
resolveFile("/config-auto-configure/config-2.yaml").getAbsolutePath() + File.pathSeparator +
resolveFile("/config-auto-configure/config-3.yaml").getAbsolutePath() + File.pathSeparator);
final var result = new ConfigBuilder().withAutoConfigure().build();
assertThat(result)
.hasFieldOrPropertyWithValue("autoConfigure", true)
.returns(resolveFile("/config-auto-configure/config-1.yaml"), Config::getFile)
.hasFieldOrPropertyWithValue("masterUrl", "https://config-1.example.com/")
.hasFieldOrPropertyWithValue("currentContext.name", "context-in-all-configs");

}

@Test
void withMultipleConfigFilesAndContext() {
System.setProperty("kubeconfig",
resolveFile("/config-auto-configure/just-current-context.yaml").getAbsolutePath() + File.pathSeparator +
resolveFile("/config-auto-configure/config-1.yaml").getAbsolutePath() + File.pathSeparator +
resolveFile("/config-auto-configure/config-2.yaml").getAbsolutePath() + File.pathSeparator +
resolveFile("/config-auto-configure/config-3.yaml").getAbsolutePath() + File.pathSeparator);
final var result = new ConfigBuilder().withAutoConfigure().build();
assertThat(result)
.hasFieldOrPropertyWithValue("autoConfigure", true)
.returns(resolveFile("/config-auto-configure/config-3.yaml"), Config::getFile)
.hasFieldOrPropertyWithValue("masterUrl", "https://config-3-special-cluster.example.com/")
.hasFieldOrPropertyWithValue("currentContext.name", "context-in-config-3");

}

// TODO: What if the user info is in a different file
}

private static File resolveFile(String path) {
return new File(Objects.requireNonNull(ConfigAutoConfigureTest.class.getResource(path)).getFile());
}
}
Loading

0 comments on commit cb63dc6

Please sign in to comment.