Skip to content

Commit

Permalink
refactor(config): changed getKubeconfigFilename to getKubeconfigFilen…
Browse files Browse the repository at this point in the history
…ames (multiple file support)

Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Nov 6, 2024
1 parent 222ee9b commit 7431bc4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
29 changes: 23 additions & 6 deletions doc/MIGRATION-v7.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
## Contents
- [Java baseline set to Java 11](#java-11)
- [Bouncy Castle is no longer needed](#bouncy-castle)
- [Apache Felix SCR Annotations removed](#apache-felix-scr-annotations)
- [Config changes](#config-changes)
- [Support for multiple kubeconfig files](#config-changes-multiple-kubeconfig)
- [Model Changes](#model-changes)
- [kubernetes-model artifact removed](#kubernetes-model-artifact-removed)
- [Moved packages](#model-changes-moved-packages)
Expand All @@ -19,7 +20,8 @@
- [OkHttp class replacements](#mockwebserver-okhttp-replacements)
- [SSL/TLS certificates](#mockwebserver-ssl-tls-certificates)
- [Deprecations and Removals](#deprecations-and-removals)
- [Service Catalog API (extension) removed](#service-catalog-extension)
- [Apache Felix SCR Annotations removed](#apache-felix-scr-annotations)
- [Service Catalog API (extension) removed](#service-catalog-extension)


> [!NOTE]
Expand All @@ -41,11 +43,20 @@ The Bouncy Castle library is no longer needed as a dependency.
In previous versions, this was an optional dependency needed for Elliptic Curve (EC) Keys.
The Kubernetes client now uses the default Java security provider which should be enough to handle all scenarios.

## Apache Felix SCR annotations removed <a href="#apache-felix-scr-annotations" id="apache-felix-scr-annotations" />
## Config changes <a href="#config-changes" id="config-changes"/>

[Apache Felix SCR annotations](https://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html) are not supported anymore by community. Fabric8 Kubernetes Client no longer uses Apache Felix SCR annotations for OSGi
Declarative Services. Fabric8 Kubernetes Client is now using official OSGi annotations instead. If you're using Apache Felix SCR
annotations, you need to switch to [Official OSGi Component annotations](https://docs.osgi.org/javadoc/r6/cmpn/org/osgi/service/component/annotations/package-summary.html).
The `Config` class has been completely refactored to improve its reliability and usability.

### Support for multiple kubeconfig files <a href="#config-changes-multiple-kubeconfig" id="config-changes-multiple-kubeconfig"/>

The `Config` class now supports the use of multiple kubeconfig files.

Additional references:
- [Organizing Cluster Access Using kubeconfig Files](https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/)

#### Config API changes

The `Config#getKubeconfigFilename` has changed its name and signature to support multiple files, it's now called `Config#getKubeconfigFilenames` and returns a collection of strings with the obtained paths.

## Model Changes <a href="#model-changes" id="model-changes"/>

Expand Down Expand Up @@ -201,6 +212,12 @@ If you need the certificates (public and private), you can retrieve them by usin

## Deprecations and Removals <a href="#deprecations-and-removals" id="deprecations-and-removals"/>

### Apache Felix SCR annotations removed <a href="#apache-felix-scr-annotations" id="apache-felix-scr-annotations" />

[Apache Felix SCR annotations](https://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html) are not supported anymore by community. Fabric8 Kubernetes Client no longer uses Apache Felix SCR annotations for OSGi
Declarative Services. Fabric8 Kubernetes Client is now using official OSGi annotations instead. If you're using Apache Felix SCR
annotations, you need to switch to [Official OSGi Component annotations](https://docs.osgi.org/javadoc/r6/cmpn/org/osgi/service/component/annotations/package-summary.html).

### Service Catalog API (extension) removed <a href="#service-catalog-extension" id="service-catalog-extension"/>

The Service Catalog API extension has been removed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand All @@ -54,6 +55,7 @@
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

@SuppressWarnings({ "LombokGetterMayBeUsed", "LombokSetterMayBeUsed" })
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(allowGetters = true, allowSetters = true)
public class Config {
Expand Down Expand Up @@ -849,7 +851,13 @@ private static File findKubeConfigFile() {
if (!Utils.getSystemPropertyOrEnvVar(KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, true)) {
return null;
}
final File kubeConfigFile = new File(getKubeconfigFilename());
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;
Expand All @@ -861,21 +869,12 @@ private static File findKubeConfigFile() {
return kubeConfigFile;
}

public static String getKubeconfigFilename() {
String fileName = Utils.getSystemPropertyOrEnvVar(KUBERNETES_KUBECONFIG_FILE,
public static Collection<String> getKubeconfigFilenames() {
final var valueOrDefault = Utils.getSystemPropertyOrEnvVar(KUBERNETES_KUBECONFIG_FILE,
new File(getHomeDir(), ".kube" + File.separator + "config").toString());

// if system property/env var contains multiple files take the first one based on the environment
// we are running in (eg. : for Linux, ; for Windows)
String[] fileNames = fileName.split(File.pathSeparator);

if (fileNames.length > 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.",
fileNames, fileNames[0]);
fileName = fileNames[0];
}
return fileName;
return Arrays.asList(valueOrDefault.split(File.pathSeparator));
}

private static String loadKubeConfigContents(File kubeConfigFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public class ConfigListUsers {

public static void main(String[] args) throws FileNotFoundException {
// Gets KubeConfig via reading KubeConfig environment variable or ~/.kube/config
String kubeConfigFileName = io.fabric8.kubernetes.client.Config.getKubeconfigFilename();
if (kubeConfigFileName != null) {
File kubeConfigFile = new File(kubeConfigFileName);
final var kubeConfigFileNames = io.fabric8.kubernetes.client.Config.getKubeconfigFilenames();
if (!kubeConfigFileNames.isEmpty()) {
File kubeConfigFile = new File(kubeConfigFileNames.iterator().next());
Config kubeConfigObj = Serialization.unmarshal(new FileInputStream(kubeConfigFile), Config.class);

// Print users
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public class ConfigViewEquivalent {

public static void main(String[] args) throws IOException {
// Gets KubeConfig via reading KUBECONFIG environment variable or ~/.kube/config
String kubeConfigFileName = io.fabric8.kubernetes.client.Config.getKubeconfigFilename();
if (kubeConfigFileName != null) {
File kubeConfigFile = new File(kubeConfigFileName);
final var kubeConfigFileNames = io.fabric8.kubernetes.client.Config.getKubeconfigFilenames();
if (!kubeConfigFileNames.isEmpty()) {
File kubeConfigFile = new File(kubeConfigFileNames.iterator().next());
Files.readAllLines(kubeConfigFile.toPath())
.forEach(logger::warn);
}
Expand Down

0 comments on commit 7431bc4

Please sign in to comment.