Repository: zalando-stups/senza

Issue:
Make some scaling properties configurable
* Period
* Cooldown
* Scaling adjustment

Discussion:

Patch:
diff --git a/senza/components/auto_scaling_group.py b/senza/components/auto_scaling_group.py
index 39fb634..c3c58c2 100644
--- a/senza/components/auto_scaling_group.py
+++ b/senza/components/auto_scaling_group.py
@@ -145,13 +145,14 @@ def component_auto_scaling_group(definition, configuration, args, info, force, a
         asg_properties["MinSize"] = as_conf["Minimum"]
         asg_properties["DesiredCapacity"] = max(int(as_conf["Minimum"]), int(as_conf.get('DesiredCapacity', 1)))
 
+        scaling_adjustment = int(as_conf.get("ScalingAdjustment", 1))
         # ScaleUp policy
         definition["Resources"][asg_name + "ScaleUp"] = {
             "Type": "AWS::AutoScaling::ScalingPolicy",
             "Properties": {
                 "AdjustmentType": "ChangeInCapacity",
-                "ScalingAdjustment": "1",
-                "Cooldown": "60",
+                "ScalingAdjustment": str(scaling_adjustment),
+                "Cooldown": str(as_conf.get("Cooldown", "60")),
                 "AutoScalingGroupName": {
                     "Ref": asg_name
                 }
@@ -163,8 +164,8 @@ def component_auto_scaling_group(definition, configuration, args, info, force, a
             "Type": "AWS::AutoScaling::ScalingPolicy",
             "Properties": {
                 "AdjustmentType": "ChangeInCapacity",
-                "ScalingAdjustment": "-1",
-                "Cooldown": "60",
+                "ScalingAdjustment": str((-1) * scaling_adjustment),
+                "Cooldown": str(as_conf.get("Cooldown", "60")),
                 "AutoScalingGroupName": {
                     "Ref": asg_name
                 }
@@ -295,15 +296,18 @@ def metric_network(asg_name, definition, configuration, args, info, force):
 
 
 def metric_cpu(asg_name, definition, configuration, args, info, force):
+    period = int(configuration.get("Period", 300))
+    evaluation_periods = int(configuration.get("EvaluationPeriods", 2))
+    statistic = configuration.get("Statistic", "Average")
     if "ScaleUpThreshold" in configuration:
         definition["Resources"][asg_name + "CPUAlarmHigh"] = {
             "Type": "AWS::CloudWatch::Alarm",
             "Properties": {
                 "MetricName": "CPUUtilization",
                 "Namespace": "AWS/EC2",
-                "Period": "300",
-                "EvaluationPeriods": "2",
-                "Statistic": "Average",
+                "Period": str(period),
+                "EvaluationPeriods": str(evaluation_periods),
+                "Statistic": statistic,
                 "Threshold": configuration["ScaleUpThreshold"],
                 "ComparisonOperator": "GreaterThanThreshold",
                 "Dimensions": [
@@ -312,7 +316,11 @@ def metric_cpu(asg_name, definition, configuration, args, info, force):
                         "Value": {"Ref": asg_name}
                     }
                 ],
-                "AlarmDescription": "Scale-up if CPU > {0}% for 10 minutes".format(configuration["ScaleUpThreshold"]),
+                "AlarmDescription": "Scale-up if CPU > {}% for {} minutes ({})".format(
+                    configuration["ScaleUpThreshold"],
+                    (period / 60) * evaluation_periods,
+                    statistic
+                ),
                 "AlarmActions": [
                     {"Ref": asg_name + "ScaleUp"}
                 ]
@@ -325,9 +333,9 @@ def metric_cpu(asg_name, definition, configuration, args, info, force):
             "Properties": {
                 "MetricName": "CPUUtilization",
                 "Namespace": "AWS/EC2",
-                "Period": "300",
-                "EvaluationPeriods": "2",
-                "Statistic": "Average",
+                "Period": str(period),
+                "EvaluationPeriods": str(evaluation_periods),
+                "Statistic": statistic,
                 "Threshold": configuration["ScaleDownThreshold"],
                 "ComparisonOperator": "LessThanThreshold",
                 "Dimensions": [
@@ -336,8 +344,11 @@ def metric_cpu(asg_name, definition, configuration, args, info, force):
                         "Value": {"Ref": asg_name}
                     }
                 ],
-                "AlarmDescription": "Scale-down if CPU < {0}% for 10 minutes".format(
-                    configuration["ScaleDownThreshold"]),
+                "AlarmDescription": "Scale-down if CPU < {}% for {} minutes ({})".format(
+                    configuration["ScaleDownThreshold"],
+                    (period / 60) * evaluation_periods,
+                    statistic
+                ),
                 "AlarmActions": [
                     {"Ref": asg_name + "ScaleDown"}
                 ]


Test patch:
diff --git a/tests/test_components.py b/tests/test_components.py
index 1e14885..beb6f5d 100644
--- a/tests/test_components.py
+++ b/tests/test_components.py
@@ -396,6 +396,59 @@ def test_component_taupage_auto_scaling_group_user_data_with_lists_and_empty_dic
     assert expected_user_data == generate_user_data(configuration)
 
 
+def test_component_auto_scaling_group_configurable_properties():
+    definition = {"Resources": {}}
+    configuration = {
+        'Name': 'Foo',
+        'InstanceType': 't2.micro',
+        'Image': 'foo',
+        'AutoScaling': {
+            'Minimum': 2,
+            'Maximum': 10,
+            'MetricType': 'CPU',
+            'Period': 60,
+            'ScaleUpThreshold': 50,
+            'ScaleDownThreshold': 20,
+            'EvaluationPeriods': 1,
+            'Cooldown': 30,
+            'Statistic': 'Maximum'
+        }
+    }
+
+    args = MagicMock()
+    args.region = "foo"
+
+    info = {
+        'StackName': 'FooStack',
+        'StackVersion': 'FooVersion'
+    }
+
+    result = component_auto_scaling_group(definition, configuration, args, info, False, MagicMock())
+
+    assert result["Resources"]["FooScaleUp"] is not None
+    assert result["Resources"]["FooScaleUp"]["Properties"] is not None
+    assert result["Resources"]["FooScaleUp"]["Properties"]["ScalingAdjustment"] == "1"
+    assert result["Resources"]["FooScaleUp"]["Properties"]["Cooldown"] == "30"
+
+    assert result["Resources"]["FooScaleDown"] is not None
+    assert result["Resources"]["FooScaleDown"]["Properties"] is not None
+    assert result["Resources"]["FooScaleDown"]["Properties"]["Cooldown"] == "30"
+    assert result["Resources"]["FooScaleDown"]["Properties"]["ScalingAdjustment"] == "-1"
+
+    assert result["Resources"]["Foo"] is not None
+    assert result["Resources"]["Foo"]["Properties"] is not None
+    assert result["Resources"]["Foo"]["Properties"]["HealthCheckType"] == "EC2"
+    assert result["Resources"]["Foo"]["Properties"]["MinSize"] == 2
+    assert result["Resources"]["Foo"]["Properties"]["DesiredCapacity"] == 2
+    assert result["Resources"]["Foo"]["Properties"]["MaxSize"] == 10
+
+    expected_desc = "Scale-down if CPU < 20% for 1.0 minutes (Maximum)"
+    assert result["Resources"]["FooCPUAlarmHigh"]["Properties"]["Statistic"] == "Maximum"
+    assert result["Resources"]["FooCPUAlarmLow"]["Properties"]["Period"] == "60"
+    assert result["Resources"]["FooCPUAlarmHigh"]["Properties"]["EvaluationPeriods"] == "1"
+    assert result["Resources"]["FooCPUAlarmLow"]["Properties"]["AlarmDescription"] == expected_desc
+
+
 def test_component_auto_scaling_group_metric_type():
     definition = {"Resources": {}}
     configuration = {
@@ -410,7 +463,7 @@ def test_component_auto_scaling_group_metric_type():
             'EvaluationPeriods': 10,
             'ScaleUpThreshold': '50 TB',
             'ScaleDownThreshold': '10',
-            'Statistic': 'Maximum',
+            'Statistic': 'Maximum'
         }
     }