diff --git a/pkg/oc/bootstrap/docker/openshift/pvsetup.go b/pkg/oc/bootstrap/docker/openshift/pvsetup.go index f5db8afd9e09..da3e83aaadfd 100644 --- a/pkg/oc/bootstrap/docker/openshift/pvsetup.go +++ b/pkg/oc/bootstrap/docker/openshift/pvsetup.go @@ -3,6 +3,7 @@ package openshift import ( "bytes" "fmt" + "os" kerrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -17,10 +18,11 @@ import ( ) const ( - pvCount = 100 - pvSetupJobName = "persistent-volume-setup" - pvInstallerSA = "pvinstaller" - pvSetupNamespace = "default" + pvCount = 100 + pvSetupJobName = "persistent-volume-setup" + pvInstallerSA = "pvinstaller" + pvSetupNamespace = "default" + pvIgnoreMarkerFile = ".skip_pv" ) const createPVScript = `#/bin/bash @@ -81,7 +83,8 @@ for i in $(seq -f "%%04g" 1 %[1]d); do done ` -func (h *Helper) SetupPersistentStorage(authorizationClient authorizationtypedclient.ClusterRoleBindingsGetter, kclient kclientset.Interface, securityClient securityclient.Interface, dir string) error { +// SetupPersistentStorage sets up persistent storage +func (h *Helper) SetupPersistentStorage(authorizationClient authorizationtypedclient.ClusterRoleBindingsGetter, kclient kclientset.Interface, securityClient securityclient.Interface, dir, HostPersistentVolumesDir string) error { err := h.ensurePVInstallerSA(authorizationClient, kclient, securityClient) if err != nil { return err @@ -96,11 +99,16 @@ func (h *Helper) SetupPersistentStorage(authorizationClient authorizationtypedcl return errors.NewError("error retrieving job to setup persistent volumes (%s/%s)", pvSetupNamespace, pvSetupJobName).WithCause(err).WithDetails(h.OriginLog()) } - setupJob := persistentStorageSetupJob(pvSetupJobName, dir, h.image) - if _, err = kclient.Batch().Jobs(pvSetupNamespace).Create(setupJob); err != nil { - return errors.NewError("cannot create job to setup persistent volumes (%s/%s)", pvSetupNamespace, pvSetupJobName).WithCause(err).WithDetails(h.OriginLog()) + // check if we need to create pv's + _, err = os.Stat(fmt.Sprintf("%s/%s", HostPersistentVolumesDir, pvIgnoreMarkerFile)) + if !os.IsNotExist(err) { + fmt.Printf("Skip persistent volume creation \n") + } else { + setupJob := persistentStorageSetupJob(pvSetupJobName, dir, h.image, pvCount) + if _, err = kclient.Batch().Jobs(pvSetupNamespace).Create(setupJob); err != nil { + return errors.NewError("cannot create job to setup persistent volumes (%s/%s)", pvSetupNamespace, pvSetupJobName).WithCause(err).WithDetails(h.OriginLog()) + } } - return nil } @@ -138,7 +146,7 @@ func (h *Helper) ensurePVInstallerSA(authorizationClient authorizationtypedclien return nil } -func persistentStorageSetupJob(name, dir, image string) *kbatch.Job { +func persistentStorageSetupJob(name, dir, image string, pvCount int) *kbatch.Job { // Job volume volume := kapi.Volume{} volume.Name = "pvdir" diff --git a/pkg/oc/bootstrap/docker/up.go b/pkg/oc/bootstrap/docker/up.go index b72af6f5ecdc..64e2cfcc62d5 100644 --- a/pkg/oc/bootstrap/docker/up.go +++ b/pkg/oc/bootstrap/docker/up.go @@ -222,6 +222,7 @@ type CommonStartConfig struct { HTTPSProxy string NoProxy []string CACert string + PVCount int dockerClient dockerhelper.Interface dockerHelper *dockerhelper.Helper @@ -910,7 +911,7 @@ func (c *ClientStartConfig) StartOpenShift(out io.Writer) error { return err } - err = c.OpenShiftHelper().SetupPersistentStorage(authorizationClient.Authorization(), kClient, securityClient, c.HostPersistentVolumesDir) + err = c.OpenShiftHelper().SetupPersistentStorage(authorizationClient.Authorization(), kClient, securityClient, c.HostPersistentVolumesDir, c.HostPersistentVolumesDir) if err != nil { return err } diff --git a/test/extended/clusterup.sh b/test/extended/clusterup.sh index 00ba7d8f9708..3bbda8ff51a4 100755 --- a/test/extended/clusterup.sh +++ b/test/extended/clusterup.sh @@ -53,6 +53,14 @@ function os::test::extended::clusterup::verify_persistent_volumes () { os::cmd::expect_success "oc login -u developer" } +function os::test::extended::clusterup::skip_persistent_volumes () { + mkdir -p /tmp/pv + touch /tmp/pv/.skip_pv + os::cmd::expect_success_and_text "oc cluster up --host-pv-dir=/tmp/pv/" "Skip persistent volume creation" + os::cmd::expect_success "oc login -u system:admin" + os::cmd::expect_success_and_text "oc get pv | wc -l" "0" +} + function os::test::extended::clusterup::verify_metrics () { os::cmd::expect_success "oc login -u system:admin" os::cmd::expect_success_and_text "oc get pods -n openshift-infra" "metrics-deployer" @@ -269,6 +277,12 @@ function os::test::extended::clusterup::portinuse_cleanup () { } +# Verifies that clusterup handle different scenarios with persistent volumes setup +function os::test::extended::clusterup::persistentvolumes () { + os::test::extended::clusterup::skip_persistent_volumes + rm -rf /tmp/pv +} + readonly default_tests=( "service_catalog" "noargs" @@ -277,6 +291,7 @@ readonly default_tests=( "numerichostname" "portinuse" "svcaccess" + "persistentvolumes" # enable once https://github.com/openshift/origin/issues/16995 is fixed # "default"