diff --git a/src/SmoothSailing/ChartInstaller.cs b/src/SmoothSailing/ChartInstaller.cs index 7e83a68..14eec4b 100644 --- a/src/SmoothSailing/ChartInstaller.cs +++ b/src/SmoothSailing/ChartInstaller.cs @@ -30,104 +30,54 @@ public ChartInstaller(IProcessOutputWriter? processOutputWriter = null) /// public async Task Install(IChart chart, string releaseName, object? overrides = null, TimeSpan? timeout = null, KubernetesContext? context = null) { - var filterParameters = new List + var listCommandParameters = new HelmCommandParameterBuilder(new List { $"--filter {releaseName}", "-o json" - }; + }); - if (context is not null) - { - ApplyContextInfo(context, filterParameters); - } + listCommandParameters.ApplyContextInfo(context); - var executeToEnd = await _processLauncher.ExecuteToEnd("helm", $"list {string.Join(" ", filterParameters)}", default); + var executeToEnd = await _processLauncher.ExecuteToEnd("helm", $"list {listCommandParameters.Build()}", default); if (executeToEnd != "[]") { - var uninstallParameters = new List + var uninstallParameters = new HelmCommandParameterBuilder(new List { "--wait" - }; + }); - if (context is not null) - { - ApplyContextInfo(context, uninstallParameters); - } - await _processLauncher.ExecuteToEnd("helm", $"uninstall {releaseName} {string.Join(" ", filterParameters)}", default); + uninstallParameters.ApplyContextInfo(context ); + + await _processLauncher.ExecuteToEnd("helm", $"uninstall {releaseName} {uninstallParameters.Build()}", default); } - var parameters = new List + var installParameters = new HelmCommandParameterBuilder(new List { "--install", "--force", "--atomic", "--wait" - }; + }); if (timeout.HasValue) { - parameters.Add($"--timeout {timeout.Value.TotalSeconds}s"); + installParameters.Add($"--timeout {timeout.Value.TotalSeconds}s"); } - if (context is not null) - { - ApplyContextInfo(context, parameters); - } + installParameters.ApplyContextInfo(context ); - chart.ApplyInstallParameters(parameters); + chart.ApplyInstallParameters(installParameters._parameters); if (overrides != null) { var serializedOverrides = JsonConvert.SerializeObject(overrides); var overridesPath = Path.Combine(Path.GetTempPath(), $"{releaseName}.json"); File.WriteAllText(overridesPath, serializedOverrides, Encoding.UTF8); - parameters.Add($"-f \"{overridesPath}\""); + installParameters.Add($"-f \"{overridesPath}\""); } - await _processLauncher.ExecuteToEnd("helm", $"upgrade {releaseName} {string.Join(" ", parameters)}", default); - return new Release(releaseName, _processLauncher); - } - - private void ApplyContextInfo(KubernetesContext options, List parameters) - { - if (options.BurstLimit.HasValue) - parameters.Add($"--burst-limit \"{options.BurstLimit.Value}\""); - - if (options.Debug.HasValue && options.Debug.Value) - parameters.Add("--debug"); - - if (!string.IsNullOrEmpty(options.APIServer)) - parameters.Add($"--kube-apiserver \"{options.APIServer}\""); - - if (options.AsGroup != null && options.AsGroup.Length > 0) - { - foreach (string group in options.AsGroup) - parameters.Add($"--kube-as-group \"{group}\""); - } - - if (!string.IsNullOrEmpty(options.AsUser)) - parameters.Add($"--kube-as-user \"{options.AsUser}\""); - - if (!string.IsNullOrEmpty(options.CAFile)) - parameters.Add($"--kube-ca-file \"{options.CAFile}\""); - - if (!string.IsNullOrEmpty(options.Context)) - parameters.Add($"--kube-context \"{options.Context}\""); - - if (options.InsecureSkipTLSVerify.HasValue && options.InsecureSkipTLSVerify.Value) - parameters.Add("--kube-insecure-skip-tls-verify"); - - if (!string.IsNullOrEmpty(options.TLSServerName)) - parameters.Add($"--kube-tls-server-name \"{options.TLSServerName}\""); - - if (!string.IsNullOrEmpty(options.Token)) - parameters.Add($"--kube-token \"{options.Token}\""); - - if (!string.IsNullOrEmpty(options.KubeConfig)) - parameters.Add($"--kubeconfig \"{options.KubeConfig}\""); - - if (!string.IsNullOrEmpty(options.Namespace)) - parameters.Add($"-n \"{options.Namespace}\""); + await _processLauncher.ExecuteToEnd("helm", $"upgrade {releaseName} {installParameters.Build()}", default); + return new Release(releaseName, _processLauncher, context); } } \ No newline at end of file diff --git a/src/SmoothSailing/HelmCommandParameterBuilder.cs b/src/SmoothSailing/HelmCommandParameterBuilder.cs new file mode 100644 index 0000000..4a30810 --- /dev/null +++ b/src/SmoothSailing/HelmCommandParameterBuilder.cs @@ -0,0 +1,63 @@ +using System.Collections.Generic; +using System.Linq; + +namespace SmoothSailing; + +internal class HelmCommandParameterBuilder +{ + internal readonly List _parameters; + + public HelmCommandParameterBuilder(IReadOnlyList? parameters = null) + { + _parameters = parameters?.ToList() ?? new List(); + } + + public void Add(string parameter) => _parameters.Add(parameter); + + public void ApplyContextInfo(KubernetesContext? context) + { + if (context == null) + return ; + + if (context.BurstLimit.HasValue) + _parameters.Add($"--burst-limit \"{context.BurstLimit.Value}\""); + + if (context.Debug.HasValue && context.Debug.Value) + _parameters.Add("--debug"); + + if (!string.IsNullOrEmpty(context.APIServer)) + _parameters.Add($"--kube-apiserver \"{context.APIServer}\""); + + if (context.AsGroup != null && context.AsGroup.Length > 0) + { + foreach (string group in context.AsGroup) + _parameters.Add($"--kube-as-group \"{group}\""); + } + + if (!string.IsNullOrEmpty(context.AsUser)) + _parameters.Add($"--kube-as-user \"{context.AsUser}\""); + + if (!string.IsNullOrEmpty(context.CAFile)) + _parameters.Add($"--kube-ca-file \"{context.CAFile}\""); + + if (!string.IsNullOrEmpty(context.Context)) + _parameters.Add($"--kube-context \"{context.Context}\""); + + if (context.InsecureSkipTLSVerify.HasValue && context.InsecureSkipTLSVerify.Value) + _parameters.Add("--kube-insecure-skip-tls-verify"); + + if (!string.IsNullOrEmpty(context.TLSServerName)) + _parameters.Add($"--kube-tls-server-name \"{context.TLSServerName}\""); + + if (!string.IsNullOrEmpty(context.Token)) + _parameters.Add($"--kube-token \"{context.Token}\""); + + if (!string.IsNullOrEmpty(context.KubeConfig)) + _parameters.Add($"--kubeconfig \"{context.KubeConfig}\""); + + if (!string.IsNullOrEmpty(context.Namespace)) + _parameters.Add($"-n \"{context.Namespace}\""); + } + + public string Build() => string.Join(" ", _parameters); +} diff --git a/src/SmoothSailing/Release.cs b/src/SmoothSailing/Release.cs index 16f844c..1caf76f 100644 --- a/src/SmoothSailing/Release.cs +++ b/src/SmoothSailing/Release.cs @@ -11,12 +11,14 @@ public class Release : IAsyncDisposable { public string DeploymentName { get; } private readonly IProcessLauncher _processExecutor; + private readonly KubernetesContext? _kubernetesContext; private readonly List<(Task, CancellationTokenSource)> _portForwards = new(); - internal Release(string deploymentName, IProcessLauncher processExecutor) + internal Release(string deploymentName, IProcessLauncher processExecutor, KubernetesContext? kubernetesContext) { DeploymentName = deploymentName; _processExecutor = processExecutor; + _kubernetesContext = kubernetesContext; } public async Task StartPortForwardForService(string serviceName, int servicePort, int? localPort = null) @@ -28,7 +30,9 @@ public async Task StartPortForwardForPod(string serviceName, int servicePor private async Task StartPortForwardFor(string elementType, string elementName, int servicePort, int? localPort) { var cancellationTokenSource = new CancellationTokenSource(); - var asyncEnumerable = _processExecutor.Execute("kubectl", $"port-forward {elementType}/{elementName} {localPort}:{servicePort}", cancellationTokenSource.Token); + var portForwardParameters = new HelmCommandParameterBuilder(); + portForwardParameters.ApplyContextInfo(_kubernetesContext); + var asyncEnumerable = _processExecutor.Execute("kubectl", $"port-forward {elementType}/{elementName} {localPort}:{servicePort} {portForwardParameters.Build()}", cancellationTokenSource.Token); var enumerator = asyncEnumerable.GetAsyncEnumerator(default); await enumerator.MoveNextAsync(); @@ -77,6 +81,8 @@ public async ValueTask DisposeAsync() Console.WriteLine(e); } - await _processExecutor.ExecuteToEnd("helm", $"uninstall {DeploymentName}", default); + var uninstallParameters = new HelmCommandParameterBuilder(); + uninstallParameters.ApplyContextInfo(_kubernetesContext); + await _processExecutor.ExecuteToEnd("helm", $"uninstall {DeploymentName} {uninstallParameters.Build()}", default); } }