Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Add the ability to specify a namespace for tye deploy. #400

Merged
merged 1 commit into from
Apr 24, 2020
Merged
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
2 changes: 2 additions & 0 deletions src/Microsoft.Tye.Core/ApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public ApplicationBuilder(FileInfo source, string name)

public string Name { get; set; }

public string? Namespace { get; set; }

public ContainerRegistry? Registry { get; set; }

public List<ExtensionConfiguration> Extensions { get; } = new List<ExtensionConfiguration>();
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Tye.Core/ApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static async Task<ApplicationBuilder> CreateAsync(OutputContext output, F
rootConfig.Validate();

var root = new ApplicationBuilder(source, rootConfig.Name ?? source.Directory.Name.ToLowerInvariant());

root.Namespace = rootConfig.Namespace;
queue.Enqueue(rootConfig);

while (queue.Count > 0)
Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.Tye.Core/ConfigModel/ConfigApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class ConfigApplication

public string? Name { get; set; }

public string? Namespace { get; set; }

public string? Registry { get; set; }

public string? Network { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.Tye.Core/GenerateKubernetesManifestStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public sealed class GenerateKubernetesManifestStep : ServiceExecutor.Step

public string Environment { get; set; } = "production";

public string? Namespace { get; set; } = null;


public override Task ExecuteAsync(OutputContext output, ApplicationBuilder application, ServiceBuilder service)
{
Expand Down
6 changes: 6 additions & 0 deletions src/Microsoft.Tye.Core/KubernetesManifestGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public static ServiceOutput CreateService(
var metadata = new YamlMappingNode();
root.Add("metadata", metadata);
metadata.Add("name", project.Name);
if (!String.IsNullOrEmpty(application.Namespace)) {
metadata.Add("namespace", application.Namespace);
}

if (service.Annotations.Count > 0)
{
Expand Down Expand Up @@ -104,6 +107,9 @@ public static ServiceOutput CreateDeployment(
var metadata = new YamlMappingNode();
root.Add("metadata", metadata);
metadata.Add("name", project.Name);
if (!String.IsNullOrEmpty(application.Namespace)) {
metadata.Add("namespace", application.Namespace);
}

if (deployment.Annotations.Count > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public static void HandleConfigApplication(YamlMappingNode yamlMappingNode, Conf
case "name":
app.Name = YamlParser.GetScalarValue(key, child.Value);
break;
case "namespace":
app.Namespace = YamlParser.GetScalarValue(key, child.Value);
break;
case "network":
app.Network = YamlParser.GetScalarValue(key, child.Value);
break;
Expand Down
13 changes: 13 additions & 0 deletions src/Microsoft.Tye.Core/StandardOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,18 @@ public static Option Verbosity
};
}
}

public static Option Namespace
{
get
{
return new Option(new[] { "-n", "--namespace" })
{
Description = "Specify the namespace for the deployment",
Required = false,
Argument = new Argument<string>(),
};
}
}
}
}
8 changes: 6 additions & 2 deletions src/tye/GenerateHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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;
using System.Collections.Generic;
using System.CommandLine;
using System.IO;
Expand All @@ -12,7 +13,7 @@ namespace Microsoft.Tye
{
public static class GenerateHost
{
public static async Task GenerateAsync(IConsole console, FileInfo path, Verbosity verbosity, bool interactive)
public static async Task GenerateAsync(IConsole console, FileInfo path, Verbosity verbosity, bool interactive, string ns)
{
var output = new OutputContext(console, verbosity);

Expand All @@ -22,7 +23,10 @@ public static async Task GenerateAsync(IConsole console, FileInfo path, Verbosit
{
throw new CommandException($"No services found in \"{application.Source.Name}\"");
}

if (!String.IsNullOrEmpty(ns))
{
application.Namespace = ns;
}
await ExecuteGenerateAsync(output, application, environment: "production", interactive);
}

Expand Down
18 changes: 14 additions & 4 deletions src/tye/Program.DeployCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
Expand All @@ -21,6 +22,7 @@ public static Command CreateDeployCommand()
CommonArguments.Path_Required,
StandardOptions.Interactive,
StandardOptions.Verbosity,
StandardOptions.Namespace,
};

command.AddOption(new Option(new[] { "-f", "--force" })
Expand All @@ -29,7 +31,7 @@ public static Command CreateDeployCommand()
Required = false
});

command.Handler = CommandHandler.Create<IConsole, FileInfo, Verbosity, bool, bool>(async (console, path, verbosity, interactive, force) =>
command.Handler = CommandHandler.Create<IConsole, FileInfo, Verbosity, bool, bool, string>(async (console, path, verbosity, interactive, force, @namespace) =>
{
// Workaround for https://github.com/dotnet/command-line-api/issues/723#issuecomment-593062654
if (path is null)
Expand All @@ -45,7 +47,10 @@ public static Command CreateDeployCommand()
{
throw new CommandException($"No services found in \"{application.Source.Name}\"");
}

if (!String.IsNullOrEmpty(@namespace))
{
application.Namespace = @namespace;
}
await ExecuteDeployAsync(new OutputContext(console, verbosity), application, environment: "production", interactive, force);
});

Expand Down Expand Up @@ -75,7 +80,7 @@ private static async Task ExecuteDeployAsync(OutputContext output, ApplicationBu
new ValidateSecretStep() { Environment = environment, Interactive = interactive, Force = force, },
};

steps.Add(new GenerateKubernetesManifestStep() { Environment = environment, });
steps.Add(new GenerateKubernetesManifestStep() { Environment = environment, Namespace = application.Namespace });
steps.Add(new DeployServiceYamlStep() { Environment = environment, });

ApplyRegistryAndDefaults(output, application, interactive, requireRegistry: true);
Expand Down Expand Up @@ -103,7 +108,12 @@ private static async Task DeployApplicationManifestAsync(OutputContext output, A
await ApplicationYamlWriter.WriteAsync(output, writer, application);
}

output.WriteDebugLine("Running 'kubectl apply'.");
string ns = $"namespace ${application.Namespace}";
if (String.IsNullOrEmpty(application.Namespace))
{
ns = "current namespace";
}
output.WriteDebugLine($"Running 'kubectl apply' in ${ns}");
output.WriteCommandLine("kubectl", $"apply -f \"{tempFile.FilePath}\"");
var capture = output.Capture();
var exitCode = await Process.ExecuteAsync(
Expand Down
5 changes: 3 additions & 2 deletions src/tye/Program.GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,22 @@ public static Command CreateGenerateCommand()
CommonArguments.Path_Required,
StandardOptions.Interactive,
StandardOptions.Verbosity,
StandardOptions.Namespace,
};

// This is a super-secret VIP-only command! It's useful for testing, but we're
// not documenting it right now.
command.IsHidden = true;

command.Handler = CommandHandler.Create<IConsole, FileInfo, Verbosity, bool>((console, path, verbosity, interactive) =>
command.Handler = CommandHandler.Create<IConsole, FileInfo, Verbosity, bool, string>((console, path, verbosity, interactive, @namespace) =>
{
// Workaround for https://github.com/dotnet/command-line-api/issues/723#issuecomment-593062654
if (path is null)
{
throw new CommandException("No project or solution file was found.");
}

return GenerateHost.GenerateAsync(console, path, verbosity, interactive);
return GenerateHost.GenerateAsync(console, path, verbosity, interactive, @namespace);
});

return command;
Expand Down