string CreateDeployment(string tenantName, string filename) { var yml = Yaml.LoadFromFileAsync <V1Deployment>(filename).Result; yml.Metadata.Name = tenantName; var ymlResult = _client.CreateNamespacedDeployment(yml, DEFAULT_NAMESPACE); return(ymlResult.Metadata.ClusterName); }
static void Main(string[] args) { var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); IKubernetes client = new Kubernetes(config); //ApiClient.setDebugging(true); Console.WriteLine("Starting Request!"); // ListPods(client); //var metaDataDictionary = new Dictionary<string, string>(); //metaDataDictionary.Add("name", "web-pod1"); //metaDataDictionary.Add("namespace", "default"); //client.CreateNamespacedPod(new k8s.Models.V1Pod() //{ // ApiVersion = "v1", // Kind = "Pod", // Metadata = new k8s.Models.V1ObjectMeta() { Labels = metaDataDictionary }, // Spec = new k8s.Models.V1PodSpec() // { // Containers = new List<k8s.Models.V1Container>() { // new k8s.Models.V1Container() { // Image = "abhishekagg24/stableapp:v1", // Name = "web-ctr1", // Ports = new List<k8s.Models.V1ContainerPort>(){ // new k8s.Models.V1ContainerPort() { ContainerPort = 8080 } // } // } // } // }, // //Status = new k8s.Models.V1PodStatus() { } //} //, "default"); //V1Pod obj = CreatePodDefinition(); //client.CreateNamespacedPod(obj, "default"); V1Deployment depl = CreateDeploymentDefinition(); var deployment = client.CreateNamespacedDeployment(depl, "default"); //if (deployment != null) //{ ListPods(client); // } Console.ReadKey(); }
public ActionResult <bool> Create() { ConnectToKube(); try { var model = new k8s.Models.V1Deployment(); model.ApiVersion = "V1"; model.Kind = "Deployment"; var guid = new Guid(); model.Metadata = new k8s.Models.V1ObjectMeta(); model.Metadata.Name = String.Concat("minecraft", guid); model.Spec = new k8s.Models.V1DeploymentSpec(); model.Spec.Replicas = 1; model.Spec.Template = new k8s.Models.V1PodTemplateSpec(); model.Spec.Template.Metadata = new k8s.Models.V1ObjectMeta(); model.Spec.Template.Metadata.Labels = new Dictionary <string, string>(); model.Spec.Template.Metadata.Labels.Add(new KeyValuePair <string, string>("app", model.Metadata.Name)); model.Spec.Template.Spec = new k8s.Models.V1PodSpec(); model.Spec.Template.Spec.Containers = new List <k8s.Models.V1Container>(); model.Spec.Template.Spec.Containers.Add(new k8s.Models.V1Container() { Name = "minecraft", Image = "openhack/minecraft-server:1.0" }); model.Spec.Template.Spec.Containers[0].Ports = new List <k8s.Models.V1ContainerPort>(); model.Spec.Template.Spec.Containers[0].Ports.Add(new k8s.Models.V1ContainerPort() { Name = "minecraft", ContainerPort = 25565 }); model.Spec.Template.Spec.Containers[0].Ports.Add(new k8s.Models.V1ContainerPort() { Name = "rcon", ContainerPort = 25575 }); model.Spec.Template.Spec.Containers[0].Env = new List <k8s.Models.V1EnvVar>(); model.Spec.Template.Spec.Containers[0].Env.Add(new k8s.Models.V1EnvVar() { Name = "EULA", Value = "TRUE" }); client.CreateNamespacedDeployment(model, "default"); } catch (Exception e) { return(false); } return(true); }
static async Task createDeploymentIfNotExists(Kubernetes client) { V1Deployment deployment = await Yaml.LoadFromFileAsync <V1Deployment>("./deploy.yaml"); try { client.CreateNamespacedDeployment( deployment, "default" ); } catch (Exception e) { /* deployment probably exists */ // Console.WriteLine($"Error creating deployment: {e}"); } }
public virtual IActionResult ServersPost(string servername) { Contract.Ensures(Contract.Result <IActionResult>() != null); try { var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); IKubernetes client = new Kubernetes(config); var deployment = ObterYaml <V1Deployment>(servername, "./aks-minecraft.yaml"); var services = ObterYaml <V1Service>(servername, "./services.yaml"); var newDeployment = client.CreateNamespacedDeployment( body: deployment, namespaceParameter: "default"); var newServices = client.CreateNamespacedService( body: services, namespaceParameter: "default"); string exampleJson = null; exampleJson = "{\n \"endpoints\" : {\n \"minecraft\" : \"minecraft\",\n \"rcon\" : \"rcon\"\n },\n \"name\" : \"name\"\n}"; var example = exampleJson != null ? JsonConvert.DeserializeObject <Server>(exampleJson) : default(Server); return(new ObjectResult(newDeployment)); } catch (Exception ex) { return(new ObjectResult(ex.Message)); } }
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, TraceWriter log, ExecutionContext context) { log.Info("C# HTTP trigger function processed a request."); //var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); var configpath = $"{context.FunctionAppDirectory}\\config\\config.txt"; var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(kubeconfigPath: configpath); Console.WriteLine("Starting Request!"); IKubernetes client = new Kubernetes(config); var labels = new Dictionary <string, string>(); labels.Add("app", "raincraft"); var ports = new List <V1ContainerPort>(); ports.Add(new V1ContainerPort() { ContainerPort = 25565 }); ports.Add(new V1ContainerPort() { ContainerPort = 25575 }); var env = new List <V1EnvVar>(); env.Add(new V1EnvVar() { Name = "EULA", Value = "true" }); var mounts = new List <V1VolumeMount>(); mounts.Add(new V1VolumeMount() { Name = "minedb", MountPath = "/data" }); var volumes = new List <V1Volume>(); volumes.Add(new V1Volume() { Name = "minedb", PersistentVolumeClaim = new V1PersistentVolumeClaimVolumeSource() { ClaimName = "minevol2" } }); var spec = new V1PodSpec(); spec.Containers = new List <V1Container>(); spec.Containers.Add(new V1Container() { Name = "raincraft-pod", Image = "openhack/minecraft-server:2.0-alpine", Ports = ports, Env = env, VolumeMounts = mounts }); spec.Volumes = volumes; var template = new V1PodTemplateSpec() { Metadata = new V1ObjectMeta() { Name = "raincraft-pod", Labels = labels }, Spec = spec }; var deployment = new V1Deployment() { ApiVersion = "apps/v1", Kind = "Deployment", Metadata = new V1ObjectMeta() { Name = "raincraft", }, Spec = new V1DeploymentSpec() { Replicas = 1, Template = template, Selector = new V1LabelSelector() { MatchLabels = labels } } }; deployment.Validate(); var dresult = client.CreateNamespacedDeployment(deployment, "default"); var list = new List <string>(); //foreach (var i in client.ListNamespacedService("default").Items) //{ // if (i.Metadata.Labels.Contains(new KeyValuePair<string, string>("app", "minepod-service"))) // list.Add(i.Metadata.Name); //} var pods = client.ListNamespacedPod("default").Items; foreach (var i in pods) { if (i.Metadata.Labels.Contains(new KeyValuePair <string, string>("app", "raincraft"))) { list.Add(i.Metadata.Name); } } return(new OkObjectResult(list) as ActionResult); }
public async System.Threading.Tasks.Task <IActionResult> Index(int id) { userName = this.User.FindFirstValue(ClaimTypes.NameIdentifier); var k8SClientConfig = KubernetesClientConfiguration.BuildDefaultConfig(); var client = new Kubernetes(k8SClientConfig); var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>(); optionsBuilder.UseSqlServer("ApplicationDbContext"); p = _context.Template.Find(id).Path; HttpContext.Session.SetInt32("TemplateId", id); var pDeployments = "/etc/athena/Templates/" + p + "/Deployment"; var pServices = "/etc/athena/Templates/" + p + "/Service"; var pIngress = "/etc/athena/Templates/" + p + "/Ingress"; string pNetPol = "/etc/athena/Templates/" + p + "/NetworkPolicy"; try { foreach (string file in Directory.EnumerateFiles(pServices)) { var fileContent = System.IO.File.ReadAllText(file); var service = Yaml.LoadFromString <V1Service>(fileContent); var result = client.CreateNamespacedService(service, userName); } foreach (string file in Directory.EnumerateFiles(pDeployments)) { var fileContent = System.IO.File.ReadAllText(file); var deployment = Yaml.LoadFromString <V1Deployment>(fileContent); var result = client.CreateNamespacedDeployment(deployment, userName); } foreach (string file in Directory.EnumerateFiles(pIngress)) { var fileContent = System.IO.File.ReadAllText(file); var ingress = Yaml.LoadFromString <Networkingv1beta1Ingress>(fileContent); AddIngress(client, ingress, userName); } foreach (string file in Directory.EnumerateFiles(pNetPol)) { V1NetworkPolicy netPol = null; var fileContent = System.IO.File.ReadAllText(file); netPol = Yaml.LoadFromString <V1NetworkPolicy>(fileContent); var result = client.CreateNamespacedNetworkPolicy(netPol, userName); } return(View()); } catch { return(View()); } }
public static Compute CreateCompute([ActivityTrigger] string caseNumber, ExecutionContext context, ILogger log) { log.LogInformation($"Creating compute for {caseNumber}."); var homeDirectory = context.FunctionAppDirectory; var compute = new Compute(); var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(Path.Combine(homeDirectory, "kubeconfig.json")); config.ClientCertificateKeyStoreFlags = X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable; var client = new Kubernetes(config); try { var name = $"dsirona-engine-ag-{caseNumber}"; var v1service = Yaml.LoadFromFileAsync <V1Service>(Path.Combine(homeDirectory, "engineService.yaml")).Result; v1service.Metadata.Name = name; v1service.Spec.Selector.Clear(); v1service.Spec.Selector.Add("app", name); var service = client.CreateNamespacedService(v1service, "default"); var v1Deployment = Yaml.LoadFromFileAsync <V1Deployment>(Path.Combine(homeDirectory, "engineDeployment.yaml")).Result; var v1PodTemplate = v1Deployment.Spec.Template; v1PodTemplate.Spec.Containers[0].Name = name; v1Deployment.Metadata.Name = name; v1Deployment.Spec.Selector.MatchLabels.Clear(); v1Deployment.Spec.Selector.MatchLabels.Add("app", name); v1Deployment.Spec.Template.Metadata.Labels.Clear(); v1Deployment.Spec.Template.Metadata.Labels.Add("app", name); var deployment = client.CreateNamespacedDeployment(v1Deployment, "default"); HttpOperationResponse <V1PodList> pods; V1Pod pod = null; //TODO: Keep trying till the status of the pod is Running, implement timeout for (int retry = 0; retry < 60; retry++) { pods = client.ListNamespacedPodWithHttpMessagesAsync("default", labelSelector: $"app={name}").Result; pod = pods.Body.Items[0]; if (pod.Status.Phase == "Running") { break; } Task.Delay(1000); } //Console.WriteLine($"Host IP : { pod.Status.HostIP}, ports: {service.Spec.Ports[0].NodePort},{service.Spec.Ports[1].NodePort},{service.Spec.Ports[2].NodePort}"); compute.Key = name; compute.IPAddress = pod.Status?.HostIP; compute.Port = service.Spec.Ports[0].NodePort.ToString(); } catch (Exception serviceException) { ///TODO: Implement rollback compute.Status = "Error"; compute.LastErrorMessage = serviceException.Message; Console.Write(serviceException.Message); } compute.Status = "Healthy"; return(compute); }