can now programmatically deploy node to k8s

This commit is contained in:
benbierens 2023-03-17 11:09:55 +01:00
parent 3d2eb6e45c
commit c2fbba0336
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
2 changed files with 117 additions and 14 deletions

View File

@ -1,31 +1,125 @@
using k8s;
using k8s.Models;
public static class Program
{
private const string ns = "codex-test-namespace";
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
var client = new Kubernetes(config);
var namespaces = client.CoreV1.ListNamespace();
foreach (var ns in namespaces.Items)
var deploymentSpec = new V1Deployment
{
Console.WriteLine(ns.Metadata.Name);
var list = client.CoreV1.ListNamespacedPod(ns.Metadata.Name);
foreach (var item in list.Items)
ApiVersion= "apps/v1",
Metadata = new V1ObjectMeta
{
Console.WriteLine(item.Metadata.Name);
Name = "codex-demo",
NamespaceProperty = ns
},
Spec = new V1DeploymentSpec
{
Replicas = 1,
Selector = new V1LabelSelector
{
MatchLabels = new Dictionary<string,string> { { "codex-node", "dist-test" } }
},
Template = new V1PodTemplateSpec
{
Metadata = new V1ObjectMeta
{
Labels = new Dictionary<string,string> { { "codex-node", "dist-test" } }
},
Spec = new V1PodSpec
{
Containers = new List<V1Container>
{
new V1Container
{
Name = "codex-node",
Image = "thatbenbierens/nim-codex:sha-c9a62de",
Ports = new List<V1ContainerPort>
{
new V1ContainerPort
{
ContainerPort = 8080,
Name = "codex-api-port"
}
},
Env = new List<V1EnvVar>
{
new V1EnvVar
{
Name = "LOG_LEVEL",
Value = "WARN"
}
}
}
}
}
}
}
};
var serviceSpec = new V1Service
{
ApiVersion = "v1",
Metadata = new V1ObjectMeta
{
Name = "codex-entrypoint",
NamespaceProperty = ns
},
Spec = new V1ServiceSpec
{
Type = "NodePort",
Selector = new Dictionary<string, string> { { "codex-node", "dist-test" } },
Ports = new List<V1ServicePort>
{
new V1ServicePort
{
Protocol = "TCP",
Port = 8080,
TargetPort = "codex-api-port",
NodePort = 30001
}
}
}
};
Console.WriteLine("specs made");
var ans = client.CreateNamespace(new V1Namespace
{
ApiVersion = "v1",
Metadata = new V1ObjectMeta
{
Name = ns,
Labels = new Dictionary<string, string> { { "name", ns } }
}
});
Console.WriteLine("created namespace");
var deployment = client.CreateNamespacedDeployment(deploymentSpec, ns);
Console.WriteLine("deploy made");
var service = client.CreateNamespacedService(serviceSpec, ns);
Console.WriteLine("Service up. Press Q to close...");
var s = "";
while (!s.StartsWith("q"))
{
s = Console.ReadLine();
}
var services = client.CoreV1.ListServiceForAllNamespaces();
foreach (var service in services)
{
Console.WriteLine($"service: {service.Metadata.Name}");
}
client.DeleteNamespacedService(service.Name(), ns);
client.DeleteNamespacedDeployment(deployment.Name(), ns);
client.DeleteNamespace(ans.Name());
Console.WriteLine("Done.");
}

View File

@ -23,11 +23,20 @@
# ---
apiVersion: v1
kind: Namespace
metadata:
name: codex-test-namespace
labels:
name: codex-test-namespace
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: codex-demo
namespace: default
namespace: codex-test-namespace
spec:
replicas: 1
selector:
@ -60,7 +69,7 @@ apiVersion: v1
kind: Service
metadata:
name: codex-entrypoint
namespace: default
namespace: codex-test-namespace
spec:
type: NodePort
selector: