From 7899373606e64492dd18b70e75bdd12fe50efeda Mon Sep 17 00:00:00 2001 From: Justin Kotalik Date: Tue, 9 Jun 2020 15:41:16 -0700 Subject: [PATCH] Fix undeploy (#537) --- src/Microsoft.Tye.Core/ApplicationFactory.cs | 2 +- .../ConfigModel/ConfigFactory.cs | 2 ++ .../ConfigModel/NameInferer.cs | 27 +++++++++++++++++++ .../Serialization/YamlParser.cs | 1 + src/tye/InitHost.cs | 4 --- src/tye/UndeployHost.cs | 12 +++++---- 6 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 src/Microsoft.Tye.Core/ConfigModel/NameInferer.cs diff --git a/src/Microsoft.Tye.Core/ApplicationFactory.cs b/src/Microsoft.Tye.Core/ApplicationFactory.cs index bd693c97..728c890b 100644 --- a/src/Microsoft.Tye.Core/ApplicationFactory.cs +++ b/src/Microsoft.Tye.Core/ApplicationFactory.cs @@ -28,7 +28,7 @@ namespace Microsoft.Tye var rootConfig = ConfigFactory.FromFile(source); rootConfig.Validate(); - var root = new ApplicationBuilder(source, rootConfig.Name ?? source.Directory.Name.ToLowerInvariant()); + var root = new ApplicationBuilder(source, rootConfig.Name!); root.Namespace = rootConfig.Namespace; queue.Enqueue((rootConfig, new HashSet())); diff --git a/src/Microsoft.Tye.Core/ConfigModel/ConfigFactory.cs b/src/Microsoft.Tye.Core/ConfigModel/ConfigFactory.cs index 8ba8f65c..49a22322 100644 --- a/src/Microsoft.Tye.Core/ConfigModel/ConfigFactory.cs +++ b/src/Microsoft.Tye.Core/ConfigModel/ConfigFactory.cs @@ -39,6 +39,7 @@ namespace Microsoft.Tye.ConfigModel var application = new ConfigApplication() { Source = file, + Name = NameInferer.InferApplicationName(file) }; var service = new ConfigService() @@ -57,6 +58,7 @@ namespace Microsoft.Tye.ConfigModel var application = new ConfigApplication() { Source = file, + Name = NameInferer.InferApplicationName(file) }; // BE CAREFUL modifying this code. Avoid proliferating MSBuild types diff --git a/src/Microsoft.Tye.Core/ConfigModel/NameInferer.cs b/src/Microsoft.Tye.Core/ConfigModel/NameInferer.cs new file mode 100644 index 00000000..2dd4195a --- /dev/null +++ b/src/Microsoft.Tye.Core/ConfigModel/NameInferer.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.IO; + +namespace Microsoft.Tye.ConfigModel +{ + internal static class NameInferer + { + public static string? InferApplicationName(FileInfo fileInfo) + { + if (fileInfo == null) + { + return null; + } + + var extension = fileInfo.Extension; + if (extension == ".sln" || extension == ".csproj" || extension == ".fsproj") + { + return Path.GetFileNameWithoutExtension(fileInfo.Name).ToLowerInvariant(); + } + + return fileInfo.Directory.Parent.Name.ToLowerInvariant(); + } + } +} diff --git a/src/Microsoft.Tye.Core/Serialization/YamlParser.cs b/src/Microsoft.Tye.Core/Serialization/YamlParser.cs index 78b22470..089f8e14 100644 --- a/src/Microsoft.Tye.Core/Serialization/YamlParser.cs +++ b/src/Microsoft.Tye.Core/Serialization/YamlParser.cs @@ -54,6 +54,7 @@ namespace Tye.Serialization ConfigApplicationParser.HandleConfigApplication((YamlMappingNode)node, app); app.Source = _fileInfo!; + app.Name ??= NameInferer.InferApplicationName(_fileInfo!); // TODO confirm if these are ever null. foreach (var service in app.Services) diff --git a/src/tye/InitHost.cs b/src/tye/InitHost.cs index 43504236..43634028 100644 --- a/src/tye/InitHost.cs +++ b/src/tye/InitHost.cs @@ -64,7 +64,6 @@ services: if (extension == ".sln" || extension == ".csproj" || extension == ".fsproj") { // If the input file is a project or solution then use that as the name - application.Name = Path.GetFileNameWithoutExtension(path.Name).ToLowerInvariant(); application.Extensions = null!; application.Ingress = null!; @@ -81,9 +80,6 @@ services: } else { - // If the input file is a yaml, then use the directory name. - application.Name = path.Directory.Name.ToLowerInvariant(); - // If the input file is a yaml, then replace it. outputFilePath = path.FullName; } diff --git a/src/tye/UndeployHost.cs b/src/tye/UndeployHost.cs index 3240f2c9..bcf32be6 100644 --- a/src/tye/UndeployHost.cs +++ b/src/tye/UndeployHost.cs @@ -27,7 +27,7 @@ namespace Microsoft.Tye // We don't need to know anything about the services, just the application name. var application = ConfigFactory.FromFile(path); - if (!String.IsNullOrEmpty(@namespace)) + if (!string.IsNullOrEmpty(@namespace)) { application.Namespace = @namespace; } @@ -72,12 +72,14 @@ namespace Microsoft.Tye // - handcrafting requests to delete each resource var resources = new List(); + var applicationName = application.Name; + try { output.WriteDebugLine("Querying services"); var response = await kubernetes.ListNamespacedServiceWithHttpMessagesAsync( config.Namespace, - labelSelector: $"app.kubernetes.io/part-of={application.Name}"); + labelSelector: $"app.kubernetes.io/part-of={applicationName}"); foreach (var resource in response.Body.Items) { @@ -99,7 +101,7 @@ namespace Microsoft.Tye output.WriteDebugLine("Querying deployments"); var response = await kubernetes.ListNamespacedDeploymentWithHttpMessagesAsync( config.Namespace, - labelSelector: $"app.kubernetes.io/part-of={application.Name}"); + labelSelector: $"app.kubernetes.io/part-of={applicationName}"); foreach (var resource in response.Body.Items) { @@ -121,7 +123,7 @@ namespace Microsoft.Tye output.WriteDebugLine("Querying secrets"); var response = await kubernetes.ListNamespacedSecretWithHttpMessagesAsync( config.Namespace, - labelSelector: $"app.kubernetes.io/part-of={application.Name}"); + labelSelector: $"app.kubernetes.io/part-of={applicationName}"); foreach (var resource in response.Body.Items) { @@ -144,7 +146,7 @@ namespace Microsoft.Tye output.WriteDebugLine("Querying ingresses"); var response = await kubernetes.ListNamespacedIngressWithHttpMessagesAsync( config.Namespace, - labelSelector: $"app.kubernetes.io/part-of={application.Name}"); + labelSelector: $"app.kubernetes.io/part-of={applicationName}"); foreach (var resource in response.Body.Items) {