Skip to content

Commit

Permalink
#166 Ensure dapr sidecar containers get created (#201)
Browse files Browse the repository at this point in the history
Dapr sidecar containers weren't created for compose output format - this fixes that #166
  • Loading branch information
prom3theu5 authored May 19, 2024
1 parent 237594a commit fb3000d
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private static List<Volume> CreateVolumes(List<Service> services)

private void ProcessIndividualComponent(KeyValuePair<string, Resource> resource, List<Service> services)
{
if (AspirateState.IsNotDeployable(resource.Value))
if (CurrentState.IsNotDeployable(resource.Value))
{
return;
}
Expand All @@ -103,6 +103,7 @@ private void ProcessIndividualComponent(KeyValuePair<string, Resource> resource,
Resource = resource,
WithDashboard = CurrentState.IncludeDashboard,
ComposeBuilds = CurrentState.ComposeBuilds?.Any(x=> x == resource.Key) ?? false,
CurrentState = CurrentState
});

if (response.IsProject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private async Task ProcessIndividualResourceManifests(KeyValuePair<string, Resou
WithDashboard = CurrentState.IncludeDashboard
});

if (success && !AspirateState.IsNotDeployable(resource.Value) && resource.Value is not DaprComponentResource)
if (success && !CurrentState.IsNotDeployable(resource.Value) && resource.Value is not DaprComponentResource)
{
CurrentState.AppendToFinalResources(resource.Key, resource.Value);
}
Expand Down
37 changes: 37 additions & 0 deletions src/Aspirate.Processors/Resources/Dapr/DaprProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,41 @@ public class DaprProcessor(
public override Task<bool> CreateManifests(CreateManifestsOptions options) =>
// Do nothing for dapr, they are there for annotations on services.
Task.FromResult(true);

public override List<object> CreateKubernetesObjects(CreateKubernetesObjectsOptions options) => [];

public override ComposeService CreateComposeEntry(CreateComposeEntryOptions options)
{
var response = new ComposeService();

var daprResource = options.Resource.Value as DaprResource;

var commands = new List<string>
{
"./daprd",
"-app-id",
daprResource.Metadata.AppId,
};

var childResource = options.CurrentState.AllSelectedSupportedComponents.FirstOrDefault(x => x.Key == daprResource.Metadata.Application);

if (childResource.Value is IResourceWithBinding childBinding)
{
var firstPort = childBinding.Bindings.FirstOrDefault();

commands.Add("-app-port");
commands.Add(firstPort.Value.Port?.ToString() ?? "8080");
}

response.Service = new Shared.Models.Compose.ComposeServiceBuilder()
.WithName(options.Resource.Key)
.WithImage("daprio/daprd:latest")
.WithCommands(commands.ToArray())
.WithDependencies(daprResource.Metadata.Application)
.WithNetworkMode($"service:{childResource.Key}")
.WithRestartPolicy(ERestartMode.UnlessStopped)
.Build();

return response;
}
}
1 change: 1 addition & 0 deletions src/Aspirate.Shared/Inputs/BaseCreateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ public abstract class BaseCreateOptions
/// </remarks>
public bool? WithPrivateRegistry { get; set; }

public AspirateState? CurrentState { get; set; }
}
11 changes: 9 additions & 2 deletions src/Aspirate.Shared/Models/Aspirate/AspirateState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,15 @@ public class AspirateState :
public void AppendToFinalResources(string key, Resource resource) =>
FinalResources.Add(key, resource);

public static bool IsNotDeployable(Resource resource) =>
(resource is DaprResource or ParameterResource or ValueResource);
public bool IsNotDeployable(Resource resource)
{
if (OutputFormat.Equals("compose", StringComparison.OrdinalIgnoreCase))
{
return (resource is ParameterResource or ValueResource);
}

return (resource is DaprResource or ParameterResource or ValueResource);
}

[JsonIgnore]
public bool? ReplaceSecrets { get; set; }
Expand Down
7 changes: 7 additions & 0 deletions src/Aspirate.Shared/Models/Compose/ComposeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Aspirate.Shared.Models.Compose;

public class ComposeService : Service
{
[YamlMember(Alias = "network_mode")]
public string? NetworkMode { get; set; }
}
233 changes: 233 additions & 0 deletions src/Aspirate.Shared/Models/Compose/ComposeServiceBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
using DockerComposeBuilder.Builders.Base;
using DockerComposeBuilder.Builders.Services;
using DockerComposeBuilder.Enums;

namespace Aspirate.Shared.Models.Compose;

