Skip to content

Commit 03276e2

Browse files
feat: Add Octopus.Action.Kubernetes.VerboseOutput variable to log CLI tool output (#1690)
* add logSuccessAsInfo * feat: Add Octopus.Action.Kubernetes.VerboseOutput variable to log CLI tool output at Info level for Kubernetes steps * update the variable name
1 parent 9c5afa0 commit 03276e2

File tree

10 files changed

+49
-25
lines changed

10 files changed

+49
-25
lines changed

source/Calamari/Kubernetes/Integration/AwsCli.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Calamari.Common.Features.Processes;
55
using Calamari.Common.Plumbing;
66
using Calamari.Common.Plumbing.Logging;
7+
using Calamari.Common.Plumbing.Variables;
78
using Newtonsoft.Json;
89
using Newtonsoft.Json.Linq;
910
using Octopus.Versioning.Semver;
@@ -16,8 +17,9 @@ public AwsCli(
1617
ILog log,
1718
ICommandLineRunner commandLineRunner,
1819
string workingDirectory,
19-
Dictionary<string, string> environmentVars)
20-
: base(log, commandLineRunner, workingDirectory, environmentVars)
20+
Dictionary<string, string> environmentVars,
21+
IVariables variables)
22+
: base(log, commandLineRunner, workingDirectory, environmentVars, variables)
2123
{
2224
}
2325

source/Calamari/Kubernetes/Integration/AzureCli.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
using Calamari.Common.Plumbing;
66
using Calamari.Common.Plumbing.FileSystem;
77
using Calamari.Common.Plumbing.Logging;
8+
using Calamari.Common.Plumbing.Variables;
89

