Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#123 from newrelic-forks/owner-refe…
Browse files Browse the repository at this point in the history
…rence

Set PV owner reference
  • Loading branch information
k8s-ci-robot authored Jul 26, 2019
2 parents 1a5b97f + 6cdb97f commit 519978a
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 24 deletions.
3 changes: 2 additions & 1 deletion cmd/local-volume-provisioner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"sigs.k8s.io/sig-storage-local-static-provisioner/pkg/metrics"
"sigs.k8s.io/sig-storage-local-static-provisioner/pkg/metrics/collectors"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
Expand Down Expand Up @@ -95,6 +95,7 @@ func main() {
Namespace: namespace,
JobContainerImage: jobImage,
LabelsForPV: provisionerConfig.LabelsForPV,
SetPVOwnerRef: provisionerConfig.SetPVOwnerRef,
})

klog.Infof("Starting metrics server at %s\n", optListenAddress)
Expand Down
3 changes: 2 additions & 1 deletion helm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ And there are [a lot of examples](TODO) to help you get started quickly.

Helm templating is used to generate the provisioner's DaemonSet, ConfigMap and
other necessary objects' specs. The generated specs can be further customized
as needed (usually not necessary), and then deployed using kubectl.
as needed (usually not necessary), and then deployed using kubectl.

**helm template** uses 3 sources of information:

Expand Down Expand Up @@ -100,6 +100,7 @@ provisioner chart and their default values.
| common.minResyncPeriod | Resync period in reflectors will be random between `minResyncPeriod` and `2*minResyncPeriod`. | str | `5m0s` |
| common.configMapName | Provisioner ConfigMap name. | str | `local-provisioner-config` |
| common.podSecurityPolicy | Whether to create pod security policy or not. | bool | `false` |
| common.setPVOwnerRef | If set to true, PVs are set to be dependents of the owner Node. | bool | `false` |
| classes.[n].name | StorageClass name. | str | `-` |
| classes.[n].hostDir | Path on the host where local volumes of this storage class are mounted under. | str | `-` |
| classes.[n].mountDir | Optionally specify mount path of local volumes. By default, we use same path as hostDir in container. | str | `-` |
Expand Down
3 changes: 3 additions & 0 deletions helm/provisioner/templates/provisioner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ data:
{{- if .Values.common.useAlphaAPI }}
useAlphaAPI: "true"
{{- end }}
{{- if .Values.common.setPVOwnerRef }}
setPVOwnerRef: "true"
{{- end }}
{{- if .Values.common.useJobForCleaning }}
useJobForCleaning: "yes"
{{- end}}
Expand Down
9 changes: 7 additions & 2 deletions helm/provisioner/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ common:
namespace: default
#
# Defines whether to create provisioner namespace
#
#
createNamespace: false
#
# Beta PV.NodeAffinity field is used by default. If running against pre-1.10
# k8s version, the `useAlphaAPI` flag must be enabled in the configMap.
#
useAlphaAPI: false
#
# Indicates if PVs should be dependents of the owner Node.
#
setPVOwnerRef: false
#
# Provisioner clean volumes in process by default. If set to true, provisioner
# will use Jobs to clean.
#
Expand Down Expand Up @@ -80,7 +85,7 @@ classes:

#
# Configure DaemonSet for provisioner.
#
#
daemonset:
#
# Defines the name of a Provisioner
Expand Down
16 changes: 15 additions & 1 deletion pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -107,6 +107,8 @@ type UserConfig struct {
UseNodeNameOnly bool
// LabelsForPV stores additional labels added to provisioned PVs
LabelsForPV map[string]string
// SetPVOwnerRef indicates if PVs should be dependents of the owner Node
SetPVOwnerRef bool
}

// MountConfig stores a configuration for discoverying a specific storageclass
Expand Down Expand Up @@ -165,6 +167,8 @@ type LocalPVConfig struct {
MountOptions []string
FsType *string
Labels map[string]string
SetPVOwnerRef bool
OwnerReference *metav1.OwnerReference
}

// BuildConfigFromFlags being defined to enable mocking during unit testing
Expand Down Expand Up @@ -199,6 +203,8 @@ type ProvisionerConfiguration struct {
// LabelsForPV could be used to specify additional labels that will be
// added to PVs created by static provisioner.
LabelsForPV map[string]string `json:"labelsForPV" yaml:"labelsForPV"`
// SetPVOwnerRef indicates if PVs should be dependents of the owner Node, default to false
SetPVOwnerRef bool `json:"setPVOwnerRef" yaml:"setPVOwnerRef"`
}

// CreateLocalPVSpec returns a PV spec that can be used for PV creation
Expand Down Expand Up @@ -230,11 +236,19 @@ func CreateLocalPVSpec(config *LocalPVConfig) *v1.PersistentVolume {
MountOptions: config.MountOptions,
},
}

if config.UseAlphaAPI {
pv.ObjectMeta.Annotations[AlphaStorageNodeAffinityAnnotation] = config.AffinityAnn
} else {
pv.Spec.NodeAffinity = config.NodeAffinity
}

if config.SetPVOwnerRef {
pv.ObjectMeta.OwnerReferences = []metav1.OwnerReference{
*config.OwnerReference,
}
}

return pv
}

Expand Down
14 changes: 12 additions & 2 deletions pkg/deleter/deleter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"sigs.k8s.io/sig-storage-local-static-provisioner/pkg/util"

batch_v1 "k8s.io/api/batch/v1"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand All @@ -42,6 +42,8 @@ import (
const (
testHostDir = "/mnt/disks"
testMountDir = "/discoveryPath"
testNodeName = "somehost.acme.com"
testNodeUID = "d9607e19-f88f-11e6-a518-42010a800195"
testStorageClass = "sc1"
)

Expand Down Expand Up @@ -558,6 +560,11 @@ func testSetup(t *testing.T, config *testConfig, cleanupCmd []string, useJobForC
Name: pvName,
HostPath: fakePath,
StorageClass: testStorageClass,
OwnerReference: &meta_v1.OwnerReference{
Kind: "Node",
Name: testNodeName,
UID: testNodeUID,
},
}
// If volume mode has been explicitly specified in the volume config, then explicitly set it in the PV.
switch vol.VolumeMode {
Expand Down Expand Up @@ -598,7 +605,10 @@ func testSetup(t *testing.T, config *testConfig, cleanupCmd []string, useJobForC
BlockCleanerCommand: cleanupCmd,
},
},
Node: &v1.Node{ObjectMeta: meta_v1.ObjectMeta{Name: "somehost.acme.com"}},
Node: &v1.Node{ObjectMeta: meta_v1.ObjectMeta{
Name: testNodeName,
UID: testNodeUID,
}},
UseJobForCleaning: useJobForCleaning,
JobContainerImage: containerImage,
Namespace: ns,
Expand Down
35 changes: 32 additions & 3 deletions pkg/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
storagev1listers "k8s.io/client-go/listers/storage/v1"
"k8s.io/client-go/tools/cache"
esUtil "sigs.k8s.io/sig-storage-lib-external-provisioner/util"
Expand All @@ -45,6 +46,7 @@ type Discoverer struct {
nodeAffinityAnn string
nodeAffinity *v1.VolumeNodeAffinity
classLister storagev1listers.StorageClassLister
ownerReference *metav1.OwnerReference
}

// NewDiscoverer creates a Discoverer object that will scan through
Expand Down Expand Up @@ -72,6 +74,12 @@ func NewDiscoverer(config *common.RuntimeConfig, cleanupTracker *deleter.Cleanup
labelMap[labelName] = labelValue
}

// Generate owner reference
ownerRef, err := generateOwnerReference(config.Node)
if err != nil {
return nil, fmt.Errorf("Failed to generate owner reference: %v", err)
}

if config.UseAlphaAPI {
nodeAffinity, err := generateNodeAffinity(config.Node)
if err != nil {
Expand All @@ -87,7 +95,8 @@ func NewDiscoverer(config *common.RuntimeConfig, cleanupTracker *deleter.Cleanup
Labels: labelMap,
CleanupTracker: cleanupTracker,
classLister: sharedInformer.Lister(),
nodeAffinityAnn: tmpAnnotations[common.AlphaStorageNodeAffinityAnnotation]}, nil
nodeAffinityAnn: tmpAnnotations[common.AlphaStorageNodeAffinityAnnotation],
ownerReference: ownerRef}, nil
}

volumeNodeAffinity, err := generateVolumeNodeAffinity(config.Node)
Expand All @@ -100,7 +109,25 @@ func NewDiscoverer(config *common.RuntimeConfig, cleanupTracker *deleter.Cleanup
Labels: labelMap,
CleanupTracker: cleanupTracker,
classLister: sharedInformer.Lister(),
nodeAffinity: volumeNodeAffinity}, nil
nodeAffinity: volumeNodeAffinity,
ownerReference: ownerRef}, nil
}

func generateOwnerReference(node *v1.Node) (*metav1.OwnerReference, error) {
if node.GetName() == "" {
return nil, fmt.Errorf("Node does not have name")
}

if node.GetUID() == "" {
return nil, fmt.Errorf("Node does not have UID")
}

return &metav1.OwnerReference{
Kind: "Node",
APIVersion: "v1",
Name: node.GetName(),
UID: node.UID,
}, nil
}

func generateNodeAffinity(node *v1.Node) (*v1.NodeAffinity, error) {
Expand Down Expand Up @@ -314,6 +341,8 @@ func (d *Discoverer) createPV(file, class string, reclaimPolicy v1.PersistentVol
VolumeMode: volMode,
Labels: d.Labels,
MountOptions: mountOptions,
SetPVOwnerRef: d.SetPVOwnerRef,
OwnerReference: d.ownerReference,
}

if d.UseAlphaAPI {
Expand Down
Loading

0 comments on commit 519978a

Please sign in to comment.