From c2fbba03366ac2cb39285ecf4155b4729457a1eb Mon Sep 17 00:00:00 2001 From: benbierens Date: Fri, 17 Mar 2023 11:09:55 +0100 Subject: [PATCH] can now programmatically deploy node to k8s --- Program.cs | 118 ++++++++++++++++++++++++++++++++++++----- codexnode-manifest.yml | 13 ++++- 2 files changed, 117 insertions(+), 14 deletions(-) diff --git a/Program.cs b/Program.cs index 4ac4eb8..a8e52fb 100644 --- a/Program.cs +++ b/Program.cs @@ -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 { { "codex-node", "dist-test" } } + }, + Template = new V1PodTemplateSpec + { + Metadata = new V1ObjectMeta + { + Labels = new Dictionary { { "codex-node", "dist-test" } } + }, + Spec = new V1PodSpec + { + Containers = new List + { + new V1Container + { + Name = "codex-node", + Image = "thatbenbierens/nim-codex:sha-c9a62de", + Ports = new List + { + new V1ContainerPort + { + ContainerPort = 8080, + Name = "codex-api-port" + } + }, + Env = new List + { + 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 { { "codex-node", "dist-test" } }, + Ports = new List + { + 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 { { "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."); } diff --git a/codexnode-manifest.yml b/codexnode-manifest.yml index eba5642..c22d3ba 100644 --- a/codexnode-manifest.yml +++ b/codexnode-manifest.yml @@ -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: