-
Notifications
You must be signed in to change notification settings - Fork 105
/
hpa_reconciler.go
202 lines (174 loc) · 6.2 KB
/
hpa_reconciler.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2021 IBM Corporation
//
// 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 hpa
import (
"context"
"strconv"
"github.com/kserve/kserve/pkg/apis/serving/v1beta1"
"github.com/kserve/kserve/pkg/constants"
"github.com/kserve/kserve/pkg/utils"
mmcontstant "github.com/kserve/modelmesh-serving/pkg/constants"
hpav2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)
var log = logf.Log.WithName("HPAReconciler")
// HPAReconciler is the struct of Raw K8S Object
type HPAReconciler struct {
client client.Client
scheme *runtime.Scheme
HPA *hpav2.HorizontalPodAutoscaler
}
func NewHPAReconciler(client client.Client,
scheme *runtime.Scheme, runtimeMeta metav1.ObjectMeta, mmDeploymentName string, mmNamespace string) *HPAReconciler {
return &HPAReconciler{
client: client,
scheme: scheme,
HPA: createHPA(runtimeMeta, mmDeploymentName, mmNamespace),
}
}
func getHPAMetrics(metadata metav1.ObjectMeta) []hpav2.MetricSpec {
var metrics []hpav2.MetricSpec
var utilization int32 = constants.DefaultCPUUtilization
annotations := metadata.Annotations
resourceName := corev1.ResourceCPU
if value, ok := annotations[constants.TargetUtilizationPercentage]; ok {
if valueInt, err := strconv.ParseInt(value, 10, 32); err != nil {
log.Error(err, "Could not parse TargetUtilizationPercentage", "value", value)
} else {
utilization = int32(valueInt)
}
}
if value, ok := annotations[constants.AutoscalerMetrics]; ok {
resourceName = corev1.ResourceName(value)
}
metricTarget := hpav2.MetricTarget{
Type: "Utilization",
AverageUtilization: &utilization,
}
ms := hpav2.MetricSpec{
Type: hpav2.ResourceMetricSourceType,
Resource: &hpav2.ResourceMetricSource{
Name: resourceName,
Target: metricTarget,
},
}
metrics = append(metrics, ms)
return metrics
}
func createHPA(runtimeMeta metav1.ObjectMeta, mmDeploymentName string, mmNamespace string) *hpav2.HorizontalPodAutoscaler {
minReplicas := int32(constants.DefaultMinReplicas)
maxReplicas := int32(constants.DefaultMinReplicas)
annotations := runtimeMeta.Annotations
if value, ok := annotations[mmcontstant.MinScaleAnnotationKey]; ok {
if valueInt, err := strconv.ParseInt(value, 10, 32); err != nil {
log.Error(err, "Could not parse MinScaleAnnotationKey", "value", value)
} else {
minReplicas = int32(valueInt)
}
}
if value, ok := annotations[mmcontstant.MaxScaleAnnotationKey]; ok {
if valueInt, err := strconv.ParseInt(value, 10, 32); err != nil {
log.Error(err, "Could not parse MaxScaleAnnotationKey", "value", value)
} else {
maxReplicas = int32(valueInt)
}
}
if maxReplicas < minReplicas {
maxReplicas = minReplicas
}
metrics := getHPAMetrics(runtimeMeta)
hpaObjectMeta := metav1.ObjectMeta{
Name: mmDeploymentName,
Namespace: mmNamespace,
Labels: utils.Union(runtimeMeta.Labels, map[string]string{
constants.InferenceServicePodLabelKey: runtimeMeta.Name,
constants.KServiceComponentLabel: string(v1beta1.PredictorComponent),
}),
Annotations: runtimeMeta.Annotations,
}
hpa := &hpav2.HorizontalPodAutoscaler{
ObjectMeta: hpaObjectMeta,
Spec: hpav2.HorizontalPodAutoscalerSpec{
ScaleTargetRef: hpav2.CrossVersionObjectReference{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: hpaObjectMeta.Name,
},
MinReplicas: &minReplicas,
MaxReplicas: maxReplicas,
Metrics: metrics,
Behavior: &hpav2.HorizontalPodAutoscalerBehavior{},
},
}
return hpa
}
// checkHPAExist checks if the hpa exists?
func (r *HPAReconciler) checkHPAExist(client client.Client) (constants.CheckResultType, *hpav2.HorizontalPodAutoscaler, error) {
existingHPA := &hpav2.HorizontalPodAutoscaler{}
err := client.Get(context.TODO(), types.NamespacedName{
Namespace: r.HPA.ObjectMeta.Namespace,
Name: r.HPA.ObjectMeta.Name,
}, existingHPA)
if err != nil {
if apierr.IsNotFound(err) {
return constants.CheckResultCreate, nil, nil
}
return constants.CheckResultUnknown, nil, err
}
//existed, check equivalent
if semanticHPAEquals(r.HPA, existingHPA) {
return constants.CheckResultExisted, existingHPA, nil
}
return constants.CheckResultUpdate, existingHPA, nil
}
func semanticHPAEquals(desired, existing *hpav2.HorizontalPodAutoscaler) bool {
return equality.Semantic.DeepEqual(desired.Spec.Metrics, existing.Spec.Metrics) &&
equality.Semantic.DeepEqual(desired.Spec.MaxReplicas, existing.Spec.MaxReplicas) &&
equality.Semantic.DeepEqual(*desired.Spec.MinReplicas, *existing.Spec.MinReplicas)
}
// Reconcile ...
func (r *HPAReconciler) Reconcile(scaleToZero bool) (*hpav2.HorizontalPodAutoscaler, error) {
//reconcile
checkResult, existingHPA, err := r.checkHPAExist(r.client)
log.Info("service reconcile", "checkResult", checkResult, "scaleToZero", scaleToZero, "err", err)
if err != nil {
return nil, err
}
if checkResult == constants.CheckResultCreate && !scaleToZero {
if err = r.client.Create(context.TODO(), r.HPA); err != nil {
return nil, err
}
return r.HPA, nil
} else if checkResult == constants.CheckResultUpdate { //CheckResultUpdate
if err = r.client.Update(context.TODO(), r.HPA); err != nil {
return nil, err
}
return r.HPA, nil
} else if checkResult == constants.CheckResultExisted && scaleToZero {
// when scaleToZero is true, delete HPA if it exist
if err = r.client.Delete(context.TODO(), existingHPA, &client.DeleteOptions{}); err != nil {
return nil, err
}
return nil, nil
} else {
return existingHPA, nil
}
}