示例#1
0
        public async Task LoadFromFile()
        {
            var content = @"apiVersion: v1
kind: Pod
metadata:
  name: foo
";

            var tempFileName = Path.GetTempFileName();

            try
            {
                await File.WriteAllTextAsync(tempFileName, content).ConfigureAwait(false);

                var obj = await Yaml.LoadFromFileAsync <V1Pod>(tempFileName).ConfigureAwait(false);

                Assert.Equal("foo", obj.Metadata.Name);
            }
            finally
            {
                if (File.Exists(tempFileName))
                {
                    File.Delete(tempFileName);
                }
            }
        }
示例#2
0
        string CreatePod(string filename)
        {
            var yml = Yaml.LoadFromFileAsync <V1Pod>(filename).Result;

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

            return(ymlResult.Metadata.Name);
        }
示例#3
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);
        }
示例#4
0
        public string CreateService(string tenant, string filename)
        {
            var yml = Yaml.LoadFromFileAsync <V1Service>(filename).Result;

            yml.Metadata.Labels.Add("tenant", tenant);
            yml.Spec.Selector.Add("tenant", tenant);
            var svc = _client.CreateNamespacedService(yml, "default");

            return(svc.Metadata.Name);
        }
示例#5
0
        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);
        }
        private bool AddNamespaceFromYaml(int suffix)
        {
            var yamlFileName    = "./Yamls/addNamespace" + "-" + suffix.ToString() + ".yaml";
            var namespaceObject = Yaml.LoadFromFileAsync <V1Namespace>(yamlFileName).Result;
            var ns = client.CreateNamespace(namespaceObject);

            if (ns != null)
            {
                return(true);
            }
            return(false);
        }
        private bool AddServiceFromYaml(int suffix)
        {
            var yamlFileName = "./Yamls/services" + "-" + suffix.ToString() + ".yaml";
            var svcObject    = Yaml.LoadFromFileAsync <V1Service>(yamlFileName).Result;
            var svc          = client.CreateNamespacedService(svcObject, namespaceName + "-" + suffix.ToString());

            if (svc != null)
            {
                return(true);
            }
            return(false);
        }
        private bool AddPersistentVolumeClaimFromYaml(int suffix)
        {
            var yamlFileName = "./Yamls/azure-premium" + "-" + suffix.ToString() + ".yaml";
            var pvcObject    = Yaml.LoadFromFileAsync <V1PersistentVolumeClaim>(yamlFileName).Result;
            var pvc          = client.CreateNamespacedPersistentVolumeClaim(pvcObject, namespaceName + "-" + suffix.ToString());

            if (pvc != null)
            {
                return(true);
            }
            return(false);
        }
        private bool AddDeploymentFromYaml(int suffix)
        {
            var yamlConfigFile   = "./Yamls/externalCfg.yaml";
            var yamlConfigObject = Yaml.LoadFromFileAsync <V1ConfigMap>(yamlConfigFile).Result;
            var config           = client.CreateNamespacedConfigMap(yamlConfigObject, namespaceName + "-" + suffix.ToString());

            var yamlFileName = "./Yamls/deployments" + "-" + suffix.ToString() + ".yaml";
            var depObject    = Yaml.LoadFromFileAsync <V1Deployment>(yamlFileName).Result;
            var dep          = client.CreateNamespacedDeployment(depObject, namespaceName + "-" + suffix.ToString());

            if (dep != null)
            {
                return(true);
            }
            return(false);
        }
示例#10
0
        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}");
            }
        }
示例#11
0
        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);
        }