2017-01-26 17:17:28 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 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.
|
|
|
|
*/
|
|
|
|
|
2017-02-17 04:33:41 +00:00
|
|
|
package framework
|
2017-01-26 17:17:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-02-23 00:49:36 +00:00
|
|
|
"strings"
|
2017-01-26 17:17:28 +00:00
|
|
|
"time"
|
|
|
|
|
2017-02-23 00:49:36 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2018-02-27 11:26:26 +00:00
|
|
|
"github.com/golang/glog"
|
2017-01-26 17:17:28 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
2017-02-23 00:49:36 +00:00
|
|
|
"google.golang.org/api/googleapi"
|
2017-06-22 18:24:23 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-01-26 17:17:28 +00:00
|
|
|
apierrs "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-03-20 22:55:24 +00:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2017-01-26 17:17:28 +00:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2017-02-23 00:49:36 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/uuid"
|
2017-06-23 20:56:37 +00:00
|
|
|
clientset "k8s.io/client-go/kubernetes"
|
2017-02-23 00:49:36 +00:00
|
|
|
awscloud "k8s.io/kubernetes/pkg/cloudprovider/providers/aws"
|
|
|
|
gcecloud "k8s.io/kubernetes/pkg/cloudprovider/providers/gce"
|
2018-02-06 08:38:41 +00:00
|
|
|
"k8s.io/kubernetes/pkg/volume/util"
|
2017-12-01 19:01:47 +00:00
|
|
|
imageutils "k8s.io/kubernetes/test/utils/image"
|
2017-01-26 17:17:28 +00:00
|
|
|
)
|
|
|
|
|
2017-02-23 00:49:36 +00:00
|
|
|
const (
|
2017-03-20 22:55:24 +00:00
|
|
|
PDRetryTimeout = 5 * time.Minute
|
|
|
|
PDRetryPollTime = 5 * time.Second
|
|
|
|
VolumeSelectorKey = "e2e-pv-pool"
|
2017-02-23 00:49:36 +00:00
|
|
|
)
|
|
|
|
|
2017-01-26 17:17:28 +00:00
|
|
|
// Map of all PVs used in the multi pv-pvc tests. The key is the PV's name, which is
|
|
|
|
// guaranteed to be unique. The value is {} (empty struct) since we're only interested
|
|
|
|
// in the PV's name and if it is present. We must always Get the pv object before
|
|
|
|
// referencing any of its values, eg its ClaimRef.
|
|
|
|
type pvval struct{}
|
2017-02-17 04:33:41 +00:00
|
|
|
type PVMap map[string]pvval
|
2017-01-26 17:17:28 +00:00
|
|
|
|
|
|
|
// Map of all PVCs used in the multi pv-pvc tests. The key is "namespace/pvc.Name". The
|
|
|
|
// value is {} (empty struct) since we're only interested in the PVC's name and if it is
|
|
|
|
// present. We must always Get the pvc object before referencing any of its values, eg.
|
|
|
|
// its VolumeName.
|
|
|
|
// Note: It's unsafe to add keys to a map in a loop. Their insertion in the map is
|
|
|
|
// unpredictable and can result in the same key being iterated over again.
|
|
|
|
type pvcval struct{}
|
2017-02-17 04:33:41 +00:00
|
|
|
type PVCMap map[types.NamespacedName]pvcval
|
2017-01-26 17:17:28 +00:00
|
|
|
|
2017-03-20 22:55:24 +00:00
|
|
|
// PersistentVolumeConfig is consumed by MakePersistentVolume() to generate a PV object
|
|
|
|
// for varying storage options (NFS, ceph, glusterFS, etc.).
|
|
|
|
// (+optional) prebind holds a pre-bound PVC
|
|
|
|
// Example pvSource:
|
|
|
|
// pvSource: api.PersistentVolumeSource{
|
|
|
|
// NFS: &api.NFSVolumeSource{
|
|
|
|
// ...
|
|
|
|
// },
|
|
|
|
// }
|
2017-02-17 04:33:41 +00:00
|
|
|
type PersistentVolumeConfig struct {
|
2017-04-25 03:41:40 +00:00
|
|
|
PVSource v1.PersistentVolumeSource
|
|
|
|
Prebind *v1.PersistentVolumeClaim
|
|
|
|
ReclaimPolicy v1.PersistentVolumeReclaimPolicy
|
|
|
|
NamePrefix string
|
|
|
|
Labels labels.Set
|
|
|
|
StorageClassName string
|
2018-01-30 23:41:57 +00:00
|
|
|
NodeAffinity *v1.VolumeNodeAffinity
|
2018-02-14 16:26:00 +00:00
|
|
|
VolumeMode *v1.PersistentVolumeMode
|
2017-03-20 22:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PersistentVolumeClaimConfig is consumed by MakePersistentVolumeClaim() to generate a PVC object.
|
|
|
|
// AccessModes defaults to all modes (RWO, RWX, ROX) if left empty
|
|
|
|
// (+optional) Annotations defines the PVC's annotations
|
|
|
|
|
|
|
|
type PersistentVolumeClaimConfig struct {
|
2017-04-25 03:41:40 +00:00
|
|
|
AccessModes []v1.PersistentVolumeAccessMode
|
|
|
|
Annotations map[string]string
|
|
|
|
Selector *metav1.LabelSelector
|
|
|
|
StorageClassName *string
|
2018-02-14 16:26:00 +00:00
|
|
|
VolumeMode *v1.PersistentVolumeMode
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up a pv and pvc in a single pv/pvc test case.
|
2017-04-01 04:42:52 +00:00
|
|
|
// Note: delete errors are appended to []error so that we can attempt to delete both the pvc and pv.
|
|
|
|
func PVPVCCleanup(c clientset.Interface, ns string, pv *v1.PersistentVolume, pvc *v1.PersistentVolumeClaim) []error {
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
if pvc != nil {
|
|
|
|
err := DeletePersistentVolumeClaim(c, pvc.Name, ns)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("failed to delete PVC %q: %v", pvc.Name, err))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Logf("pvc is nil")
|
|
|
|
}
|
|
|
|
if pv != nil {
|
|
|
|
err := DeletePersistentVolume(c, pv.Name)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("failed to delete PV %q: %v", pv.Name, err))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Logf("pv is nil")
|
|
|
|
}
|
|
|
|
return errs
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 04:42:52 +00:00
|
|
|
// Clean up pvs and pvcs in multi-pv-pvc test cases. Entries found in the pv and claim maps are
|
|
|
|
// deleted as long as the Delete api call succeeds.
|
|
|
|
// Note: delete errors are appended to []error so that as many pvcs and pvs as possible are deleted.
|
|
|
|
func PVPVCMapCleanup(c clientset.Interface, ns string, pvols PVMap, claims PVCMap) []error {
|
|
|
|
var errs []error
|
|
|
|
|
2017-01-26 17:17:28 +00:00
|
|
|
for pvcKey := range claims {
|
2017-04-01 04:42:52 +00:00
|
|
|
err := DeletePersistentVolumeClaim(c, pvcKey.Name, ns)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("failed to delete PVC %q: %v", pvcKey.Name, err))
|
|
|
|
} else {
|
|
|
|
delete(claims, pvcKey)
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for pvKey := range pvols {
|
2017-04-01 04:42:52 +00:00
|
|
|
err := DeletePersistentVolume(c, pvKey)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("failed to delete PV %q: %v", pvKey, err))
|
|
|
|
} else {
|
|
|
|
delete(pvols, pvKey)
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
2017-04-01 04:42:52 +00:00
|
|
|
return errs
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the PV.
|
2017-04-01 04:42:52 +00:00
|
|
|
func DeletePersistentVolume(c clientset.Interface, pvName string) error {
|
2017-01-26 17:17:28 +00:00
|
|
|
if c != nil && len(pvName) > 0 {
|
2017-04-01 04:42:52 +00:00
|
|
|
Logf("Deleting PersistentVolume %q", pvName)
|
2017-02-23 00:53:23 +00:00
|
|
|
err := c.CoreV1().PersistentVolumes().Delete(pvName, nil)
|
2017-01-26 17:17:28 +00:00
|
|
|
if err != nil && !apierrs.IsNotFound(err) {
|
2017-04-01 04:42:52 +00:00
|
|
|
return fmt.Errorf("PV Delete API error: %v", err)
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-01 04:42:52 +00:00
|
|
|
return nil
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the Claim
|
2017-04-01 04:42:52 +00:00
|
|
|
func DeletePersistentVolumeClaim(c clientset.Interface, pvcName string, ns string) error {
|
2017-01-26 17:17:28 +00:00
|
|
|
if c != nil && len(pvcName) > 0 {
|
2017-04-01 04:42:52 +00:00
|
|
|
Logf("Deleting PersistentVolumeClaim %q", pvcName)
|
2017-02-23 00:53:23 +00:00
|
|
|
err := c.CoreV1().PersistentVolumeClaims(ns).Delete(pvcName, nil)
|
2017-01-26 17:17:28 +00:00
|
|
|
if err != nil && !apierrs.IsNotFound(err) {
|
2017-04-01 04:42:52 +00:00
|
|
|
return fmt.Errorf("PVC Delete API error: %v", err)
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-01 04:42:52 +00:00
|
|
|
return nil
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 21:43:20 +00:00
|
|
|
// Delete the PVC and wait for the PV to enter its expected phase. Validate that the PV
|
|
|
|
// has been reclaimed (assumption here about reclaimPolicy). Caller tells this func which
|
2017-01-26 17:17:28 +00:00
|
|
|
// phase value to expect for the pv bound to the to-be-deleted claim.
|
2017-04-01 04:42:52 +00:00
|
|
|
func DeletePVCandValidatePV(c clientset.Interface, ns string, pvc *v1.PersistentVolumeClaim, pv *v1.PersistentVolume, expectPVPhase v1.PersistentVolumePhase) error {
|
2017-01-26 17:17:28 +00:00
|
|
|
pvname := pvc.Spec.VolumeName
|
2017-02-17 04:33:41 +00:00
|
|
|
Logf("Deleting PVC %v to trigger reclamation of PV %v", pvc.Name, pvname)
|
2017-04-01 04:42:52 +00:00
|
|
|
err := DeletePersistentVolumeClaim(c, pvc.Name, ns)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
|
2017-02-14 21:43:20 +00:00
|
|
|
// Wait for the PV's phase to return to be `expectPVPhase`
|
2017-02-17 04:33:41 +00:00
|
|
|
Logf("Waiting for reclaim process to complete.")
|
2018-01-31 05:33:16 +00:00
|
|
|
err = WaitForPersistentVolumePhase(expectPVPhase, c, pv.Name, Poll, PVReclaimingTimeout)
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("pv %q phase did not become %v: %v", pv.Name, expectPVPhase, err)
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
|
|
|
|
// examine the pv's ClaimRef and UID and compare to expected values
|
2017-02-23 00:53:23 +00:00
|
|
|
pv, err = c.CoreV1().PersistentVolumes().Get(pv.Name, metav1.GetOptions{})
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("PV Get API error: %v", err)
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
cr := pv.Spec.ClaimRef
|
2017-02-14 21:43:20 +00:00
|
|
|
if expectPVPhase == v1.VolumeAvailable {
|
2017-04-01 04:42:52 +00:00
|
|
|
if cr != nil && len(cr.UID) > 0 {
|
|
|
|
return fmt.Errorf("PV is 'Available' but ClaimRef.UID is not empty")
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
2017-02-14 21:43:20 +00:00
|
|
|
} else if expectPVPhase == v1.VolumeBound {
|
2017-04-01 04:42:52 +00:00
|
|
|
if cr == nil {
|
|
|
|
return fmt.Errorf("PV is 'Bound' but ClaimRef is nil")
|
|
|
|
}
|
|
|
|
if len(cr.UID) == 0 {
|
|
|
|
return fmt.Errorf("PV is 'Bound' but ClaimRef.UID is empty")
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
2017-02-17 04:33:41 +00:00
|
|
|
Logf("PV %v now in %q phase", pv.Name, expectPVPhase)
|
2017-04-01 04:42:52 +00:00
|
|
|
return nil
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 04:42:52 +00:00
|
|
|
// Wraps deletePVCandValidatePV() by calling the function in a loop over the PV map. Only bound PVs
|
|
|
|
// are deleted. Validates that the claim was deleted and the PV is in the expected Phase (Released,
|
|
|
|
// Available, Bound).
|
|
|
|
// Note: if there are more claims than pvs then some of the remaining claims may bind to just made
|
|
|
|
// available pvs.
|
|
|
|
func DeletePVCandValidatePVGroup(c clientset.Interface, ns string, pvols PVMap, claims PVCMap, expectPVPhase v1.PersistentVolumePhase) error {
|
2017-01-26 17:17:28 +00:00
|
|
|
var boundPVs, deletedPVCs int
|
|
|
|
|
|
|
|
for pvName := range pvols {
|
2017-02-23 00:53:23 +00:00
|
|
|
pv, err := c.CoreV1().PersistentVolumes().Get(pvName, metav1.GetOptions{})
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("PV Get API error: %v", err)
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
cr := pv.Spec.ClaimRef
|
|
|
|
// if pv is bound then delete the pvc it is bound to
|
|
|
|
if cr != nil && len(cr.Name) > 0 {
|
|
|
|
boundPVs++
|
|
|
|
// Assert bound PVC is tracked in this test. Failing this might
|
|
|
|
// indicate external PVCs interfering with the test.
|
|
|
|
pvcKey := makePvcKey(ns, cr.Name)
|
2017-04-01 04:42:52 +00:00
|
|
|
if _, found := claims[pvcKey]; !found {
|
|
|
|
return fmt.Errorf("internal: claims map is missing pvc %q", pvcKey)
|
|
|
|
}
|
|
|
|
// get the pvc for the delete call below
|
2017-02-23 00:53:23 +00:00
|
|
|
pvc, err := c.CoreV1().PersistentVolumeClaims(ns).Get(cr.Name, metav1.GetOptions{})
|
2017-04-01 04:42:52 +00:00
|
|
|
if err == nil {
|
|
|
|
if err = DeletePVCandValidatePV(c, ns, pvc, pv, expectPVPhase); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if !apierrs.IsNotFound(err) {
|
|
|
|
return fmt.Errorf("PVC Get API error: %v", err)
|
|
|
|
}
|
|
|
|
// delete pvckey from map even if apierrs.IsNotFound above is true and thus the
|
|
|
|
// claim was not actually deleted here
|
2017-01-26 17:17:28 +00:00
|
|
|
delete(claims, pvcKey)
|
|
|
|
deletedPVCs++
|
|
|
|
}
|
|
|
|
}
|
2017-04-01 04:42:52 +00:00
|
|
|
if boundPVs != deletedPVCs {
|
|
|
|
return fmt.Errorf("expect number of bound PVs (%v) to equal number of deleted PVCs (%v)", boundPVs, deletedPVCs)
|
|
|
|
}
|
|
|
|
return nil
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create the PV resource. Fails test on error.
|
2017-04-01 04:42:52 +00:00
|
|
|
func createPV(c clientset.Interface, pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
2017-02-23 00:53:23 +00:00
|
|
|
pv, err := c.CoreV1().PersistentVolumes().Create(pv)
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("PV Create API error: %v", err)
|
|
|
|
}
|
|
|
|
return pv, nil
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 09:10:46 +00:00
|
|
|
// create the PV resource. Fails test on error.
|
|
|
|
func CreatePV(c clientset.Interface, pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
|
|
|
|
return createPV(c, pv)
|
|
|
|
}
|
|
|
|
|
2017-01-26 17:17:28 +00:00
|
|
|
// create the PVC resource. Fails test on error.
|
2017-04-01 04:42:52 +00:00
|
|
|
func CreatePVC(c clientset.Interface, ns string, pvc *v1.PersistentVolumeClaim) (*v1.PersistentVolumeClaim, error) {
|
2017-02-23 00:53:23 +00:00
|
|
|
pvc, err := c.CoreV1().PersistentVolumeClaims(ns).Create(pvc)
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("PVC Create API error: %v", err)
|
|
|
|
}
|
|
|
|
return pvc, nil
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a PVC followed by the PV based on the passed in nfs-server ip and
|
|
|
|
// namespace. If the "preBind" bool is true then pre-bind the PV to the PVC
|
|
|
|
// via the PV's ClaimRef. Return the pv and pvc to reflect the created objects.
|
|
|
|
// Note: in the pre-bind case the real PVC name, which is generated, is not
|
|
|
|
// known until after the PVC is instantiated. This is why the pvc is created
|
|
|
|
// before the pv.
|
2017-04-01 04:42:52 +00:00
|
|
|
func CreatePVCPV(c clientset.Interface, pvConfig PersistentVolumeConfig, pvcConfig PersistentVolumeClaimConfig, ns string, preBind bool) (*v1.PersistentVolume, *v1.PersistentVolumeClaim, error) {
|
|
|
|
// make the pvc spec
|
2017-03-20 22:55:24 +00:00
|
|
|
pvc := MakePersistentVolumeClaim(pvcConfig, ns)
|
2017-04-01 04:42:52 +00:00
|
|
|
preBindMsg := ""
|
2017-01-26 17:17:28 +00:00
|
|
|
if preBind {
|
|
|
|
preBindMsg = " pre-bound"
|
2017-02-17 04:33:41 +00:00
|
|
|
pvConfig.Prebind = pvc
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
// make the pv spec
|
2017-03-20 22:55:24 +00:00
|
|
|
pv := MakePersistentVolume(pvConfig)
|
2017-01-26 17:17:28 +00:00
|
|
|
|
|
|
|
By(fmt.Sprintf("Creating a PVC followed by a%s PV", preBindMsg))
|
2017-04-01 04:42:52 +00:00
|
|
|
pvc, err := CreatePVC(c, ns, pvc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
|
|
|
|
// instantiate the pv, handle pre-binding by ClaimRef if needed
|
|
|
|
if preBind {
|
|
|
|
pv.Spec.ClaimRef.Name = pvc.Name
|
|
|
|
}
|
2017-04-01 04:42:52 +00:00
|
|
|
pv, err = createPV(c, pv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, pvc, err
|
|
|
|
}
|
|
|
|
return pv, pvc, nil
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a PV followed by the PVC based on the passed in nfs-server ip and
|
|
|
|
// namespace. If the "preBind" bool is true then pre-bind the PVC to the PV
|
|
|
|
// via the PVC's VolumeName. Return the pv and pvc to reflect the created
|
|
|
|
// objects.
|
|
|
|
// Note: in the pre-bind case the real PV name, which is generated, is not
|
|
|
|
// known until after the PV is instantiated. This is why the pv is created
|
|
|
|
// before the pvc.
|
2017-04-01 04:42:52 +00:00
|
|
|
func CreatePVPVC(c clientset.Interface, pvConfig PersistentVolumeConfig, pvcConfig PersistentVolumeClaimConfig, ns string, preBind bool) (*v1.PersistentVolume, *v1.PersistentVolumeClaim, error) {
|
2017-01-26 17:17:28 +00:00
|
|
|
preBindMsg := ""
|
|
|
|
if preBind {
|
|
|
|
preBindMsg = " pre-bound"
|
|
|
|
}
|
2017-02-17 04:33:41 +00:00
|
|
|
Logf("Creating a PV followed by a%s PVC", preBindMsg)
|
2017-01-26 17:17:28 +00:00
|
|
|
|
|
|
|
// make the pv and pvc definitions
|
2017-03-20 22:55:24 +00:00
|
|
|
pv := MakePersistentVolume(pvConfig)
|
|
|
|
pvc := MakePersistentVolumeClaim(pvcConfig, ns)
|
2017-01-26 17:17:28 +00:00
|
|
|
|
|
|
|
// instantiate the pv
|
2017-04-01 04:42:52 +00:00
|
|
|
pv, err := createPV(c, pv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
// instantiate the pvc, handle pre-binding by VolumeName if needed
|
|
|
|
if preBind {
|
|
|
|
pvc.Spec.VolumeName = pv.Name
|
|
|
|
}
|
2017-04-01 04:42:52 +00:00
|
|
|
pvc, err = CreatePVC(c, ns, pvc)
|
|
|
|
if err != nil {
|
|
|
|
return pv, nil, err
|
|
|
|
}
|
|
|
|
return pv, pvc, nil
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the desired number of PVs and PVCs and return them in separate maps. If the
|
|
|
|
// number of PVs != the number of PVCs then the min of those two counts is the number of
|
2017-04-01 04:42:52 +00:00
|
|
|
// PVs expected to bind. If a Create error occurs, the returned maps may contain pv and pvc
|
|
|
|
// entries for the resources that were successfully created. In other words, when the caller
|
|
|
|
// sees an error returned, it needs to decide what to do about entries in the maps.
|
|
|
|
// Note: when the test suite deletes the namespace orphaned pvcs and pods are deleted. However,
|
|
|
|
// orphaned pvs are not deleted and will remain after the suite completes.
|
|
|
|
func CreatePVsPVCs(numpvs, numpvcs int, c clientset.Interface, ns string, pvConfig PersistentVolumeConfig, pvcConfig PersistentVolumeClaimConfig) (PVMap, PVCMap, error) {
|
2017-02-17 04:33:41 +00:00
|
|
|
pvMap := make(PVMap, numpvs)
|
|
|
|
pvcMap := make(PVCMap, numpvcs)
|
2017-04-01 04:42:52 +00:00
|
|
|
extraPVCs := 0
|
|
|
|
extraPVs := numpvs - numpvcs
|
2017-01-26 17:17:28 +00:00
|
|
|
if extraPVs < 0 {
|
|
|
|
extraPVCs = -extraPVs
|
|
|
|
extraPVs = 0
|
|
|
|
}
|
|
|
|
pvsToCreate := numpvs - extraPVs // want the min(numpvs, numpvcs)
|
|
|
|
|
|
|
|
// create pvs and pvcs
|
2017-04-01 04:42:52 +00:00
|
|
|
for i := 0; i < pvsToCreate; i++ {
|
|
|
|
pv, pvc, err := CreatePVPVC(c, pvConfig, pvcConfig, ns, false)
|
|
|
|
if err != nil {
|
|
|
|
return pvMap, pvcMap, err
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
pvMap[pv.Name] = pvval{}
|
|
|
|
pvcMap[makePvcKey(ns, pvc.Name)] = pvcval{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create extra pvs or pvcs as needed
|
2017-04-01 04:42:52 +00:00
|
|
|
for i := 0; i < extraPVs; i++ {
|
|
|
|
pv := MakePersistentVolume(pvConfig)
|
|
|
|
pv, err := createPV(c, pv)
|
|
|
|
if err != nil {
|
|
|
|
return pvMap, pvcMap, err
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
pvMap[pv.Name] = pvval{}
|
|
|
|
}
|
2017-04-01 04:42:52 +00:00
|
|
|
for i := 0; i < extraPVCs; i++ {
|
|
|
|
pvc := MakePersistentVolumeClaim(pvcConfig, ns)
|
|
|
|
pvc, err := CreatePVC(c, ns, pvc)
|
|
|
|
if err != nil {
|
|
|
|
return pvMap, pvcMap, err
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
pvcMap[makePvcKey(ns, pvc.Name)] = pvcval{}
|
|
|
|
}
|
2017-04-01 04:42:52 +00:00
|
|
|
return pvMap, pvcMap, nil
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the pv and pvc to bind to each other.
|
2017-04-01 04:42:52 +00:00
|
|
|
func WaitOnPVandPVC(c clientset.Interface, ns string, pv *v1.PersistentVolume, pvc *v1.PersistentVolumeClaim) error {
|
2017-01-26 17:17:28 +00:00
|
|
|
// Wait for newly created PVC to bind to the PV
|
2017-02-17 04:33:41 +00:00
|
|
|
Logf("Waiting for PV %v to bind to PVC %v", pv.Name, pvc.Name)
|
2018-01-31 05:33:16 +00:00
|
|
|
err := WaitForPersistentVolumeClaimPhase(v1.ClaimBound, c, ns, pvc.Name, Poll, ClaimBindingTimeout)
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("PVC %q did not become Bound: %v", pvc.Name, err)
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
|
|
|
|
// Wait for PersistentVolume.Status.Phase to be Bound, which it should be
|
|
|
|
// since the PVC is already bound.
|
2018-01-31 05:33:16 +00:00
|
|
|
err = WaitForPersistentVolumePhase(v1.VolumeBound, c, pv.Name, Poll, PVBindingTimeout)
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("PV %q did not become Bound: %v", pv.Name, err)
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
|
|
|
|
// Re-get the pv and pvc objects
|
2017-02-23 00:53:23 +00:00
|
|
|
pv, err = c.CoreV1().PersistentVolumes().Get(pv.Name, metav1.GetOptions{})
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("PV Get API error: %v", err)
|
|
|
|
}
|
2017-02-23 00:53:23 +00:00
|
|
|
pvc, err = c.CoreV1().PersistentVolumeClaims(ns).Get(pvc.Name, metav1.GetOptions{})
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("PVC Get API error: %v", err)
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
|
|
|
|
// The pv and pvc are both bound, but to each other?
|
|
|
|
// Check that the PersistentVolume.ClaimRef matches the PVC
|
2017-04-01 04:42:52 +00:00
|
|
|
if pv.Spec.ClaimRef == nil {
|
|
|
|
return fmt.Errorf("PV %q ClaimRef is nil", pv.Name)
|
|
|
|
}
|
|
|
|
if pv.Spec.ClaimRef.Name != pvc.Name {
|
|
|
|
return fmt.Errorf("PV %q ClaimRef's name (%q) should be %q", pv.Name, pv.Spec.ClaimRef.Name, pvc.Name)
|
|
|
|
}
|
|
|
|
if pvc.Spec.VolumeName != pv.Name {
|
|
|
|
return fmt.Errorf("PVC %q VolumeName (%q) should be %q", pvc.Name, pvc.Spec.VolumeName, pv.Name)
|
|
|
|
}
|
|
|
|
if pv.Spec.ClaimRef.UID != pvc.UID {
|
|
|
|
return fmt.Errorf("PV %q ClaimRef's UID (%q) should be %q", pv.Name, pv.Spec.ClaimRef.UID, pvc.UID)
|
|
|
|
}
|
|
|
|
return nil
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search for bound PVs and PVCs by examining pvols for non-nil claimRefs.
|
|
|
|
// NOTE: Each iteration waits for a maximum of 3 minutes per PV and, if the PV is bound,
|
|
|
|
// up to 3 minutes for the PVC. When the number of PVs != number of PVCs, this can lead
|
|
|
|
// to situations where the maximum wait times are reached several times in succession,
|
|
|
|
// extending test time. Thus, it is recommended to keep the delta between PVs and PVCs
|
|
|
|
// small.
|
2017-04-01 04:42:52 +00:00
|
|
|
func WaitAndVerifyBinds(c clientset.Interface, ns string, pvols PVMap, claims PVCMap, testExpected bool) error {
|
2017-01-26 17:17:28 +00:00
|
|
|
var actualBinds int
|
|
|
|
expectedBinds := len(pvols)
|
|
|
|
if expectedBinds > len(claims) { // want the min of # pvs or #pvcs
|
|
|
|
expectedBinds = len(claims)
|
|
|
|
}
|
|
|
|
|
|
|
|
for pvName := range pvols {
|
2018-01-31 05:33:16 +00:00
|
|
|
err := WaitForPersistentVolumePhase(v1.VolumeBound, c, pvName, Poll, PVBindingTimeout)
|
2017-01-26 17:17:28 +00:00
|
|
|
if err != nil && len(pvols) > len(claims) {
|
2017-02-17 04:33:41 +00:00
|
|
|
Logf("WARN: pv %v is not bound after max wait", pvName)
|
|
|
|
Logf(" This may be ok since there are more pvs than pvcs")
|
2017-01-26 17:17:28 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("PV %q did not become Bound: %v", pvName, err)
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
|
2017-02-23 00:53:23 +00:00
|
|
|
pv, err := c.CoreV1().PersistentVolumes().Get(pvName, metav1.GetOptions{})
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("PV Get API error: %v", err)
|
|
|
|
}
|
|
|
|
cr := pv.Spec.ClaimRef
|
|
|
|
if cr != nil && len(cr.Name) > 0 {
|
2017-01-26 17:17:28 +00:00
|
|
|
// Assert bound pvc is a test resource. Failing assertion could
|
|
|
|
// indicate non-test PVC interference or a bug in the test
|
|
|
|
pvcKey := makePvcKey(ns, cr.Name)
|
2017-04-01 04:42:52 +00:00
|
|
|
if _, found := claims[pvcKey]; !found {
|
|
|
|
return fmt.Errorf("internal: claims map is missing pvc %q", pvcKey)
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
|
2018-01-31 05:33:16 +00:00
|
|
|
err := WaitForPersistentVolumeClaimPhase(v1.ClaimBound, c, ns, cr.Name, Poll, ClaimBindingTimeout)
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("PVC %q did not become Bound: %v", cr.Name, err)
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
actualBinds++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 04:42:52 +00:00
|
|
|
if testExpected && actualBinds != expectedBinds {
|
|
|
|
return fmt.Errorf("expect number of bound PVs (%v) to equal number of claims (%v)", actualBinds, expectedBinds)
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
2017-04-01 04:42:52 +00:00
|
|
|
return nil
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test the pod's exit code to be zero.
|
2017-04-01 04:42:52 +00:00
|
|
|
func testPodSuccessOrFail(c clientset.Interface, ns string, pod *v1.Pod) error {
|
2017-01-26 17:17:28 +00:00
|
|
|
By("Pod should terminate with exitcode 0 (success)")
|
2017-04-01 04:42:52 +00:00
|
|
|
if err := WaitForPodSuccessInNamespace(c, pod.Name, ns); err != nil {
|
|
|
|
return fmt.Errorf("pod %q failed to reach Success: %v", pod.Name, err)
|
|
|
|
}
|
2017-02-17 04:33:41 +00:00
|
|
|
Logf("Pod %v succeeded ", pod.Name)
|
2017-04-01 04:42:52 +00:00
|
|
|
return nil
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deletes the passed-in pod and waits for the pod to be terminated. Resilient to the pod
|
|
|
|
// not existing.
|
2017-04-01 04:42:52 +00:00
|
|
|
func DeletePodWithWait(f *Framework, c clientset.Interface, pod *v1.Pod) error {
|
2017-10-06 17:07:29 +00:00
|
|
|
const maxWait = 5 * time.Minute
|
2017-01-26 17:17:28 +00:00
|
|
|
if pod == nil {
|
2017-04-01 04:42:52 +00:00
|
|
|
return nil
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
2017-10-06 17:07:29 +00:00
|
|
|
Logf("Deleting pod %q in namespace %q", pod.Name, pod.Namespace)
|
2017-02-23 00:53:23 +00:00
|
|
|
err := c.CoreV1().Pods(pod.Namespace).Delete(pod.Name, nil)
|
2017-01-26 17:17:28 +00:00
|
|
|
if err != nil {
|
|
|
|
if apierrs.IsNotFound(err) {
|
2017-10-06 17:07:29 +00:00
|
|
|
return nil // assume pod was already deleted
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
2017-10-06 17:07:29 +00:00
|
|
|
return fmt.Errorf("pod Delete API error: %v", err)
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
2017-10-06 17:07:29 +00:00
|
|
|
Logf("Wait up to %v for pod %q to be fully deleted", maxWait, pod.Name)
|
|
|
|
err = f.WaitForPodNotFound(pod.Name, maxWait)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("pod %q was not deleted: %v", pod.Name, err)
|
2017-04-01 04:42:52 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the test pod, wait for (hopefully) success, and then delete the pod.
|
|
|
|
// Note: need named return value so that the err assignment in the defer sets the returned error.
|
|
|
|
// Has been shown to be necessary using Go 1.7.
|
|
|
|
func CreateWaitAndDeletePod(f *Framework, c clientset.Interface, ns string, pvc *v1.PersistentVolumeClaim) (err error) {
|
|
|
|
Logf("Creating nfs test pod")
|
|
|
|
pod := MakeWritePod(ns, pvc)
|
|
|
|
runPod, err := c.CoreV1().Pods(ns).Create(pod)
|
2017-02-24 22:27:56 +00:00
|
|
|
if err != nil {
|
2017-04-01 04:42:52 +00:00
|
|
|
return fmt.Errorf("pod Create API error: %v", err)
|
2017-02-14 11:50:42 +00:00
|
|
|
}
|
2017-04-01 04:42:52 +00:00
|
|
|
defer func() {
|
|
|
|
delErr := DeletePodWithWait(f, c, runPod)
|
|
|
|
if err == nil { // don't override previous err value
|
|
|
|
err = delErr // assign to returned err, can be nil
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = testPodSuccessOrFail(c, ns, runPod)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("pod %q did not exit with Success: %v", runPod.Name, err)
|
|
|
|
}
|
|
|
|
return // note: named return value
|
2017-02-24 22:27:56 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 17:17:28 +00:00
|
|
|
// Sanity check for GCE testing. Verify the persistent disk attached to the node.
|
2017-04-01 04:42:52 +00:00
|
|
|
func VerifyGCEDiskAttached(diskName string, nodeName types.NodeName) (bool, error) {
|
2017-02-17 04:33:41 +00:00
|
|
|
gceCloud, err := GetGCECloud()
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("GetGCECloud error: %v", err)
|
|
|
|
}
|
2017-01-26 17:17:28 +00:00
|
|
|
isAttached, err := gceCloud.DiskIsAttached(diskName, nodeName)
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("cannot verify if GCE disk is attached: %v", err)
|
|
|
|
}
|
|
|
|
return isAttached, nil
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return a pvckey struct.
|
|
|
|
func makePvcKey(ns, name string) types.NamespacedName {
|
|
|
|
return types.NamespacedName{Namespace: ns, Name: name}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a PV definition based on the nfs server IP. If the PVC is not nil
|
|
|
|
// then the PV is defined with a ClaimRef which includes the PVC's namespace.
|
2017-02-14 21:43:20 +00:00
|
|
|
// If the PVC is nil then the PV is not defined with a ClaimRef. If no reclaimPolicy
|
2017-04-01 04:42:52 +00:00
|
|
|
// is assigned, assumes "Retain". Specs are expected to match the test's PVC.
|
|
|
|
// Note: the passed-in claim does not have a name until it is created and thus the PV's
|
|
|
|
// ClaimRef cannot be completely filled-in in this func. Therefore, the ClaimRef's name
|
2017-04-25 03:41:40 +00:00
|
|
|
// is added later in CreatePVCPV.
|
2017-03-20 22:55:24 +00:00
|
|
|
func MakePersistentVolume(pvConfig PersistentVolumeConfig) *v1.PersistentVolume {
|
2017-01-26 17:17:28 +00:00
|
|
|
var claimRef *v1.ObjectReference
|
2017-02-14 21:43:20 +00:00
|
|
|
// If the reclaimPolicy is not provided, assume Retain
|
2017-02-17 04:33:41 +00:00
|
|
|
if pvConfig.ReclaimPolicy == "" {
|
2017-03-20 22:55:24 +00:00
|
|
|
Logf("PV ReclaimPolicy unspecified, default: Retain")
|
2017-02-17 04:33:41 +00:00
|
|
|
pvConfig.ReclaimPolicy = v1.PersistentVolumeReclaimRetain
|
2017-02-14 21:43:20 +00:00
|
|
|
}
|
2017-02-17 04:33:41 +00:00
|
|
|
if pvConfig.Prebind != nil {
|
2017-01-26 17:17:28 +00:00
|
|
|
claimRef = &v1.ObjectReference{
|
2017-02-17 04:33:41 +00:00
|
|
|
Name: pvConfig.Prebind.Name,
|
|
|
|
Namespace: pvConfig.Prebind.Namespace,
|
2017-01-26 17:17:28 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-14 16:26:00 +00:00
|
|
|
return &v1.PersistentVolume{
|
2017-01-26 17:17:28 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-02-17 04:33:41 +00:00
|
|
|
GenerateName: pvConfig.NamePrefix,
|
2017-03-20 22:55:24 +00:00
|
|
|
Labels: pvConfig.Labels,
|
2017-01-26 17:17:28 +00:00
|
|
|
Annotations: map[string]string{
|
2018-02-06 08:38:41 +00:00
|
|
|
util.VolumeGidAnnotationKey: "777",
|
2017-01-26 17:17:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: v1.PersistentVolumeSpec{
|
2017-02-17 04:33:41 +00:00
|
|
|
PersistentVolumeReclaimPolicy: pvConfig.ReclaimPolicy,
|
2017-01-26 17:17:28 +00:00
|
|
|
Capacity: v1.ResourceList{
|
|
|
|
v1.ResourceName(v1.ResourceStorage): resource.MustParse("2Gi"),
|
|
|
|
},
|
2017-02-17 04:33:41 +00:00
|
|
|
PersistentVolumeSource: pvConfig.PVSource,
|
2017-01-26 17:17:28 +00:00
|
|
|
AccessModes: []v1.PersistentVolumeAccessMode{
|
|
|
|
v1.ReadWriteOnce,
|
|
|
|
v1.ReadOnlyMany,
|
|
|
|
v1.ReadWriteMany,
|
|
|
|
},
|
2017-04-25 03:41:40 +00:00
|
|
|
ClaimRef: claimRef,
|
|
|
|
StorageClassName: pvConfig.StorageClassName,
|
2018-01-30 23:41:57 +00:00
|
|
|
NodeAffinity: pvConfig.NodeAffinity,
|
2018-02-14 16:26:00 +00:00
|
|
|
VolumeMode: pvConfig.VolumeMode,
|
2017-01-26 17:17:28 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a PVC definition based on the namespace.
|
|
|
|
// Note: if this PVC is intended to be pre-bound to a PV, whose name is not
|
2017-02-17 04:33:41 +00:00
|
|
|
// known until the PV is instantiated, then the func CreatePVPVC will add
|
2017-01-26 17:17:28 +00:00
|
|
|
// pvc.Spec.VolumeName to this claim.
|
2017-03-20 22:55:24 +00:00
|
|
|
func MakePersistentVolumeClaim(cfg PersistentVolumeClaimConfig, ns string) *v1.PersistentVolumeClaim {
|
2017-01-26 17:17:28 +00:00
|
|
|
// Specs are expected to match this test's PersistentVolume
|
|
|
|
|
2017-03-20 22:55:24 +00:00
|
|
|
if len(cfg.AccessModes) == 0 {
|
|
|
|
Logf("AccessModes unspecified, default: all modes (RWO, RWX, ROX).")
|
|
|
|
cfg.AccessModes = append(cfg.AccessModes, v1.ReadWriteOnce, v1.ReadOnlyMany, v1.ReadOnlyMany)
|
|
|
|
}
|
|
|
|
|
2017-01-26 17:17:28 +00:00
|
|
|
return &v1.PersistentVolumeClaim{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
GenerateName: "pvc-",
|
|
|
|
Namespace: ns,
|
2017-03-20 22:55:24 +00:00
|
|
|
Annotations: cfg.Annotations,
|
2017-01-26 17:17:28 +00:00
|
|
|
},
|
|
|
|
Spec: v1.PersistentVolumeClaimSpec{
|
2017-03-20 22:55:24 +00:00
|
|
|
Selector: cfg.Selector,
|
|
|
|
AccessModes: cfg.AccessModes,
|
2017-01-26 17:17:28 +00:00
|
|
|
Resources: v1.ResourceRequirements{
|
|
|
|
Requests: v1.ResourceList{
|
|
|
|
v1.ResourceName(v1.ResourceStorage): resource.MustParse("1Gi"),
|
|
|
|
},
|
|
|
|
},
|
2017-04-25 03:41:40 +00:00
|
|
|
StorageClassName: cfg.StorageClassName,
|
2018-02-14 16:26:00 +00:00
|
|
|
VolumeMode: cfg.VolumeMode,
|
2017-01-26 17:17:28 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 06:26:41 +00:00
|
|
|
func createPDWithRetry(zone string) (string, error) {
|
2017-02-23 00:49:36 +00:00
|
|
|
var err error
|
|
|
|
for start := time.Now(); time.Since(start) < PDRetryTimeout; time.Sleep(PDRetryPollTime) {
|
2017-03-01 06:26:41 +00:00
|
|
|
newDiskName, err := createPD(zone)
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
Logf("Couldn't create a new PD, sleeping 5 seconds: %v", err)
|
2017-02-23 00:49:36 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
Logf("Successfully created a new PD: %q.", newDiskName)
|
2017-04-01 04:42:52 +00:00
|
|
|
return newDiskName, nil
|
2017-02-23 00:49:36 +00:00
|
|
|
}
|
2017-04-01 04:42:52 +00:00
|
|
|
return "", err
|
2017-02-23 00:49:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 06:26:41 +00:00
|
|
|
func CreatePDWithRetry() (string, error) {
|
|
|
|
return createPDWithRetry("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreatePDWithRetryAndZone(zone string) (string, error) {
|
|
|
|
return createPDWithRetry(zone)
|
|
|
|
}
|
|
|
|
|
2017-04-01 04:42:52 +00:00
|
|
|
func DeletePDWithRetry(diskName string) error {
|
2017-02-23 00:49:36 +00:00
|
|
|
var err error
|
|
|
|
for start := time.Now(); time.Since(start) < PDRetryTimeout; time.Sleep(PDRetryPollTime) {
|
2017-04-01 04:42:52 +00:00
|
|
|
err = deletePD(diskName)
|
|
|
|
if err != nil {
|
|
|
|
Logf("Couldn't delete PD %q, sleeping %v: %v", diskName, PDRetryPollTime, err)
|
2017-02-23 00:49:36 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
Logf("Successfully deleted PD %q.", diskName)
|
2017-04-01 04:42:52 +00:00
|
|
|
return nil
|
2017-02-23 00:49:36 +00:00
|
|
|
}
|
2017-04-01 04:42:52 +00:00
|
|
|
return fmt.Errorf("unable to delete PD %q: %v", diskName, err)
|
2017-02-23 00:49:36 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 11:26:26 +00:00
|
|
|
func newAWSClient(zone string) *ec2.EC2 {
|
|
|
|
var cfg *aws.Config
|
|
|
|
|
|
|
|
if zone == "" {
|
|
|
|
zone = TestContext.CloudConfig.Zone
|
|
|
|
}
|
|
|
|
if zone == "" {
|
|
|
|
glog.Warning("No AWS zone configured!")
|
|
|
|
cfg = nil
|
|
|
|
} else {
|
|
|
|
region := zone[:len(zone)-1]
|
|
|
|
cfg = &aws.Config{Region: aws.String(region)}
|
|
|
|
}
|
|
|
|
return ec2.New(session.New(), cfg)
|
|
|
|
}
|
|
|
|
|
2017-03-01 06:26:41 +00:00
|
|
|
func createPD(zone string) (string, error) {
|
|
|
|
if zone == "" {
|
|
|
|
zone = TestContext.CloudConfig.Zone
|
|
|
|
}
|
|
|
|
|
2017-02-23 00:49:36 +00:00
|
|
|
if TestContext.Provider == "gce" || TestContext.Provider == "gke" {
|
|
|
|
pdName := fmt.Sprintf("%s-%s", TestContext.Prefix, string(uuid.NewUUID()))
|
|
|
|
|
|
|
|
gceCloud, err := GetGCECloud()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-03-15 22:00:13 +00:00
|
|
|
if zone == "" && TestContext.CloudConfig.MultiZone {
|
|
|
|
zones, err := gceCloud.GetAllZonesFromCloudProvider()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
zone, _ = zones.PopAny()
|
|
|
|
}
|
|
|
|
|
2017-02-23 00:49:36 +00:00
|
|
|
tags := map[string]string{}
|
2017-03-01 06:26:41 +00:00
|
|
|
err = gceCloud.CreateDisk(pdName, gcecloud.DiskTypeSSD, zone, 10 /* sizeGb */, tags)
|
2017-02-23 00:49:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return pdName, nil
|
|
|
|
} else if TestContext.Provider == "aws" {
|
2018-02-27 11:26:26 +00:00
|
|
|
client := newAWSClient(zone)
|
2017-02-23 00:49:36 +00:00
|
|
|
request := &ec2.CreateVolumeInput{}
|
2017-03-01 06:26:41 +00:00
|
|
|
request.AvailabilityZone = aws.String(zone)
|
2017-02-23 00:49:36 +00:00
|
|
|
request.Size = aws.Int64(10)
|
|
|
|
request.VolumeType = aws.String(awscloud.DefaultVolumeType)
|
|
|
|
response, err := client.CreateVolume(request)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
az := aws.StringValue(response.AvailabilityZone)
|
|
|
|
awsID := aws.StringValue(response.VolumeId)
|
|
|
|
|
|
|
|
volumeName := "aws://" + az + "/" + awsID
|
|
|
|
return volumeName, nil
|
2017-04-28 12:37:50 +00:00
|
|
|
} else if TestContext.Provider == "azure" {
|
|
|
|
pdName := fmt.Sprintf("%s-%s", TestContext.Prefix, string(uuid.NewUUID()))
|
|
|
|
azureCloud, err := GetAzureCloud()
|
2017-07-13 11:55:32 +00:00
|
|
|
|
2017-04-28 12:37:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-10-27 08:52:46 +00:00
|
|
|
_, diskURI, _, err := azureCloud.CreateVolume(pdName, "" /* account */, "" /* sku */, "" /* location */, 1 /* sizeGb */)
|
2017-04-28 12:37:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-10-27 08:52:46 +00:00
|
|
|
return diskURI, nil
|
2017-02-23 00:49:36 +00:00
|
|
|
} else {
|
2017-04-01 04:42:52 +00:00
|
|
|
return "", fmt.Errorf("provider does not support volume creation")
|
2017-02-23 00:49:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deletePD(pdName string) error {
|
|
|
|
if TestContext.Provider == "gce" || TestContext.Provider == "gke" {
|
|
|
|
gceCloud, err := GetGCECloud()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = gceCloud.DeleteDisk(pdName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && len(gerr.Errors) > 0 && gerr.Errors[0].Reason == "notFound" {
|
|
|
|
// PD already exists, ignore error.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-01 04:42:52 +00:00
|
|
|
Logf("error deleting PD %q: %v", pdName, err)
|
2017-02-23 00:49:36 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
} else if TestContext.Provider == "aws" {
|
2018-02-27 11:26:26 +00:00
|
|
|
client := newAWSClient("")
|
2017-02-23 00:49:36 +00:00
|
|
|
|
|
|
|
tokens := strings.Split(pdName, "/")
|
|
|
|
awsVolumeID := tokens[len(tokens)-1]
|
|
|
|
|
|
|
|
request := &ec2.DeleteVolumeInput{VolumeId: aws.String(awsVolumeID)}
|
|
|
|
_, err := client.DeleteVolume(request)
|
|
|
|
if err != nil {
|
|
|
|
if awsError, ok := err.(awserr.Error); ok && awsError.Code() == "InvalidVolume.NotFound" {
|
2017-04-01 04:42:52 +00:00
|
|
|
Logf("volume deletion implicitly succeeded because volume %q does not exist.", pdName)
|
2017-02-23 00:49:36 +00:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("error deleting EBS volumes: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2017-04-28 12:37:50 +00:00
|
|
|
} else if TestContext.Provider == "azure" {
|
|
|
|
azureCloud, err := GetAzureCloud()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-27 08:52:46 +00:00
|
|
|
err = azureCloud.DeleteVolume(pdName)
|
2017-04-28 12:37:50 +00:00
|
|
|
if err != nil {
|
|
|
|
Logf("failed to delete Azure volume %q: %v", pdName, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2017-02-23 00:49:36 +00:00
|
|
|
} else {
|
2017-04-01 04:42:52 +00:00
|
|
|
return fmt.Errorf("provider does not support volume deletion")
|
2017-02-23 00:49:36 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-31 17:15:23 +00:00
|
|
|
|
|
|
|
// Returns a pod definition based on the namespace. The pod references the PVC's
|
|
|
|
// name.
|
|
|
|
func MakeWritePod(ns string, pvc *v1.PersistentVolumeClaim) *v1.Pod {
|
2017-11-07 23:33:27 +00:00
|
|
|
return MakePod(ns, nil, []*v1.PersistentVolumeClaim{pvc}, true, "touch /mnt/volume1/SUCCESS && (id -G | grep -E '\\b777\\b')")
|
2017-03-31 17:15:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a pod definition based on the namespace. The pod references the PVC's
|
|
|
|
// name. A slice of BASH commands can be supplied as args to be run by the pod
|
2017-11-07 23:33:27 +00:00
|
|
|
func MakePod(ns string, nodeSelector map[string]string, pvclaims []*v1.PersistentVolumeClaim, isPrivileged bool, command string) *v1.Pod {
|
2017-03-31 17:15:23 +00:00
|
|
|
if len(command) == 0 {
|
2018-05-18 08:55:00 +00:00
|
|
|
command = "trap exit TERM; while true; do sleep 1; done"
|
2017-03-31 17:15:23 +00:00
|
|
|
}
|
|
|
|
podSpec := &v1.Pod{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "Pod",
|
2018-05-01 14:54:37 +00:00
|
|
|
APIVersion: "v1",
|
2017-03-31 17:15:23 +00:00
|
|
|
},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
GenerateName: "pvc-tester-",
|
|
|
|
Namespace: ns,
|
|
|
|
},
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
|
|
|
{
|
|
|
|
Name: "write-pod",
|
2017-08-29 08:32:08 +00:00
|
|
|
Image: BusyBoxImage,
|
2017-03-31 17:15:23 +00:00
|
|
|
Command: []string{"/bin/sh"},
|
|
|
|
Args: []string{"-c", command},
|
|
|
|
SecurityContext: &v1.SecurityContext{
|
|
|
|
Privileged: &isPrivileged,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RestartPolicy: v1.RestartPolicyOnFailure,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var volumeMounts = make([]v1.VolumeMount, len(pvclaims))
|
|
|
|
var volumes = make([]v1.Volume, len(pvclaims))
|
|
|
|
for index, pvclaim := range pvclaims {
|
|
|
|
volumename := fmt.Sprintf("volume%v", index+1)
|
|
|
|
volumeMounts[index] = v1.VolumeMount{Name: volumename, MountPath: "/mnt/" + volumename}
|
|
|
|
volumes[index] = v1.Volume{Name: volumename, VolumeSource: v1.VolumeSource{PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ClaimName: pvclaim.Name, ReadOnly: false}}}
|
|
|
|
}
|
|
|
|
podSpec.Spec.Containers[0].VolumeMounts = volumeMounts
|
|
|
|
podSpec.Spec.Volumes = volumes
|
2017-11-07 23:33:27 +00:00
|
|
|
if nodeSelector != nil {
|
|
|
|
podSpec.Spec.NodeSelector = nodeSelector
|
|
|
|
}
|
2017-03-31 17:15:23 +00:00
|
|
|
return podSpec
|
|
|
|
}
|
|
|
|
|
2018-03-14 15:15:58 +00:00
|
|
|
// Returns a pod definition based on the namespace using nginx image
|
|
|
|
func MakeNginxPod(ns string, nodeSelector map[string]string, pvclaims []*v1.PersistentVolumeClaim) *v1.Pod {
|
|
|
|
podSpec := &v1.Pod{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "Pod",
|
2018-05-01 14:54:37 +00:00
|
|
|
APIVersion: "v1",
|
2018-03-14 15:15:58 +00:00
|
|
|
},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
GenerateName: "pvc-tester-",
|
|
|
|
Namespace: ns,
|
|
|
|
},
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
|
|
|
{
|
|
|
|
Name: "write-pod",
|
|
|
|
Image: "nginx",
|
|
|
|
Ports: []v1.ContainerPort{
|
|
|
|
{
|
|
|
|
Name: "http-server",
|
|
|
|
ContainerPort: 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var volumeMounts = make([]v1.VolumeMount, len(pvclaims))
|
|
|
|
var volumes = make([]v1.Volume, len(pvclaims))
|
|
|
|
for index, pvclaim := range pvclaims {
|
|
|
|
volumename := fmt.Sprintf("volume%v", index+1)
|
|
|
|
volumeMounts[index] = v1.VolumeMount{Name: volumename, MountPath: "/mnt/" + volumename}
|
|
|
|
volumes[index] = v1.Volume{Name: volumename, VolumeSource: v1.VolumeSource{PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ClaimName: pvclaim.Name, ReadOnly: false}}}
|
|
|
|
}
|
|
|
|
podSpec.Spec.Containers[0].VolumeMounts = volumeMounts
|
|
|
|
podSpec.Spec.Volumes = volumes
|
|
|
|
if nodeSelector != nil {
|
|
|
|
podSpec.Spec.NodeSelector = nodeSelector
|
|
|
|
}
|
|
|
|
return podSpec
|
|
|
|
}
|
|
|
|
|
2017-07-27 21:10:14 +00:00
|
|
|
// Returns a pod definition based on the namespace. The pod references the PVC's
|
|
|
|
// name. A slice of BASH commands can be supplied as args to be run by the pod.
|
|
|
|
// SELinux testing requires to pass HostIPC and HostPID as booleansi arguments.
|
2018-04-16 12:39:23 +00:00
|
|
|
func MakeSecPod(ns string, pvclaims []*v1.PersistentVolumeClaim, isPrivileged bool, command string, hostIPC bool, hostPID bool, seLinuxLabel *v1.SELinuxOptions, fsGroup *int64) *v1.Pod {
|
2017-07-27 21:10:14 +00:00
|
|
|
if len(command) == 0 {
|
2018-05-18 08:55:00 +00:00
|
|
|
command = "trap exit TERM; while true; do sleep 1; done"
|
2017-07-27 21:10:14 +00:00
|
|
|
}
|
|
|
|
podName := "security-context-" + string(uuid.NewUUID())
|
2018-04-16 12:39:23 +00:00
|
|
|
if fsGroup == nil {
|
|
|
|
fsGroup = func(i int64) *int64 {
|
|
|
|
return &i
|
|
|
|
}(1000)
|
|
|
|
}
|
2017-07-27 21:10:14 +00:00
|
|
|
podSpec := &v1.Pod{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "Pod",
|
2018-05-01 14:54:37 +00:00
|
|
|
APIVersion: "v1",
|
2017-07-27 21:10:14 +00:00
|
|
|
},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: podName,
|
|
|
|
Namespace: ns,
|
|
|
|
},
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
HostIPC: hostIPC,
|
|
|
|
HostPID: hostPID,
|
|
|
|
SecurityContext: &v1.PodSecurityContext{
|
2018-04-16 12:39:23 +00:00
|
|
|
FSGroup: fsGroup,
|
2017-07-27 21:10:14 +00:00
|
|
|
},
|
|
|
|
Containers: []v1.Container{
|
|
|
|
{
|
|
|
|
Name: "write-pod",
|
2017-12-01 19:01:47 +00:00
|
|
|
Image: imageutils.GetE2EImage(imageutils.BusyBox),
|
2017-07-27 21:10:14 +00:00
|
|
|
Command: []string{"/bin/sh"},
|
|
|
|
Args: []string{"-c", command},
|
|
|
|
SecurityContext: &v1.SecurityContext{
|
|
|
|
Privileged: &isPrivileged,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RestartPolicy: v1.RestartPolicyOnFailure,
|
|
|
|
},
|
|
|
|
}
|
2018-02-14 16:26:00 +00:00
|
|
|
var volumeMounts = make([]v1.VolumeMount, 0)
|
|
|
|
var volumeDevices = make([]v1.VolumeDevice, 0)
|
2017-07-27 21:10:14 +00:00
|
|
|
var volumes = make([]v1.Volume, len(pvclaims))
|
|
|
|
for index, pvclaim := range pvclaims {
|
|
|
|
volumename := fmt.Sprintf("volume%v", index+1)
|
2018-02-14 16:26:00 +00:00
|
|
|
if pvclaim.Spec.VolumeMode != nil && *pvclaim.Spec.VolumeMode == v1.PersistentVolumeBlock {
|
|
|
|
volumeDevices = append(volumeDevices, v1.VolumeDevice{Name: volumename, DevicePath: "/mnt/" + volumename})
|
|
|
|
} else {
|
|
|
|
volumeMounts = append(volumeMounts, v1.VolumeMount{Name: volumename, MountPath: "/mnt/" + volumename})
|
|
|
|
}
|
|
|
|
|
2017-07-27 21:10:14 +00:00
|
|
|
volumes[index] = v1.Volume{Name: volumename, VolumeSource: v1.VolumeSource{PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ClaimName: pvclaim.Name, ReadOnly: false}}}
|
|
|
|
}
|
|
|
|
podSpec.Spec.Containers[0].VolumeMounts = volumeMounts
|
2018-02-14 16:26:00 +00:00
|
|
|
podSpec.Spec.Containers[0].VolumeDevices = volumeDevices
|
2017-07-27 21:10:14 +00:00
|
|
|
podSpec.Spec.Volumes = volumes
|
|
|
|
podSpec.Spec.SecurityContext.SELinuxOptions = seLinuxLabel
|
|
|
|
return podSpec
|
|
|
|
}
|
|
|
|
|
2017-11-07 23:33:27 +00:00
|
|
|
// CreatePod with given claims based on node selector
|
|
|
|
func CreatePod(client clientset.Interface, namespace string, nodeSelector map[string]string, pvclaims []*v1.PersistentVolumeClaim, isPrivileged bool, command string) (*v1.Pod, error) {
|
|
|
|
pod := MakePod(namespace, nodeSelector, pvclaims, isPrivileged, command)
|
2017-04-01 04:42:52 +00:00
|
|
|
pod, err := client.CoreV1().Pods(namespace).Create(pod)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("pod Create API error: %v", err)
|
|
|
|
}
|
2017-03-31 17:15:23 +00:00
|
|
|
// Waiting for pod to be running
|
2017-04-01 04:42:52 +00:00
|
|
|
err = WaitForPodNameRunningInNamespace(client, pod.Name, namespace)
|
|
|
|
if err != nil {
|
|
|
|
return pod, fmt.Errorf("pod %q is not Running: %v", pod.Name, err)
|
|
|
|
}
|
2017-03-31 17:15:23 +00:00
|
|
|
// get fresh pod info
|
|
|
|
pod, err = client.CoreV1().Pods(namespace).Get(pod.Name, metav1.GetOptions{})
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return pod, fmt.Errorf("pod Get API error: %v", err)
|
2018-03-14 15:15:58 +00:00
|
|
|
}
|
|
|
|
return pod, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateNginxPod(client clientset.Interface, namespace string, nodeSelector map[string]string, pvclaims []*v1.PersistentVolumeClaim) (*v1.Pod, error) {
|
|
|
|
pod := MakeNginxPod(namespace, nodeSelector, pvclaims)
|
|
|
|
pod, err := client.CoreV1().Pods(namespace).Create(pod)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("pod Create API error: %v", err)
|
|
|
|
}
|
|
|
|
// Waiting for pod to be running
|
|
|
|
err = WaitForPodNameRunningInNamespace(client, pod.Name, namespace)
|
|
|
|
if err != nil {
|
|
|
|
return pod, fmt.Errorf("pod %q is not Running: %v", pod.Name, err)
|
|
|
|
}
|
|
|
|
// get fresh pod info
|
|
|
|
pod, err = client.CoreV1().Pods(namespace).Get(pod.Name, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return pod, fmt.Errorf("pod Get API error: %v", err)
|
2017-04-01 04:42:52 +00:00
|
|
|
}
|
|
|
|
return pod, nil
|
2017-03-31 17:15:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 21:10:14 +00:00
|
|
|
// create security pod with given claims
|
2018-04-16 08:32:48 +00:00
|
|
|
func CreateSecPod(client clientset.Interface, namespace string, pvclaims []*v1.PersistentVolumeClaim, isPrivileged bool, command string, hostIPC bool, hostPID bool, seLinuxLabel *v1.SELinuxOptions, fsGroup *int64, timeout time.Duration) (*v1.Pod, error) {
|
2018-04-16 12:39:23 +00:00
|
|
|
pod := MakeSecPod(namespace, pvclaims, isPrivileged, command, hostIPC, hostPID, seLinuxLabel, fsGroup)
|
2017-07-27 21:10:14 +00:00
|
|
|
pod, err := client.CoreV1().Pods(namespace).Create(pod)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("pod Create API error: %v", err)
|
|
|
|
}
|
|
|
|
// Waiting for pod to be running
|
2018-04-16 08:32:48 +00:00
|
|
|
err = WaitTimeoutForPodRunningInNamespace(client, pod.Name, namespace, timeout)
|
2017-07-27 21:10:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return pod, fmt.Errorf("pod %q is not Running: %v", pod.Name, err)
|
|
|
|
}
|
|
|
|
// get fresh pod info
|
|
|
|
pod, err = client.CoreV1().Pods(namespace).Get(pod.Name, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return pod, fmt.Errorf("pod Get API error: %v", err)
|
|
|
|
}
|
|
|
|
return pod, nil
|
|
|
|
}
|
|
|
|
|
2017-03-31 17:15:23 +00:00
|
|
|
// Define and create a pod with a mounted PV. Pod runs infinite loop until killed.
|
2017-04-01 04:42:52 +00:00
|
|
|
func CreateClientPod(c clientset.Interface, ns string, pvc *v1.PersistentVolumeClaim) (*v1.Pod, error) {
|
2017-11-07 23:33:27 +00:00
|
|
|
return CreatePod(c, ns, nil, []*v1.PersistentVolumeClaim{pvc}, true, "")
|
2017-03-31 17:15:23 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 12:02:13 +00:00
|
|
|
// CreateUnschedulablePod with given claims based on node selector
|
|
|
|
func CreateUnschedulablePod(client clientset.Interface, namespace string, nodeSelector map[string]string, pvclaims []*v1.PersistentVolumeClaim, isPrivileged bool, command string) (*v1.Pod, error) {
|
|
|
|
pod := MakePod(namespace, nodeSelector, pvclaims, isPrivileged, command)
|
|
|
|
pod, err := client.CoreV1().Pods(namespace).Create(pod)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("pod Create API error: %v", err)
|
|
|
|
}
|
|
|
|
// Waiting for pod to become Unschedulable
|
|
|
|
err = WaitForPodNameUnschedulableInNamespace(client, pod.Name, namespace)
|
|
|
|
if err != nil {
|
|
|
|
return pod, fmt.Errorf("pod %q is not Unschedulable: %v", pod.Name, err)
|
|
|
|
}
|
|
|
|
// get fresh pod info
|
|
|
|
pod, err = client.CoreV1().Pods(namespace).Get(pod.Name, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return pod, fmt.Errorf("pod Get API error: %v", err)
|
|
|
|
}
|
|
|
|
return pod, nil
|
|
|
|
}
|
|
|
|
|
2017-03-31 17:15:23 +00:00
|
|
|
// wait until all pvcs phase set to bound
|
2017-10-03 23:23:39 +00:00
|
|
|
func WaitForPVClaimBoundPhase(client clientset.Interface, pvclaims []*v1.PersistentVolumeClaim, timeout time.Duration) ([]*v1.PersistentVolume, error) {
|
2017-04-01 04:42:52 +00:00
|
|
|
persistentvolumes := make([]*v1.PersistentVolume, len(pvclaims))
|
|
|
|
|
2017-03-31 17:15:23 +00:00
|
|
|
for index, claim := range pvclaims {
|
2017-10-03 23:23:39 +00:00
|
|
|
err := WaitForPersistentVolumeClaimPhase(v1.ClaimBound, client, claim.Namespace, claim.Name, Poll, timeout)
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return persistentvolumes, err
|
|
|
|
}
|
2017-03-31 17:15:23 +00:00
|
|
|
// Get new copy of the claim
|
2017-04-01 04:42:52 +00:00
|
|
|
claim, err = client.CoreV1().PersistentVolumeClaims(claim.Namespace).Get(claim.Name, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return persistentvolumes, fmt.Errorf("PVC Get API error: %v", err)
|
|
|
|
}
|
2017-03-31 17:15:23 +00:00
|
|
|
// Get the bounded PV
|
|
|
|
persistentvolumes[index], err = client.CoreV1().PersistentVolumes().Get(claim.Spec.VolumeName, metav1.GetOptions{})
|
2017-04-01 04:42:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return persistentvolumes, fmt.Errorf("PV Get API error: %v", err)
|
|
|
|
}
|
2017-03-31 17:15:23 +00:00
|
|
|
}
|
2017-04-01 04:42:52 +00:00
|
|
|
return persistentvolumes, nil
|
2017-03-31 17:15:23 +00:00
|
|
|
}
|
2017-03-01 06:26:41 +00:00
|
|
|
|
|
|
|
func CreatePVSource(zone string) (*v1.PersistentVolumeSource, error) {
|
|
|
|
diskName, err := CreatePDWithRetryAndZone(zone)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if TestContext.Provider == "gce" || TestContext.Provider == "gke" {
|
|
|
|
return &v1.PersistentVolumeSource{
|
|
|
|
GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
|
|
|
|
PDName: diskName,
|
|
|
|
FSType: "ext3",
|
|
|
|
ReadOnly: false,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
} else if TestContext.Provider == "aws" {
|
|
|
|
return &v1.PersistentVolumeSource{
|
|
|
|
AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{
|
|
|
|
VolumeID: diskName,
|
|
|
|
FSType: "ext3",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("Provider not supported")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeletePVSource(pvSource *v1.PersistentVolumeSource) error {
|
|
|
|
if TestContext.Provider == "gce" || TestContext.Provider == "gke" {
|
|
|
|
return DeletePDWithRetry(pvSource.GCEPersistentDisk.PDName)
|
|
|
|
} else if TestContext.Provider == "aws" {
|
|
|
|
return DeletePDWithRetry(pvSource.AWSElasticBlockStore.VolumeID)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Provider not supported")
|
|
|
|
}
|
|
|
|
}
|