Content-Length: 767542 | pFad | http://github.com/dotnet/tye/commit/c9561a408f1062619487dffe71196fa904985f09

29 Added tags to yaml definition to be used in CLI run filter (#528) · dotnet/tye@c9561a4 · GitHub
Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Commit

Permalink
Added tags to yaml definition to be used in CLI run filter (#528)
Browse files Browse the repository at this point in the history
* fixed unit test: Services_UnrecognizedKey

* TyeAssert - duplicated check removed

* YAML service.tags added to parser and unit tests

* filter services by tags - run argument + ApplicationFactory filter

* filter ingress by tags

* Make tags work for all scenarios

Co-authored-by: Justin Kotalik <jukotali@microsoft.com>
  • Loading branch information
kkoziarski and jkotalik authored Jun 18, 2020
1 parent 18712f9 commit c9561a4
Show file tree
Hide file tree
Showing 19 changed files with 228 additions and 56 deletions.
15 changes: 11 additions & 4 deletions src/Microsoft.Tye.Core/ApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
Expand All @@ -15,7 +14,7 @@ namespace Microsoft.Tye
{
public static class ApplicationFactory
{
public static async Task<ApplicationBuilder> CreateAsync(OutputContext output, FileInfo source)
public static async Task<ApplicationBuilder> CreateAsync(OutputContext output, FileInfo source, ApplicationFactoryFilter? filter = null)
{
if (source is null)
{
Expand Down Expand Up @@ -71,7 +70,11 @@ public static async Task<ApplicationBuilder> CreateAsync(OutputContext output, F
root.Extensions.Add(extension);
}

foreach (var configService in config.Services)
var services = filter?.ServicesFilter != null ?
config.Services.Where(filter.ServicesFilter).ToList() :
config.Services;

foreach (var configService in services)
{
ServiceBuilder service;
if (root.Services.Any(s => s.Name == configService.Name))
Expand Down Expand Up @@ -320,7 +323,11 @@ service is ProjectServiceBuilder project2 &&
}
}

foreach (var configIngress in config.Ingress)
var ingresses = filter?.IngressFilter != null ?
config.Ingress.Where(filter.IngressFilter).ToList() :
config.Ingress;

foreach (var configIngress in ingresses)
{
var ingress = new IngressBuilder(configIngress.Name!);
ingress.Replicas = configIngress.Replicas ?? 1;
Expand Down
32 changes: 32 additions & 0 deletions src/Microsoft.Tye.Core/ApplicationFactoryFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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;
using System.Linq;
using Microsoft.Tye.ConfigModel;

namespace Microsoft.Tye
{
public class ApplicationFactoryFilter
{
public Func<ConfigService, bool>? ServicesFilter { get; set; }
public Func<ConfigIngress, bool>? IngressFilter { get; set; }

public static ApplicationFactoryFilter? GetApplicationFactoryFilter(string[] tags)
{
ApplicationFactoryFilter? filter = null;

if (tags != null && tags.Any())
{
filter = new ApplicationFactoryFilter
{
ServicesFilter = service => tags.Any(b => service.Tags.Contains(b)),
IngressFilter = service => tags.Any(b => service.Tags.Contains(b))
};
}

return filter;
}
}
}
1 change: 1 addition & 0 deletions src/Microsoft.Tye.Core/ConfigModel/ConfigIngress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public class ConfigIngress
public int? Replicas { get; set; }
public List<ConfigIngressRule> Rules { get; set; } = new List<ConfigIngressRule>();
public List<ConfigIngressBinding> Bindings { get; set; } = new List<ConfigIngressBinding>();
public List<string> Tags { get; set; } = new List<string>();
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.Tye.Core/ConfigModel/ConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Tye;
using YamlDotNet.Serialization;

namespace Microsoft.Tye.ConfigModel
Expand Down Expand Up @@ -37,6 +36,7 @@ public class ConfigService
[YamlMember(Alias = "env")]
public List<ConfigConfigurationSource> Configuration { get; set; } = new List<ConfigConfigurationSource>();
public List<BuildProperty> BuildProperties { get; set; } = new List<BuildProperty>();
public List<string> Tags { get; set; } = new List<string>();
public ConfigProbe? Liveness { get; set; }
public ConfigProbe? Readiness { get; set; }
}
Expand Down
17 changes: 17 additions & 0 deletions src/Microsoft.Tye.Core/Serialization/ConfigIngressParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ private static void HandleIngressMapping(YamlMappingNode yamlMappingNode, Config
}
HandleIngressBindings((child.Value as YamlSequenceNode)!, configIngress.Bindings);
break;
case "tags":
if (child.Value.NodeType != YamlNodeType.Sequence)
{
throw new TyeYamlException(child.Value.Start, CoreStrings.FormatExpectedYamlSequence(key));
}

HandleIngressTags((child.Value as YamlSequenceNode)!, configIngress.Tags);
break;
default:
throw new TyeYamlException(child.Key.Start, CoreStrings.FormatUnrecognizedKey(key));
}
Expand Down Expand Up @@ -137,5 +145,14 @@ private static void HandleIngressBindingMapping(YamlMappingNode yamlMappingNode,
}
}
}

private static void HandleIngressTags(YamlSequenceNode yamlSequenceNode, List<string> tags)
{
foreach (var child in yamlSequenceNode!.Children)
{
var tag = YamlParser.GetScalarValue(child);
tags.Add(tag);
}
}
}
}
19 changes: 17 additions & 2 deletions src/Microsoft.Tye.Core/Serialization/ConfigServiceParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Linq;
using Microsoft.Build.Evaluation;
using Microsoft.Tye.ConfigModel;
using YamlDotNet.RepresentationModel;

