-
-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Andre Hofmeister <[email protected]>
- Loading branch information
1 parent
7a5cc2e
commit 710987a
Showing
14 changed files
with
284 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ httpd | |
identitytoken | ||
initdb | ||
isready | ||
kubeconfig | ||
lipsum | ||
ltsc | ||
memopt | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
root = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
namespace Testcontainers.K3s; | ||
|
||
/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> | ||
[PublicAPI] | ||
public sealed class K3sBuilder : ContainerBuilder<K3sBuilder, K3sContainer, K3sConfiguration> | ||
{ | ||
public const string RancherImage = "rancher/k3s:v1.26.2-k3s1"; | ||
|
||
public const ushort KubeSecurePort = 6443; | ||
|
||
public const ushort RancherWebhookPort = 8443; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="K3sBuilder" /> class. | ||
/// </summary> | ||
public K3sBuilder() | ||
: this(new K3sConfiguration()) | ||
{ | ||
DockerResourceConfiguration = Init().DockerResourceConfiguration; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="K3sBuilder" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
private K3sBuilder(K3sConfiguration resourceConfiguration) | ||
: base(resourceConfiguration) | ||
{ | ||
DockerResourceConfiguration = resourceConfiguration; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override K3sConfiguration DockerResourceConfiguration { get; } | ||
|
||
/// <inheritdoc /> | ||
public override K3sContainer Build() | ||
{ | ||
Validate(); | ||
return new K3sContainer(DockerResourceConfiguration, TestcontainersSettings.Logger); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override K3sBuilder Init() | ||
{ | ||
return base.Init() | ||
.WithImage(RancherImage) | ||
.WithPrivileged(true) | ||
.WithPortBinding(KubeSecurePort, true) | ||
.WithPortBinding(RancherWebhookPort, true) | ||
.WithBindMount("/sys/fs/cgroup", "/sys/fs/cgroup", AccessMode.ReadWrite) | ||
.WithTmpfsMount("/run") | ||
.WithTmpfsMount("/var/run") | ||
.WithCommand("server", "--disable=traefik") | ||
.WithCreateParameterModifier(parameterModifier => parameterModifier.HostConfig.CgroupnsMode = "host") | ||
.WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new WaitUntil())); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override K3sBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
{ | ||
return Merge(DockerResourceConfiguration, new K3sConfiguration(resourceConfiguration)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override K3sBuilder Clone(IContainerConfiguration resourceConfiguration) | ||
{ | ||
return Merge(DockerResourceConfiguration, new K3sConfiguration(resourceConfiguration)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override K3sBuilder Merge(K3sConfiguration oldValue, K3sConfiguration newValue) | ||
{ | ||
return new K3sBuilder(new K3sConfiguration(oldValue, newValue)); | ||
} | ||
|
||
/// <inheritdoc cref="IWaitUntil" /> | ||
private sealed class WaitUntil : IWaitUntil | ||
{ | ||
/// <inheritdoc /> | ||
public async Task<bool> UntilAsync(IContainer container) | ||
{ | ||
var (_, stderr) = await container.GetLogsAsync(timestampsEnabled: false) | ||
.ConfigureAwait(false); | ||
|
||
return stderr.Contains("Node controller sync successful"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
namespace Testcontainers.K3s; | ||
|
||
/// <inheritdoc cref="ContainerConfiguration" /> | ||
[PublicAPI] | ||
public sealed class K3sConfiguration : ContainerConfiguration | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="K3sConfiguration" /> class. | ||
/// </summary> | ||
public K3sConfiguration() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="K3sConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public K3sConfiguration(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
: base(resourceConfiguration) | ||
{ | ||
// Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="K3sConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public K3sConfiguration(IContainerConfiguration resourceConfiguration) | ||
: base(resourceConfiguration) | ||
{ | ||
// Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="K3sConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public K3sConfiguration(K3sConfiguration resourceConfiguration) | ||
: this(new K3sConfiguration(), resourceConfiguration) | ||
{ | ||
// Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="K3sConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="oldValue">The old Docker resource configuration.</param> | ||
/// <param name="newValue">The new Docker resource configuration.</param> | ||
public K3sConfiguration(K3sConfiguration oldValue, K3sConfiguration newValue) | ||
: base(oldValue, newValue) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
namespace Testcontainers.K3s; | ||
|
||
/// <inheritdoc cref="DockerContainer" /> | ||
[PublicAPI] | ||
public sealed class K3sContainer : DockerContainer | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="K3sContainer" /> class. | ||
/// </summary> | ||
/// <param name="configuration">The container configuration.</param> | ||
/// <param name="logger">The logger.</param> | ||
public K3sContainer(K3sConfiguration configuration, ILogger logger) | ||
: base(configuration, logger) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets the Kubeconfig. | ||
/// </summary> | ||
/// <returns>Task that completes when the Kubeconfig has been read.</returns> | ||
public async Task<string> GetKubeconfigAsync() | ||
{ | ||
var kubeconfigBytes = await ReadFileAsync("/etc/rancher/k3s/k3s.yaml") | ||
.ConfigureAwait(false); | ||
|
||
var kubeconfig = Encoding.Default.GetString(kubeconfigBytes); | ||
|
||
var server = new UriBuilder(Uri.UriSchemeHttps, Hostname, GetMappedPublicPort(K3sBuilder.KubeSecurePort)).ToString(); | ||
|
||
return Regex.Replace(kubeconfig, "server:\\s?[:/\\.\\d\\w]+", "server: " + server, RegexOptions.None, TimeSpan.FromSeconds(1)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks> | ||
<LangVersion>latest</LangVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="$(SolutionDir)src/Testcontainers/Testcontainers.csproj"/> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
global using System; | ||
global using System.Text; | ||
global using System.Text.RegularExpressions; | ||
global using System.Threading.Tasks; | ||
global using Docker.DotNet.Models; | ||
global using DotNet.Testcontainers.Builders; | ||
global using DotNet.Testcontainers.Configurations; | ||
global using DotNet.Testcontainers.Containers; | ||
global using JetBrains.Annotations; | ||
global using Microsoft.Extensions.Logging; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
root = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
namespace Testcontainers.K3s; | ||
|
||
public sealed class K3sContainerTest : IAsyncLifetime | ||
{ | ||
private readonly K3sContainer _k3sConainter = new K3sBuilder().Build(); | ||
|
||
public Task InitializeAsync() | ||
{ | ||
return _k3sConainter.StartAsync(); | ||
} | ||
|
||
public Task DisposeAsync() | ||
{ | ||
return _k3sConainter.DisposeAsync().AsTask(); | ||
} | ||
|
||
[Fact] | ||
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))] | ||
public async Task CreateNamespaceReturnsHttpStatusCodeCreated() | ||
{ | ||
// Given | ||
using var kubeconfigStream = new MemoryStream(); | ||
|
||
var kubeconfig = await _k3sConainter.GetKubeconfigAsync() | ||
.ConfigureAwait(false); | ||
|
||
await kubeconfigStream.WriteAsync(Encoding.Default.GetBytes(kubeconfig)) | ||
.ConfigureAwait(false); | ||
|
||
var clientConfiguration = await KubernetesClientConfiguration.BuildConfigFromConfigFileAsync(kubeconfigStream) | ||
.ConfigureAwait(false); | ||
|
||
using var client = new Kubernetes(clientConfiguration); | ||
|
||
// When | ||
using var response = await client.CoreV1.CreateNamespaceWithHttpMessagesAsync(new V1Namespace(metadata: new V1ObjectMeta(name: Guid.NewGuid().ToString("D")))) | ||
.ConfigureAwait(false); | ||
|
||
// Then | ||
Assert.Equal(HttpStatusCode.Created, response.Response.StatusCode); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/Testcontainers.K3s.Tests/Testcontainers.K3s.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<IsPublishable>false</IsPublishable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1"/> | ||
<PackageReference Include="coverlet.collector" Version="3.2.0"/> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"/> | ||
<PackageReference Include="xunit" Version="2.4.2"/> | ||
<PackageReference Include="KubernetesClient" Version="10.1.4"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="$(SolutionDir)src/Testcontainers.K3s/Testcontainers.K3s.csproj"/> | ||
<ProjectReference Include="$(SolutionDir)tests/Testcontainers.Commons/Testcontainers.Commons.csproj"/> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
global using System; | ||
global using System.IO; | ||
global using System.Net; | ||
global using System.Text; | ||
global using System.Threading.Tasks; | ||
global using DotNet.Testcontainers.Commons; | ||
global using k8s; | ||
global using k8s.Models; | ||
global using Xunit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters