Skip to content
Permalink
Browse files

Attach built storage class label automatically

  • Loading branch information...
cofyc committed Jun 26, 2019
1 parent 601f79d commit a8fdfdfe247a0c0d3817e2561bf5592efbca11dc
Showing with 18 additions and 4 deletions.
  1. +5 −1 pkg/common/common.go
  2. +10 −2 pkg/discovery/discovery.go
  3. +3 −1 pkg/discovery/discovery_test.go
@@ -31,7 +31,7 @@ import (

"hash/fnv"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
@@ -81,6 +81,10 @@ const (

// DefaultVolumeMode is the default volume mode of created PV object.
DefaultVolumeMode = "Filesystem"

// LocalStorageClassLabel is the name of local storage class label which is
// automatically added for local PVs.
LocalStorageClassLabel = "local.storage.k8s.io/storageclass"
)

// UserConfig stores all the user-defined parameters to the provisioner
@@ -27,7 +27,8 @@ import (
"sigs.k8s.io/sig-storage-local-static-provisioner/pkg/common"
"sigs.k8s.io/sig-storage-local-static-provisioner/pkg/metrics"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/validation"
storagev1listers "k8s.io/client-go/listers/storage/v1"
"k8s.io/client-go/tools/cache"
esUtil "sigs.k8s.io/sig-storage-lib-external-provisioner/util"
@@ -304,6 +305,13 @@ func (d *Discoverer) createPV(file, class string, reclaimPolicy v1.PersistentVol
klog.Infof("Found new volume at host path %q with capacity %d, creating Local PV %q, required volumeMode %q",
outsidePath, capacityByte, pvName, volMode)

pvLabels := make(map[string]string)
for k, v := range d.Labels {
pvLabels[k] = v
}
// truncate the class name if it exceeds the max allowed length
pvLabels[common.LocalStorageClassLabel] = class[:validation.LabelValueMaxLength]

localPVConfig := &common.LocalPVConfig{
Name: pvName,
HostPath: outsidePath,
@@ -312,7 +320,7 @@ func (d *Discoverer) createPV(file, class string, reclaimPolicy v1.PersistentVol
ReclaimPolicy: reclaimPolicy,
ProvisionerName: d.Name,
VolumeMode: volMode,
Labels: d.Labels,
Labels: pvLabels,
MountOptions: mountOptions,
}

@@ -29,7 +29,7 @@ import (
"sigs.k8s.io/sig-storage-local-static-provisioner/pkg/deleter"
"sigs.k8s.io/sig-storage-local-static-provisioner/pkg/util"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -68,6 +68,7 @@ var expectedPVLabels = map[string]string{
"failure-domain.beta.kubernetes.io/zone": "west-1",
"failure-domain.beta.kubernetes.io/region": "west",
common.NodeLabelKey: testNodeName,
common.LocalStorageClassLabel: "sc1",
"local-storage-cr-name": "foobar"}

var testNode = &v1.Node{
@@ -497,6 +498,7 @@ func verifyPVLabels(t *testing.T, pv *v1.PersistentVolume) {
t.Errorf("Labels not set")
return
}
expectedPVLabels[common.LocalStorageClassLabel] = pv.Spec.StorageClassName
eq := reflect.DeepEqual(pv.Labels, expectedPVLabels)
if !eq {
t.Errorf("Labels not as expected %v != %v", pv.Labels, expectedPVLabels)

0 comments on commit a8fdfdf

Please sign in to comment.
You can’t perform that action at this time.