910
namespace Calamari.Kubernetes.Integration
1011
{
1112
public class AzureCli : CommandLineTool
1213
{
13-
public AzureCli(ILog log, ICommandLineRunner commandLineRunner, string workingDirectory, Dictionary<string, string> environmentVars)
14-
: base(log, commandLineRunner, workingDirectory, environmentVars)
14+
public AzureCli(ILog log, ICommandLineRunner commandLineRunner, string workingDirectory, Dictionary<string, string> environmentVars, IVariables variables)
15+
: base(log, commandLineRunner, workingDirectory, environmentVars, variables)
1516
{
1617
}
1718

source/Calamari/Kubernetes/Integration/CommandLineTool.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using Calamari.Common.Features.Processes;
44
using Calamari.Common.Plumbing.Logging;
5+
using Calamari.Common.Plumbing.Variables;
56

67
namespace Calamari.Kubernetes.Integration
78
{
@@ -10,23 +11,31 @@ public class CommandLineTool
1011
protected readonly ILog log;
1112
protected string workingDirectory;
1213
protected Dictionary<string, string> environmentVars;
14+
protected readonly IVariables variables;
1315

1416
readonly ICommandLineRunner commandLineRunner;
1517

1618
protected CommandLineTool(
1719
ILog log,
1820
ICommandLineRunner commandLineRunner,
1921
string workingDirectory,
20-
Dictionary<string, string> environmentVars)
22+
Dictionary<string, string> environmentVars,
23+
IVariables variables)
2124
{
2225
this.log = log;
2326
this.commandLineRunner = commandLineRunner;
2427
this.workingDirectory = workingDirectory;
2528
this.environmentVars = environmentVars;
29+
this.variables = variables;
2630
}
2731

2832
public string ExecutableLocation { get; protected set; }
2933

34+
protected virtual bool ShouldLogSuccessfulOutputAsInfo()
35+
{
36+
return variables.GetFlag(SpecialVariables.LogCliOutputAsInfo);
37+
}
38+
3039
protected CommandResult ExecuteCommandAndLogOutput(CommandLineInvocation invocation)
3140
=> ExecuteCommandAndLogOutput(invocation, false);
3241

@@ -52,7 +61,8 @@ CommandResult ExecuteCommandAndLogOutput(CommandLineInvocation invocation, bool
5261

5362
var result = commandLineRunner.Execute(invocation);
5463

55-
LogCapturedOutput(result, captureCommandOutput, logOutputAsVerbose);
64+
var logSuccessAsInfo = ShouldLogSuccessfulOutputAsInfo();
65+
LogCapturedOutput(result, captureCommandOutput, logOutputAsVerbose, logSuccessAsInfo);
5666

5767
return result;
5868
}
@@ -62,13 +72,21 @@ void LogCommandText(CommandLineInvocation invocation)
6272
log.Verbose(invocation.ToString());
6373
}
6474

65-
void LogCapturedOutput(CommandResult result, CaptureCommandOutput captureCommandOutput, bool logOutputAsVerbose)
75+
void LogCapturedOutput(CommandResult result, CaptureCommandOutput captureCommandOutput, bool logOutputAsVerbose, bool logSuccessAsInfo)
6676
{
6777
foreach (var message in captureCommandOutput.Messages)
6878
{
6979
if (result.ExitCode == 0)
7080
{
71-
log.Verbose(message.Text);
81+
// When logSuccessAsInfo is true, log successful output at Info level
82+
if (logSuccessAsInfo)
83+
{
84+
log.Info(message.Text);
85+
}
86+
else
87+
{
88+
log.Verbose(message.Text);
89+
}
7290
continue;
7391
}
7492

source/Calamari/Kubernetes/Integration/GCloud.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
using Calamari.Common.Plumbing;
77
using Calamari.Common.Plumbing.FileSystem;
88
using Calamari.Common.Plumbing.Logging;
9+
using Calamari.Common.Plumbing.Variables;
910

1011
namespace Calamari.Kubernetes.Integration
1112
{
1213
public class GCloud : CommandLineTool
1314
{
14-
private readonly ICalamariFileSystem fileSystem;
15+
readonly ICalamariFileSystem fileSystem;
1516

16-
public GCloud(ILog log, ICommandLineRunner commandLineRunner, ICalamariFileSystem fileSystem, string workingDirectory, Dictionary<string, string> environmentVars)
17-
: base(log, commandLineRunner, workingDirectory, environmentVars)
17+
public GCloud(ILog log, ICommandLineRunner commandLineRunner, ICalamariFileSystem fileSystem, string workingDirectory, Dictionary<string, string> environmentVars, IVariables variables)
18+
: base(log, commandLineRunner, workingDirectory, environmentVars, variables)
1819
{
1920
this.fileSystem = fileSystem;
2021
}

source/Calamari/Kubernetes/Integration/GkeGcloudAuthPlugin.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
using Calamari.Common.Features.Processes;
44
using Calamari.Common.Plumbing;
55
using Calamari.Common.Plumbing.Logging;
6+
using Calamari.Common.Plumbing.Variables;
67

78
namespace Calamari.Kubernetes.Integration
89
{
910
public class GkeGcloudAuthPlugin : CommandLineTool
1011
{
11-
public GkeGcloudAuthPlugin(ILog log, ICommandLineRunner commandLineRunner, string workingDirectory, Dictionary<string, string> environmentVars)
12-
: base(log, commandLineRunner, workingDirectory, environmentVars)
12+
public GkeGcloudAuthPlugin(ILog log, ICommandLineRunner commandLineRunner, string workingDirectory, Dictionary<string, string> environmentVars, IVariables variables)
13+
: base(log, commandLineRunner, workingDirectory, environmentVars, variables)
1314
{
1415
}
1516

source/Calamari/Kubernetes/Integration/HelmCli.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ public class HelmCli : CommandLineTool
2323
{
2424
readonly ICommandLineRunner commandLineRunner;
2525
readonly ICalamariFileSystem fileSystem;
26-
readonly IVariables variables;
2726
bool isCustomExecutable;
2827

2928
public HelmCli(ILog log, ICommandLineRunner commandLineRunner, RunningDeployment runningDeployment, ICalamariFileSystem fileSystem)
30-
: base(log, commandLineRunner, runningDeployment.CurrentDirectory, runningDeployment.EnvironmentVariables)
29+
: base(log, commandLineRunner, runningDeployment.CurrentDirectory, runningDeployment.EnvironmentVariables, runningDeployment.Variables)
3130
{
3231
this.commandLineRunner = commandLineRunner;
3332
this.fileSystem = fileSystem;
34-
variables = runningDeployment.Variables;
3533
ExecutableLocation = SetExecutable();
3634
}
3735

@@ -116,7 +114,7 @@ public string GetManifest(string releaseName, int revisionNumber)
116114

117115
return result.Output.MergeInfoLogs();
118116
}
119-
117+
120118
public CommandResult Upgrade(string releaseName, string packagePath, IEnumerable<string> upgradeArgs)
121119
{
122120
var buildArgs = new List<string>

source/Calamari/Kubernetes/Integration/Kubectl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public Kubectl(IVariables variables,
3131
ILog log,
3232
ICommandLineRunner commandLineRunner,
3333
string workingDirectory,
34-
Dictionary<string, string> environmentVariables) : base(log, commandLineRunner, workingDirectory, environmentVariables)
34+
Dictionary<string, string> environmentVariables) : base(log, commandLineRunner, workingDirectory, environmentVariables, variables)
3535
{
3636
customKubectlExecutable = variables.Get("Octopus.Action.Kubernetes.CustomKubectlExecutable");
3737
}

source/Calamari/Kubernetes/Integration/Kubelogin.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
using Calamari.Common.Features.Processes;
55
using Calamari.Common.Plumbing;
66
using Calamari.Common.Plumbing.Logging;
7+
using Calamari.Common.Plumbing.Variables;
78

89
namespace Calamari.Kubernetes.Integration
910
{
1011
public class KubeLogin : CommandLineTool
1112
{
12-
public KubeLogin(ILog log, ICommandLineRunner commandLineRunner, string workingDirectory, Dictionary<string, string> environmentVars)
13-
: base(log, commandLineRunner, workingDirectory, environmentVars)
13+
public KubeLogin(ILog log, ICommandLineRunner commandLineRunner, string workingDirectory, Dictionary<string, string> environmentVars, IVariables variables)
14+
: base(log, commandLineRunner, workingDirectory, environmentVars, variables)
1415
{
1516
}
1617

source/Calamari/Kubernetes/SetupKubectlAuthentication.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ void SetupGCloudContext(string @namespace)
174174
commandLineRunner,
175175
fileSystem,
176176
workingDirectory,
177-
environmentVars);
178-
var gkeGcloudAuthPlugin = new GkeGcloudAuthPlugin(log, commandLineRunner, workingDirectory, environmentVars);
177+
environmentVars,
178+
variables);
179+
var gkeGcloudAuthPlugin = new GkeGcloudAuthPlugin(log, commandLineRunner, workingDirectory, environmentVars, variables);
179180
var gcloudAuth = new GoogleKubernetesEngineAuth(gcloudCli,
180181
gkeGcloudAuthPlugin,
181182
kubectl,
@@ -186,8 +187,8 @@ void SetupGCloudContext(string @namespace)
186187

187188
void SetupAzureContext(string @namespace, string kubeConfig)
188189
{
189-
var azureCli = new AzureCli(log, commandLineRunner, workingDirectory, environmentVars);
190-
var kubeloginCli = new KubeLogin(log, commandLineRunner, workingDirectory, environmentVars);
190+
var azureCli = new AzureCli(log, commandLineRunner, workingDirectory, environmentVars, variables);
191+
var kubeloginCli = new KubeLogin(log, commandLineRunner, workingDirectory, environmentVars, variables);
191192
var azureAuth = new AzureKubernetesServicesAuth(azureCli, kubectl, kubeloginCli, variables);
192193
azureAuth.Configure(@namespace, kubeConfig);
193194
}
@@ -317,7 +318,7 @@ void SetupContextForUsernamePassword(string user)
317318

318319
void SetupAwsContext(string @namespace, string clusterUrl, string user)
319320
{
320-
var awsCli = new AwsCli(log, commandLineRunner, workingDirectory, environmentVars);
321+
var awsCli = new AwsCli(log, commandLineRunner, workingDirectory, environmentVars, variables);
321322
var awsAuth = new AwsCliAuth(awsCli, kubectl, variables, environmentVars, log);
322323

323324
awsAuth.Configure(@namespace, clusterUrl, user);

source/Calamari/Kubernetes/SpecialVariables.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static class SpecialVariables
1818
public const string OutputKubeConfig = "Octopus.Action.Kubernetes.OutputKubeConfig";
1919
public const string CustomKubectlExecutable = "Octopus.Action.Kubernetes.CustomKubectlExecutable";
2020
public const string ResourceStatusCheck = "Octopus.Action.Kubernetes.ResourceStatusCheck";
21+
public const string LogCliOutputAsInfo = "Octopus.Action.Kubernetes.LogCliOutputAsInfo";
2122
public const string DeploymentStyle = "Octopus.Action.KubernetesContainers.DeploymentStyle";
2223
public const string DeploymentWait = "Octopus.Action.KubernetesContainers.DeploymentWait";
2324
public const string CustomResourceYamlFileName = "Octopus.Action.KubernetesContainers.CustomResourceYamlFileName";

0 commit comments

Comments
 (0)