From bfa32a0ce50cda31aa058158a8005a81adf5d173 Mon Sep 17 00:00:00 2001 From: rahulrayal <110025849+rahulrayal@users.noreply.github.com> Date: Wed, 12 Jul 2023 19:48:53 +0530 Subject: [PATCH] Conformation for autoscaling enabled scaler onboarded components (#201) Co-authored-by: rahulrayal --- api/service/service.go | 4 +++ app/app.go | 2 +- internal/backend/service.go | 12 +++++++ internal/command/commands/component.go | 27 +++++++++++++++ internal/command/commands/service.go | 47 ++++++++++++++++++++++++-- 5 files changed, 89 insertions(+), 3 deletions(-) diff --git a/api/service/service.go b/api/service/service.go index e00b3960..f3dca724 100644 --- a/api/service/service.go +++ b/api/service/service.go @@ -76,3 +76,7 @@ type OperationRequest struct { type OperationValidationResponse struct { Response OperationValidationResponseBody `yaml:"resp,omitempty" json:"resp,omitempty"` } + +type ScalingConsentResponse struct { + Response []string `yaml:"resp,omitempty" json:"resp,omitempty"` +} diff --git a/app/app.go b/app/app.go index 79adef80..280d84db 100644 --- a/app/app.go +++ b/app/app.go @@ -8,5 +8,5 @@ type application struct { // App (Application) interface var App application = application{ Name: "odin", - Version: "1.3.1-beta.1", + Version: "1.3.1", } diff --git a/internal/backend/service.go b/internal/backend/service.go index 1822e029..72d2a695 100644 --- a/internal/backend/service.go +++ b/internal/backend/service.go @@ -186,3 +186,15 @@ func (s *Service) OperateService(serviceName string, data service.OperationReque response := client.streamWithRetry(path.Join(serviceEntity, serviceName)+"/operate/", "PUT", data) response.Process(true) } + +func (s *Service) ScalingServiceConsent(serviceName string, data interface{}) (service.ScalingConsentResponse, error) { + client := newApiClient() + + response := client.actionWithRetry(path.Join(serviceEntity, serviceName)+"/reactive-scaled-scaler-components", "POST", data) + response.Process(true) + + var scalingConsentResponse service.ScalingConsentResponse + err := json.Unmarshal(response.Body, &scalingConsentResponse) + + return scalingConsentResponse, err +} diff --git a/internal/command/commands/component.go b/internal/command/commands/component.go index 915a0550..fa6febef 100644 --- a/internal/command/commands/component.go +++ b/internal/command/commands/component.go @@ -180,6 +180,33 @@ func (c *Component) Run(args []string) int { } } + dataForScalingConsent := map[string]interface{}{ + "env_name": *envName, + "component_name": *name, + "action": *operation, + "config": optionsData, + } + componentListResponse, err := serviceClient.ScalingServiceConsent(*serviceName, dataForScalingConsent) + if err != nil { + c.Logger.Error(err.Error()) + return 1 + } + for _, component := range componentListResponse.Response { + consentMessage := fmt.Sprintf("\nAs you have enabled ASG auto scaling policy for %s, It will no longer be scaled using Scaler post this operation. Do you wish to continue? [Y/n]:", component) + allowedInputs := map[string]struct{}{"Y": {}, "n": {}} + val, err := c.Input.AskWithConstraints(consentMessage, allowedInputs) + + if err != nil { + c.Logger.Error(err.Error()) + return 1 + } + + if val != "Y" { + c.Logger.Info("Aborting...") + return 1 + } + } + componentClient.OperateComponent(*name, data) return 0 } diff --git a/internal/command/commands/service.go b/internal/command/commands/service.go index f5321880..01c19aa2 100644 --- a/internal/command/commands/service.go +++ b/internal/command/commands/service.go @@ -352,7 +352,7 @@ func (s *Service) Run(args []string) int { } if !isOperationPresnt { - s.Logger.Error("--opertion cannot be blank") + s.Logger.Error("--operation cannot be blank") return 1 } @@ -399,6 +399,16 @@ func (s *Service) Run(args []string) int { return 1 } + dataForScalingConsent := map[string]interface{}{ + "env_name": *envName, + "action": *operation, + "config": optionsData, + } + scalingConsent := s.askForScalingConsent(serviceName, envName, dataForScalingConsent) + if scalingConsent == 1 { + return 1 + } + data := service.OperationRequest{ EnvName: *envName, Operations: []service.Operation{ @@ -471,6 +481,30 @@ func (s *Service) askForConsent(envName *string) int { return 0 } +func (s *Service) askForScalingConsent(serviceName *string, envName *string, data map[string]interface{}) int { + componentListResponse, err := serviceClient.ScalingServiceConsent(*serviceName, data) + if err != nil { + s.Logger.Error(err.Error()) + return 1 + } + for _, component := range componentListResponse.Response { + consentMessage := fmt.Sprintf("\nAs you have enabled ASG auto scaling policy for %s, It will no longer be scaled using Scaler post this operation. Do you wish to continue? [Y/n]:", component) + allowedInputs := map[string]struct{}{"Y": {}, "n": {}} + val, err := s.Input.AskWithConstraints(consentMessage, allowedInputs) + + if err != nil { + s.Logger.Error(err.Error()) + return 1 + } + + if val != "Y" { + s.Logger.Info("\nAborting...") + return 1 + } + } + return 0 +} + func (s *Service) deployUnreleasedService(envName *string, serviceDefinition map[string]interface{}, provisioningConfigFile *string, configStoreNamespace *string) int { if serviceDefinition["name"] == nil || len(serviceDefinition["name"].(string)) == 0 { @@ -500,7 +534,16 @@ func (s *Service) deployReleasedService(envName *string, serviceName *string, se if done { return i } - + dataForScalingConsent := map[string]interface{}{ + "env_name": *envName, + "service_version": *serviceVersion, + "action": "released_service_deploy", + "config": parsedProvisioningConfig, + } + scalingConsent := s.askForScalingConsent(serviceName, envName, dataForScalingConsent) + if scalingConsent == 1 { + return 1 + } s.Logger.Debug(fmt.Sprintf("%s: %s : %s: %s:", *serviceName, *serviceVersion, *envName, *configStoreNamespace)) s.Logger.Info("Initiating service deployment: " + *serviceName + "@" + *serviceVersion + " in " + *envName) serviceClient.DeployReleasedServiceStream(*serviceName, *serviceVersion, *envName, *configStoreNamespace, parsedProvisioningConfig)