This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
/
autoscale.go
111 lines (102 loc) · 2.98 KB
/
autoscale.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
Copyright 2016 Skippbox, Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package autoscale
import (
"fmt"
"strconv"
"github.com/spf13/cobra"
"k8s.io/api/autoscaling/v2beta1"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
//AutoscaleCmd contains first-class command for autoscale
var AutoscaleCmd = &cobra.Command{
Use: "autoscale SUBCOMMAND",
Short: "manage autoscale to function on Kubeless",
Long: `autoscale command allows user to list, create, delete autoscale rule for function on Kubeless`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
func init() {
cmds := []*cobra.Command{autoscaleCreateCmd, autoscaleListCmd, autoscaleDeleteCmd}
for _, cmd := range cmds {
AutoscaleCmd.AddCommand(cmd)
cmd.Flags().StringP("namespace", "n", "", "Specify namespace for the autoscale")
}
}
func getHorizontalAutoscaleDefinition(name, ns, metric string, min, max int32, value string, labels map[string]string) (v2beta1.HorizontalPodAutoscaler, error) {
m := []v2beta1.MetricSpec{}
switch metric {
case "cpu":
i, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return v2beta1.HorizontalPodAutoscaler{}, err
}
i32 := int32(i)
m = []v2beta1.MetricSpec{
{
Type: v2beta1.ResourceMetricSourceType,
Resource: &v2beta1.ResourceMetricSource{
Name: v1.ResourceCPU,
TargetAverageUtilization: &i32,
},
},
}
case "qps":
q, err := resource.ParseQuantity(value)
if err != nil {
return v2beta1.HorizontalPodAutoscaler{}, err
}
m = []v2beta1.MetricSpec{
{
Type: v2beta1.ObjectMetricSourceType,
Object: &v2beta1.ObjectMetricSource{
MetricName: "function_calls",
TargetValue: q,
Target: v2beta1.CrossVersionObjectReference{
Kind: "Service",
Name: name,
},
},
},
}
if err != nil {
return v2beta1.HorizontalPodAutoscaler{}, err
}
default:
return v2beta1.HorizontalPodAutoscaler{}, fmt.Errorf("metric %s is not supported", metric)
}
return v2beta1.HorizontalPodAutoscaler{
TypeMeta: metav1.TypeMeta{
APIVersion: "autoscaling/v2beta1",
Kind: "HorizontalPodAutoscaler",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ns,
Labels: labels,
},
Spec: v2beta1.HorizontalPodAutoscalerSpec{
ScaleTargetRef: v2beta1.CrossVersionObjectReference{
APIVersion: "apps/v1beta1",
Kind: "Deployment",
Name: name,
},
MinReplicas: &min,
MaxReplicas: max,
Metrics: m,
},
}, nil
}