示例#1
0
        public V1Pod CreateCadReaderPod(string _PodName, string _Namespace, Dictionary <string, string> _PythonWorkerVars, Dictionary <string, string> _FileWorkerVars, Action <string> _ErrorMessageAction = null)
        {
            PodBuilder Builder = new PodBuilder();

            Builder.SetName(_PodName)
            .SetNamespace(_Namespace)
            .SetRestartPolicy("Never")
            .AddLabel("pod-type", "batch-pod")
            .AddAnnotation("cluster-autoscaler.kubernetes.io/safe-to-evict", "true")
            .AddNodeSelector("batch-node", "true")
            .AddToleration("NoSchedule", "reserved-pool", "true", "Equal")
            .AddPodAntiAffinity("kubernetes.io/hostname", "pod-type", "In", new List <string>()
            {
                "batch-pod"
            })
            .AddPodAntiAffinityPreference("kubernetes.io/hostname", "pod-type", "In", new List <string>()
            {
                "optimizer-pod-ue"
            })
            .AddContainer("redis", "redis", "IfNotPresent", new int[] { 6379 }, new Dictionary <string, string>())
            .AddContainer("cad-reader", CadReaderReaderImage, "IfNotPresent", new int[] { 8080 }, _PythonWorkerVars)
            .AddContainer("worker", WorkerImage, "IfNotPresent", new int[] { 8081 }, _FileWorkerVars);

            try
            {
                return(KClient.CreateNamespacedPod(Builder.GetPod(), Builder.GetNamespace()));
            }
            catch (Exception ex)
            {
                _ErrorMessageAction?.Invoke($"Failed to start Pod {_PodName} : {ex.Message}\n{ex.StackTrace}");
                return(null);
            }
        }
示例#2
0
        public string CreatePod(string tenant, string filename)
        {
            var yml = Yaml.LoadFromFileAsync <V1Pod>(filename).Result;

            yml.Metadata.Labels.Add("tenant", tenant);
            var pod = _client.CreateNamespacedPod(yml, "default");

            return(pod.Metadata.Name);
        }
示例#3
0
        public IActionResult Post([FromQuery] string name, string namespaceName)
        {
            var pod = new V1Pod
            {
                ApiVersion = "v1",
                Kind       = "Pod",
                Metadata   = new V1ObjectMeta
                {
                    Name = name
                },
                Spec = new V1PodSpec
                {
                    Containers = new List <V1Container>()
                    {
                        new V1Container()
                        {
                            Name  = "container-test",
                            Image = "hello-world"
                        }
                    }
                }
            };

            var result = kubeClient.CreateNamespacedPod(pod, namespaceName);

            return(Ok());
        }
示例#4
0
        string CreatePod(string filename)
        {
            var yml = Yaml.LoadFromFileAsync <V1Pod>(filename).Result;

            var ymlResult = _client.CreateNamespacedPod(yml, DEFAULT_NAMESPACE);

            return(ymlResult.Metadata.Name);
        }
示例#5
0
        static void Main(string[] args)
        {
            var client = new Kubernetes(KubernetesClientConfiguration.InClusterConfig());

            var pods = client.ListNamespacedPod("default");

            foreach (var p in pods.Items)
                Console.WriteLine($"Pod = {p.Metadata.Name}");

            //Create Pod
            var pod = new V1Pod
            {
                ApiVersion = "v1",
                Kind = "Pod",
                Metadata = new V1ObjectMeta
                {
                    Name = "my-test-pod"
                },
                Spec = new V1PodSpec
                {
                    Containers = new List<V1Container>()
                }
            };

            pod.Spec.Containers.Add(new V1Container
            {
                Name = "my-test-container",
                 Image = "ngnix",
                 
            });


            //Create pod
            var result = client.CreateNamespacedPod(pod, "default");

            foreach (var status in result.Status.ContainerStatuses)
                Console.WriteLine($"Image = {status.Image}, StartedAt = {status.State.Running.StartedAt}");


            pods = client.ListNamespacedPod("default");
            foreach (var p in pods.Items)
                Console.WriteLine($"Pod = {p.Metadata.Name}");

            Console.ReadLine();
        }
示例#6
0
        public ActionResult Create()
        {
            var id     = Guid.NewGuid().ToString("N").Substring(0, 5);
            var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
            var client = new Kubernetes(config);

            var service = client.CreateNamespacedService(ServiceModel.CreateService(id), "minecraft");

            service.Validate();

            try
            {
                client.CreateNamespacedStatefulSet(StatefulSetModel.CreateStatefulSet(id), "minecraft");
            }
            catch { }

            var pod = client.CreateNamespacedPod(PodModel.CreatePod(id), "minecraft");

            pod.Validate();

            return(Created("/servers", new object[] { service, pod }));
        }
示例#7
0
        static void Main(string[] args)
        {
            var k8SClientConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile();
            var client          = new Kubernetes(k8SClientConfig);

            var pod = new V1Pod()
            {
                ApiVersion = "v1",
                Kind       = "Pod",
                Metadata   = new V1ObjectMeta()
                {
                    Name = "test-pod"
                },
                Spec = new V1PodSpec()
                {
                    Containers = new List <V1Container>()
                    {
                        new V1Container()
                        {
                            Name  = "test-container",
                            Image = "nginx",
                            Env   = new List <V1EnvVar>()
                            {
                                new V1EnvVar()
                                {
                                    Name = "ENV", Value = "dev"
                                }
                            }
                        }
                    }
                }
            };

            var result = client.CreateNamespacedPod(pod, "default");

            Console.WriteLine(result);
        }
示例#8
0
        public async Task ContinueWatchingThroughInactivity()
        {
            try
            {
                var eventsReceived = new AsyncCountdownEvent(1);


                var client = new Kubernetes(KubernetesClientConfiguration.BuildConfigFromConfigFile());

                var listTask = await client.ListNamespacedPodWithHttpMessagesAsync("testns", watch : true).ConfigureAwait(false);

                var events     = new HashSet <V1Pod>();
                var errors     = 0;
                var connclosed = 0;
                var eventcount = 0;
                var watcher    = listTask.Watch <V1Pod, V1PodList>(
                    (type, item) =>
                {
                    testOutput.WriteLine($"Watcher received '{type}' event.");

                    events.Add(item);
                    eventsReceived.Signal();
                },
                    error =>
                {
                    testOutput.WriteLine($"Watcher received '{error.GetType().FullName}' error.");

                    errors += 1;
                    eventsReceived.Signal();
                },
                    () =>
                {
                    connclosed += 1;
                });

                var pod1 = GetTestPod("test1");
                client.CreateNamespacedPod(pod1, "testns");

                // wait server yields all events
                await Task.WhenAny(eventsReceived.WaitAsync(), Task.Delay(3000)).ConfigureAwait(false);

                // wait for some time to pass before generating another event
                await Task.Delay(TimeSpan.FromMinutes(2)).ConfigureAwait(false);

                eventcount = events.Count;

                var pod2 = GetTestPod("test2");
                client.CreateNamespacedPod(pod2, "testns");

                // Allow 30 seconds for the event to come through
                await Task.Delay(TimeSpan.FromSeconds(30)).ConfigureAwait(false);

                // validate that one new event has been recieved after a pause
                Assert.True(
                    events.Count == (eventcount + 1),
                    $"No Events Were Received after Pause Event Count Is {eventcount}");


                Assert.Equal(1, errors);

                Assert.True(watcher.Watching);
                // cleanup after
                client.DeleteNamespacedPod("test1", "default");

                client.DeleteNamespacedPod("test2", "default");
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }