Created
March 5, 2020 03:05
-
-
Save blachniet/2f19e13f00aafe95596c41af424e2f51 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Threading; | |
using k8s; | |
using k8s.Models; | |
namespace k8s_job_launcher | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var stop = false; | |
var pid = System.Diagnostics.Process.GetCurrentProcess().Id; | |
var config = KubernetesClientConfiguration.InClusterConfig(); | |
var client = new Kubernetes(config); | |
Console.WriteLine($"Started with process ID {pid}"); | |
AppDomain.CurrentDomain.ProcessExit += delegate { | |
Console.WriteLine("SIGTERM received."); | |
stop = true; | |
}; | |
while (!stop) | |
{ | |
var job = new V1Job | |
{ | |
ApiVersion = "batch/v1", | |
Kind = "Job", | |
Metadata = new V1ObjectMeta | |
{ | |
GenerateName = "swish-", | |
}, | |
Spec = new V1JobSpec | |
{ | |
BackoffLimit = 2, | |
Template = new V1PodTemplateSpec | |
{ | |
Spec = new V1PodSpec | |
{ | |
RestartPolicy = "Never", | |
Containers = new List<V1Container> | |
{ | |
new V1Container | |
{ | |
Name = "swish-container", | |
Image = "alpine:latest", | |
Command = new List<string> | |
{ | |
"/bin/sh", | |
}, | |
Args = new List<string> | |
{ | |
"-c", | |
"for i in 1 2 3 4 5; do echo $i && sleep 5s; done", | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}; | |
job = client.CreateNamespacedJob(job, "default"); | |
Console.WriteLine($"Created new job: {job.Metadata.Name}"); | |
Thread.Sleep(10000); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment