Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robe/manifest as servicemessage #1315

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,17 @@ public static class CalamariDeploymentMetric
public const string MetricAttribute = "metric";
public const string ValueAttribute = "value";
}

public static class Kubernetes
{


public static class AppliedManifest
{
public const string Name = "k8s-applied-manifest";
public const string Yaml = "yaml";
}
public const string ResourceStatus = "k8s-status";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
using System.Threading.Tasks;
using Assent;
using Calamari.Aws.Kubernetes.Discovery;
using Calamari.CloudAccounts;
using Calamari.Common.FeatureToggles;
using Calamari.Common.Plumbing.ServiceMessages;
using Calamari.Common.Plumbing.Variables;
using Calamari.Deployment;
using Calamari.Kubernetes.Commands;
Expand Down Expand Up @@ -136,7 +135,7 @@ public void DeployRawYaml_WithRawYamlDeploymentScriptOrCommand_OutputShouldIndic

AssertObjectStatusMonitoringStarted(rawLogs, (SimpleDeploymentResourceType, SimpleDeploymentResourceName));

var objectStatusUpdates = Log.Messages.GetServiceMessagesOfType("k8s-status");
var objectStatusUpdates = Log.Messages.GetServiceMessagesOfType(ServiceMessageNames.Kubernetes.ResourceStatus);

objectStatusUpdates.Where(m => m.Properties["status"] == "Successful").Should().HaveCount(5);

Expand Down Expand Up @@ -273,7 +272,7 @@ string CreatePackageWithMultipleYamlFiles(string directory)
(Name: SimpleConfigMap2ResourceName, Label: "ConfigMap3")
};

var statusMessages = Log.Messages.GetServiceMessagesOfType("k8s-status");
var statusMessages = Log.Messages.GetServiceMessagesOfType(ServiceMessageNames.Kubernetes.ResourceStatus);

foreach (var (name, label) in resources)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Calamari.Common.Plumbing.ServiceMessages;
using Calamari.Common.Plumbing.Variables;
using Calamari.Kubernetes;
using Calamari.Kubernetes.ResourceStatus;
Expand Down Expand Up @@ -29,7 +30,7 @@ public void ReportsCreatedResourcesCorrectly()
reporter.ReportUpdatedResources(originalStatuses, newStatuses, 1);

var serviceMessages = log.ServiceMessages
.Where(message => message.Name == SpecialVariables.KubernetesResourceStatusServiceMessageName)
.Where(message => message.Name == ServiceMessageNames.Kubernetes.ResourceStatus)
.ToList();

serviceMessages.Select(message => message.Properties["name"])
Expand Down Expand Up @@ -72,7 +73,7 @@ public void ReportsUpdatedResourcesCorrectly()
reporter.ReportUpdatedResources(originalStatuses, newStatuses, 1);

var serviceMessages = log.ServiceMessages
.Where(message => message.Name == SpecialVariables.KubernetesResourceStatusServiceMessageName)
.Where(message => message.Name == ServiceMessageNames.Kubernetes.ResourceStatus)
.ToList();

serviceMessages.Should().ContainSingle().Which.Properties
Expand Down Expand Up @@ -102,7 +103,7 @@ public void ReportsRemovedResourcesCorrectly()
reporter.ReportUpdatedResources(originalStatuses, newStatuses, 1);

var serviceMessages = log.ServiceMessages
.Where(message => message.Name == SpecialVariables.KubernetesResourceStatusServiceMessageName)
.Where(message => message.Name == ServiceMessageNames.Kubernetes.ResourceStatus)
.ToList();

serviceMessages.Should().ContainSingle().Which.Properties
Expand All @@ -129,7 +130,7 @@ public void ClusterScopedResourcesAreIgnored()
reporter.ReportUpdatedResources(new Dictionary<string, Resource>(), newStatuses, 1);

var serviceMessages = log.ServiceMessages
.Where(message => message.Name == SpecialVariables.KubernetesResourceStatusServiceMessageName)
.Where(message => message.Name == ServiceMessageNames.Kubernetes.ResourceStatus)
.ToList();

