CreateHPAAsync(Kubernetes k8sClient, V1HorizontalPodAutoscaler yamlModel,
                       MTAHPAModel hpaModel)
        {
            try
            {
                var namespaceParams = PrepareNamespaceParams(_groupName);
                var hpaParams       = PrepareHPAParams(_hpaName, _deployName);

                yamlModel.Metadata.Name = hpaParams.Item1;
                yamlModel.Metadata.NamespaceProperty          = namespaceParams;
                yamlModel.Spec.ScaleTargetRef.Name            = hpaParams.Item2;
                yamlModel.Spec.MaxReplicas                    = hpaModel.MaxReplicas;
                yamlModel.Spec.MinReplicas                    = hpaModel.MinReplicas;
                yamlModel.Spec.TargetCPUUtilizationPercentage = hpaModel.AvgCPU;

                var v1HorizontalPodAutoscaler = await k8sClient
                                                .CreateNamespacedHorizontalPodAutoscalerAsync
                                                    (yamlModel, namespaceParams);

                hpaModel = new MTAHPAModel(v1HorizontalPodAutoscaler);
                return(new Tuple <MTAHPAModel, MTAErrorModel>(hpaModel, null));
            }
            catch (HttpOperationException ex)
            {
                var errorModel = new MTAErrorModel(ex);
                return(new Tuple <MTAHPAModel, MTAErrorModel>(null, errorModel));
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> CreateHPAAsync
            ([FromBody] MTAHPAModel hpaBody,
            [FromRoute] string hpaName,
            [FromRoute] string tenantName,
            [FromRoute] string groupName)
        {
            var path    = string.Concat(GetTemplatesPath(), "/template-hpa.yaml");
            var hpaList = await Yaml.LoadAllFromFileAsync(path);

            var yamlBody      = hpaList[0] as V1HorizontalPodAutoscaler;
            var deployName    = hpaBody.DeploymentName;
            var hpaController = new MTAHPAController(hpaName, deployName, tenantName, groupName);
            var respondeModel = await hpaController.CreateHPAAsync
                                    (_k8sClient, yamlBody, hpaBody);

            if (respondeModel.Item2 != null)
            {
                return(BadRequest(respondeModel.Item2));
            }

            return(Created(Url.RouteUrl("CreateService", new { hpaName, groupName }),
                           respondeModel.Item1));
        }
        ReadHPAAsync(Kubernetes k8sClient)
        {
            try
            {
                var namespaceParams = PrepareNamespaceParams(_groupName);
                var hpaParams       = PrepareHPAParams(_hpaName, _deployName);

                var v1HorizontalPodAutoscaler = await k8sClient
                                                .ReadNamespacedHorizontalPodAutoscalerAsync
                                                    (hpaParams.Item1, namespaceParams);

                var hpaModel = new MTAHPAModel(v1HorizontalPodAutoscaler);
                return(new Tuple <MTAHPAModel, MTAErrorModel>(hpaModel, null));
            }
            catch (HttpOperationException ex)
            {
                var errorModel = new MTAErrorModel(ex);
                return(new Tuple <MTAHPAModel, MTAErrorModel>(null, errorModel));
            }
            catch (Exception)
            {
                throw;
            }
        }