/
crd_scaling.go
192 lines (179 loc) · 6.89 KB
/
crd_scaling.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
// Copyright (C) 2020-2024 Red Hat, Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//nolint:dupl
package scaling
import (
"context"
"errors"
"time"
"github.com/test-network-function/cnf-certification-test/cnf-certification-test/lifecycle/podsets"
"github.com/test-network-function/cnf-certification-test/internal/clientsholder"
"github.com/test-network-function/cnf-certification-test/internal/log"
"github.com/test-network-function/cnf-certification-test/pkg/provider"
scalingv1 "k8s.io/api/autoscaling/v1"
scale "k8s.io/client-go/scale"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
hps "k8s.io/client-go/kubernetes/typed/autoscaling/v1"
retry "k8s.io/client-go/util/retry"
)
func TestScaleCrd(crScale *provider.CrScale, groupResourceSchema schema.GroupResource, timeout time.Duration, logger *log.Logger) bool {
if crScale == nil {
logger.Error("CR object is nill")
return false
}
clients := clientsholder.GetClientsHolder()
replicas := crScale.Spec.Replicas
name := crScale.GetName()
namespace := crScale.GetNamespace()
if replicas <= 1 {
// scale up
replicas++
if !scaleCrHelper(clients.ScalingClient, groupResourceSchema, crScale, replicas, true, timeout, logger) {
logger.Error("Cannot scale CR %q in namespace %q", name, namespace)
return false
}
// scale down
replicas--
if !scaleCrHelper(clients.ScalingClient, groupResourceSchema, crScale, replicas, false, timeout, logger) {
logger.Error("Cannot scale CR %q in namespace %q", name, namespace)
return false
}
} else {
// scale down
replicas--
if !scaleCrHelper(clients.ScalingClient, groupResourceSchema, crScale, replicas, false, timeout, logger) {
logger.Error("Cannot scale CR %q in namespace %q", name, namespace)
return false
} // scale up
replicas++
if !scaleCrHelper(clients.ScalingClient, groupResourceSchema, crScale, replicas, true, timeout, logger) {
logger.Error("Cannot scale CR %q in namespace %q", name, namespace)
return false
}
}
return true
}
func scaleCrHelper(scalesGetter scale.ScalesGetter, rc schema.GroupResource, autoscalerpram *provider.CrScale, replicas int32, up bool, timeout time.Duration, logger *log.Logger) bool {
if up {
logger.Debug("Scale UP CRS to %d replicas", replicas)
} else {
logger.Debug("Scale DOWN CRS to %d replicas", replicas)
}
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
// RetryOnConflict uses exponential backoff to avoid exhausting the apiserver
namespace := autoscalerpram.GetNamespace()
name := autoscalerpram.GetName()
scalingObject, err := scalesGetter.Scales(namespace).Get(context.TODO(), rc, name, metav1.GetOptions{})
if err != nil {
return err
}
scalingObject.Spec.Replicas = replicas
_, err = scalesGetter.Scales(namespace).Update(context.TODO(), rc, scalingObject, metav1.UpdateOptions{})
if err != nil {
logger.Error("Cannot update DynamicClient, err=%v", err)
return err
}
if !podsets.WaitForScalingToComplete(namespace, name, timeout, rc, logger) {
logger.Error("Cannot update CR %s:%s", namespace, name)
return errors.New("can not update cr")
}
return nil
})
if retryErr != nil {
logger.Error("Can notscale DynamicClient, err=%v", retryErr)
return false
}
return true
}
func TestScaleHPACrd(cr *provider.CrScale, hpa *scalingv1.HorizontalPodAutoscaler, groupResourceSchema schema.GroupResource, timeout time.Duration, logger *log.Logger) bool {
if cr == nil {
logger.Error("CR object is nill")
return false
}
clients := clientsholder.GetClientsHolder()
namespace := cr.GetNamespace()
hpscaler := clients.K8sClient.AutoscalingV1().HorizontalPodAutoscalers(namespace)
var min int32
if hpa.Spec.MinReplicas != nil {
min = *hpa.Spec.MinReplicas
} else {
min = 1
}
replicas := cr.Spec.Replicas
name := cr.GetName()
max := hpa.Spec.MaxReplicas
if replicas <= 1 {
// scale up
replicas++
logger.Debug("Scale UP HPA %s:%s to min=%d max=%d", namespace, hpa.Name, replicas, replicas)
pass := scaleHpaCRDHelper(hpscaler, hpa.Name, name, namespace, replicas, replicas, timeout, groupResourceSchema, logger)
if !pass {
return false
}
// scale down
replicas--
logger.Debug("Scale DOWN HPA %s:%s to min=%d max=%d", namespace, hpa.Name, replicas, replicas)
pass = scaleHpaCRDHelper(hpscaler, hpa.Name, name, namespace, min, max, timeout, groupResourceSchema, logger)
if !pass {
return false
}
} else {
// scale down
replicas--
logger.Debug("Scale DOWN HPA %s:%s to min=%d max=%d", namespace, hpa.Name, replicas, replicas)
pass := scaleHpaCRDHelper(hpscaler, hpa.Name, name, namespace, replicas, replicas, timeout, groupResourceSchema, logger)
if !pass {
return false
}
// scale up
replicas++
logger.Debug("Scale UP HPA %s:%s to min=%d max=%d", namespace, hpa.Name, replicas, replicas)
pass = scaleHpaCRDHelper(hpscaler, hpa.Name, name, namespace, replicas, replicas, timeout, groupResourceSchema, logger)
if !pass {
return false
}
}
// back the min and the max value of the hpa
logger.Debug("Back HPA %s:%s to min=%d max=%d", namespace, hpa.Name, min, max)
return scaleHpaCRDHelper(hpscaler, hpa.Name, name, namespace, min, max, timeout, groupResourceSchema, logger)
}
func scaleHpaCRDHelper(hpscaler hps.HorizontalPodAutoscalerInterface, hpaName, crName, namespace string, min, max int32, timeout time.Duration, groupResourceSchema schema.GroupResource, logger *log.Logger) bool {
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
hpa, err := hpscaler.Get(context.TODO(), hpaName, metav1.GetOptions{})
if err != nil {
logger.Error("Cannot update autoscaler to scale %s:%s, err=%v", namespace, crName, err)
return err
}
hpa.Spec.MinReplicas = &min
hpa.Spec.MaxReplicas = max
_, err = hpscaler.Update(context.TODO(), hpa, metav1.UpdateOptions{})
if err != nil {
logger.Error("Cannot update autoscaler to scale %s:%s, err=%v", namespace, crName, err)
return err
}
if !podsets.WaitForScalingToComplete(namespace, crName, timeout, groupResourceSchema, logger) {
logger.Error("Cannot update CR %s:%s", namespace, crName)
return errors.New("can not update cr")
}
return nil
})
if retryErr != nil {
logger.Error("Cannot scale hpa %s:%s, err=%v", namespace, hpaName, retryErr)
return false
}
return true
}