serviceMessages.Should().ContainSingle().Which.Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@
using Calamari.Common.Commands;
using Calamari.Common.Plumbing.FileSystem;
using Calamari.Common.Plumbing.Logging;
using Calamari.Common.Plumbing.ServiceMessages;
using Calamari.Common.Plumbing.Variables;
using Calamari.Kubernetes.Integration;
using Calamari.Kubernetes.ResourceStatus.Resources;
using Octopus.CoreUtilities.Extensions;
using YamlDotNet.Core;
using YamlDotNet.RepresentationModel;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace Calamari.Kubernetes.Commands.Executors
{
public interface IRawYamlKubernetesApplyExecutor : IKubernetesApplyExecutor
{
}

class GatherAndApplyRawYamlExecutor : BaseKubernetesApplyExecutor, IRawYamlKubernetesApplyExecutor
{
const string GroupedDirectoryName = "grouped";
Expand All @@ -40,12 +45,12 @@ protected override async Task<IEnumerable<ResourceIdentifier>> ApplyAndGetResour
{
var variables = deployment.Variables;
var globs = variables.GetPaths(SpecialVariables.CustomResourceYamlFileName);

if (globs.IsNullOrEmpty())
return Enumerable.Empty<ResourceIdentifier>();

var directories = GroupFilesIntoDirectories(deployment, globs, variables);

var resourcesIdentifiers = new HashSet<ResourceIdentifier>();
foreach (var directory in directories)
{
Expand All @@ -55,7 +60,7 @@ protected override async Task<IEnumerable<ResourceIdentifier>> ApplyAndGetResour
{
await appliedResourcesCallback(res);
}

resourcesIdentifiers.UnionWith(res);
}

Expand All @@ -66,13 +71,12 @@ IEnumerable<GlobDirectory> GroupFilesIntoDirectories(RunningDeployment deploymen
{
var stagingDirectory = deployment.CurrentDirectory;
var packageDirectory =
Path.Combine(stagingDirectory, KubernetesDeploymentCommandBase.PackageDirectoryName) +
Path.DirectorySeparatorChar;
Path.Combine(stagingDirectory, KubernetesDeploymentCommandBase.PackageDirectoryName) + Path.DirectorySeparatorChar;

var directories = new List<GlobDirectory>();
for (var i = 1; i <= globs.Count; i ++)
for (var i = 1; i <= globs.Count; i++)
{
var glob = globs[i-1];
var glob = globs[i - 1];
var directoryPath = Path.Combine(stagingDirectory, GroupedDirectoryName, i.ToString());
var directory = new GlobDirectory(i, glob, directoryPath);
fileSystem.CreateDirectory(directoryPath);
Expand All @@ -87,14 +91,15 @@ IEnumerable<GlobDirectory> GroupFilesIntoDirectories(RunningDeployment deploymen
{
fileSystem.CreateDirectory(targetDirectory);
}

fileSystem.CopyFile(file, targetPath);
}

directories.Add(directory);
}

variables.Set(SpecialVariables.GroupedYamlDirectories,
string.Join(";", directories.Select(d => d.Directory)));
string.Join(";", directories.Select(d => d.Directory)));
return directories;
}

Expand All @@ -114,9 +119,46 @@ IEnumerable<ResourceIdentifier> ApplyBatchAndReturnResourceIdentifiers(RunningDe
foreach (var file in files)
{
log.Verbose($"Matched file: {fileSystem.GetRelativePath(directoryWithTrailingSlash, file)}");

var serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();

// var updatedDocuments = new List<string>();

using (var yamlFile = fileSystem.OpenFile(file, FileAccess.ReadWrite))
{
try
{
var yamlStream = new YamlStream();
yamlStream.Load(new StreamReader(yamlFile));
foreach (var document in yamlStream.Documents)
{
if (!(document.RootNode is YamlMappingNode rootNode))
{
continue;
}

var updatedDocument = serializer.Serialize(rootNode);

log.WriteServiceMessage(new ServiceMessage(ServiceMessageNames.Kubernetes.AppliedManifest.Name,
new Dictionary<string, string>
{
{ ServiceMessageNames.Kubernetes.AppliedManifest.Yaml, updatedDocument }
}));
}
}
catch (SemanticErrorException)
{
log.Warn("Invalid YAML syntax found, resources will not be added to live object status");
}
}

// TODO: EM - Add octopus labels and re-write file
// fileSystem.OverwriteFile(file, string.Join("\n---\n", updatedDocuments));
}

string[] executeArgs = {"apply", "-f", $@"""{globDirectory.Directory}""", "--recursive", "-o", "json"};
string[] executeArgs = { "apply", "-f", $@"""{globDirectory.Directory}""", "--recursive", "-o", "json" };
executeArgs = executeArgs.AddOptionsForServerSideApply(deployment.Variables, log);

var result = kubectl.ExecuteCommandAndReturnOutput(executeArgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ private void SendServiceMessage(Resource resource, bool removed, int checkCount)

var parameters = new Dictionary<string, string>
{
{"type", "k8s-status"},
{"actionId", variables.Get("Octopus.Action.Id")},
{"stepName", $"Step {stepNumber}: {stepName}"},
{"taskId", variables.Get(KnownVariables.ServerTask.Id)},
Expand All @@ -99,7 +98,7 @@ private void SendServiceMessage(Resource resource, bool removed, int checkCount)
{"checkCount", checkCount.ToString()}
};

var message = new ServiceMessage(SpecialVariables.KubernetesResourceStatusServiceMessageName, parameters);
var message = new ServiceMessage(ServiceMessageNames.Kubernetes.ResourceStatus, parameters);
log.WriteServiceMessage(message);
}
}
Expand Down
2 changes: 0 additions & 2 deletions source/Calamari/Kubernetes/SpecialVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public static class SpecialVariables
public const string KubeConfig = "Octopus.KubeConfig.Path";
public const string KustomizeManifest = "Octopus.Kustomize.Manifest.Path";

public const string KubernetesResourceStatusServiceMessageName = "k8s-status";

public const string ServerSideApplyEnabled = "Octopus.Action.Kubernetes.ServerSideApply.Enabled";
public const string ServerSideApplyForceConflicts = "Octopus.Action.Kubernetes.ServerSideApply.ForceConflicts";

Expand Down