Skip to content

Commit

Permalink
Add option to define Kubernetes cluster context
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Jul 3, 2023
1 parent e519601 commit f912a34
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 2 deletions.
52 changes: 50 additions & 2 deletions src/SmoothSailing/ChartInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ChartInstaller(IProcessLauncher processLauncher)
/// <param name="releaseName"></param>
/// <param name="overrides"></param>
/// <param name="timeout"></param>
public async Task<Release> Install(IChart chart, string releaseName, object? overrides = null, TimeSpan? timeout = null)
public async Task<Release> Install(IChart chart, string releaseName, object? overrides = null, TimeSpan? timeout = null, KubernetesContext? context = null)
{
var executeToEnd = await _processLauncher.ExecuteToEnd("helm", $"list --filter {releaseName} -o json", default);
if (executeToEnd != "[]")
Expand All @@ -47,6 +47,11 @@ public async Task<Release> Install(IChart chart, string releaseName, object? ove
parameters.Add($"--timeout {timeout.Value.TotalSeconds}s");
}

if (context is not null)
{
ApplyContextInfo(context, parameters);
}

chart.ApplyInstallParameters(parameters);

if (overrides != null)
Expand All @@ -60,4 +65,47 @@ public async Task<Release> Install(IChart chart, string releaseName, object? ove
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 is {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}");

}
}
67 changes: 67 additions & 0 deletions src/SmoothSailing/KubernetesContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace SmoothSailing;

public class KubernetesContext
{
/// <summary>
/// Client-side default throttling limit (default 100).
/// </summary>
public int? BurstLimit { get; set; }

/// <summary>
/// Enable verbose output.
/// </summary>
public bool? Debug { get; set; }

/// <summary>
/// The address and the port for the Kubernetes API server.
/// </summary>
public string? APIServer { get; set; }

/// <summary>
/// Group to impersonate for the operation. This flag can be repeated to specify multiple groups.
/// </summary>
public string[]? AsGroup { get; set; }

/// <summary>
/// Username to impersonate for the operation.
/// </summary>
public string? AsUser { get; set; }

/// <summary>
/// The certificate authority file for the Kubernetes API server connection.
/// </summary>
public string? CAFile { get; set; }

/// <summary>
/// Name of the kubeconfig context to use.
/// </summary>
public string? Context { get; set; }

/// <summary>
/// If true, the Kubernetes API server's certificate will not be checked for validity.
/// This will make your HTTPS connections insecure.
/// </summary>
public bool? InsecureSkipTLSVerify { get; set; }

/// <summary>
/// Server name to use for Kubernetes API server certificate validation.
/// If it is not provided, the hostname used to contact the server is used.
/// </summary>
public string? TLSServerName { get; set; }

/// <summary>
/// Bearer token used for authentication.
/// </summary>
public string? Token { get; set; }

/// <summary>
/// Path to the kubeconfig file.
/// </summary>
public string? KubeConfig { get; set; }

/// <summary>
/// Namespace scope for this request.
/// </summary>
public string? Namespace { get; set; }

}

0 comments on commit f912a34

Please sign in to comment.