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 IActionResult Index() { string a; var UserEmail = this.User.Identity.Name; var result = GetUserRoles(UserEmail, "*****@*****.**", out a); ViewBag.Tutor = a; ViewBag.Admin = result; if (a == "/Staff") { HttpContext.Session.SetString("Role", "staff"); } else { HttpContext.Session.SetString("Role", "student"); } var UserId = this.User.FindFirstValue(ClaimTypes.NameIdentifier); try { var k8SClientConfig = KubernetesClientConfiguration.BuildDefaultConfig(); var client = new Kubernetes(k8SClientConfig); var namespaces = client.ListNamespace(null, null, "metadata.name=" + UserId); if (namespaces != null && namespaces.Items.Count > 0) { return(View()); } var ns = new V1Namespace { Metadata = new V1ObjectMeta { Name = UserId } }; var result2 = client.CreateNamespace(ns); var netPolFile = "default-network-policy.yaml"; if (System.IO.File.Exists(netPolFile)) { var fileContent = System.IO.File.ReadAllText(netPolFile); var netPol = Yaml.LoadFromString <V1NetworkPolicy>(fileContent); client.CreateNamespacedNetworkPolicy(netPol, UserId); } ViewData["Message"] = ""; return(View()); } catch (Exception e) { return(RedirectToAction("Error", e)); } }