Skip to content

Commit

Permalink
fix: follow service k8s naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed May 23, 2023
1 parent 980f809 commit 83d3a23
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/GZCTF/Services/K8sService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,22 @@ public K8sService(IOptions<RegistryConfig> _registry, IOptions<ContainerProvider

public async Task<Container?> CreateContainerAsync(ContainerConfig config, CancellationToken token = default)
{
// use uuid avoid conflict
var name = $"{config.Image.Split("/").LastOrDefault()?.Split(":").FirstOrDefault()}-{Guid.NewGuid().ToString("N")[..16]}"
.Replace('_', '-'); // ensure name is available
var imageName = config.Image.Split("/").LastOrDefault()?.Split(":").FirstOrDefault();

if (imageName is null)
{
logger.SystemLog($"无法解析镜像名称 {config.Image}", TaskStatus.Failed, LogLevel.Warning);
return null;
}

// add prefix when meet the leading numbers
// which is not allowed in k8s dns name
if (char.IsDigit(imageName[0]))
imageName = $"chal-{imageName}";

// follow the k8s naming convention
// use uuid to avoid name conflict
var name = $"{imageName}-{Guid.NewGuid().ToString("N")[..16]}".Replace('_', '-');

var pod = new V1Pod("v1", "Pod")
{
Expand Down Expand Up @@ -183,12 +196,24 @@ public K8sService(IOptions<RegistryConfig> _registry, IOptions<ContainerProvider
}
catch (HttpOperationException e)
{
try
{
// remove the pod if service creation failed, ignore the error
await kubernetesClient.CoreV1.DeleteNamespacedPodAsync(name, Namespace, cancellationToken: token);
}
catch { }
logger.SystemLog($"服务 {name} 创建失败, 状态:{e.Response.StatusCode}", TaskStatus.Failed, LogLevel.Warning);
logger.SystemLog($"服务 {name} 创建失败, 响应:{e.Response.Content}", TaskStatus.Failed, LogLevel.Error);
return null;
}
catch (Exception e)
{
try
{
// remove the pod if service creation failed, ignore the error
await kubernetesClient.CoreV1.DeleteNamespacedPodAsync(name, Namespace, cancellationToken: token);
}
catch { }
logger.LogError(e, "创建服务失败");
return null;
}
Expand Down

0 comments on commit 83d3a23

Please sign in to comment.