Skip to content

Commit

Permalink
using Utils to check parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Aug 27, 2024
1 parent 20ad89a commit 6525bd1
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ public Config(String masterUrl, String apiVersion, String namespace, Boolean tru
if (Utils.isNotNullOrEmpty(autoOAuthToken)) {
this.autoOAuthToken = autoOAuthToken;
}
if (contexts != null && !contexts.isEmpty()) {
if (Utils.isNotNullOrEmpty(contexts)) {
this.contexts = contexts;
}
if (Utils.isNotNull(currentContext)) {
Expand Down Expand Up @@ -716,7 +716,7 @@ public static void configFromSysPropsOrEnvVars(Config config) {
}

String tlsVersionsVar = Utils.getSystemPropertyOrEnvVar(KUBERNETES_TLS_VERSIONS);
if (tlsVersionsVar != null && !tlsVersionsVar.isEmpty()) {
if (Utils.isNotNullOrEmpty(tlsVersionsVar)) {
String[] tlsVersionsSplit = tlsVersionsVar.split(",");
TlsVersion[] tlsVersions = new TlsVersion[tlsVersionsSplit.length];
for (int i = 0; i < tlsVersionsSplit.length; i++) {
Expand Down Expand Up @@ -832,7 +832,7 @@ public static Config fromKubeconfig(String context, String kubeconfigContents, S
*/
public Config refresh() {
final String currentContextName = this.getCurrentContext() != null ? this.getCurrentContext().getName() : null;
if (this.oauthToken != null && !this.oauthToken.isEmpty()) {
if (Utils.isNotNullOrEmpty(this.oauthToken)) {
return this;
}
if (this.autoConfigure) {
Expand All @@ -856,8 +856,7 @@ private static boolean tryKubeConfig(Config config, String context) {
return false;
}
String[] kubeConfigFilenames = getKubeconfigFilenames();
if (kubeConfigFilenames == null
|| kubeConfigFilenames.length == 0) {
if (Utils.isNullOrEmpty(kubeConfigFilenames)) {
return false;
}
List<File> allFiles = Arrays.stream(kubeConfigFilenames)
Expand All @@ -869,28 +868,26 @@ private static boolean tryKubeConfig(Config config, String context) {
}

private static io.fabric8.kubernetes.api.model.Config mergeKubeConfigs(List<File> files) {
if (files == null
|| files.isEmpty()) {
if (Utils.isNullOrEmpty(files)) {
return null;
}
return files.stream()
.map(Config::createKubeconfig)
.reduce(null, (merged, additionalConfig) -> {
if (additionalConfig != null) {
return KubeConfigUtils.merge(additionalConfig, merged);
} else {
return merged;
}
});
.map(Config::createKubeconfig)
.reduce(null, (merged, additionalConfig) -> {
if (additionalConfig != null) {
return KubeConfigUtils.merge(additionalConfig, merged);
} else {
return merged;
}
});
}

private static io.fabric8.kubernetes.api.model.Config createKubeconfig(File file) {
io.fabric8.kubernetes.api.model.Config kubeConfig = null;
LOGGER.debug("Found for Kubernetes config at: [{}].", file.getPath());
try {
String content = getKubeconfigContents(file);
if (content != null
&& !content.isEmpty()) {
if (Utils.isNotNullOrEmpty(content)) {
kubeConfig = KubeConfigUtils.parseConfigFromString(content);
}
} catch (KubernetesClientException e) {
Expand All @@ -908,7 +905,6 @@ public static String getKubeconfigFilename() {
String fileName = null;
String[] fileNames = getKubeconfigFilenames();
// 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)
if (fileNames.length >= 1) {
fileName = fileNames[0];
if (fileNames.length > 1) {
Expand All @@ -922,17 +918,15 @@ public static String getKubeconfigFilename() {
public static String[] getKubeconfigFilenames() {
String[] fileNames = null;
String fileName = Utils.getSystemPropertyOrEnvVar(KUBERNETES_KUBECONFIG_FILES);
if (fileName != null
&& !fileName.isEmpty()) {
if (Utils.isNotNullOrEmpty(fileName)) {
fileNames = fileName.split(File.pathSeparator);
}
if (fileNames == null
|| fileNames.length == 0) {
if (Utils.isNullOrEmpty(fileNames)) {
fileNames = new String[] { DEFAULT_KUBECONFIG_FILE.toString() };
}
return Arrays.stream(fileNames)
.filter(filename -> isReadableKubeconfFile(new File(filename)))
.toArray(String[]::new);
.filter(filename -> isReadableKubeconfFile(new File(filename)))
.toArray(String[]::new);
}

private static boolean isReadableKubeconfFile(File file) {
Expand Down Expand Up @@ -964,7 +958,7 @@ private static String getKubeconfigContents(File kubeConfigFile) {
// It is only used to rewrite relative tls asset paths inside kubeconfig when a file is passed, and in the case that
// the kubeconfig references some assets via relative paths.
private static boolean loadFromKubeconfig(Config config, String context, String kubeconfigContents) {
if (kubeconfigContents != null && !kubeconfigContents.isEmpty()) {
if (Utils.isNotNullOrEmpty(kubeconfigContents)) {
return loadFromKubeconfig(config, context, KubeConfigUtils.parseConfigFromString(kubeconfigContents));
} else {
return false;
Expand Down Expand Up @@ -1116,7 +1110,7 @@ protected static List<String> getAuthenticatorCommandFromExecConfig(ExecConfig e
command = shellQuote(command);

List<String> args = exec.getArgs();
if (args != null && !args.isEmpty()) {
if (Utils.isNotNullOrEmpty(args)) {
command += " " + args
.stream()
.map(Config::shellQuote)
Expand Down Expand Up @@ -1217,7 +1211,7 @@ private static String getSystemEnvVariable(String envVariableName) {

protected static String getHomeDir(Predicate<String> directoryExists, UnaryOperator<String> getEnvVar) {
String home = getEnvVar.apply("HOME");
if (home != null && !home.isEmpty() && directoryExists.test(home)) {
if (Utils.isNotNullOrEmpty(home) && directoryExists.test(home)) {
return home;
}
String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT);
Expand All @@ -1231,7 +1225,7 @@ protected static String getHomeDir(Predicate<String> directoryExists, UnaryOpera
}
}
String userProfile = getEnvVar.apply("USERPROFILE");
if (userProfile != null && !userProfile.isEmpty() && directoryExists.test(userProfile)) {
if (Utils.isNotNullOrEmpty(userProfile) && directoryExists.test(userProfile)) {
return userProfile;
}
}
Expand Down Expand Up @@ -1751,8 +1745,7 @@ public void setCurrentContext(NamedContext context) {
*/
@Deprecated
public File getFile() {
if (files != null
&& !files.isEmpty()) {
if (Utils.isNotNullOrEmpty(files)) {
return files.get(0);
} else {
return null;
Expand All @@ -1771,24 +1764,23 @@ public List<File> getFiles() {
}

public KubeConfigFile getFile(String username) {
if (username == null
|| username.isEmpty()) {
if (Utils.isNullOrEmpty(username)) {
return null;
}
return Arrays.stream(getKubeconfigFilenames())
.map(File::new)
.map(file -> {
try {
return new KubeConfigFile(file, KubeConfigUtils.parseConfig(file));
} catch (IOException e) {
return null;
}
})
.filter(entry -> entry != null
&& entry.getConfig() != null
&& hasAuthInfo(username, entry.getConfig()))
.findFirst()
.orElse(null);
.map(File::new)
.map(file -> {
try {
return new KubeConfigFile(file, KubeConfigUtils.parseConfig(file));
} catch (IOException e) {
return null;
}
})
.filter(entry -> entry != null
&& entry.getConfig() != null
&& hasAuthInfo(username, entry.getConfig()))
.findFirst()
.orElse(null);
}

private boolean hasAuthInfo(String username, io.fabric8.kubernetes.api.model.Config kubeConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.fabric8.kubernetes.api.model.NamedExtension;
import io.fabric8.kubernetes.api.model.PreferencesBuilder;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.kubernetes.client.utils.Utils;

import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -183,17 +184,16 @@ public static Config merge(Config thisConfig, Config thatConfig) {
builder.addAllToExtensions(thisConfig.getExtensions());
}
if (!builder.hasCurrentContext()
&& thisConfig.getCurrentContext() != null
&& !thisConfig.getCurrentContext().isEmpty()) {
&& Utils.isNotNullOrEmpty(thisConfig.getCurrentContext())) {
builder.withCurrentContext(thisConfig.getCurrentContext());
}
Config merged = builder.build();
mergePreferences(thisConfig, merged);
return merged;

}

public static void mergePreferences(io.fabric8.kubernetes.api.model.Config source, io.fabric8.kubernetes.api.model.Config destination) {
public static void mergePreferences(io.fabric8.kubernetes.api.model.Config source,
io.fabric8.kubernetes.api.model.Config destination) {
if (source.getPreferences() != null) {
PreferencesBuilder builder = new PreferencesBuilder(destination.getPreferences());
if (source.getPreferences() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -291,16 +292,32 @@ public static boolean isNullOrEmpty(String str) {
return str == null || str.isEmpty();
}

public static boolean isNotNullOrEmpty(Map map) {
return !(map == null || map.isEmpty());
public static boolean isNotNullOrEmpty(Map<?, ?> map) {
return !isNullOrEmpty(map);
}

public static boolean isNullOrEmpty(Map<?, ?> map) {
return map == null || map.isEmpty();
}

public static boolean isNotNullOrEmpty(Collection<?> collection) {
return !isNullOrEmpty(collection);
}

public static boolean isNullOrEmpty(Collection<?> collection) {
return collection == null || collection.isEmpty();
}

public static boolean isNotNullOrEmpty(String str) {
return !isNullOrEmpty(str);
}

public static boolean isNotNullOrEmpty(String[] array) {
return !(array == null || array.length == 0);
return !isNullOrEmpty(array);
}

public static boolean isNullOrEmpty(String[] array) {
return array == null || array.length == 0;
}

public static <T> boolean isNotNull(T... refList) {
Expand Down
Loading

0 comments on commit 6525bd1

Please sign in to comment.