From 5574a5330e358163d5d6ebe64b101a08cde9d666 Mon Sep 17 00:00:00 2001 From: Hardik Dodiya Date: Wed, 29 Apr 2020 11:29:30 +0530 Subject: [PATCH] Annotate machine-deployment if machine-types are not available in cloud --- pkg/controller/constants.go | 20 +++++++++++++++++++ pkg/controller/deployment.go | 2 +- pkg/controller/machine.go | 37 ++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 pkg/controller/constants.go diff --git a/pkg/controller/constants.go b/pkg/controller/constants.go new file mode 100644 index 000000000..1ec977a08 --- /dev/null +++ b/pkg/controller/constants.go @@ -0,0 +1,20 @@ +package controller + +// TODO: Split and move this constants to provider-specific machine-controllers in future. +const ( + // MachineTypeNotAvailableAzure is a log message depicting certain machine-types are not available in the azure-cloud. + MachineTypeNotAvailableAzure = "The requested size for resource '.*' is currently not available in location '.*' zones '.*' for subscription '.*'" + + // MachineTypeNotAvailableAWS is a log message depicting certain machine-types are not available in the azure-cloud. + MachineTypeNotAvailableAWS = "Unsupported: Your requested instance type (.*) is not supported in your requested Availability Zone (.*). Please retry your request by not specifying an Availability Zone or choosing .*" + + // MachineTypeNotAvailableAnnotation annotation is put on machine and node obejcts when cloud-provider is out of certain machine-types. + MachineTypeNotAvailableAnnotation = "machine.sapcloud.io/machine-type-not-available" +) + +// Original error messages: +// AWS: +// Cloud provider message - Unsupported: Your requested instance type (m5.4xlarge) is not supported in your requested Availability Zone (us-east-1e). Please retry your request by not specifying an Availability Zone or choosing us-east-1b, us-east-1d, us-east-1a, us-east-1f, us-east-1c. + +// Azure: +// The requested size for resource '/subscriptions/123c2-4-1234/resourceGroups/shoot--12-12/providers/Microsoft.Compute/virtualMac2321321312/1234-nz7vw' is currently not available in location 'westeurope' zones '1' for subscription '2c3d1231-s21321321-8c04-2711234'. diff --git a/pkg/controller/deployment.go b/pkg/controller/deployment.go index 05fb231c1..c89dd3ad6 100644 --- a/pkg/controller/deployment.go +++ b/pkg/controller/deployment.go @@ -302,7 +302,7 @@ func (dc *controller) getMachineDeploymentForMachine(machine *v1alpha1.Machine) // No controller owns this Machine. return nil } - if controllerRef.Kind != "MachineDeployment" { //TODO: Remove hardcoded string + if controllerRef.Kind != "MachineSet" { //TODO: Remove hardcoded string // Not a Machine owned by a machine set. return nil } diff --git a/pkg/controller/machine.go b/pkg/controller/machine.go index 09a36d6ae..777719349 100644 --- a/pkg/controller/machine.go +++ b/pkg/controller/machine.go @@ -22,6 +22,7 @@ import ( "errors" "fmt" "math" + "regexp" "strings" "time" @@ -428,6 +429,27 @@ func (c *controller) machineCreate(machine *v1alpha1.Machine, driver driver.Driv klog.Warning(err) } + // Check if the creation-error logs contain the specific pattern, and annotate the machine-deployment if found. + contains, _ := c.containsSpecialPattern(err.Error(), MachineTypeNotAvailableAzure, MachineTypeNotAvailableAWS) + + if contains { + md := c.getMachineDeploymentForMachine(machine) + if md != nil { + mdCopy := md.DeepCopy() + annotations := mdCopy.Spec.Template.Spec.NodeTemplateSpec.Annotations + if annotations == nil { + annotations = make(map[string]string) + } + annotations[MachineTypeNotAvailableAnnotation] = "True" + mdCopy.Spec.Template.Spec.NodeTemplateSpec.Annotations = annotations + + _, errUpdate := c.controlMachineClient.MachineDeployments(machine.Namespace).Update(mdCopy) + if errUpdate != nil { + klog.Errorf("Error updating the machine-deployment %+v", errUpdate) + } + } + } + return err } klog.V(2).Infof("Created/Adopted machine: %q, MachineID: %s", machine.Name, actualProviderID) @@ -1025,3 +1047,18 @@ func decodeMachineID(id string) string { splitProviderID := strings.Split(id, "/") return splitProviderID[len(splitProviderID)-1] } + +// containsSpecialPattern checks if the errorString contains any of the patterns +func (c *controller) containsSpecialPattern(errorString string, patterns ...string) (bool, error) { + for _, pattern := range patterns { + matched, err := regexp.MatchString(pattern, errorString) + if err != nil { + klog.Errorf("Error matching the string %+v", err) + return false, err + } + if matched { + return true, nil + } + } + return false, nil +}