Пример #1
0
        public static async Task <IActionResult> RunAsync(
            [HttpTrigger(AuthorizationLevel.Function, "put", Route = "servers")] HttpRequest req,
            TraceWriter log, ExecutionContext context)
        {
            try
            {
                log.Info("Came into.");
                string  name        = req.Query["name"];
                string  replicas    = req.Query["replicas"];
                string  requestBody = new StreamReader(req.Body).ReadToEnd();
                dynamic data        = JsonConvert.DeserializeObject(requestBody);
                name     = name ?? data?.name;
                replicas = replicas ?? data?.replicas;
                var configFile = Path.Combine(context.FunctionAppDirectory, configPath);
                var file       = new FileInfo(configFile);
                var config     = KubernetesClientConfiguration.BuildConfigFromConfigFile(file);
                var client     = new Kubernetes(config);

                var deployment = SpecsFactory.CreateDeployment(name, int.Parse(replicas));
                await client.ReplaceNamespacedDeployment1WithHttpMessagesAsync(deployment, name, "default");

                return(new OkResult());
            }
            catch (Exception e)
            {
                log.Error("Error", e);
                return(new BadRequestResult());
            }
        }
Пример #2
0
        static async Task CreateService(string name)
        {
            //if (client == null)
            // throw new ArgumentNullException(nameof(client));

            var service = SpecsFactory.CreateService(name);
            await client.CreateNamespacedServiceWithHttpMessagesAsync(service, "default");
        }
Пример #3
0
        private static async Task DeleteDeployment(TraceWriter log, string instanceName, string namespaceParameter = "default")
        {
            var depName = "minecraft-server-" + instanceName;

            log.Verbose($"deleting deploy: {depName}");

            var dep = SpecsFactory.CreateDeployment(instanceName, 0);

            log.Verbose($"emptying replicas for deployment");
            await client.ReplaceNamespacedDeployment1WithHttpMessagesAsync(dep, depName, "default");

            var depls = await client.ReadNamespacedDeployment3WithHttpMessagesAsync(depName, namespaceParameter);

            log.Verbose($"replicasets found");
            if (depls.Response?.IsSuccessStatusCode ?? true)
            {
                var sets =
                    await client.ListReplicaSetForAllNamespaces2WithHttpMessagesAsync(labelSelector : "app=" + depName);

                foreach (var st in sets.Body.Items)
                {
                    log.Verbose($"emptying replicas in {st.Metadata.Name}");
                    st.Spec.Replicas = 0;
                    await client.ReplaceNamespacedReplicaSet2WithHttpMessagesAsync(st, st.Metadata.Name,
                                                                                   "default");
                }


                foreach (var name in sets.Body.Items.Select(k => k.Metadata.Name))
                {
                    bool deleted;
                    do
                    {
                        var st2 = await client.ReadNamespacedReplicaSet2WithHttpMessagesAsync(name,
                                                                                              "default");

                        deleted = st2.Body.Status.ReadyReplicas == 0;
                    } while (!deleted);
                    log.Verbose($"replicaset {name} deleted");
                    await client.DeleteNamespacedReplicaSet2WithHttpMessagesAsync(new V1DeleteOptions(), name, "default");
                }
                await client.DeleteNamespacedDeployment3WithHttpMessagesAsync(new V1DeleteOptions(),
                                                                              "minecraft-server-" + instanceName, namespaceParameter, propagationPolicy : "Background");
            }
            else
            {
                log.Verbose($"deleted it says");
            }
            log.Verbose($"done");
        }
Пример #4
0
 public static async Task CreateDeployment(string instanceName, int replicasCount = 1)
 {
     var deployment = SpecsFactory.CreateDeployment(instanceName, replicasCount);
     await client.CreateNamespacedDeployment1WithHttpMessagesAsync(deployment, "default");
 }
Пример #5
0
 static async Task CreateLabeledClaim(string serverName)
 {
     var claim = SpecsFactory.CreateVolumeClaim(serverName);
     await client.CreateNamespacedPersistentVolumeClaimWithHttpMessagesAsync(claim, "default");
 }