From 66fe5a724c3db0f3ad41279a9aa74f31cc734950 Mon Sep 17 00:00:00 2001 From: ZiyunYang Date: Mon, 24 Jun 2019 11:09:58 +0800 Subject: [PATCH] modify pvc name --- lib/controller/controller.go | 17 +++++++++++++---- lib/controller/volume.go | 9 +++++++++ .../cmd/nfs-client-provisioner/provisioner.go | 18 +++++++++++------- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/controller/controller.go b/lib/controller/controller.go index cee9d07a1ea..5ab87ff3659 100644 --- a/lib/controller/controller.go +++ b/lib/controller/controller.go @@ -744,6 +744,12 @@ func (ctrl *ProvisionController) updateDeleteStats(volume *v1.PersistentVolume, } } +func (ctrl *ProvisionController) getProvisionedTenantAndStackAndServerNameForClaim( + claim *v1.PersistentVolumeClaim) (tenant string, stack string, service string) { + tenant, stack, service = claim.Labels["io.wise2c.tenant"], claim.Labels["io.wise2c.stack"], claim.Labels["io.wise2c.service"] + return +} + // provisionClaimOperation attempts to provision a volume for the given claim. // Returns an error for use by goroutinemap when expbackoff is enabled: if nil, // the operation is deleted, else the operation may be retried with expbackoff. @@ -791,12 +797,15 @@ func (ctrl *ProvisionController) provisionClaimOperation(claim *v1.PersistentVol return err } } - + tenant, stack, service := ctrl.getProvisionedTenantAndStackAndServerNameForClaim(claim) options := VolumeOptions{ PersistentVolumeReclaimPolicy: reclaimPolicy, - PVName: pvName, - PVC: claim, - Parameters: parameters, + PVName: pvName, + PVC: claim, + Parameters: parameters, + Tenant: tenant, + Stack: stack, + Service: service, } ctrl.eventRecorder.Event(claim, v1.EventTypeNormal, "Provisioning", fmt.Sprintf("External provisioner is provisioning volume for claim %q", claimToClaimKey(claim))) diff --git a/lib/controller/volume.go b/lib/controller/volume.go index 1522cb148a9..e3be75bfb5f 100644 --- a/lib/controller/volume.go +++ b/lib/controller/volume.go @@ -66,4 +66,13 @@ type VolumeOptions struct { PVC *v1.PersistentVolumeClaim // Volume provisioning parameters from StorageClass Parameters map[string]string + + // Tenant: Service tenant name for the persistent volume. + Tenant string + + // Stack: The service stack for the persistent volume. + Stack string + + // Service: The service name of the persistent volume. + Service string } diff --git a/nfs-client/cmd/nfs-client-provisioner/provisioner.go b/nfs-client/cmd/nfs-client-provisioner/provisioner.go index e11aad1ea5d..971722aa349 100644 --- a/nfs-client/cmd/nfs-client-provisioner/provisioner.go +++ b/nfs-client/cmd/nfs-client-provisioner/provisioner.go @@ -68,8 +68,8 @@ func isMounted(mp string) bool { return false } -func pvName(ns string, name string) string { - return fmt.Sprintf("%s-%s", ns, name) +func pvName(tenant string, stack string, service string, name string) string { + return fmt.Sprintf("%s-%s-%s-%s", tenant, stack, service, name) } func mountPoint(server string, path string) string { @@ -100,13 +100,11 @@ func (p *nfsProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis } server := params["nfsServer"] path := params["nfsPath"] - mp, err := ensureMount(server, path) if err != nil { return nil, fmt.Errorf("unable to mount NFS volume: " + err.Error()) } - - pvName := pvName(options.PVC.Namespace, options.PVName) + pvName := pvName(options.Tenant, options.Stack, options.Service, options.PVName) if err := os.MkdirAll(filepath.Join(mp, pvName), 0777); err != nil { return nil, errors.New("unable to create directory to provision new pv: " + err.Error()) } @@ -114,6 +112,11 @@ func (p *nfsProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis pv := &v1.PersistentVolume{ ObjectMeta: metav1.ObjectMeta{ Name: options.PVName, + Labels: map[string]string{ + "io.wise2c.tenant": options.Tenant, + "io.wise2c.stack": options.Stack, + "io.wise2c.service": options.Service, + }, }, Spec: v1.PersistentVolumeSpec{ PersistentVolumeReclaimPolicy: options.PersistentVolumeReclaimPolicy, @@ -139,11 +142,12 @@ func (p *nfsProvisioner) Delete(volume *v1.PersistentVolume) error { path := path.Dir(volume.Spec.PersistentVolumeSource.NFS.Path) mp, err := ensureMount(server, path) if err != nil { - glog.Errorf("Failed to mount %s:%s", server, path) + glog.Errorf("Failed to mount %s:%s %s", server, path, mp) return err } // PV is **not** namespaced - pvName := pvName(volume.Spec.ClaimRef.Namespace, volume.ObjectMeta.Name) + tenant, stack, service := volume.Labels["io.wise2c.tenant"], volume.Labels["io.wise2c.stack"], volume.Labels["io.wise2c.service"] + pvName := pvName(tenant, stack, service, volume.ObjectMeta.Name) oldPath := filepath.Join(mp, pvName) archivePath := filepath.Join(mp, "archived-"+pvName) glog.Infof("archiving path %s to %s", oldPath, archivePath)