Skip to content

Commit

Permalink
Style changes + namespace related methods renaming and changes (#55)
Browse files Browse the repository at this point in the history
* style changes + namespace related methods renaming

Signed-off-by: Lukas Kral <[email protected]>

* change the position of the namespace command

Signed-off-by: Lukas Kral <[email protected]>

---------

Signed-off-by: Lukas Kral <[email protected]>
  • Loading branch information
im-konge authored May 7, 2024
1 parent c8c5de0 commit 807ec13
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static void logSeparator(String delimiterChar, int length) {
* @param resource resource
* @param <T> The type of the resources.
*/
public static <T extends HasMetadata> void logResource(String operation, T resource) {
public static <T extends HasMetadata> void logResource(String operation, T resource) {
if (resource.getMetadata().getNamespace() == null) {
LOGGER.info(LoggerUtils.RESOURCE_LOGGER_PATTERN,
operation, resource.getKind(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ protected Context adminContext() {
}

protected List<String> namespacedCommand(String... rest) {
return command(asList(rest), true);
List<String> cmd = new ArrayList<>(List.of("--namespace", namespace));
cmd.addAll(asList(rest));

return command(cmd);
}

/**
Expand Down Expand Up @@ -268,7 +271,7 @@ public K deleteContentInNamespace(String yamlContent) {
@SuppressWarnings("unchecked")
public K applyContent(String yamlContent) {
try (Context context = defaultContext()) {
Exec.exec(yamlContent, command(Arrays.asList(APPLY, "-f", "-"), false), 0,
Exec.exec(yamlContent, command(Arrays.asList(APPLY, "-f", "-")), 0,
true, true);
return (K) this;
}
Expand All @@ -284,7 +287,7 @@ public K applyContent(String yamlContent) {
@SuppressWarnings("unchecked")
public K deleteContent(String yamlContent) {
try (Context context = defaultContext()) {
Exec.exec(yamlContent, command(Arrays.asList(DELETE, "-f", "-"), false), 0,
Exec.exec(yamlContent, command(Arrays.asList(DELETE, "-f", "-")), 0,
true, false);
return (K) this;
}
Expand Down Expand Up @@ -413,7 +416,7 @@ public ExecResult exec(boolean throwError, String... command) {
*/
@Override
public ExecResult exec(boolean throwError, boolean logToOutput, String... command) {
List<String> cmd = command(asList(command), false);
List<String> cmd = command(asList(command));
return Exec.exec(null, cmd, 0, logToOutput, throwError);
}

Expand All @@ -428,7 +431,7 @@ public ExecResult exec(boolean throwError, boolean logToOutput, String... comman
*/
@Override
public ExecResult exec(boolean throwError, boolean logToOutput, int timeout, String... command) {
List<String> cmd = command(asList(command), false);
List<String> cmd = command(asList(command));
return Exec.exec(null, cmd, timeout, logToOutput, throwError);
}

Expand Down Expand Up @@ -630,17 +633,15 @@ public List<String> listResourcesByLabel(String resourceType, String label) {
.out().split("\\s+"));
}

private List<String> command(List<String> rest, boolean namespaced) {
private List<String> command(List<String> rest) {
List<String> result = new ArrayList<>();
result.add(cmd());

if (config != null) {
result.add("--kubeconfig");
result.add(config);
}
if (namespaced) {
result.add("--namespace");
result.add(namespace);
}

result.addAll(rest);
return result;
}
Expand All @@ -656,10 +657,11 @@ private List<String> command(List<String> rest, boolean namespaced) {
@Override
@SuppressWarnings("unchecked")
public K process(Map<String, String> parameters, String file, Consumer<String> c) {
List<String> command = command(asList(PROCESS, "-f", file), false);
List<String> command = command(asList(PROCESS, "-f", file));
command.addAll(parameters.entrySet().stream()
.map(e -> "-p " + e.getKey() + "=" + e.getValue())
.collect(Collectors.toList()));
.toList());

c.accept(Exec.exec(null, command, 0, false).out());
return (K) this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public interface KubeCmdClient<K extends KubeCmdClient<K>> {
* @param namespace The namespace to set.
* @return This kube client.
*/
KubeCmdClient<K> namespace(String namespace);
KubeCmdClient<K> inNamespace(String namespace);

/**
* Retrieves the currently set namespace for the Kubernetes client.
*
* @return The currently set namespace.
*/
String namespace();
String getCurrentNamespace();

/**
* Creates resources from the provided files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private Kubectl(String futureNamespace, String config) {
* @return A new Kubectl instance with the specified namespace.
*/
@Override
public Kubectl namespace(String namespace) {
public Kubectl inNamespace(String namespace) {
return new Kubectl(namespace, config);
}

Expand All @@ -52,7 +52,7 @@ public Kubectl namespace(String namespace) {
* @return The current namespace.
*/
@Override
public String namespace() {
public String getCurrentNamespace() {
return namespace;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public String defaultOlmNamespace() {
* @return A new Oc instance with the specified namespace.
*/
@Override
public Oc namespace(String namespace) {
public Oc inNamespace(String namespace) {
return new Oc(namespace, config);
}

Expand All @@ -77,7 +77,7 @@ public Oc namespace(String namespace) {
* @return The current namespace.
*/
@Override
public String namespace() {
public String getCurrentNamespace() {
return namespace;
}

Expand Down

0 comments on commit 807ec13

Please sign in to comment.