Expand Down Expand Up @@ -131,6 +129,14 @@ private static void HandleServiceNameMapping(YamlMappingNode yamlMappingNode, Co
service.Readiness = new ConfigProbe();
HandleServiceProbe((YamlMappingNode)child.Value, service.Readiness!);
break;
case "tags":
if (child.Value.NodeType != YamlNodeType.Sequence)
{
throw new TyeYamlException(child.Value.Start, CoreStrings.FormatExpectedYamlSequence(key));
}

HandleServiceTags((child.Value as YamlSequenceNode)!, service.Tags);
break;
default:
throw new TyeYamlException(child.Key.Start, CoreStrings.FormatUnrecognizedKey(key));
}
Expand Down Expand Up @@ -444,5 +450,14 @@ private static void HandleServiceBuildPropertyNameMapping(YamlMappingNode yamlMa
}
}
}

private static void HandleServiceTags(YamlSequenceNode yamlSequenceNode, List<string> tags)
{
foreach (var child in yamlSequenceNode!.Children)
{
var tag = YamlParser.GetScalarValue(child);
tags.Add(tag);
}
}
}
}
2 changes: 2 additions & 0 deletions src/Microsoft.Tye.Core/Serialization/YamlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ public ConfigApplication ParseConfigApplication()
service.Bindings ??= new List<ConfigServiceBinding>();
service.Configuration ??= new List<ConfigConfigurationSource>();
service.Volumes ??= new List<ConfigVolume>();
service.Tags ??= new List<string>();
}

foreach (var ingress in app.Ingress)
{
ingress.Bindings ??= new List<ConfigIngressBinding>();
ingress.Rules ??= new List<ConfigIngressRule>();
ingress.Tags ??= new List<string>();
}

return app;
Expand Down
16 changes: 16 additions & 0 deletions src/Microsoft.Tye.Core/StandardOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ public static Option Environment
}
}

public static Option Tags
{
get
{
return new Option("--tags", "--filter")
{
Argument = new Argument<List<string>>("tags")
{
Arity = ArgumentArity.OneOrMore
},
Description = "Filter the group of running services by tag.",
Required = false
};
}
}

