From a99a4d49ea812a0e3d37867a355415463b45a281 Mon Sep 17 00:00:00 2001 From: Alastair Pitts Date: Sun, 10 Nov 2024 17:08:06 -0700 Subject: [PATCH 1/4] Remove Helm V2 --- .../FeatureToggles/FeatureToggle.cs | 1 - source/Calamari.Tests/Calamari.Tests.csproj | 1 + .../KubernetesFixtures/Helm2UpgradeFixture.cs | 60 --------------- .../HelmInstalledVersionUpgradeFixture.cs | 2 - .../KubernetesFixtures/HelmUpgradeFixture.cs | 36 +-------- .../Conventions/HelmUpgradeConvention.cs | 75 ++++--------------- .../Calamari/Kubernetes/SpecialVariables.cs | 3 - 7 files changed, 21 insertions(+), 157 deletions(-) delete mode 100644 source/Calamari.Tests/KubernetesFixtures/Helm2UpgradeFixture.cs diff --git a/source/Calamari.Common/FeatureToggles/FeatureToggle.cs b/source/Calamari.Common/FeatureToggles/FeatureToggle.cs index 13a5c73fa..8173e12cc 100644 --- a/source/Calamari.Common/FeatureToggles/FeatureToggle.cs +++ b/source/Calamari.Common/FeatureToggles/FeatureToggle.cs @@ -12,7 +12,6 @@ public enum FeatureToggle { AsynchronousAzureZipDeployFeatureToggle, FSharpDeprecationFeatureToggle, AzureRMDeprecationFeatureToggle, - PreventHelmV2DeploymentsFeatureToggle, KubernetesLiveObjectStatusFeatureToggle, KubernetesAuthAwsCliWithExecFeatureToggle } diff --git a/source/Calamari.Tests/Calamari.Tests.csproj b/source/Calamari.Tests/Calamari.Tests.csproj index 6bf283fc5..095c25bcc 100644 --- a/source/Calamari.Tests/Calamari.Tests.csproj +++ b/source/Calamari.Tests/Calamari.Tests.csproj @@ -226,6 +226,7 @@ + diff --git a/source/Calamari.Tests/KubernetesFixtures/Helm2UpgradeFixture.cs b/source/Calamari.Tests/KubernetesFixtures/Helm2UpgradeFixture.cs deleted file mode 100644 index 8bee8d15c..000000000 --- a/source/Calamari.Tests/KubernetesFixtures/Helm2UpgradeFixture.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System.Threading.Tasks; -using Calamari.Testing.Helpers; -using Calamari.Testing.Requirements; -using Calamari.Tests.Helpers; -using Calamari.Tests.Fixtures; -using FluentAssertions; -using NUnit.Framework; - -namespace Calamari.Tests.KubernetesFixtures -{ - [TestFixture] - public class Helm2UpgradeFixture : HelmUpgradeFixture - { - [Test] - [RequiresNonFreeBSDPlatform] - [RequiresNon32BitWindows] - [RequiresNonMac] - [Category(TestCategory.PlatformAgnostic)] - public void Upgrade_Succeeds() - { - var result = DeployPackage(); - - result.AssertSuccess(); - result.AssertOutputMatches($"NAMESPACE: {Namespace}"); - result.AssertOutputMatches("STATUS: DEPLOYED"); - result.AssertOutputMatches(ConfigMapName); - result.AssertOutputMatches($"release \"{ReleaseName}\" deleted"); - result.AssertOutput("Using custom helm executable at " + HelmExePath); - - Assert.AreEqual(ReleaseName.ToLower(), result.CapturedOutput.OutputVariables["ReleaseName"]); - } - - [Test] - [RequiresNonFreeBSDPlatform] - [RequiresNon32BitWindows] - [RequiresNonMacAttribute] - [Category(TestCategory.PlatformAgnostic)] - public void TillerNamespace_CannotFindIfRandomNamespaceUsed() - { - // We're basically just testing here that setting the tiller namespace does put the param into the cmd - Variables.Set(Kubernetes.SpecialVariables.Helm.TillerNamespace, "random-foobar"); - - var result = DeployPackage(); - result.AssertFailure(); - Log.StandardError.Should().ContainMatch("*Error: could not find tiller*"); - } - - [Test] - [RequiresNonFreeBSDPlatform] - [RequiresNon32BitWindows] - [RequiresNonMac] - [Category(TestCategory.PlatformAgnostic)] - public async Task CustomHelmExeInPackage_RelativePath() - { - await TestCustomHelmExeInPackage_RelativePath("2.9.0"); - } - - protected override string ExplicitExeVersion => "2.9.1"; - } -} diff --git a/source/Calamari.Tests/KubernetesFixtures/HelmInstalledVersionUpgradeFixture.cs b/source/Calamari.Tests/KubernetesFixtures/HelmInstalledVersionUpgradeFixture.cs index ef401537f..fa0cd8aaf 100644 --- a/source/Calamari.Tests/KubernetesFixtures/HelmInstalledVersionUpgradeFixture.cs +++ b/source/Calamari.Tests/KubernetesFixtures/HelmInstalledVersionUpgradeFixture.cs @@ -1,7 +1,5 @@ using Calamari.Testing.Helpers; using Calamari.Testing.Requirements; -using Calamari.Tests.Fixtures; -using Calamari.Tests.Helpers; using NUnit.Framework; namespace Calamari.Tests.KubernetesFixtures diff --git a/source/Calamari.Tests/KubernetesFixtures/HelmUpgradeFixture.cs b/source/Calamari.Tests/KubernetesFixtures/HelmUpgradeFixture.cs index 94fc23d2e..5a2117ea3 100644 --- a/source/Calamari.Tests/KubernetesFixtures/HelmUpgradeFixture.cs +++ b/source/Calamari.Tests/KubernetesFixtures/HelmUpgradeFixture.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Net; using System.Net.Http; using System.Text; using System.Threading; @@ -18,7 +17,6 @@ using Calamari.Testing; using Calamari.Testing.Helpers; using Calamari.Testing.Requirements; -using Calamari.Tests.Fixtures; using Calamari.Tests.Helpers; using Calamari.Util; using FluentAssertions; @@ -112,10 +110,8 @@ public virtual void SetUp() //Helm Options Variables.Set(Kubernetes.SpecialVariables.Helm.ReleaseName, ReleaseName); - Variables.Set(Kubernetes.SpecialVariables.Helm.ClientVersion, helmVersion.ToString()); - //K8S Auth - Variables.Set(Kubernetes.SpecialVariables.ClusterUrl, ServerUrl); + //K8S AuthVariables.Set(Kubernetes.SpecialVariables.ClusterUrl, ServerUrl); Variables.Set(Kubernetes.SpecialVariables.SkipTlsVerification, "True"); Variables.Set(Kubernetes.SpecialVariables.Namespace, Namespace); Variables.Set(SpecialVariables.Account.AccountType, "Token"); @@ -453,33 +449,9 @@ static HelmVersion GetVersion() result.ExitCode.Should().Be(0, $"Failed to retrieve version from Helm (Exit code {result.ExitCode}). Error output: \r\n{result.ErrorOutput}"); - return ParseVersion(stdout.ToString()); - } - - //versionString from "helm version --client --short" - static HelmVersion ParseVersion(string versionString) - { - //eg of output for helm 2: Client: v2.16.1+gbbdfe5e - //eg of output for helm 3: v3.0.1+g7c22ef9 - - var indexOfVersionIdentifier = versionString.IndexOf('v'); - if (indexOfVersionIdentifier == -1) - throw new FormatException($"Failed to find version identifier from '{versionString}'."); - - var indexOfVersionNumber = indexOfVersionIdentifier + 1; - if (indexOfVersionNumber >= versionString.Length) - throw new FormatException($"Failed to find version number from '{versionString}'."); - - var version = versionString[indexOfVersionNumber]; - switch (version) - { - case '3': - return HelmVersion.V3; - case '2': - return HelmVersion.V2; - default: - throw new InvalidOperationException($"Unsupported helm version '{version}'"); - } + var version = stdout.ToString(); + return HelmVersionParser.ParseVersion(version) + ?? throw new InvalidOperationException($"Unsupported helm version '{version}'"); } } } diff --git a/source/Calamari/Kubernetes/Conventions/HelmUpgradeConvention.cs b/source/Calamari/Kubernetes/Conventions/HelmUpgradeConvention.cs index 3ceb85a45..4197dc113 100644 --- a/source/Calamari/Kubernetes/Conventions/HelmUpgradeConvention.cs +++ b/source/Calamari/Kubernetes/Conventions/HelmUpgradeConvention.cs @@ -68,20 +68,8 @@ string BuildHelmCommand(RunningDeployment deployment, ScriptSyntax syntax) var packagePath = GetChartLocation(deployment); var customHelmExecutable = CustomHelmExecutableFullPath(deployment.Variables, deployment.CurrentDirectory); - var helmVersion = GetVersion(deployment.Variables); - CheckHelmToolVersion(customHelmExecutable, helmVersion); - if (helmVersion == HelmVersion.V2) - { - if (FeatureToggle.PreventHelmV2DeploymentsFeatureToggle.IsEnabled(deployment.Variables)) - { - throw new CommandException("Helm V2 is no longer supported. Please migrate to Helm V3."); - } - else - { - log.Warn("This step is currently configured to use Helm V2. Support for Helm V2 will be completely removed in Octopus Server 2025.1. Please migrate to Helm V3 as soon as possible."); - } - } + AssertHelmV3(customHelmExecutable); var sb = new StringBuilder(); @@ -89,12 +77,6 @@ string BuildHelmCommand(RunningDeployment deployment, ScriptSyntax syntax) sb.Append($" upgrade --install"); SetNamespaceParameter(deployment, sb); SetResetValuesParameter(deployment, sb); - if (helmVersion == HelmVersion.V2) - { - SetTillerTimeoutParameter(deployment, sb); - SetTillerNamespaceParameter(deployment, sb); - } - SetTimeoutParameter(deployment, sb); SetValuesParameters(deployment, sb); SetAdditionalArguments(deployment, sb); @@ -104,16 +86,6 @@ string BuildHelmCommand(RunningDeployment deployment, ScriptSyntax syntax) return sb.ToString(); } - HelmVersion GetVersion(IVariables variables) - { - var clientVersionText = variables.Get(SpecialVariables.Helm.ClientVersion); - - if (Enum.TryParse(clientVersionText, out HelmVersion version)) - return version; - - throw new CommandException($"Unrecognized Helm version: '{clientVersionText}'"); - } - void SetExecutable(StringBuilder sb, ScriptSyntax syntax, string customHelmExecutable) { if (customHelmExecutable != null) @@ -211,14 +183,6 @@ void SetAdditionalArguments(RunningDeployment deployment, StringBuilder sb) } } - static void SetTillerNamespaceParameter(RunningDeployment deployment, StringBuilder sb) - { - if (deployment.Variables.IsSet(SpecialVariables.Helm.TillerNamespace)) - { - sb.Append($" --tiller-namespace \"{deployment.Variables.Get(SpecialVariables.Helm.TillerNamespace)}\""); - } - } - static void SetTimeoutParameter(RunningDeployment deployment, StringBuilder sb) { if (!deployment.Variables.IsSet(SpecialVariables.Helm.Timeout)) return; @@ -233,20 +197,7 @@ static void SetTimeoutParameter(RunningDeployment deployment, StringBuilder sb) sb.Append($" --timeout \"{timeout}\""); } - static void SetTillerTimeoutParameter(RunningDeployment deployment, StringBuilder sb) - { - if (!deployment.Variables.IsSet(SpecialVariables.Helm.TillerTimeout)) return; - - var tillerTimeout = deployment.Variables.Get(SpecialVariables.Helm.TillerTimeout); - if (!int.TryParse(tillerTimeout, out _)) - { - throw new CommandException($"Tiller timeout period is not a valid integer: {tillerTimeout}"); - } - - sb.Append($" --tiller-connection-timeout \"{tillerTimeout}\""); - } - - string SyntaxSpecificFileName(RunningDeployment deployment, ScriptSyntax syntax) + static string SyntaxSpecificFileName(RunningDeployment deployment, ScriptSyntax syntax) { return Path.Combine(deployment.CurrentDirectory, syntax == ScriptSyntax.PowerShell ? "Calamari.HelmUpgrade.ps1" : "Calamari.HelmUpgrade.sh"); } @@ -399,12 +350,10 @@ bool TryGenerateVariablesFile(RunningDeployment deployment, out string fileName) return fileName != null; } - - void CheckHelmToolVersion(string customHelmExecutable, HelmVersion selectedVersion) + + void AssertHelmV3(string customHelmExecutable) { - log.Verbose($"Helm version selected: {selectedVersion}"); - - StringBuilder stdout = new StringBuilder(); + var stdout = new StringBuilder(); var result = SilentProcessRunner.ExecuteCommand(customHelmExecutable ?? "helm", "version --client --short", Environment.CurrentDirectory, @@ -412,14 +361,22 @@ void CheckHelmToolVersion(string customHelmExecutable, HelmVersion selectedVersi error => { }); if (result.ExitCode != 0) + { log.Warn("Unable to retrieve the Helm tool version"); - + return; + } + var toolVersion = HelmVersionParser.ParseVersion(stdout.ToString()); if (!toolVersion.HasValue) + { log.Warn("Unable to parse the Helm tool version text: " + stdout); + return; + } - if (toolVersion.Value != selectedVersion) - log.Warn($"The Helm tool version '{toolVersion.Value}' ('{stdout}') doesn't match the Helm version selected '{selectedVersion}'"); + if (toolVersion.Value != HelmVersion.V3) + { + throw new CommandException("Helm V2 is no longer supported. Please migrate to Helm V3."); + } } } } \ No newline at end of file diff --git a/source/Calamari/Kubernetes/SpecialVariables.cs b/source/Calamari/Kubernetes/SpecialVariables.cs index aa79808c7..ad904cc36 100644 --- a/source/Calamari/Kubernetes/SpecialVariables.cs +++ b/source/Calamari/Kubernetes/SpecialVariables.cs @@ -49,10 +49,7 @@ public static class Helm public const string TemplateValuesSources = "Octopus.Action.Helm.TemplateValuesSources"; public const string AdditionalArguments = "Octopus.Action.Helm.AdditionalArgs"; public const string CustomHelmExecutable = "Octopus.Action.Helm.CustomHelmExecutable"; - public const string ClientVersion = "Octopus.Action.Helm.ClientVersion"; public const string Timeout = "Octopus.Action.Helm.Timeout"; - public const string TillerNamespace = "Octopus.Action.Helm.TillerNamespace"; - public const string TillerTimeout = "Octopus.Action.Helm.TillerTimeout"; public const string ChartDirectory = "Octopus.Action.Helm.ChartDirectory"; public static class Packages From 247e313a93f0fdb9c5094c96e6c6f6168d615546 Mon Sep 17 00:00:00 2001 From: Alastair Pitts Date: Sun, 10 Nov 2024 17:13:22 -0700 Subject: [PATCH 2/4] Remove more Helm V2 logic --- .../KubernetesFixtures/Helm3UpgradeFixture.cs | 2 +- .../KubernetesFixtures/HelmUpgradeFixture.cs | 52 +++++-------------- 2 files changed, 13 insertions(+), 41 deletions(-) diff --git a/source/Calamari.Tests/KubernetesFixtures/Helm3UpgradeFixture.cs b/source/Calamari.Tests/KubernetesFixtures/Helm3UpgradeFixture.cs index f66bbcc9e..5cd97eb6a 100644 --- a/source/Calamari.Tests/KubernetesFixtures/Helm3UpgradeFixture.cs +++ b/source/Calamari.Tests/KubernetesFixtures/Helm3UpgradeFixture.cs @@ -38,6 +38,6 @@ public async Task CustomHelmExeInPackage_RelativePath() await TestCustomHelmExeInPackage_RelativePath("3.0.1"); } - protected override string ExplicitExeVersion => "3.0.2"; + protected override string ExplicitExeVersion => "3.16.2"; } } \ No newline at end of file diff --git a/source/Calamari.Tests/KubernetesFixtures/HelmUpgradeFixture.cs b/source/Calamari.Tests/KubernetesFixtures/HelmUpgradeFixture.cs index 5a2117ea3..3964bce15 100644 --- a/source/Calamari.Tests/KubernetesFixtures/HelmUpgradeFixture.cs +++ b/source/Calamari.Tests/KubernetesFixtures/HelmUpgradeFixture.cs @@ -42,9 +42,8 @@ public abstract class HelmUpgradeFixture : CalamariFixture static string HelmOsPlatform => CalamariEnvironment.IsRunningOnWindows ? "windows-amd64" : "linux-amd64"; - HelmVersion? helmVersion; TemporaryDirectory explicitVersionTempDirectory; - + static readonly CancellationTokenSource CancellationTokenSource = new CancellationTokenSource(); readonly CancellationToken cancellationToken = CancellationTokenSource.Token; @@ -53,15 +52,10 @@ public async Task OneTimeSetUp() { ServerUrl = await ExternalVariables.Get(ExternalVariable.KubernetesClusterUrl, cancellationToken); ClusterToken = await ExternalVariables.Get(ExternalVariable.KubernetesClusterToken, cancellationToken); - + if (ExplicitExeVersion != null) { await DownloadExplicitHelmExecutable(); - helmVersion = new SemanticVersion(ExplicitExeVersion).Major == 2 ? HelmVersion.V2 : HelmVersion.V3; - } - else - { - helmVersion = GetVersion(); } async Task DownloadExplicitHelmExecutable() @@ -99,7 +93,7 @@ public virtual void SetUp() FileSystem.PurgeDirectory(packageExtractionDirectory, FailureOptions.ThrowOnFailure); Environment.SetEnvironmentVariable("TentacleJournal", - Path.Combine(StagingDirectory, "DeploymentJournal.xml")); + Path.Combine(StagingDirectory, "DeploymentJournal.xml")); Variables = new VariablesFactory(FileSystem).Create(new CommonOptions("test")); Variables.Set(TentacleVariables.Agent.ApplicationDirectoryPath, StagingDirectory); @@ -191,7 +185,6 @@ public void ValuesFromPackage_NewValuesUsed() Assert.AreEqual("Hello Variable Replaced In Package", result.CapturedOutput.OutputVariables["Message"]); } - [Test] [RequiresNonFreeBSDPlatform] [RequiresNon32BitWindows] @@ -251,7 +244,7 @@ public void ValuesFromPackageAndExplicit_ExplicitTakesPrecedence() Variables.Set(PackageVariables.IndexedPackageId("Pack-1"), "CustomValues"); Variables.Set(PackageVariables.IndexedPackageVersion("Pack-1"), "2.0.0"); Variables.Set(PackageVariables.IndexedOriginalPath("Pack-1"), - GetFixtureResource("Charts", "CustomValues.2.0.0.zip")); + GetFixtureResource("Charts", "CustomValues.2.0.0.zip")); Variables.Set(Kubernetes.SpecialVariables.Helm.Packages.ValuesFilePath("Pack-1"), "values.yaml"); //Variable that will replace packaged value in package @@ -357,7 +350,7 @@ public void WhenTheChartDirectoryVariableIsSet_AndTheChartDoesNotExist_AnErrorIs result.AssertFailure(); result.AssertOutputContains("Chart was not found in 'specific/location/for/my/chart'"); } - + [Test] [RequiresNonFreeBSDPlatform] [RequiresNon32BitWindows] @@ -398,18 +391,8 @@ void AddPostDeployMessageCheckAndCleanup(string explicitNamespace = null, bool d Variables.Set(KnownVariables.Package.EnabledFeatures, KnownVariables.Features.CustomScripts); } - string DeleteCommand(string @namespace, string releaseName) - { - switch (helmVersion) - { - case HelmVersion.V2: - return $"delete {releaseName} --purge"; - case HelmVersion.V3: - return $"uninstall {releaseName} --namespace {@namespace}"; - default: - throw new ArgumentOutOfRangeException(nameof(helmVersion), helmVersion, "Unrecognized Helm version"); - } - } + static string DeleteCommand(string @namespace, string releaseName) + => $"uninstall {releaseName} --namespace {@namespace}"; protected CalamariResult DeployPackage(string packageName = null) { @@ -419,14 +402,15 @@ protected CalamariResult DeployPackage(string packageName = null) { packageName = $"{Variables.Get(PackageVariables.PackageId)}-{Variables.Get(PackageVariables.PackageVersion)}.tgz"; } + Log.VerboseFormat("Deploying test chart from package: {0}", packageName); var pkg = GetFixtureResource("Charts", packageName); Variables.Save(variablesFile.FilePath); return InvokeInProcess(Calamari() - .Action("helm-upgrade") - .Argument("package", pkg) - .Argument("variables", variablesFile.FilePath)); + .Action("helm-upgrade") + .Argument("package", pkg) + .Argument("variables", variablesFile.FilePath)); } } @@ -441,17 +425,5 @@ static async Task DownloadHelmPackage(string version, string fileName) } } } - - static HelmVersion GetVersion() - { - StringBuilder stdout = new StringBuilder(); - var result = SilentProcessRunner.ExecuteCommand("helm", "version --client --short", Environment.CurrentDirectory, output => stdout.AppendLine(output), error => { }); - - result.ExitCode.Should().Be(0, $"Failed to retrieve version from Helm (Exit code {result.ExitCode}). Error output: \r\n{result.ErrorOutput}"); - - var version = stdout.ToString(); - return HelmVersionParser.ParseVersion(version) - ?? throw new InvalidOperationException($"Unsupported helm version '{version}'"); - } } -} +} \ No newline at end of file From 107aad2c1a381c0a2b0ecc06801e30fcc7dec182 Mon Sep 17 00:00:00 2001 From: Alastair Pitts Date: Sun, 10 Nov 2024 17:17:28 -0700 Subject: [PATCH 3/4] Remove empty folder reference --- source/Calamari.Tests/Calamari.Tests.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/source/Calamari.Tests/Calamari.Tests.csproj b/source/Calamari.Tests/Calamari.Tests.csproj index 095c25bcc..6bf283fc5 100644 --- a/source/Calamari.Tests/Calamari.Tests.csproj +++ b/source/Calamari.Tests/Calamari.Tests.csproj @@ -226,7 +226,6 @@ - From 7046d57fecb995757fe8141ce9003a71f530988d Mon Sep 17 00:00:00 2001 From: Alastair Pitts Date: Sun, 10 Nov 2024 17:18:29 -0700 Subject: [PATCH 4/4] Fix comment issue --- source/Calamari.Tests/KubernetesFixtures/HelmUpgradeFixture.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/Calamari.Tests/KubernetesFixtures/HelmUpgradeFixture.cs b/source/Calamari.Tests/KubernetesFixtures/HelmUpgradeFixture.cs index 3964bce15..5b04086e0 100644 --- a/source/Calamari.Tests/KubernetesFixtures/HelmUpgradeFixture.cs +++ b/source/Calamari.Tests/KubernetesFixtures/HelmUpgradeFixture.cs @@ -105,7 +105,8 @@ public virtual void SetUp() //Helm Options Variables.Set(Kubernetes.SpecialVariables.Helm.ReleaseName, ReleaseName); - //K8S AuthVariables.Set(Kubernetes.SpecialVariables.ClusterUrl, ServerUrl); + //K8S Auth + Variables.Set(Kubernetes.SpecialVariables.ClusterUrl, ServerUrl); Variables.Set(Kubernetes.SpecialVariables.SkipTlsVerification, "True"); Variables.Set(Kubernetes.SpecialVariables.Namespace, Namespace); Variables.Set(SpecialVariables.Account.AccountType, "Token");