@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);
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()) {
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);
}
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 ...");
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 ...");
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 ...");
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 ...");
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 ...");
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 ...");
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 ...");