private void EnsureGroupNamespace(User user)
        {
            var groupNamespaceName = user.Spec.GetGroupNamespace();
            var kiamolNamespaces   = _client.ListNamespace(fieldSelector: $"metadata.name={groupNamespaceName}");

            if (!kiamolNamespaces.Items.Any())
            {
                var groupNamespace = new V1Namespace
                {
                    Metadata = new V1ObjectMeta
                    {
                        Name   = groupNamespaceName,
                        Labels = new Dictionary <string, string>()
                        {
                            { "kiamol", "ch20" },
                        }
                    }
                };
                _client.CreateNamespace(groupNamespace);
                Console.WriteLine($"** Created group namespace: {groupNamespaceName}");
            }
            else
            {
                Console.WriteLine($"** Group namespace exists: {groupNamespaceName}");
            }
        }
        private static void Main(string[] args)
        {
            var         k8SClientConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile();
            IKubernetes client          = new Kubernetes(k8SClientConfig);

            ListNamespaces(client);

            var ns = new V1Namespace {
                Metadata = new V1ObjectMeta {
                    Name = "test"
                }
            };

            var result = client.CreateNamespace(ns);

            Console.WriteLine(result);

            ListNamespaces(client);

            var status = client.DeleteNamespace(ns.Metadata.Name, new V1DeleteOptions());

            if (status.HasObject)
            {
                var obj = status.ObjectView <V1Namespace>();
                Console.WriteLine(obj.Status.Phase);

                Delete(client, ns.Metadata.Name, 3 * 1000);
            }
            else
            {
                Console.WriteLine(status.Message);
            }

            ListNamespaces(client);
        }
Beispiel #3
0
        public IActionResult Post([FromQuery] string name)
        {
            var ns = new V1Namespace
            {
                Metadata = new V1ObjectMeta
                {
                    Name = name
                }
            };

            var result = kubeClient.CreateNamespace(ns);

            return(Ok());
        }
Beispiel #4
0
        public ActionResult <IEnumerable <string> > CreateNamespace()
        {
            var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
            var client = new Kubernetes(config);
            var ns     = new V1Namespace
            {
                Metadata = new V1ObjectMeta
                {
                    Name = "test"
                }
            };
            var result = client.CreateNamespace(ns);

            return(new string[] { "Operation", "Namespace Created" });
        }
Beispiel #5
0
        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));
            }
        }