Exemple #1
0
        public IActionResult Post([FromQuery] string name, string namespaceName)
        {
            var job = new V1Job
            {
                ApiVersion = "batch/v1",
                Kind       = "Job",
                Metadata   = new V1ObjectMeta
                {
                    Name = name
                },
                Spec = new V1JobSpec
                {
                    Template = new V1PodTemplateSpec()
                    {
                        Spec = new V1PodSpec()
                        {
                            Containers = new List <V1Container>()
                            {
                                new V1Container()
                                {
                                    Name  = "container-test",
                                    Image = "hello-world"
                                }
                            },
                            RestartPolicy = "Never"
                        }
                    }
                }
            };

            var result = kubeClient.CreateNamespacedJob(job, namespaceName);

            return(Ok());
        }
Exemple #2
0
        public V1JobStatus CreateK8sJob(dynamic messageBody)
        {
            var job = new V1Job();

            var container = new V1Container();

            container.Image   = kubernetesJob.ImageName;
            container.Name    = kubernetesJob.KubernetesNamespace;
            container.Command = new List <string>();

            // container entry point
            container.Command.Add("dotnet");
            container.Command.Add(kubernetesJob.EntryPoint);

            // job arguments
            container.Command.Add(messageBody);

            job.Kind          = "Job";
            job.ApiVersion    = "batch/v1";
            job.Metadata      = new V1ObjectMeta();
            job.Metadata.Name = $"{kubernetesJob.JobName.ToLower()}-{DateTime.Now.Ticks.ToString()}";
            job.Metadata.NamespaceProperty = kubernetesJob.KubernetesNamespace;
            job.Spec                          = new V1JobSpec();
            job.Spec.Template                 = new V1PodTemplateSpec();
            job.Spec.Template.Spec            = new V1PodSpec();
            job.Spec.Template.Spec.Containers = new List <V1Container>();
            job.Spec.Template.Spec.Containers.Add(container);
            job.Spec.Template.Spec.RestartPolicy = "Never";
            job.Validate();

            var config = KubernetesClientConfiguration.BuildDefaultConfig();

            Console.WriteLine(config);
            IKubernetes client     = new Kubernetes(config);
            var         createdJob = client.CreateNamespacedJob(job, kubernetesJob.KubernetesNamespace);

            return(createdJob.Status);
        }