public class ComposeServiceBuilder : BaseBuilder<ComposeServiceBuilder, ComposeService>
{
protected BuildBuilder? BuildBuilder;

public ComposeServiceBuilder()
{
}

public ComposeServiceBuilder WithName(string name)
{
WorkingObject.Name = name;

return this;
}

public ComposeServiceBuilder WithContainerName(string containerName)
{
WorkingObject.ContainerName = containerName;
return this;
}

public ComposeServiceBuilder WithDependencies(params string[] services)
{
WorkingObject.DependsOn ??= new List<string>();

var dependsOnList = WorkingObject.DependsOn;

dependsOnList?.AddRange(services);
return this;
}

public ComposeServiceBuilder WithDependencies(params Service[] services)
{
return WithDependencies(services.Select(t => t.Name).ToArray());
}

public ComposeServiceBuilder WithHealthCheck(Action<HealthCheck> healthCheck)
{
WorkingObject.HealthCheck ??= new HealthCheck();

healthCheck(WorkingObject.HealthCheck);

return this;
}

public ComposeServiceBuilder WithEnvironment(IDictionary<string, string?> environmentMappings)
{
WorkingObject.Environment ??= new Dictionary<string, string?>();

return AddToDictionary(WorkingObject.Environment, environmentMappings);
}

public ComposeServiceBuilder WithEnvironment(Action<IDictionary<string, string?>> environmentExpression)
{
WorkingObject.Environment ??= new Dictionary<string, string?>();

environmentExpression(WorkingObject.Environment);

return this;
}

public ComposeServiceBuilder WithExposed(params string[] exposed)
{
if (WorkingObject.Expose == null)
{
WorkingObject.Expose = new List<string>();
}

WorkingObject.Expose.AddRange(exposed);
return this;
}

public ComposeServiceBuilder WithExposed(params object[] exposed)
{
WorkingObject.Expose ??= new List<string>();

WorkingObject.Expose.AddRange(exposed.Select(t => t.ToString())!);
return this;
}

public ComposeServiceBuilder WithHostname(string hostname)
{
WorkingObject.Hostname = hostname;
return this;
}

public ComposeServiceBuilder WithNetworkMode(string networkMode)
{
WorkingObject.NetworkMode = networkMode;
return this;
}

public ComposeServiceBuilder WithLabels(IDictionary<string, string> labels)
{
WorkingObject.Labels ??= new Dictionary<string, string>();

return AddToDictionary(WorkingObject.Labels, labels);
}

public ComposeServiceBuilder WithLabels(Action<IDictionary<string, string>> environmentExpression)
{
WorkingObject.Labels ??= new Dictionary<string, string>();

environmentExpression(WorkingObject.Labels);

return this;
}

public ComposeServiceBuilder WithBuild(Action<BuildBuilder> build)
{
BuildBuilder ??= new BuildBuilder();

build(BuildBuilder);

return this;
}

public ComposeServiceBuilder WithImage(string image)
{
WorkingObject.Image = image;
return this;
}

public ComposeServiceBuilder WithPrivileged(bool? privileged = true)
{
WorkingObject.Privileged = privileged;
return this;
}

public ComposeServiceBuilder WithNetworks(params string[] networks)
{
if (WorkingObject.Networks == null)
{
WorkingObject.Networks = new List<string>();
}

WorkingObject.Networks.AddRange(networks);
return this;
}

public ComposeServiceBuilder WithNetworks(params Network[] networks)
{
if (WorkingObject.Networks == null)
{
WorkingObject.Networks = new List<string>();
}

WorkingObject.Networks.AddRange(networks.Select(t => t.Name));
return this;
}

public ComposeServiceBuilder WithPortMappings(params Port[] mappings)
{
if (WorkingObject.Ports == null)
{
WorkingObject.Ports = new List<Port>();
}

WorkingObject.Ports.AddRange(mappings);
return this;
}

public ComposeServiceBuilder WithExtraHosts(params string[] extraHosts)
{
if (WorkingObject.ExtraHosts == null)
{
WorkingObject.ExtraHosts = new List<string>();
}

WorkingObject.ExtraHosts.AddRange(extraHosts);
return this;
}

public ComposeServiceBuilder WithCommands(params string[] commands)
{
if (WorkingObject.Commands == null)
{
WorkingObject.Commands = new List<string>();
}

WorkingObject.Commands.AddRange(commands);
return this;
}

public ComposeServiceBuilder WithRestartPolicy(ERestartMode mode)
{
WorkingObject.Restart = mode;
return this;
}

public ComposeServiceBuilder WithSecrets(params string[] secrets)
{
if (WorkingObject.Secrets == null)
{
WorkingObject.Secrets = new List<string>();
}

WorkingObject.Secrets.AddRange(secrets);
return this;
}

public ComposeServiceBuilder WithSecrets(params Secret[] secrets)
{
return WithSecrets(secrets.Select(t => t.Name).ToArray());
}

public ComposeServiceBuilder WithVolumes(params string[] volumes)
{
if (WorkingObject.Volumes == null)
{
WorkingObject.Volumes = new List<string>();
}

WorkingObject.Volumes.AddRange(volumes);
return this;
}

public override ComposeService Build()
{
if (BuildBuilder != null)
{
WorkingObject.Build = BuildBuilder.Build();
}

return base.Build();
}
}

0 comments on commit fb3000d

Please sign in to comment.