-
Notifications
You must be signed in to change notification settings - Fork 39.7k
/
volumeperf.go
311 lines (276 loc) · 11.4 KB
/
volumeperf.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
Copyright 2021 The Kubernetes Authors.
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 testsuites
import (
"context"
"fmt"
"github.com/davecgh/go-spew/spew"
"sync"
"time"
"github.com/onsi/ginkgo/v2"
"k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/watch"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/kubernetes/test/e2e/framework"
e2epv "k8s.io/kubernetes/test/e2e/framework/pv"
e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
storageframework "k8s.io/kubernetes/test/e2e/storage/framework"
admissionapi "k8s.io/pod-security-admission/api"
)
type volumePerformanceTestSuite struct {
tsInfo storageframework.TestSuiteInfo
}
var _ storageframework.TestSuite = &volumePerformanceTestSuite{}
const testTimeout = 15 * time.Minute
// Time intervals when a resource was created, it enters
// the desired state and the elapsed time between these
// two states
type interval struct {
create time.Time
enterDesiredState time.Time
elapsed time.Duration
}
// Consolidates performance stats for any operation
type performanceStats struct {
mutex *sync.Mutex
perObjectInterval map[string]*interval
operationMetrics *storageframework.Metrics
}
// waitForProvisionCh receives a signal from controller
// when all PVCs are Bound
// The signal received on this channel is the list of
// PVC objects that are created in the test
// The test blocks until waitForProvisionCh receives a signal
// or the test times out
var waitForProvisionCh chan []*v1.PersistentVolumeClaim
// InitVolumePerformanceTestSuite returns volumePerformanceTestSuite that implements TestSuite interface
func InitVolumePerformanceTestSuite() storageframework.TestSuite {
return &volumePerformanceTestSuite{
tsInfo: storageframework.TestSuiteInfo{
Name: "volume-lifecycle-performance",
TestPatterns: []storageframework.TestPattern{
storageframework.FsVolModeDynamicPV,
},
},
}
}
func (t *volumePerformanceTestSuite) GetTestSuiteInfo() storageframework.TestSuiteInfo {
return t.tsInfo
}
func (t *volumePerformanceTestSuite) SkipUnsupportedTests(driver storageframework.TestDriver, pattern storageframework.TestPattern) {
}
func (t *volumePerformanceTestSuite) DefineTests(driver storageframework.TestDriver, pattern storageframework.TestPattern) {
type local struct {
config *storageframework.PerTestConfig
testCleanup func()
cs clientset.Interface
ns *v1.Namespace
scName string
pvcs []*v1.PersistentVolumeClaim
options *storageframework.PerformanceTestOptions
stopCh chan struct{}
}
var (
dInfo *storageframework.DriverInfo
l *local
)
ginkgo.BeforeEach(func() {
// Check preconditions
dDriver := driver.(storageframework.DynamicPVTestDriver)
if dDriver == nil {
e2eskipper.Skipf("Test driver does not support dynamically created volumes")
}
dInfo = dDriver.GetDriverInfo()
if dInfo == nil {
e2eskipper.Skipf("Failed to get Driver info -- skipping")
}
if dInfo.PerformanceTestOptions == nil || dInfo.PerformanceTestOptions.ProvisioningOptions == nil {
e2eskipper.Skipf("Driver %s doesn't specify performance test options -- skipping", dInfo.Name)
}
})
frameworkOptions := framework.Options{
ClientQPS: 200,
ClientBurst: 400,
}
f := framework.NewFramework("volume-lifecycle-performance", frameworkOptions, nil)
f.NamespacePodSecurityEnforceLevel = admissionapi.LevelPrivileged
f.AddAfterEach("cleanup", func(f *framework.Framework, failed bool) {
ginkgo.By("Closing informer channel")
close(l.stopCh)
ginkgo.By("Deleting all PVCs")
for _, pvc := range l.pvcs {
err := e2epv.DeletePersistentVolumeClaim(l.cs, pvc.Name, pvc.Namespace)
framework.ExpectNoError(err)
err = e2epv.WaitForPersistentVolumeDeleted(l.cs, pvc.Spec.VolumeName, 1*time.Second, 5*time.Minute)
framework.ExpectNoError(err)
}
ginkgo.By(fmt.Sprintf("Deleting Storage Class %s", l.scName))
err := l.cs.StorageV1().StorageClasses().Delete(context.TODO(), l.scName, metav1.DeleteOptions{})
framework.ExpectNoError(err)
l.testCleanup()
})
ginkgo.It("should provision volumes at scale within performance constraints [Slow] [Serial]", func() {
l = &local{
cs: f.ClientSet,
ns: f.Namespace,
options: dInfo.PerformanceTestOptions,
}
l.config, l.testCleanup = driver.PrepareTest(f)
// Stats for volume provisioning operation
// TODO: Add stats for attach, resize and snapshot
provisioningStats := &performanceStats{
mutex: &sync.Mutex{},
perObjectInterval: make(map[string]*interval),
operationMetrics: &storageframework.Metrics{},
}
sc := driver.(storageframework.DynamicPVTestDriver).GetDynamicProvisionStorageClass(l.config, pattern.FsType)
ginkgo.By(fmt.Sprintf("Creating Storage Class %v", sc))
// TODO: Add support for WaitForFirstConsumer volume binding mode
if sc.VolumeBindingMode != nil && *sc.VolumeBindingMode == storagev1.VolumeBindingWaitForFirstConsumer {
e2eskipper.Skipf("WaitForFirstConsumer binding mode currently not supported for performance tests")
}
ginkgo.By(fmt.Sprintf("Creating Storage Class %s", sc.Name))
sc, err := l.cs.StorageV1().StorageClasses().Create(context.TODO(), sc, metav1.CreateOptions{})
framework.ExpectNoError(err)
l.scName = sc.Name
// Create a controller to watch on PVCs
// When all PVCs provisioned by this test are in the Bound state, the controller
// sends a signal to the channel
controller := newPVCWatch(f, l.options.ProvisioningOptions.Count, provisioningStats)
l.stopCh = make(chan struct{})
go controller.Run(l.stopCh)
waitForProvisionCh = make(chan []*v1.PersistentVolumeClaim)
ginkgo.By(fmt.Sprintf("Creating %d PVCs of size %s", l.options.ProvisioningOptions.Count, l.options.ProvisioningOptions.VolumeSize))
for i := 0; i < l.options.ProvisioningOptions.Count; i++ {
pvc := e2epv.MakePersistentVolumeClaim(e2epv.PersistentVolumeClaimConfig{
ClaimSize: l.options.ProvisioningOptions.VolumeSize,
StorageClassName: &sc.Name,
}, l.ns.Name)
pvc, err = l.cs.CoreV1().PersistentVolumeClaims(l.ns.Name).Create(context.TODO(), pvc, metav1.CreateOptions{})
framework.ExpectNoError(err)
// Store create time for each PVC
provisioningStats.mutex.Lock()
provisioningStats.perObjectInterval[pvc.Name] = &interval{
create: pvc.CreationTimestamp.Time,
}
provisioningStats.mutex.Unlock()
}
ginkgo.By("Waiting for all PVCs to be Bound...")
select {
case l.pvcs = <-waitForProvisionCh:
framework.Logf("All PVCs in Bound state")
case <-time.After(testTimeout):
ginkgo.Fail(fmt.Sprintf("expected all PVCs to be in Bound state within %v minutes", testTimeout))
}
ginkgo.By("Calculating performance metrics for provisioning operations")
createPerformanceStats(provisioningStats, l.options.ProvisioningOptions.Count, l.pvcs)
ginkgo.By(fmt.Sprintf("Validating performance metrics for provisioning operations against baseline %v", spew.Sdump(l.options.ProvisioningOptions.ExpectedMetrics)))
errList := validatePerformanceStats(provisioningStats.operationMetrics, l.options.ProvisioningOptions.ExpectedMetrics)
framework.ExpectNoError(errors.NewAggregate(errList), "while validating performance metrics")
})
}
// createPerformanceStats calculates individual metrics for an operation
// given the intervals collected during that operation
func createPerformanceStats(stats *performanceStats, provisionCount int, pvcs []*v1.PersistentVolumeClaim) {
var min, max, sum time.Duration
for _, pvc := range pvcs {
pvcMetric, ok := stats.perObjectInterval[pvc.Name]
framework.ExpectEqual(ok, true)
elapsedTime := pvcMetric.elapsed
sum += elapsedTime
if elapsedTime < min || min == 0 {
min = elapsedTime
}
if elapsedTime > max {
max = elapsedTime
}
}
stats.operationMetrics = &storageframework.Metrics{
AvgLatency: time.Duration(int64(sum) / int64(provisionCount)),
Throughput: float64(provisionCount) / max.Seconds(),
}
}
// validatePerformanceStats validates if test performance metrics meet the baseline target
func validatePerformanceStats(operationMetrics *storageframework.Metrics, baselineMetrics *storageframework.Metrics) []error {
var errList []error
framework.Logf("Metrics to evaluate: %+v", spew.Sdump(operationMetrics))
if operationMetrics.AvgLatency > baselineMetrics.AvgLatency {
err := fmt.Errorf("expected latency to be less than %v but calculated latency %v", baselineMetrics.AvgLatency, operationMetrics.AvgLatency)
errList = append(errList, err)
}
if operationMetrics.Throughput < baselineMetrics.Throughput {
err := fmt.Errorf("expected throughput to be greater than %f but calculated throughput %f", baselineMetrics.Throughput, operationMetrics.Throughput)
errList = append(errList, err)
}
return errList
}
// newPVCWatch creates an informer to check whether all PVCs are Bound
// When all PVCs are bound, the controller sends a signal to
// waitForProvisionCh to unblock the test
func newPVCWatch(f *framework.Framework, provisionCount int, pvcMetrics *performanceStats) cache.Controller {
defer ginkgo.GinkgoRecover()
count := 0
countLock := &sync.Mutex{}
ns := f.Namespace.Name
var pvcs []*v1.PersistentVolumeClaim
checkPVCBound := func(oldPVC *v1.PersistentVolumeClaim, newPVC *v1.PersistentVolumeClaim) {
now := time.Now()
pvcMetrics.mutex.Lock()
defer pvcMetrics.mutex.Unlock()
countLock.Lock()
defer countLock.Unlock()
// Check if PVC entered the bound state
if oldPVC.Status.Phase != v1.ClaimBound && newPVC.Status.Phase == v1.ClaimBound {
newPVCInterval, ok := pvcMetrics.perObjectInterval[newPVC.Name]
framework.ExpectEqual(ok, true, "PVC %s should exist in interval map already", newPVC.Name)
count++
newPVCInterval.enterDesiredState = now
newPVCInterval.elapsed = now.Sub(newPVCInterval.create)
pvcs = append(pvcs, newPVC)
}
if count == provisionCount {
// Number of Bound PVCs equals the number of PVCs
// provisioned by this test
// Send those PVCs to the channel to unblock test
waitForProvisionCh <- pvcs
}
}
_, controller := cache.NewInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
obj, err := f.ClientSet.CoreV1().PersistentVolumeClaims(ns).List(context.TODO(), metav1.ListOptions{})
return runtime.Object(obj), err
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return f.ClientSet.CoreV1().PersistentVolumeClaims(ns).Watch(context.TODO(), metav1.ListOptions{})
},
},
&v1.PersistentVolumeClaim{},
0,
cache.ResourceEventHandlerFuncs{
UpdateFunc: func(oldObj, newObj interface{}) {
oldPVC, ok := oldObj.(*v1.PersistentVolumeClaim)
framework.ExpectEqual(ok, true)
newPVC, ok := newObj.(*v1.PersistentVolumeClaim)
framework.ExpectEqual(ok, true)
checkPVCBound(oldPVC, newPVC)
},
},
)
return controller
}