Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Carael committed Aug 28, 2024
1 parent 63db2de commit f23216b
Show file tree
Hide file tree
Showing 22 changed files with 254 additions and 127 deletions.
3 changes: 2 additions & 1 deletion src/AzureStorage/Blob/AzureStorageBlobDefaultOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public override void Configure(ContainerResourceBuilder builder)
builder
.Name(name)
.Image("mcr.microsoft.com/azure-storage/azurite")
.InternalPort(10000);
.InternalPort(10000)
.PreferLocalImage();
}
}
}
3 changes: 2 additions & 1 deletion src/AzureStorage/Queue/AzureStorageQueueDefaultOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public override void Configure(ContainerResourceBuilder builder)
builder
.Name(name)
.Image("mcr.microsoft.com/azure-storage/azurite")
.InternalPort(10001);
.InternalPort(10001)
.PreferLocalImage();
}
}
}
3 changes: 2 additions & 1 deletion src/ClickHouse/ClickHouseDefaultOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public override void Configure(ContainerResourceBuilder builder)
builder
.Name("clickhouse-server")
.Image("clickhouse/clickhouse-server")
.InternalPort(8123);
.InternalPort(8123)
.PreferLocalImage();
}
}
}
83 changes: 72 additions & 11 deletions src/Core/ContainerResourceOptions.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace Squadron
{
/// <summary>
/// Abstract base class for container resource options
/// </summary>
/// <summary>
/// Abstract base class for container resource options
/// </summary>
public abstract class ContainerResourceOptions
{
/// <summary>
/// Configures the resource
/// </summary>
/// <param name="builder">The builder.</param>
/// <summary>
/// Configures the resource
/// </summary>
/// <param name="builder">The builder.</param>
public abstract void Configure(ContainerResourceBuilder builder);



public static DockerConfiguration DefaultDockerConfigResolver()
{
IConfigurationRoot configuration = new ConfigurationBuilder()
Expand All @@ -27,8 +31,65 @@ public static DockerConfiguration DefaultDockerConfigResolver()

IConfigurationSection section = configuration.GetSection("Squadron:Docker");

DockerConfiguration containerConfig = section.Get<DockerConfiguration>();
return containerConfig ?? new DockerConfiguration();
DockerConfiguration containerConfig = section.Get<DockerConfiguration>() ?? new DockerConfiguration();

AddLocalDockerAuthentication(containerConfig);

return containerConfig;
}

private static void AddLocalDockerAuthentication(DockerConfiguration containerConfig)
{
var dockerAuthRootObject = TryGetDockerAuthRootObject();

if (dockerAuthRootObject != null)
{
foreach (var auth in dockerAuthRootObject.Auths)
{
var address = new Uri(auth.Key);

if (containerConfig.Registries.Any(p =>
p.Address.Equals(address.ToString(), StringComparison.InvariantCultureIgnoreCase)))
{
continue;
}

containerConfig.Registries.Add(new DockerRegistryConfiguration
{
Name = address.Host,
Address = address.ToString(),
Auth = auth.Value.Auth,
Email = auth.Value.Email
});
}
}
}

private static DockerAuthRootObject? TryGetDockerAuthRootObject()
{
var dockerConfigPath = Environment.GetEnvironmentVariable("DOCKER_CONFIG");
if (string.IsNullOrEmpty(dockerConfigPath))
{
return null;
}

// Construct the full path to the config.json file
var configFilePath = Path.Combine(dockerConfigPath, "config.json");

if (!File.Exists(configFilePath))
{
return null;
}

try
{
var jsonString = File.ReadAllText(configFilePath);

return JsonSerializer.Deserialize<DockerAuthRootObject>(jsonString);
}
catch { }

return null;
}
}
}
1 change: 1 addition & 0 deletions src/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Polly" Version="7.2.4" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions src/Core/DockerAuth.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Squadron
{
public class DockerAuth
{
[JsonPropertyName("auth")]
public string Auth { get; set; }

Check warning on line 9 in src/Core/DockerAuth.cs

View workflow job for this annotation

GitHub Actions / tests

Non-nullable property 'Auth' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 9 in src/Core/DockerAuth.cs

View workflow job for this annotation

GitHub Actions / sonar

Non-nullable property 'Auth' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[JsonPropertyName("email")]
public string Email { get; set; }

Check warning on line 12 in src/Core/DockerAuth.cs

View workflow job for this annotation

GitHub Actions / tests

Non-nullable property 'Email' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}

public class DockerAuthRootObject
{
[JsonPropertyName("auths")]
public Dictionary<string, DockerAuth> Auths { get; set; }

Check warning on line 18 in src/Core/DockerAuth.cs

View workflow job for this annotation

GitHub Actions / tests

Non-nullable property 'Auths' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[JsonPropertyName("HttpHeaders")]
public Dictionary<string, string> HttpHeaders { get; set; }

Check warning on line 21 in src/Core/DockerAuth.cs

View workflow job for this annotation

GitHub Actions / tests

Non-nullable property 'HttpHeaders' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
}
21 changes: 11 additions & 10 deletions src/Core/DockerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

namespace Squadron
{
/// <summary>
/// Docker configuration
/// </summary>
/// <summary>
/// Docker configuration
/// </summary>
public class DockerConfiguration
{
/// <summary>
/// Gets or sets the registries.
/// </summary>
/// <value>
/// The registries.
/// </value>
public IEnumerable<DockerRegistryConfiguration> Registries { get; set; }
/// <summary>
/// Gets or sets the registries.
/// </summary>
/// <value>
/// The registries.
/// </value>
public IList<DockerRegistryConfiguration> Registries { get; set; } =
new List<DockerRegistryConfiguration>();

public ContainerAddressMode DefaultAddressMode { get; internal set; } = ContainerAddressMode.Port;
}
Expand Down
Loading

0 comments on commit f23216b

Please sign in to comment.