Skip to content
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
24 changes: 24 additions & 0 deletions cli/src/Vdk/Commands/CreateCloudProviderKindCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.CommandLine;
using Vdk.Services;
using IConsole = Vdk.Services.IConsole;

namespace Vdk.Commands;

public class CreateCloudProviderKindCommand: Command
{
private readonly IConsole _console;
private readonly IHubClient _client;

public CreateCloudProviderKindCommand(IConsole console, IHubClient client): base("cloud-provider-kind", "Create Vega VDK Cloud Provider kind container registry")
{
_console = console;
_client = client;
this.SetHandler(InvokeAsync);
}

public Task InvokeAsync()
{
_client.CreateCloudProviderKind();
return Task.CompletedTask;
}
}
4 changes: 2 additions & 2 deletions cli/src/Vdk/Commands/CreateClusterCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public async Task InvokeAsync(string name = Defaults.ClusterName, int controlPla
// check if the hub and proxy are there
if (!_reverseProxy.Exists())
_reverseProxy.Create();
if (!_hub.Exists())
_hub.Create();
if (!_hub.ExistRegistry())
_hub.CreateRegistry();

var map = await _kindVersionInfo.GetVersionInfoAsync();
string? kindVersion = null;
Expand Down
3 changes: 2 additions & 1 deletion cli/src/Vdk/Commands/CreateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ namespace Vdk.Commands;

public class CreateCommand: Command
{
public CreateCommand(CreateClusterCommand createCluster, CreateRegistryCommand createRegistry, CreateProxyCommand createProxy)
public CreateCommand(CreateClusterCommand createCluster, CreateRegistryCommand createRegistry, CreateProxyCommand createProxy, CreateCloudProviderKindCommand createCloudProviderKindCommand)
: base("create", "Create vega development resources")
{
AddCommand(createCluster);
AddCommand(createRegistry);
AddCommand(createProxy);
AddCommand(createCloudProviderKindCommand);
}
}
2 changes: 1 addition & 1 deletion cli/src/Vdk/Commands/CreateRegistryCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public CreateRegistryCommand(IConsole console, IHubClient client): base("registr

public Task InvokeAsync()
{
_client.Create();
_client.CreateRegistry();
return Task.CompletedTask;
}
}
24 changes: 24 additions & 0 deletions cli/src/Vdk/Commands/RemoveCloudProviderKindCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.CommandLine;
using Vdk.Services;
using IConsole = Vdk.Services.IConsole;

namespace Vdk.Commands;

public class RemoveCloudProviderKindCommand: Command
{
private readonly IConsole _console;
private readonly IHubClient _client;

public RemoveCloudProviderKindCommand(IConsole console, IHubClient client) : base("cloud-provider-kind", "Remove Vega VDK Cloud Provider Kind")
{
_console = console;
_client = client;
this.SetHandler(InvokeAsync);
}

public Task InvokeAsync()
{
_client.DestroyCloudProviderKind();
return Task.CompletedTask;
}
}
3 changes: 2 additions & 1 deletion cli/src/Vdk/Commands/RemoveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ namespace Vdk.Commands;

public class RemoveCommand: Command
{
public RemoveCommand(RemoveClusterCommand removeCluster, RemoveRegistryCommand removeRegistry, RemoveProxyCommand removeProxy)
public RemoveCommand(RemoveClusterCommand removeCluster, RemoveRegistryCommand removeRegistry, RemoveProxyCommand removeProxy, RemoveCloudProviderKindCommand removeCloudProviderKindCommand)
: base("remove", "Remove Vega development resources")
{
AddCommand(removeCluster);
AddCommand(removeRegistry);
AddCommand(removeProxy);
AddCommand(removeCloudProviderKindCommand);
}
}
2 changes: 1 addition & 1 deletion cli/src/Vdk/Commands/RemoveRegistryCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public RemoveRegistryCommand(IConsole console, IHubClient client) : base("regist

public Task InvokeAsync()
{
_client.Destroy();
_client.DestroyRegistry();
return Task.CompletedTask;
}
}
2 changes: 2 additions & 0 deletions cli/src/Vdk/Constants/Containers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public static class Containers
public const int RegistryHostPort = 5000;
public const string ProxyName = "vega-proxy";
public const string ProxyImage = "nginx:latest";
public const string CloudProviderKindName = "cloud-provider-kind";
public const string CloudProviderKindImage = "registry.k8s.io/cloud-provider-kind/cloud-controller-manager:v0.6.0";
}
2 changes: 1 addition & 1 deletion cli/src/Vdk/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"create-clutser": {
"commandName": "Project",
"commandLineArgs": "create cluster"
"commandLineArgs": "create cloud-provider-kind"
},
"WSL": {
"commandName": "WSL2",
Expand Down
2 changes: 2 additions & 0 deletions cli/src/Vdk/ServiceProviderBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public static IServiceProvider Build()
.AddSingleton<CreateProxyCommand>()
.AddSingleton<RemoveProxyCommand>()
.AddSingleton<CreateRegistryCommand>()
.AddSingleton<CreateCloudProviderKindCommand>()
.AddSingleton<RemoveRegistryCommand>()
.AddSingleton<RemoveCloudProviderKindCommand>()
.AddSingleton<UpdateCommand>()
.AddSingleton<UpdateKindVersionInfoCommand>()
.AddSingleton<IKindVersionInfoService, KindVersionInfoService>()
Expand Down
41 changes: 36 additions & 5 deletions cli/src/Vdk/Services/DockerHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace Vdk.Services;

public class DockerHubClient(IDockerEngine docker, IConsole console) : IHubClient
{
public void Create()
public void CreateRegistry()
{
if (Exists()) return;
if (ExistRegistry()) return;
console.WriteLine("Creating Vega VDK Registry");
console.WriteLine(" - This may take a few minutes...");
docker.Run(Containers.RegistryImage,
Expand All @@ -18,16 +18,47 @@ public void Create()
null);
}

public void Destroy()
public void CreateCloudProviderKind()
{
if (!Exists()) return;
if (ExistCloudProviderKind()) return;
console.WriteLine("Creating Cloud Provider Kind");
// docker run -d --name cloud-provider-kind --network kind -v /var/run/docker.sock:/var/run/docker.sock registry.k8s.io/cloud-provider-kind/cloud-controller-manager:v0.6.0
var volumes = new List<FileMapping>()
{
new()
{
Source = "/var/run/docker.sock",
Destination = "/var/run/docker.sock"
}
};
docker.Run(Containers.CloudProviderKindImage,
Containers.CloudProviderKindName,
null, null, volumes.ToArray(), null, "kind");
}


public void DestroyRegistry()
{
if (!ExistRegistry()) return;
console.WriteWarning("Deleting Vega VDK Registry from Docker");
console.WriteLine("You can recreate the registry using command 'vega create registry'");
docker.Delete(Containers.RegistryName);
}

public bool Exists()
public void DestroyCloudProviderKind()
{
if (!ExistCloudProviderKind()) return;
console.WriteWarning("Deleting Cloud Provider Kind from Docker");
docker.Delete(Containers.CloudProviderKindName);
}

public bool ExistRegistry()
{
return docker.Exists(Containers.RegistryName);
}

public bool ExistCloudProviderKind()
{
return docker.Exists(Containers.CloudProviderKindName);
}
}
2 changes: 1 addition & 1 deletion cli/src/Vdk/Services/IDockerEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Vdk.Services;

public interface IDockerEngine
{
bool Run(string image, string name, PortMapping[]? ports, Dictionary<string, string>? envs, FileMapping[]? volumes, string[]? commands);
bool Run(string image, string name, PortMapping[]? ports, Dictionary<string, string>? envs, FileMapping[]? volumes, string[]? commands, string? network = null);

bool Exists(string name,bool checkRunning = true);

Expand Down
12 changes: 9 additions & 3 deletions cli/src/Vdk/Services/IHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ namespace Vdk.Services;

public interface IHubClient
{
void Create();
void CreateRegistry();

void Destroy();
void CreateCloudProviderKind();

bool Exists();
void DestroyRegistry();

void DestroyCloudProviderKind();

bool ExistRegistry();

bool ExistCloudProviderKind();
}
33 changes: 31 additions & 2 deletions cli/src/Vdk/Services/LocalDockerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public LocalDockerClient(Docker.DotNet.IDockerClient dockerClient)
_dockerClient = dockerClient;
}

public bool Run(string image, string name, PortMapping[]? ports, Dictionary<string, string>? envs, FileMapping[]? volumes, string[]? commands)
public bool Run(string image, string name, PortMapping[]? ports, Dictionary<string, string>? envs, FileMapping[]? volumes, string[]? commands, string? network = null)
{
_dockerClient.Images.CreateImageAsync(
new ImagesCreateParameters
Expand All @@ -27,7 +27,8 @@ public bool Run(string image, string name, PortMapping[]? ports, Dictionary<stri
{
Image = image,
Name = name,



Labels = new Dictionary<string, string> { { "vega-component", name } },
ExposedPorts = ports?.ToDictionary(x => $"{x.ContainerPort}/tcp", y => default(EmptyStruct)),
Volumes = volumes?.ToDictionary(x => x.Destination, y => new EmptyStruct()),
Expand All @@ -43,6 +44,34 @@ public bool Run(string image, string name, PortMapping[]? ports, Dictionary<stri

}
}).GetAwaiter().GetResult();
// Connect to custom Docker network if specified
if (!string.IsNullOrWhiteSpace(network))
{
var networkList = _dockerClient.Networks
.ListNetworksAsync(new NetworksListParameters
{
Filters = new Dictionary<string, IDictionary<string, bool>>
{
{ "name", new Dictionary<string, bool> { { network, true } } }
}
})
.GetAwaiter()
.GetResult();

var dockerNetwork = networkList?.FirstOrDefault();
if (dockerNetwork == null)
{
throw new Exception($"Docker network '{network}' not found.");
}

_dockerClient.Networks
.ConnectNetworkAsync(dockerNetwork.ID, new NetworkConnectParameters
{
Container = response.ID
})
.GetAwaiter()
.GetResult();
}

return _dockerClient.Containers.StartContainerAsync(response.ID, new ContainerStartParameters() { }).GetAwaiter().GetResult();
}
Expand Down
7 changes: 7 additions & 0 deletions docs/usage/command-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,10 @@ Gets the kubeconfig path for a cluster.
**Flags:**

* `--name string`: Name of the cluster (default: `kind`)

# `vdk create cloud-provider-kind`

Creates a cloud Provider KIND docker image which runs as a standalone binary in the local machine
and will connect to the Kind cluster and provision new Load balancer containers for the services.


Loading