From f912a34cff723f83370ddc90eeed388465278f69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20Pi=C4=85tek?= Date: Mon, 3 Jul 2023 23:31:23 +0200 Subject: [PATCH] Add option to define Kubernetes cluster context --- src/SmoothSailing/ChartInstaller.cs | 52 +++++++++++++++++++- src/SmoothSailing/KubernetesContext.cs | 67 ++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 src/SmoothSailing/KubernetesContext.cs diff --git a/src/SmoothSailing/ChartInstaller.cs b/src/SmoothSailing/ChartInstaller.cs index a2b6d80..9a19453 100644 --- a/src/SmoothSailing/ChartInstaller.cs +++ b/src/SmoothSailing/ChartInstaller.cs @@ -26,7 +26,7 @@ public ChartInstaller(IProcessLauncher processLauncher) /// /// /// - public async Task Install(IChart chart, string releaseName, object? overrides = null, TimeSpan? timeout = null) + public async Task 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 != "[]") @@ -47,6 +47,11 @@ public async Task 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) @@ -60,4 +65,47 @@ public async Task 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 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}"); + + } +} \ No newline at end of file diff --git a/src/SmoothSailing/KubernetesContext.cs b/src/SmoothSailing/KubernetesContext.cs new file mode 100644 index 0000000..9c5293e --- /dev/null +++ b/src/SmoothSailing/KubernetesContext.cs @@ -0,0 +1,67 @@ +namespace SmoothSailing; + +public class KubernetesContext +{ + /// + /// Client-side default throttling limit (default 100). + /// + public int? BurstLimit { get; set; } + + /// + /// Enable verbose output. + /// + public bool? Debug { get; set; } + + /// + /// The address and the port for the Kubernetes API server. + /// + public string? APIServer { get; set; } + + /// + /// Group to impersonate for the operation. This flag can be repeated to specify multiple groups. + /// + public string[]? AsGroup { get; set; } + + /// + /// Username to impersonate for the operation. + /// + public string? AsUser { get; set; } + + /// + /// The certificate authority file for the Kubernetes API server connection. + /// + public string? CAFile { get; set; } + + /// + /// Name of the kubeconfig context to use. + /// + public string? Context { get; set; } + + /// + /// If true, the Kubernetes API server's certificate will not be checked for validity. + /// This will make your HTTPS connections insecure. + /// + public bool? InsecureSkipTLSVerify { get; set; } + + /// + /// Server name to use for Kubernetes API server certificate validation. + /// If it is not provided, the hostname used to contact the server is used. + /// + public string? TLSServerName { get; set; } + + /// + /// Bearer token used for authentication. + /// + public string? Token { get; set; } + + /// + /// Path to the kubeconfig file. + /// + public string? KubeConfig { get; set; } + + /// + /// Namespace scope for this request. + /// + public string? Namespace { get; set; } + +}