forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ubernetes_lite_volumes.go
149 lines (131 loc) · 5.11 KB
/
ubernetes_lite_volumes.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
/*
Copyright 2017 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 storage
import (
"context"
"fmt"
"sync"
"github.com/onsi/ginkgo/v2"
"github.com/sanposhiho/kubernetes/test/e2e/framework"
e2enode "github.com/sanposhiho/kubernetes/test/e2e/framework/node"
e2epod "github.com/sanposhiho/kubernetes/test/e2e/framework/pod"
e2epv "github.com/sanposhiho/kubernetes/test/e2e/framework/pv"
e2eskipper "github.com/sanposhiho/kubernetes/test/e2e/framework/skipper"
"github.com/sanposhiho/kubernetes/test/e2e/storage/utils"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
clientset "k8s.io/client-go/kubernetes"
admissionapi "k8s.io/pod-security-admission/api"
)
var _ = utils.SIGDescribe("Multi-AZ Cluster Volumes", func() {
f := framework.NewDefaultFramework("multi-az")
f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged
var zoneCount int
var err error
image := framework.ServeHostnameImage
ginkgo.BeforeEach(func(ctx context.Context) {
e2eskipper.SkipUnlessProviderIs("gce", "gke")
if zoneCount <= 0 {
zoneCount, err = getZoneCount(ctx, f.ClientSet)
framework.ExpectNoError(err)
}
ginkgo.By(fmt.Sprintf("Checking for multi-zone cluster. Zone count = %d", zoneCount))
msg := fmt.Sprintf("Zone count is %d, only run for multi-zone clusters, skipping test", zoneCount)
e2eskipper.SkipUnlessAtLeast(zoneCount, 2, msg)
// TODO: SkipUnlessDefaultScheduler() // Non-default schedulers might not spread
})
ginkgo.It("should schedule pods in the same zones as statically provisioned PVs", func(ctx context.Context) {
PodsUseStaticPVsOrFail(ctx, f, (2*zoneCount)+1, image)
})
})
// Return the number of zones in which we have nodes in this cluster.
func getZoneCount(ctx context.Context, c clientset.Interface) (int, error) {
zoneNames, err := e2enode.GetSchedulableClusterZones(ctx, c)
if err != nil {
return -1, err
}
return len(zoneNames), nil
}
type staticPVTestConfig struct {
pvSource *v1.PersistentVolumeSource
pv *v1.PersistentVolume
pvc *v1.PersistentVolumeClaim
pod *v1.Pod
}
// PodsUseStaticPVsOrFail Check that the pods using statically
// created PVs get scheduled to the same zone that the PV is in.
func PodsUseStaticPVsOrFail(ctx context.Context, f *framework.Framework, podCount int, image string) {
var err error
c := f.ClientSet
ns := f.Namespace.Name
zones, err := e2enode.GetSchedulableClusterZones(ctx, c)
framework.ExpectNoError(err)
zonelist := sets.List(zones)
ginkgo.By("Creating static PVs across zones")
configs := make([]*staticPVTestConfig, podCount)
for i := range configs {
configs[i] = &staticPVTestConfig{}
}
ginkgo.DeferCleanup(func(ctx context.Context) {
ginkgo.By("Cleaning up pods and PVs")
for _, config := range configs {
e2epod.DeletePodOrFail(ctx, c, ns, config.pod.Name)
}
var wg sync.WaitGroup
wg.Add(len(configs))
for i := range configs {
go func(config *staticPVTestConfig) {
defer ginkgo.GinkgoRecover()
defer wg.Done()
err := e2epod.WaitForPodNotFoundInNamespace(ctx, c, config.pod.Name, ns, f.Timeouts.PodDelete)
framework.ExpectNoError(err, "while waiting for pod to disappear")
errs := e2epv.PVPVCCleanup(ctx, c, ns, config.pv, config.pvc)
framework.ExpectNoError(utilerrors.NewAggregate(errs), "while cleaning up PVs and PVCs")
err = e2epv.DeletePVSource(ctx, config.pvSource)
framework.ExpectNoError(err, "while deleting PVSource")
}(configs[i])
}
wg.Wait()
})
for i, config := range configs {
zone := zonelist[i%len(zones)]
config.pvSource, err = e2epv.CreatePVSource(ctx, zone)
framework.ExpectNoError(err)
pvConfig := e2epv.PersistentVolumeConfig{
NamePrefix: "multizone-pv",
PVSource: *config.pvSource,
Prebind: nil,
}
className := ""
pvcConfig := e2epv.PersistentVolumeClaimConfig{StorageClassName: &className}
config.pv, config.pvc, err = e2epv.CreatePVPVC(ctx, c, f.Timeouts, pvConfig, pvcConfig, ns, true)
framework.ExpectNoError(err)
}
ginkgo.By("Waiting for all PVCs to be bound")
for _, config := range configs {
e2epv.WaitOnPVandPVC(ctx, c, f.Timeouts, ns, config.pv, config.pvc)
}
ginkgo.By("Creating pods for each static PV")
for _, config := range configs {
podConfig := e2epod.MakePod(ns, nil, []*v1.PersistentVolumeClaim{config.pvc}, false, "")
config.pod, err = c.CoreV1().Pods(ns).Create(ctx, podConfig, metav1.CreateOptions{})
framework.ExpectNoError(err)
}
ginkgo.By("Waiting for all pods to be running")
for _, config := range configs {
err = e2epod.WaitForPodRunningInNamespace(ctx, c, config.pod)
framework.ExpectNoError(err)
}
}