Skip to content

Commit

Permalink
FIX: Add missing KubernetesContext for all installation steps
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Sep 8, 2023
1 parent 334e3d7 commit 6c08a14
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 70 deletions.
84 changes: 17 additions & 67 deletions src/SmoothSailing/ChartInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,104 +30,54 @@ public ChartInstaller(IProcessOutputWriter? processOutputWriter = null)
/// <param name="timeout"></param>
public async Task<Release> Install(IChart chart, string releaseName, object? overrides = null, TimeSpan? timeout = null, KubernetesContext? context = null)
{
var filterParameters = new List<string>
var listCommandParameters = new HelmCommandParameterBuilder(new List<string>
{
$"--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<string>
var uninstallParameters = new HelmCommandParameterBuilder(new List<string>
{
"--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<string>
var installParameters = new HelmCommandParameterBuilder(new List<string>
{
"--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<string> 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);
}
}
63 changes: 63 additions & 0 deletions src/SmoothSailing/HelmCommandParameterBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Collections.Generic;
using System.Linq;

namespace SmoothSailing;

internal class HelmCommandParameterBuilder
{
internal readonly List<string> _parameters;

public HelmCommandParameterBuilder(IReadOnlyList<string>? parameters = null)
{
_parameters = parameters?.ToList() ?? new List<string>();
}

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);
}
12 changes: 9 additions & 3 deletions src/SmoothSailing/Release.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> StartPortForwardForService(string serviceName, int servicePort, int? localPort = null)
Expand All @@ -28,7 +30,9 @@ public async Task<int> StartPortForwardForPod(string serviceName, int servicePor
private async Task<int> 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();
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 6c08a14

Please sign in to comment.