Fixing code smells 61/133061/1
authorwaqas.ikram <waqas.ikram@est.tech>
Mon, 30 Jan 2023 11:39:05 +0000 (11:39 +0000)
committerwaqas.ikram <waqas.ikram@est.tech>
Mon, 30 Jan 2023 11:39:19 +0000 (11:39 +0000)
Change-Id: I64289f09e7881e0f706a0b4ff657a080bc904aca
Issue-ID: SO-4052
Signed-off-by: waqas.ikram <waqas.ikram@est.tech>
so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/extclients/helm/HelmClientImpl.java
so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/extclients/kubernetes/KubernetesClientImpl.java
so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/extclients/kubernetes/KubernetesClientProviderImpl.java

index ffce0ad..af99167 100644 (file)
@@ -46,6 +46,7 @@ import org.springframework.stereotype.Service;
 
 @Service
 public class HelmClientImpl implements HelmClient {
+    private static final String DEFAULT_NAMESPACE = "default";
     private static final String KIND_KEY = "kind: ";
     private static final String ANY_UNICODE_NEWLINE = "\\R";
     private static final Logger logger = LoggerFactory.getLogger(HelmClientImpl.class);
@@ -149,25 +150,23 @@ public class HelmClientImpl implements HelmClient {
         logger.info("uninstalling the release {} from cluster {}", releaseName, kubeConfigFilePath);
         final ProcessBuilder processBuilder = prepareUnInstallCommand(releaseName, kubeConfigFilePath);
         final String commandResponse = executeCommand(processBuilder);
-        if (!StringUtils.isEmpty(commandResponse)) {
-            if (commandResponse.contains("Release not loaded")) {
-                throw new HelmClientExecuteException(
-                        "Unable to find the installed Helm chart by using releaseName: " + releaseName);
-            }
+        if (!StringUtils.isEmpty(commandResponse) && commandResponse.contains("Release not loaded")) {
+            throw new HelmClientExecuteException(
+                    "Unable to find the installed Helm chart by using releaseName: " + releaseName);
         }
 
         logger.info("Release {} uninstalled successfully", releaseName);
     }
 
     private ProcessBuilder prepareDryRunCommand(final String releaseName, final Path kubeconfig, final Path helmChart) {
-        final List<String> helmArguments = List.of("helm", "install", releaseName, "-n", "default",
+        final List<String> helmArguments = List.of("helm", "install", releaseName, "-n", DEFAULT_NAMESPACE,
                 helmChart.toString(), "--dry-run", "--kubeconfig", kubeconfig.toString());
         return new ProcessBuilder().command(helmArguments);
     }
 
     private ProcessBuilder prepareInstallCommand(final String releaseName, final Path kubeconfig, final Path helmChart,
             final Map<String, String> lifeCycleParams) {
-        final List<String> commands = new ArrayList<String>(List.of("helm", "install", releaseName, "-n", "default",
+        final List<String> commands = new ArrayList<>(List.of("helm", "install", releaseName, "-n", DEFAULT_NAMESPACE,
                 helmChart.toString(), "--kubeconfig", kubeconfig.toString()));
 
         if (lifeCycleParams != null && !lifeCycleParams.isEmpty()) {
@@ -185,31 +184,31 @@ public class HelmClientImpl implements HelmClient {
         logger.debug("Yaml file content : {}", yamlContent);
         try {
             Files.write(Paths.get(fileName), yamlContent.getBytes());
-        } catch (final IOException e) {
-            logger.error("Failed to create the run time life cycle yaml file: {} " + e.getMessage());
+        } catch (final IOException ioException) {
             throw new HelmClientExecuteException(
-                    "Failed to create the run time life cycle yaml file: {} " + e.getMessage());
+                    "Failed to create the run time life cycle yaml file: {} " + ioException.getMessage(), ioException);
         }
     }
 
     private ProcessBuilder prepareUnInstallCommand(final String releaseName, final Path kubeConfig) {
         logger.debug("Will remove tis log after checking ubeconfig path: {}", kubeConfig.toFile().getName());
-        final List<String> helmArguments = new ArrayList<>(
-                List.of("helm", "uninstall", releaseName, "-n", "default", "--kubeconfig", kubeConfig.toString()));
+        final List<String> helmArguments = new ArrayList<>(List.of("helm", "uninstall", releaseName, "-n",
+                DEFAULT_NAMESPACE, "--kubeconfig", kubeConfig.toString()));
         return new ProcessBuilder().command(helmArguments);
     }
 
     private ProcessBuilder prepareKubeKindCommand(final String releaseName, final Path kubeconfig,
             final Path helmChart) {
-        final List<String> commands = List.of("helm", "template", releaseName, "-n", "default", helmChart.toString(),
-                "--dry-run", "--kubeconfig", kubeconfig.toString(), "--skip-tests", "| grep kind | uniq");
+        final List<String> commands =
+                List.of("helm", "template", releaseName, "-n", DEFAULT_NAMESPACE, helmChart.toString(), "--dry-run",
+                        "--kubeconfig", kubeconfig.toString(), "--skip-tests", "| grep kind | uniq");
         final List<String> helmArguments = List.of("sh", "-c", toString(commands));
         return new ProcessBuilder().command(helmArguments);
     }
 
     private ProcessBuilder prepareGetKubeKindCommand(final String releaseName, final Path kubeconfig) {
-        final List<String> commands = List.of("helm", "get", "manifest", releaseName, "-n", "default", "--kubeconfig",
-                kubeconfig.toString(), "| grep kind | uniq");
+        final List<String> commands = List.of("helm", "get", "manifest", releaseName, "-n", DEFAULT_NAMESPACE,
+                "--kubeconfig", kubeconfig.toString(), "| grep kind | uniq");
         final List<String> helmArguments = List.of("sh", "-c", toString(commands));
         return new ProcessBuilder().command(helmArguments);
     }
index 4731c24..15f6d83 100644 (file)
@@ -395,10 +395,8 @@ public class KubernetesClientImpl implements KubernetesClient {
             logger.debug("Response from list service for all Namespaces: {}", v1ServiceList);
             return v1ServiceList.getItems().isEmpty();
         } catch (final ApiException exception) {
-            logger.debug("Return false because of exception occurred: {}", exception.getMessage());
             handleApiException(KIND_SERVICE, labelSelector, exception);
         } catch (final RuntimeException runtimeException) {
-            logger.debug("Return false because of Runtime exception occurred: {}", runtimeException.getMessage());
             handleRuntimeException(KIND_SERVICE, labelSelector, runtimeException);
         }
         logger.debug("Returning false as Service is not Deleted ...");
@@ -416,10 +414,8 @@ public class KubernetesClientImpl implements KubernetesClient {
             logger.debug("Response from list Pod for all Namespaces: {}", v1PodList);
             return v1PodList.getItems().isEmpty();
         } catch (final ApiException exception) {
-            logger.debug("Return false because of exception occurred: {}", exception.getMessage());
             handleApiException(KIND_POD, labelSelector, exception);
         } catch (final RuntimeException runtimeException) {
-            logger.debug("Return false because of Runtime exception occurred: {}", runtimeException.getMessage());
             handleRuntimeException(KIND_POD, labelSelector, runtimeException);
         }
         logger.debug("Returning false as Pod is not Deleted ...");
@@ -437,10 +433,8 @@ public class KubernetesClientImpl implements KubernetesClient {
             logger.debug("Response from list Job for all Namespaces: {}", v1JobList);
             return v1JobList.getItems().isEmpty();
         } catch (final ApiException exception) {
-            logger.debug("Return false because of exception occurred: {}", exception.getMessage());
             handleApiException(KIND_JOB, labelSelector, exception);
         } catch (final RuntimeException runtimeException) {
-            logger.debug("Return false because of Runtime exception occurred: {}", runtimeException.getMessage());
             handleRuntimeException(KIND_JOB, labelSelector, runtimeException);
         }
         logger.debug("Returning false as Job is not Deleted ...");
@@ -458,10 +452,8 @@ public class KubernetesClientImpl implements KubernetesClient {
             logger.debug("Response from list Deployment for all Namespaces: {}", v1DeploymentList);
             return v1DeploymentList.getItems().isEmpty();
         } catch (final ApiException exception) {
-            logger.debug("Return false because of exception occurred: {}", exception.getMessage());
             handleApiException(KIND_DEPLOYMENT, labelSelector, exception);
         } catch (final RuntimeException runtimeException) {
-            logger.debug("Return false because of Runtime exception occurred: {}", runtimeException.getMessage());
             handleRuntimeException(KIND_DEPLOYMENT, labelSelector, runtimeException);
         }
         logger.debug("Returning false as Deployment is not Deleted ...");
@@ -479,10 +471,8 @@ public class KubernetesClientImpl implements KubernetesClient {
             logger.debug("Response from list ReplicaSet for all Namespaces: {}", v1ReplicaSetList);
             return v1ReplicaSetList.getItems().isEmpty();
         } catch (final ApiException exception) {
-            logger.debug("Return false because of exception occurred: {}", exception.getMessage());
             handleApiException(KIND_REPLICA_SET, labelSelector, exception);
         } catch (final RuntimeException runtimeException) {
-            logger.debug("Return false because of Runtime exception occurred: {}", runtimeException.getMessage());
             handleRuntimeException(KIND_REPLICA_SET, labelSelector, runtimeException);
         }
         logger.debug("Returning false as ReplicaSet is not Deleted ...");
@@ -500,10 +490,8 @@ public class KubernetesClientImpl implements KubernetesClient {
             logger.debug("Response from list DaemonSet for all Namespaces: {}", v1DaemonSetList);
             return v1DaemonSetList.getItems().isEmpty();
         } catch (final ApiException exception) {
-            logger.debug("Return false because of exception occurred: {}", exception.getMessage());
             handleApiException(KIND_DAEMON_SET, labelSelector, exception);
         } catch (final RuntimeException runtimeException) {
-            logger.debug("Return false because of Runtime exception occurred: {}", runtimeException.getMessage());
             handleRuntimeException(KIND_DAEMON_SET, labelSelector, runtimeException);
         }
         logger.debug("Returning false as DaemonSet is not Deleted ...");
@@ -521,10 +509,8 @@ public class KubernetesClientImpl implements KubernetesClient {
             logger.debug("Response from list StatefulSet for all Namespaces: {}", v1StatefulSetList);
             return v1StatefulSetList.getItems().isEmpty();
         } catch (final ApiException exception) {
-            logger.debug("Return false because of exception occurred: {}", exception.getMessage());
             handleApiException(KIND_STATEFUL_SET, labelSelector, exception);
         } catch (final RuntimeException runtimeException) {
-            logger.debug("Return false because of Runtime exception occurred: {}", runtimeException.getMessage());
             handleRuntimeException(KIND_STATEFUL_SET, labelSelector, runtimeException);
         }
         logger.debug("Returning false as StatefulSet is not Deleted ...");
index f8837a9..23d346f 100644 (file)
@@ -48,7 +48,7 @@ public class KubernetesClientProviderImpl implements KubernetesClientProvider {
     @Override
     public ApiClient getApiClient(final String kubeConfigPath) {
 
-        ApiClient client = INSTANCES.get(kubeConfigPath.toString());
+        ApiClient client = INSTANCES.get(kubeConfigPath);
         if (client == null) {
             synchronized (this) {
                 try (final Reader input = new FileReader(kubeConfigPath);) {
@@ -59,13 +59,11 @@ public class KubernetesClientProviderImpl implements KubernetesClientProvider {
                     logger.debug("ApiClient created successfully");
                     INSTANCES.put(kubeConfigPath, client);
                 } catch (final FileNotFoundException fileNotFoundException) {
-                    logger.error("{} KubeConfig not found", kubeConfigPath, fileNotFoundException);
                     throw new KubeConfigFileProcessingException(kubeConfigPath + " kube-config file not found",
                             fileNotFoundException);
                 } catch (final Exception exception) {
-                    final String message = "Unexpected exception while processing kube-config file";
-                    logger.error(message, exception);
-                    throw new KubeConfigFileProcessingException(message, exception);
+                    throw new KubeConfigFileProcessingException(
+                            "Unexpected exception while processing kube-config file", exception);
                 }
             }
         }