Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add boilerplate to support using kubernetes as container provider #1522

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 8 additions & 0 deletions src/Runner.Common/HostContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,14 @@ public async Task Delay(TimeSpan delay, CancellationToken cancellationToken)
await Task.Delay(delay, cancellationToken);
}

/// <summary>
/// Register a default dynamic service.
/// </summary>
public void RegisterService(Type type, Type target)
{
_serviceTypes.TryAdd(type, target);
}

/// <summary>
/// Creates a new instance of T.
/// </summary>
Expand Down
10 changes: 5 additions & 5 deletions src/Runner.Worker/ActionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,12 +530,12 @@ private async Task PullActionContainerAsync(IExecutionContext executionContext,
executionContext.Output($"##[group]Pull down action image '{setupInfo.Container.Image}'");

// Pull down docker image with retry up to 3 times
var dockerManager = HostContext.GetService<IDockerCommandManager>();
var containerManager = HostContext.GetService<IContainerCommandManager>();
int retryCount = 0;
int pullExitCode = 0;
while (retryCount < 3)
{
pullExitCode = await dockerManager.DockerPull(executionContext, setupInfo.Container.Image);
pullExitCode = await containerManager.DockerPull(executionContext, setupInfo.Container.Image);
if (pullExitCode == 0)
{
break;
Expand Down Expand Up @@ -574,13 +574,13 @@ private async Task BuildActionContainerAsync(IExecutionContext executionContext,
executionContext.Output($"##[group]Build container for action use: '{setupInfo.Container.Dockerfile}'.");

// Build docker image with retry up to 3 times
var dockerManager = HostContext.GetService<IDockerCommandManager>();
var containerManager = HostContext.GetService<IContainerCommandManager>();
int retryCount = 0;
int buildExitCode = 0;
var imageName = $"{dockerManager.DockerInstanceLabel}:{Guid.NewGuid().ToString("N")}";
var imageName = $"{containerManager.DockerInstanceLabel}:{Guid.NewGuid().ToString("N")}";
while (retryCount < 3)
{
buildExitCode = await dockerManager.DockerBuild(
buildExitCode = await containerManager.DockerBuild(
executionContext,
setupInfo.Container.WorkingDirectory,
setupInfo.Container.Dockerfile,
Expand Down
28 changes: 2 additions & 26 deletions src/Runner.Worker/Container/DockerCommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,9 @@

namespace GitHub.Runner.Worker.Container
{
[ServiceLocator(Default = typeof(DockerCommandManager))]
public interface IDockerCommandManager : IRunnerService
{
string DockerPath { get; }
string DockerInstanceLabel { get; }
Task<DockerVersion> DockerVersion(IExecutionContext context);
Task<int> DockerPull(IExecutionContext context, string image);
Task<int> DockerPull(IExecutionContext context, string image, string configFileDirectory);
Task<int> DockerBuild(IExecutionContext context, string workingDirectory, string dockerFile, string dockerContext, string tag);
Task<string> DockerCreate(IExecutionContext context, ContainerInfo container);
Task<int> DockerRun(IExecutionContext context, ContainerInfo container, EventHandler<ProcessDataReceivedEventArgs> stdoutDataReceived, EventHandler<ProcessDataReceivedEventArgs> stderrDataReceived);
Task<int> DockerStart(IExecutionContext context, string containerId);
Task<int> DockerLogs(IExecutionContext context, string containerId);
Task<List<string>> DockerPS(IExecutionContext context, string options);
Task<int> DockerRemove(IExecutionContext context, string containerId);
Task<int> DockerNetworkCreate(IExecutionContext context, string network);
Task<int> DockerNetworkRemove(IExecutionContext context, string network);
Task<int> DockerNetworkPrune(IExecutionContext context);
Task<int> DockerExec(IExecutionContext context, string containerId, string options, string command);
Task<int> DockerExec(IExecutionContext context, string containerId, string options, string command, List<string> outputs);
Task<List<string>> DockerInspect(IExecutionContext context, string dockerObject, string options);
Task<List<PortMapping>> DockerPort(IExecutionContext context, string containerId);
Task<int> DockerLogin(IExecutionContext context, string configFileDirectory, string registry, string username, string password);
}

public class DockerCommandManager : RunnerService, IDockerCommandManager
public class DockerCommandManager : RunnerService, IContainerCommandManager
{
public string Type { get { return "docker"; } }
public string DockerPath { get; private set; }

public string DockerInstanceLabel { get; private set; }
Expand Down
39 changes: 39 additions & 0 deletions src/Runner.Worker/Container/IContainerCommandManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using GitHub.Runner.Common;
using GitHub.Runner.Sdk;

namespace GitHub.Runner.Worker.Container
{
[ServiceLocator(Default = typeof(DockerCommandManager))]
public interface IContainerCommandManager : IRunnerService
{
string Type { get; }
string DockerPath { get; }
string DockerInstanceLabel { get; }
Task<DockerVersion> DockerVersion(IExecutionContext context);
Task<int> DockerPull(IExecutionContext context, string image);
Task<int> DockerPull(IExecutionContext context, string image, string configFileDirectory);
Task<int> DockerBuild(IExecutionContext context, string workingDirectory, string dockerFile, string dockerContext, string tag);
Task<string> DockerCreate(IExecutionContext context, ContainerInfo container);
Task<int> DockerRun(IExecutionContext context, ContainerInfo container, EventHandler<ProcessDataReceivedEventArgs> stdoutDataReceived, EventHandler<ProcessDataReceivedEventArgs> stderrDataReceived);
Task<int> DockerStart(IExecutionContext context, string containerId);
Task<int> DockerLogs(IExecutionContext context, string containerId);
Task<List<string>> DockerPS(IExecutionContext context, string options);
Task<int> DockerRemove(IExecutionContext context, string containerId);
Task<int> DockerNetworkCreate(IExecutionContext context, string network);
Task<int> DockerNetworkRemove(IExecutionContext context, string network);
Task<int> DockerNetworkPrune(IExecutionContext context);
Task<int> DockerExec(IExecutionContext context, string containerId, string options, string command);
Task<int> DockerExec(IExecutionContext context, string containerId, string options, string command, List<string> outputs);
Task<List<string>> DockerInspect(IExecutionContext context, string dockerObject, string options);
Task<List<PortMapping>> DockerPort(IExecutionContext context, string containerId);
Task<int> DockerLogin(IExecutionContext context, string configFileDirectory, string registry, string username, string password);
}
}
111 changes: 111 additions & 0 deletions src/Runner.Worker/Container/KubernetesCommandManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using GitHub.Runner.Common;
using GitHub.Runner.Sdk;

namespace GitHub.Runner.Worker.Container
{
public class KubernetesCommandManager : RunnerService, IContainerCommandManager
{
public string Type { get { return "kubernetes"; } }
public string DockerPath { get; private set; }

public string DockerInstanceLabel { get; private set; }

public Task<DockerVersion> DockerVersion(IExecutionContext context)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<int> DockerPull(IExecutionContext context, string image)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<int> DockerPull(IExecutionContext context, string image, string configFileDirectory)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<int> DockerBuild(IExecutionContext context, string workingDirectory, string dockerFile, string dockerContext, string tag)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<string> DockerCreate(IExecutionContext context, ContainerInfo container)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<int> DockerRun(IExecutionContext context, ContainerInfo container, EventHandler<ProcessDataReceivedEventArgs> stdoutDataReceived, EventHandler<ProcessDataReceivedEventArgs> stderrDataReceived)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<int> DockerStart(IExecutionContext context, string containerId)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<int> DockerRemove(IExecutionContext context, string containerId)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<int> DockerLogs(IExecutionContext context, string containerId)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<List<string>> DockerPS(IExecutionContext context, string options)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<int> DockerNetworkCreate(IExecutionContext context, string network)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<int> DockerNetworkRemove(IExecutionContext context, string network)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<int> DockerNetworkPrune(IExecutionContext context)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<int> DockerExec(IExecutionContext context, string containerId, string options, string command)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<int> DockerExec(IExecutionContext context, string containerId, string options, string command, List<string> output)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<List<string>> DockerInspect(IExecutionContext context, string dockerObject, string options)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<List<PortMapping>> DockerPort(IExecutionContext context, string containerId)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}

public Task<int> DockerLogin(IExecutionContext context, string configFileDirectory, string registry, string username, string password)
{
throw new NotImplementedException("Kubernetes support to be implemented");
}
}
}
Loading