Skip to content

Commit

Permalink
Merge pull request kubernetes-csi#2 from ggriffiths/add_pvc_metadata_…
Browse files Browse the repository at this point in the history
…labels_name_namespace

add PVC Metadata to req.Parameters for openstorage
  • Loading branch information
lpabon authored Jun 21, 2019
2 parents f79190b + 7cc2cab commit 49c055d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 34 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller

import (
"context"
"encoding/json"
"fmt"
"math"
"os"
Expand Down Expand Up @@ -56,6 +57,12 @@ type deprecatedSecretParamsMap struct {
}

const (
// Openstorage specific parameters
osdParameterPrefix = "csi.openstorage.org/"
osdPvcNameKey = osdParameterPrefix + "pvc-name"
osdPvcNamespaceKey = osdParameterPrefix + "pvc-namespace"
osdPvcAnnotationsAndLabelsKey = osdParameterPrefix + "pvc-annotations-and-labels"

// CSI Parameters prefixed with csiParameterPrefix are not passed through
// to the driver on CreateVolumeRequest calls. Instead they are intended
// to used by the CSI external-provisioner and maybe used to populate
Expand Down Expand Up @@ -418,6 +425,12 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
},
}

// add pvc name, namespace, annotations and labels in the parameters
req.Parameters, err = p.addPVCMetadataParams(req.Parameters, options.PVC)
if err != nil {
return nil, err
}

if needSnapshotSupport {
volumeContentSource, err := p.getVolumeContentSource(options)
if err != nil {
Expand Down Expand Up @@ -561,6 +574,27 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
return pv, nil
}

func (p *csiProvisioner) addPVCMetadataParams(params map[string]string, pvc *v1.PersistentVolumeClaim) (map[string]string, error) {
for k, v := range pvc.Annotations {
// add all annotations to labels. Annotations take precedence, so we will overwrite
// labels with annotations if they have overlapping keys.
pvc.Labels[k] = v
}
labelsEncoded, err := json.Marshal(pvc.Labels)
if err != nil {
klog.Errorf("Failed to encode PVC labels: %v", err)
return nil, err
}
if len(params) == 0 {
params = make(map[string]string)
}
params[osdPvcNameKey] = pvc.Name
params[osdPvcNamespaceKey] = pvc.Namespace
params[osdPvcAnnotationsAndLabelsKey] = string(labelsEncoded)

return params, nil
}

func (p *csiProvisioner) supportsTopology() bool {
return p.pluginCapabilities[csi.PluginCapability_Service_VOLUME_ACCESSIBILITY_CONSTRAINTS] &&
utilfeature.DefaultFeatureGate.Enabled(features.Topology)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ func TestProvision(t *testing.T) {
},
},
expectCreateVolDo: func(ctx context.Context, req *csi.CreateVolumeRequest) {
if len(req.Parameters) != 0 {
if _, ok := req.Parameters[prefixedFsTypeKey]; ok {
t.Errorf("Parameters should have been stripped")
}
},
Expand Down

0 comments on commit 49c055d

Please sign in to comment.