diff --git a/test-frame-common/src/main/java/io/skodjob/testframe/clients/cmdClient/BaseCmdKubeClient.java b/test-frame-common/src/main/java/io/skodjob/testframe/clients/cmdClient/BaseCmdKubeClient.java index d17a120..396d018 100644 --- a/test-frame-common/src/main/java/io/skodjob/testframe/clients/cmdClient/BaseCmdKubeClient.java +++ b/test-frame-common/src/main/java/io/skodjob/testframe/clients/cmdClient/BaseCmdKubeClient.java @@ -43,7 +43,7 @@ public abstract class BaseCmdKubeClient> implemen protected String config; - String namespace = defaultNamespace(); + String namespace; /** * Constructor for BaseCmdKubeClient. @@ -54,6 +54,28 @@ protected BaseCmdKubeClient(String config) { this.config = config; } + protected List command(String... args) { + return command(Arrays.stream(args).toList()); + } + + protected List command(List args) { + List result = new ArrayList<>(); + result.add(cmd()); + + if (config != null) { + result.add("--kubeconfig"); + result.add(config); + } + + if (namespace != null) { + result.add("--namespace"); + result.add(namespace); + } + + result.addAll(args); + return result; + } + /** * Abstract method to retrieve the command. * @@ -72,7 +94,7 @@ protected BaseCmdKubeClient(String config) { @Override @SuppressWarnings("unchecked") public K deleteByName(String resourceType, String resourceName) { - Exec.exec(namespacedCommand(DELETE, resourceType, resourceName)); + Exec.exec(command(DELETE, resourceType, resourceName)); return (K) this; } @@ -94,13 +116,6 @@ protected Context adminContext() { return defaultContext(); } - protected List namespacedCommand(String... rest) { - List cmd = new ArrayList<>(List.of("--namespace", namespace)); - cmd.addAll(asList(rest)); - - return command(cmd); - } - /** * Retrieves the YAML representation of a resource. * @@ -110,7 +125,7 @@ protected List namespacedCommand(String... rest) { */ @Override public String get(String resource, String resourceName) { - return Exec.exec(namespacedCommand(GET, resource, resourceName, "-o", "yaml")).out(); + return Exec.exec(command(GET, resource, resourceName, "-o", "yaml")).out(); } /** @@ -120,7 +135,7 @@ public String get(String resource, String resourceName) { */ @Override public String getEvents() { - return Exec.exec(namespacedCommand(GET, "events")).out(); + return Exec.exec(command(GET, "events")).out(); } /** @@ -194,7 +209,7 @@ private Map execRecursive(String subcommand, File[] files, Com for (File f : files) { if (f.isFile()) { if (f.getName().endsWith(".yaml")) { - execResults.put(f, Exec.exec(null, namespacedCommand(subcommand, "-f", + execResults.put(f, Exec.exec(null, command(subcommand, "-f", f.getAbsolutePath()), 0, false, false)); } } else if (f.isDirectory()) { @@ -231,36 +246,6 @@ public K replace(File... files) { } } - /** - * Applies YAML content in the current namespace. - * - * @param yamlContent The YAML content. - * @return The instance of the client. - */ - @Override - @SuppressWarnings("unchecked") - public K applyContentInNamespace(String yamlContent) { - try (Context context = defaultContext()) { - Exec.exec(yamlContent, namespacedCommand(APPLY, "-f", "-")); - return (K) this; - } - } - - /** - * Deletes YAML content in the current namespace. - * - * @param yamlContent The YAML content. - * @return The instance of the client. - */ - @Override - @SuppressWarnings("unchecked") - public K deleteContentInNamespace(String yamlContent) { - try (Context context = defaultContext()) { - Exec.exec(yamlContent, namespacedCommand(DELETE, "-f", "-"), 0, true, false); - return (K) this; - } - } - /** * Applies YAML content. * @@ -303,7 +288,7 @@ public K deleteContent(String yamlContent) { @SuppressWarnings("unchecked") public K createNamespace(String name) { try (Context context = adminContext()) { - Exec.exec(namespacedCommand(CREATE, "namespace", name)); + Exec.exec(command(CREATE, "namespace", name)); } return (K) this; } @@ -318,7 +303,7 @@ public K createNamespace(String name) { @SuppressWarnings("unchecked") public K deleteNamespace(String name) { try (Context context = adminContext()) { - Exec.exec(null, namespacedCommand(DELETE, "namespace", name), 0, true, false); + Exec.exec(null, command(DELETE, "namespace", name), 0, true, false); } return (K) this; } @@ -335,7 +320,7 @@ public K deleteNamespace(String name) { @SuppressWarnings("unchecked") public K scaleByName(String kind, String name, int replicas) { try (Context context = defaultContext()) { - Exec.exec(null, namespacedCommand("scale", kind, name, "--replicas", Integer.toString(replicas))); + Exec.exec(null, command("scale", kind, name, "--replicas", Integer.toString(replicas))); return (K) this; } } @@ -349,7 +334,7 @@ public K scaleByName(String kind, String name, int replicas) { */ @Override public ExecResult execInPod(String pod, String... command) { - List cmd = namespacedCommand("exec", pod, "--"); + List cmd = command("exec", pod, "--"); cmd.addAll(asList(command)); return Exec.exec(cmd); } @@ -378,7 +363,7 @@ public ExecResult execInPodContainer(String pod, String container, String... com */ @Override public ExecResult execInPodContainer(boolean logToOutput, String pod, String container, String... command) { - List cmd = namespacedCommand("exec", pod, "-c", container, "--"); + List cmd = command("exec", pod, "-c", container, "--"); cmd.addAll(asList(command)); return Exec.exec(null, cmd, 0, logToOutput); } @@ -435,29 +420,6 @@ public ExecResult exec(boolean throwError, boolean logToOutput, int timeout, Str return Exec.exec(null, cmd, timeout, logToOutput, throwError); } - /** - * Executes a command in the current namespace. - * - * @param commands The commands to execute. - * @return The execution result. - */ - @Override - public ExecResult execInCurrentNamespace(String... commands) { - return Exec.exec(namespacedCommand(commands)); - } - - /** - * Executes a command in the current namespace with the option to log to output. - * - * @param logToOutput Whether to log the output. - * @param commands The commands to execute. - * @return The execution result. - */ - @Override - public ExecResult execInCurrentNamespace(boolean logToOutput, String... commands) { - return Exec.exec(null, namespacedCommand(commands), 0, logToOutput); - } - /** * Converts the object to a string representation. * @@ -476,26 +438,12 @@ public String toString() { */ @Override public List list(String resourceType) { - return Arrays.stream(Exec.exec(namespacedCommand(GET, resourceType, + return Arrays.stream(Exec.exec(command(GET, resourceType, "-o", "jsonpath={range .items[*]}{.metadata.name} ")) .out().trim().split(" +")) .filter(s -> !s.trim().isEmpty()).collect(Collectors.toList()); } - /** - * Lists cluster wide resources of a certain type. - * - * @param resourceType The type of the resource. - * @return A list of resource names. - */ - @Override - public List listClusterWide(String resourceType) { - return Arrays.stream(Exec.exec(command(List.of(GET, resourceType, - "-o", "jsonpath={range .items[*]}{.metadata.name} "))) - .out().trim().split(" +")) - .filter(s -> !s.trim().isEmpty()).collect(Collectors.toList()); - } - /** * Retrieves a resource as JSON. * @@ -505,7 +453,7 @@ public List listClusterWide(String resourceType) { */ @Override public String getResourceAsJson(String resourceType, String resourceName) { - return Exec.exec(namespacedCommand(GET, resourceType, resourceName, "-o", "json")).out(); + return Exec.exec(command(GET, resourceType, resourceName, "-o", "json")).out(); } /** @@ -517,7 +465,7 @@ public String getResourceAsJson(String resourceType, String resourceName) { */ @Override public String getResourceAsYaml(String resourceType, String resourceName) { - return Exec.exec(namespacedCommand(GET, resourceType, resourceName, "-o", "yaml")).out(); + return Exec.exec(command(GET, resourceType, resourceName, "-o", "yaml")).out(); } /** @@ -528,30 +476,7 @@ public String getResourceAsYaml(String resourceType, String resourceName) { */ @Override public String getResourcesAsYaml(String resourceType) { - return Exec.exec(namespacedCommand(GET, resourceType, "-o", "yaml")).out(); - } - - /** - * Retrieves a cluster wide resource as YAML. - * - * @param resourceType The type of the resource. - * @param resourceName The name of the resource. - * @return The resource as YAML. - */ - @Override - public String getClusterWideResourceAsYaml(String resourceType, String resourceName) { - return Exec.exec(command(List.of(GET, resourceType, resourceName, "-o", "yaml"))).out(); - } - - /** - * Retrieves resources as YAML. - * - * @param resourceType The type of the resource. - * @return The resources as YAML. - */ - @Override - public String getClusterWideResourcesAsYaml(String resourceType) { - return Exec.exec(command(List.of(GET, resourceType, "-o", "yaml"))).out(); + return Exec.exec(command(GET, resourceType, "-o", "yaml")).out(); } /** @@ -562,14 +487,14 @@ public String getClusterWideResourcesAsYaml(String resourceType) { */ @Override public void createResourceAndApply(String template, Map params) { - List cmd = namespacedCommand("process", template, "-l", "app=" + template, "-o", "yaml"); + List cmd = command("process", template, "-l", "app=" + template, "-o", "yaml"); for (Map.Entry entry : params.entrySet()) { cmd.add("-p"); cmd.add(entry.getKey() + "=" + entry.getValue()); } String yaml = Exec.exec(cmd).out(); - applyContentInNamespace(yaml); + this.applyContent(yaml); } /** @@ -581,7 +506,7 @@ public void createResourceAndApply(String template, Map params) */ @Override public String describe(String resourceType, String resourceName) { - return Exec.exec(namespacedCommand("describe", resourceType, resourceName)).out(); + return Exec.exec(command("describe", resourceType, resourceName)).out(); } /** @@ -599,7 +524,7 @@ public String logs(String pod, String container) { } else { args = new String[]{"logs", pod}; } - return Exec.exec(namespacedCommand(args)).out(); + return Exec.exec(command(args)).out(); } /** @@ -615,7 +540,7 @@ public String logs(String pod, String container) { public String searchInLog(String resourceType, String resourceName, long sinceSeconds, String... grepPattern) { try { return Exec.exec("bash", "-c", join(" ", - namespacedCommand("logs", resourceType + "/" + resourceName, + command("logs", resourceType + "/" + resourceName, "--since=" + sinceSeconds + "s", "|", "grep", " -e " + join(" -e ", grepPattern), "-B", "1"))) .out(); @@ -643,7 +568,7 @@ public String searchInLog(String resourceType, String resourceName, long sinceSe public String searchInLog(String resourceType, String resourceName, String resourceContainer, long sinceSeconds, String... grepPattern) { try { - return Exec.exec("bash", "-c", join(" ", namespacedCommand("logs", + return Exec.exec("bash", "-c", join(" ", command("logs", resourceType + "/" + resourceName, "-c " + resourceContainer, "--since=" + sinceSeconds + "s", "|", "grep", " -e " + join(" -e ", grepPattern), "-B", "1"))).out(); } catch (KubeClusterException e) { @@ -665,24 +590,11 @@ public String searchInLog(String resourceType, String resourceName, String resou */ @Override public List listResourcesByLabel(String resourceType, String label) { - return asList(Exec.exec(namespacedCommand(GET, resourceType, + return asList(Exec.exec(command(GET, resourceType, "-l", label, "-o", "jsonpath={range .items[*]}{.metadata.name} ")) .out().split("\\s+")); } - private List command(List rest) { - List result = new ArrayList<>(); - result.add(cmd()); - - if (config != null) { - result.add("--kubeconfig"); - result.add(config); - } - - result.addAll(rest); - return result; - } - /** * Processes a template file with parameters. * diff --git a/test-frame-common/src/main/java/io/skodjob/testframe/clients/cmdClient/KubeCmdClient.java b/test-frame-common/src/main/java/io/skodjob/testframe/clients/cmdClient/KubeCmdClient.java index 3af2fc7..3ad99a1 100644 --- a/test-frame-common/src/main/java/io/skodjob/testframe/clients/cmdClient/KubeCmdClient.java +++ b/test-frame-common/src/main/java/io/skodjob/testframe/clients/cmdClient/KubeCmdClient.java @@ -19,13 +19,6 @@ */ public interface KubeCmdClient> { - /** - * Retrieves the default namespace for the Kubernetes client. - * - * @return The default namespace. - */ - String defaultNamespace(); - /** * Retrieves the default OLM (Operator Lifecycle Manager) namespace for the Kubernetes client. * @@ -119,22 +112,6 @@ default K delete(String... files) { */ K replace(File... files); - /** - * Applies resource content within the current namespace. - * - * @param yamlContent The YAML content representing the resources. - * @return This kube client. - */ - K applyContentInNamespace(String yamlContent); - - /** - * Deletes resource content within the current namespace. - * - * @param yamlContent The YAML content representing the resources to delete. - * @return This kube client. - */ - K deleteContentInNamespace(String yamlContent); - /** * Applies resource content. * @@ -186,23 +163,6 @@ default K delete(String... files) { */ ExecResult execInPod(String pod, String... command); - /** - * Executes a command within the current namespace. - * - * @param commands The commands to execute. - * @return The execution result. - */ - ExecResult execInCurrentNamespace(String... commands); - - /** - * Executes a command within the current namespace with logging to output control. - * - * @param logToOutput Determines if the output should be logged. - * @param commands The commands to execute. - * @return The execution result. - */ - ExecResult execInCurrentNamespace(boolean logToOutput, String... commands); - /** * Executes a command within a pod container. * @@ -286,14 +246,6 @@ default K delete(String... files) { */ List list(String resourceType); - /** - * Retrieves a list of cluster wide resources by type. - * - * @param resourceType The type of the resources. - * @return The list of resources. - */ - List listClusterWide(String resourceType); - /** * Retrieves the YAML content of a resource by type and name. * @@ -311,23 +263,6 @@ default K delete(String... files) { */ String getResourcesAsYaml(String resourceType); - /** - * Retrieves the YAML content of a cluster wide resource by type and name. - * - * @param resourceType The type of the resource. - * @param resourceName The name of the resource. - * @return The YAML content of the resource. - */ - String getClusterWideResourceAsYaml(String resourceType, String resourceName); - - /** - * Retrieves the YAML content of cluster wide resources by type. - * - * @param resourceType The type of the resources. - * @return The YAML content of the resources. - */ - String getClusterWideResourcesAsYaml(String resourceType); - /** * Creates a resource from a template and applies it. * diff --git a/test-frame-common/src/main/java/io/skodjob/testframe/clients/cmdClient/Kubectl.java b/test-frame-common/src/main/java/io/skodjob/testframe/clients/cmdClient/Kubectl.java index 60643ca..5557a82 100644 --- a/test-frame-common/src/main/java/io/skodjob/testframe/clients/cmdClient/Kubectl.java +++ b/test-frame-common/src/main/java/io/skodjob/testframe/clients/cmdClient/Kubectl.java @@ -56,16 +56,6 @@ public String getCurrentNamespace() { return namespace; } - /** - * Gets the default namespace. - * - * @return The default namespace. - */ - @Override - public String defaultNamespace() { - return "default"; - } - /** * Gets the default OLM (Operator Lifecycle Manager) namespace. * diff --git a/test-frame-common/src/main/java/io/skodjob/testframe/clients/cmdClient/Oc.java b/test-frame-common/src/main/java/io/skodjob/testframe/clients/cmdClient/Oc.java index 5382160..37d5d94 100644 --- a/test-frame-common/src/main/java/io/skodjob/testframe/clients/cmdClient/Oc.java +++ b/test-frame-common/src/main/java/io/skodjob/testframe/clients/cmdClient/Oc.java @@ -40,16 +40,6 @@ private Oc(String futureNamespace, String config) { namespace = futureNamespace; } - /** - * Gets the default namespace. - * - * @return The default namespace. - */ - @Override - public String defaultNamespace() { - return "myproject"; - } - /** * Gets the default OLM (Operator Lifecycle Manager) namespace. * @@ -103,7 +93,7 @@ public Oc createNamespace(String name) { * @return The Oc instance. */ public Oc newApp(String template, Map params) { - List cmd = namespacedCommand("new-app", template); + List cmd = command("new-app", template); for (Map.Entry entry : params.entrySet()) { cmd.add("-p"); cmd.add(entry.getKey() + "=" + entry.getValue()); diff --git a/test-frame-log-collector/src/main/java/io/skodjob/testframe/LogCollector.java b/test-frame-log-collector/src/main/java/io/skodjob/testframe/LogCollector.java index 975f576..e467927 100644 --- a/test-frame-log-collector/src/main/java/io/skodjob/testframe/LogCollector.java +++ b/test-frame-log-collector/src/main/java/io/skodjob/testframe/LogCollector.java @@ -191,7 +191,7 @@ public void collectClusterWideResourcesToFolder(boolean logPerFile, String folde } else { String yaml = executeCollectionCall( String.format("collect descriptions of type: %s", resourceType), - () -> kubeCmdClient.getClusterWideResourcesAsYaml(resourceType) + () -> kubeCmdClient.getResourcesAsYaml(resourceType) ); String resFileName = LogCollectorUtils.getYamlFileNameForResource(resourceType); String filePath = LogCollectorUtils @@ -305,14 +305,14 @@ private void collectPodDescription(String namespaceName, String podsFolderPath, * @param resourceType resource kind for collect */ private void collectClusterWideResourcesPerFile(String clusterWideFolderPath, String resourceType) { - List resources = kubeCmdClient.listClusterWide(resourceType); + List resources = kubeCmdClient.list(resourceType); if (resources != null && !resources.isEmpty()) { String fullFolderPath = createResourceDirectoryInNamespaceDir(clusterWideFolderPath, resourceType); resources.forEach(resourceName -> { String yaml = executeCollectionCall( String.format("collect YAML description for %s:%s", resourceType, resourceName), - () -> kubeCmdClient.getClusterWideResourceAsYaml(resourceType, resourceName) + () -> kubeCmdClient.getResourceAsYaml(resourceType, resourceName) ); String resFileName = LogCollectorUtils.getYamlFileNameForResource(resourceName);