-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Report manifests applied via service messages (#1330)
* Report manifests applied via service messages * typo * Send manifests as JSON --------- Co-authored-by: Eddy Moulton <[email protected]>
- Loading branch information
1 parent
b3c520b
commit 082dcf6
Showing
9 changed files
with
274 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
source/Calamari.Tests/KubernetesFixtures/ManifestReporterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Calamari.Common.Plumbing.FileSystem; | ||
using Calamari.Common.Plumbing.ServiceMessages; | ||
using Calamari.Common.Plumbing.Variables; | ||
using Calamari.Kubernetes; | ||
using Calamari.Testing.Helpers; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
|
||
namespace Calamari.Tests.KubernetesFixtures | ||
{ | ||
[TestFixture] | ||
public class ManifestReporterTests | ||
{ | ||
[Test] | ||
public void GivenValidYaml_ShouldPostSingleServiceMessage() | ||
{ | ||
var memoryLog = new InMemoryLog(); | ||
var variables = new CalamariVariables(); | ||
|
||
var yaml = @"foo: bar"; | ||
var expectedJson = "{\"foo\": \"bar\"}"; | ||
using (CreateFile(yaml, out var filePath)) | ||
{ | ||
var mr = new ManifestReporter(variables, CalamariPhysicalFileSystem.GetPhysicalFileSystem(), memoryLog); | ||
|
||
mr.ReportManifestApplied(filePath); | ||
|
||
var expected = ServiceMessage.Create(SpecialVariables.ServiceMessageNames.ManifestApplied.Name, ("ns", "default"), ("manifest", expectedJson)); | ||
memoryLog.ServiceMessages.Should().BeEquivalentTo(new List<ServiceMessage> { expected }); | ||
} | ||
} | ||
|
||
[Test] | ||
public void GivenInValidManifest_ShouldNotPostServiceMessage() | ||
{ | ||
var memoryLog = new InMemoryLog(); | ||
var variables = new CalamariVariables(); | ||
|
||
var yaml = @"text - Bar"; | ||
using (CreateFile(yaml, out var filePath)) | ||
{ | ||
var mr = new ManifestReporter(variables, CalamariPhysicalFileSystem.GetPhysicalFileSystem(), memoryLog); | ||
|
||
mr.ReportManifestApplied(filePath); | ||
|
||
memoryLog.ServiceMessages.Should().BeEmpty(); | ||
} | ||
} | ||
|
||
[Test] | ||
public void GivenNamespaceInManifest_ShouldReportManifestNamespace() | ||
{ | ||
var memoryLog = new InMemoryLog(); | ||
var variables = new CalamariVariables(); | ||
var yaml = @"metadata: | ||
name: game-demo | ||
namespace: XXX"; | ||
using (CreateFile(yaml, out var filePath)) | ||
{ | ||
var variableNs = Some.String(); | ||
variables.Set(SpecialVariables.Namespace, variableNs); | ||
var mr = new ManifestReporter(variables, CalamariPhysicalFileSystem.GetPhysicalFileSystem(), memoryLog); | ||
|
||
mr.ReportManifestApplied(filePath); | ||
|
||
memoryLog.ServiceMessages.First().Properties.Should().Contain(new KeyValuePair<string, string>("ns", "XXX")); | ||
} | ||
} | ||
|
||
[Test] | ||
public void GivenNamespaceNotInManifest_ShouldReportVariableNamespace() | ||
{ | ||
var memoryLog = new InMemoryLog(); | ||
var variables = new CalamariVariables(); | ||
var yaml = @"foo: bar"; | ||
using (CreateFile(yaml, out var filePath)) | ||
{ | ||
var variableNs = Some.String(); | ||
variables.Set(SpecialVariables.Namespace, variableNs); | ||
var mr = new ManifestReporter(variables, CalamariPhysicalFileSystem.GetPhysicalFileSystem(), memoryLog); | ||
|
||
mr.ReportManifestApplied(filePath); | ||
|
||
memoryLog.ServiceMessages.First().Properties.Should().Contain(new KeyValuePair<string, string>("ns", variableNs)); | ||
} | ||
} | ||
|
||
[Test] | ||
public void GiveNoNamespaces_ShouldDefaultNamespace() | ||
{ | ||
var memoryLog = new InMemoryLog(); | ||
var variables = new CalamariVariables(); | ||
var yaml = @"foo: bar"; | ||
using (CreateFile(yaml, out var filePath)) | ||
{ | ||
var mr = new ManifestReporter(variables, CalamariPhysicalFileSystem.GetPhysicalFileSystem(), memoryLog); | ||
|
||
mr.ReportManifestApplied(filePath); | ||
|
||
memoryLog.ServiceMessages.First().Properties.Should().Contain(new KeyValuePair<string, string>("ns", "default")); | ||
} | ||
} | ||
|
||
IDisposable CreateFile(string yaml, out string filePath) | ||
{ | ||
var tempDir = TemporaryDirectory.Create(); | ||
filePath = Path.Combine(tempDir.DirectoryPath, $"{Guid.NewGuid():d}.tmp"); | ||
File.WriteAllText(filePath, yaml); | ||
return tempDir; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Threading; | ||
|
||
namespace Calamari.Tests | ||
{ | ||
public static class Some | ||
{ | ||
static int next; | ||
|
||
public static string String() => "S__" + Int(); | ||
|
||
public static int Int() => Interlocked.Increment(ref next); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using Calamari.Common.Plumbing.FileSystem; | ||
using Calamari.Common.Plumbing.Logging; | ||
using Calamari.Common.Plumbing.ServiceMessages; | ||
using Calamari.Common.Plumbing.Variables; | ||
using YamlDotNet.Core; | ||
using YamlDotNet.RepresentationModel; | ||
using YamlDotNet.Serialization; | ||
using YamlDotNet.Serialization.NamingConventions; | ||
|
||
namespace Calamari.Kubernetes | ||
{ | ||
public interface IManifestReporter | ||
{ | ||
void ReportManifestApplied(string filePath); | ||
} | ||
|
||
public class ManifestReporter : IManifestReporter | ||
{ | ||
readonly IVariables variables; | ||
readonly ICalamariFileSystem fileSystem; | ||
readonly ILog log; | ||
|
||
static readonly IDeserializer YamlDeserializer = new Deserializer(); | ||
|
||
static readonly ISerializer YamlSerializer = new SerializerBuilder() | ||
.WithNamingConvention(CamelCaseNamingConvention.Instance) | ||
.JsonCompatible() | ||
.Build(); | ||
|
||
public ManifestReporter(IVariables variables, ICalamariFileSystem fileSystem, ILog log) | ||
{ | ||
this.variables = variables; | ||
this.fileSystem = fileSystem; | ||
this.log = log; | ||
} | ||
|
||
string GetNamespace(YamlMappingNode yamlRoot) | ||
{ | ||
var implicitNamespace = variables.Get(SpecialVariables.Namespace) ?? "default"; | ||
|
||
if (yamlRoot.Children.TryGetValue("metadata", out var metadataNode) && metadataNode is YamlMappingNode metadataMappingNode && | ||
metadataMappingNode.Children.TryGetValue("namespace", out var namespaceNode) && namespaceNode is YamlScalarNode namespaceScalarNode && | ||
!string.IsNullOrWhiteSpace(namespaceScalarNode.Value)) | ||
{ | ||
implicitNamespace = namespaceScalarNode.Value; | ||
} | ||
|
||
return implicitNamespace; | ||
} | ||
|
||
public void ReportManifestApplied(string filePath) | ||
{ | ||
using (var yamlFile = fileSystem.OpenFile(filePath, FileAccess.ReadWrite)) | ||
{ | ||
try | ||
{ | ||
var yamlStream = new YamlStream(); | ||
yamlStream.Load(new StreamReader(yamlFile)); | ||
|
||
foreach (var document in yamlStream.Documents) | ||
{ | ||
if (!(document.RootNode is YamlMappingNode rootNode)) | ||
{ | ||
log.Warn("Could not parse manifest, resources will not be added to live object status"); | ||
continue; | ||
} | ||
|
||
var updatedDocument = YamlNodeToJson(rootNode); | ||
|
||
var ns = GetNamespace(rootNode); | ||
log.WriteServiceMessage(new ServiceMessage(SpecialVariables.ServiceMessageNames.ManifestApplied.Name, | ||
new Dictionary<string, string> | ||
{ | ||
{ SpecialVariables.ServiceMessageNames.ManifestApplied.ManifestAttribute, updatedDocument }, | ||
{ SpecialVariables.ServiceMessageNames.ManifestApplied.NamespaceAttribute, ns } | ||
})); | ||
} | ||
} | ||
catch (SemanticErrorException) | ||
{ | ||
log.Warn("Invalid YAML syntax found, resources will not be added to live object status"); | ||
} | ||
} | ||
} | ||
|
||
static string YamlNodeToJson(YamlNode node) | ||
{ | ||
var stream = new YamlStream { new YamlDocument(node) }; | ||
using (var writer = new StringWriter()) | ||
{ | ||
stream.Save(writer); | ||
|
||
using (var reader = new StringReader(writer.ToString())) | ||
{ | ||
var yamlObject = YamlDeserializer.Deserialize(reader); | ||
return yamlObject is null ? string.Empty : YamlSerializer.Serialize(yamlObject).Trim(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.