public static Option Force
{
get
Expand Down
5 changes: 3 additions & 2 deletions src/tye/BuildHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ namespace Microsoft.Tye
{
public static class BuildHost
{
public static async Task BuildAsync(IConsole console, FileInfo path, Verbosity verbosity, bool interactive)
public static async Task BuildAsync(IConsole console, FileInfo path, Verbosity verbosity, bool interactive, string[] tags)
{
var output = new OutputContext(console, verbosity);
var application = await ApplicationFactory.CreateAsync(output, path);
var filter = ApplicationFactoryFilter.GetApplicationFactoryFilter(tags);
var application = await ApplicationFactory.CreateAsync(output, path, filter);
if (application.Services.Count == 0)
{
throw new CommandException($"No services found in \"{application.Source.Name}\"");
Expand Down
5 changes: 3 additions & 2 deletions src/tye/GenerateHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ namespace Microsoft.Tye
{
public static class GenerateHost
{
public static async Task GenerateAsync(IConsole console, FileInfo path, Verbosity verbosity, bool interactive, string ns)
public static async Task GenerateAsync(IConsole console, FileInfo path, Verbosity verbosity, bool interactive, string ns, string[] tags)
{
var output = new OutputContext(console, verbosity);

output.WriteInfoLine("Loading Application Details...");
var application = await ApplicationFactory.CreateAsync(output, path);
var filter = ApplicationFactoryFilter.GetApplicationFactoryFilter(tags);
var application = await ApplicationFactory.CreateAsync(output, path, filter);
if (application.Services.Count == 0)
{
throw new CommandException($"No services found in \"{application.Source.Name}\"");
Expand Down
5 changes: 3 additions & 2 deletions src/tye/Program.BuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ public static Command CreateBuildCommand()
CommonArguments.Path_Required,
StandardOptions.Interactive,
StandardOptions.Verbosity,
StandardOptions.Tags
};

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, tags) =>
{
// 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 BuildHost.BuildAsync(console, path, verbosity, interactive);
return BuildHost.BuildAsync(console, path, verbosity, interactive, tags);
});

return command;
Expand Down
11 changes: 8 additions & 3 deletions src/tye/Program.DeployCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Tye.ConfigModel;
Expand All @@ -23,6 +24,7 @@ public static Command CreateDeployCommand()
StandardOptions.Interactive,
StandardOptions.Verbosity,
StandardOptions.Namespace,
StandardOptions.Tags,
};

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

command.Handler = CommandHandler.Create<IConsole, FileInfo, Verbosity, bool, bool, string>(async (console, path, verbosity, interactive, force, @namespace) =>
command.Handler = CommandHandler.Create<IConsole, FileInfo, Verbosity, bool, bool, string, string[]>(async (console, path, verbosity, interactive, force, @namespace, tags) =>
{
// Workaround for https://github.com/dotnet/command-line-api/issues/723#issuecomment-593062654
if (path is null)
Expand All @@ -42,12 +44,15 @@ public static Command CreateDeployCommand()
var output = new OutputContext(console, verbosity);

output.WriteInfoLine("Loading Application Details...");
var application = await ApplicationFactory.CreateAsync(output, path);

var filter = ApplicationFactoryFilter.GetApplicationFactoryFilter(tags);

var application = await ApplicationFactory.CreateAsync(output, path, filter);
if (application.Services.Count == 0)
{
throw new CommandException($"No services found in \"{application.Source.Name}\"");
}
if (!String.IsNullOrEmpty(@namespace))
if (!string.IsNullOrEmpty(@namespace))
{
application.Namespace = @namespace;
}
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 @@ -22,21 +22,22 @@ public static Command CreateGenerateCommand()
StandardOptions.Interactive,
StandardOptions.Verbosity,
StandardOptions.Namespace,
StandardOptions.Tags
};

// 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, string>((console, path, verbosity, interactive, @namespace) =>
command.Handler = CommandHandler.Create<IConsole, FileInfo, Verbosity, bool, string, string[]>((console, path, verbosity, interactive, @namespace, tags) =>
{
// 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, @namespace);
return GenerateHost.GenerateAsync(console, path, verbosity, interactive, @namespace, tags);
});

return command;
Expand Down
7 changes: 5 additions & 2 deletions src/tye/Program.PushCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static Command CreatePushCommand()
CommonArguments.Path_Required,
StandardOptions.Interactive,
StandardOptions.Verbosity,
StandardOptions.Tags
};

command.AddOption(new Option(new[] { "-f", "--force" })
Expand All @@ -26,7 +27,7 @@ public static Command CreatePushCommand()
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, tags) =>
{
// Workaround for https://github.com/dotnet/command-line-api/issues/723#issuecomment-593062654
if (path is null)
Expand All @@ -37,7 +38,9 @@ public static Command CreatePushCommand()
var output = new OutputContext(console, verbosity);

output.WriteInfoLine("Loading Application Details...");
var application = await ApplicationFactory.CreateAsync(output, path);
var filter = ApplicationFactoryFilter.GetApplicationFactoryFilter(tags);

var application = await ApplicationFactory.CreateAsync(output, path, filter);
if (application.Services.Count == 0)
{
throw new CommandException($"No services found in \"{application.Source.Name}\"");
Expand Down
Loading

0 comments on commit c9561a4

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/dotnet/tye/commit/c9561a408f1062619487dffe71196fa904985f09

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy