/
provisioner_blockdevice.go
148 lines (126 loc) · 4.98 KB
/
provisioner_blockdevice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Copyright 2019 The OpenEBS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package app
import (
"github.com/openebs/maya/pkg/alertlog"
"github.com/pkg/errors"
"k8s.io/klog"
pvController "sigs.k8s.io/sig-storage-lib-external-provisioner/controller"
//pvController "github.com/kubernetes-sigs/sig-storage-lib-external-provisioner/controller"
mconfig "github.com/openebs/maya/pkg/apis/openebs.io/v1alpha1"
mPV "github.com/openebs/maya/pkg/kubernetes/persistentvolume/v1alpha1"
v1 "k8s.io/api/core/v1"
//metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ProvisionBlockDevice is invoked by the Provisioner to create a Local PV
// with a Block Device
func (p *Provisioner) ProvisionBlockDevice(opts pvController.VolumeOptions, volumeConfig *VolumeConfig) (*v1.PersistentVolume, error) {
pvc := opts.PVC
nodeHostname := GetNodeHostname(opts.SelectedNode)
name := opts.PVName
capacity := opts.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
stgType := volumeConfig.GetStorageType()
fsType := volumeConfig.GetFSType()
//Extract the details to create a Block Device Claim
blkDevOpts := &HelperBlockDeviceOptions{
nodeHostname: nodeHostname,
name: name,
capacity: capacity.String(),
volumeMode: *opts.PVC.Spec.VolumeMode,
bdTagValue: volumeConfig.GetBDTagValue(),
}
path, blkPath, err := p.getBlockDevicePath(blkDevOpts)
if err != nil {
klog.Infof("Initialize volume %v failed: %v", name, err)
alertlog.Logger.Errorw("",
"eventcode", "local.pv.provision.failure",
"msg", "Failed to provision Local PV",
"rname", opts.PVName,
"reason", "Block device initialization failed",
"storagetype", stgType,
)
return nil, err
}
klog.Infof("Creating volume %v on %v at %v(%v)", name, nodeHostname, path, blkPath)
// Over-ride the path, with the blockPath, when path is empty.
if path == "" {
path = blkPath
klog.Infof("Using block device{%v} with fs{%v}", blkPath, fsType)
}
// It is possible that the HostPath doesn't already exist on the node.
// Set the Local PV to create it.
//hostPathType := v1.HostPathDirectoryOrCreate
// TODO initialize the Labels and annotations
// Use annotations to specify the context using which the PV was created.
volAnnotations := make(map[string]string)
volAnnotations[bdcStorageClassAnnotation] = blkDevOpts.bdcName
//fstype := casVolume.Spec.FSType
labels := make(map[string]string)
labels[string(mconfig.CASTypeKey)] = "local-" + stgType
//labels[string(v1alpha1.StorageClassKey)] = *className
//TODO Change the following to a builder pattern
pvObjBuilder := mPV.NewBuilder().
WithName(name).
WithLabels(labels).
WithAnnotations(volAnnotations).
WithReclaimPolicy(opts.PersistentVolumeReclaimPolicy).
WithAccessModes(pvc.Spec.AccessModes).
WithCapacityQty(pvc.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]).
WithLocalHostPathFormat(path, fsType).
WithNodeAffinity(nodeHostname)
// If volumeMode set to "Block", then provide the appropriate volumeMode, to pvObj
if *opts.PVC.Spec.VolumeMode == v1.PersistentVolumeBlock {
pvObjBuilder.WithVolumeMode(v1.PersistentVolumeBlock)
}
//Build the pvObject
pvObj, err := pvObjBuilder.Build()
if err != nil {
alertlog.Logger.Errorw("",
"eventcode", "local.pv.provision.failure",
"msg", "Failed to provision Local PV",
"rname", opts.PVName,
"reason", "Building volume failed",
"storagetype", stgType,
)
return nil, err
}
alertlog.Logger.Infow("",
"eventcode", "local.pv.provision.success",
"msg", "Successfully provisioned Local PV",
"rname", opts.PVName,
"storagetype", stgType,
)
return pvObj, nil
}
// DeleteBlockDevice is invoked by the PVC controller to perform clean-up
// activities before deleteing the PV object. If reclaim policy is
// set to not-retain, then this function will delete the associated BDC
func (p *Provisioner) DeleteBlockDevice(pv *v1.PersistentVolume) (err error) {
defer func() {
err = errors.Wrapf(err, "failed to delete volume %v", pv.Name)
}()
blkDevOpts := &HelperBlockDeviceOptions{
name: pv.Name,
}
//Determine if a BDC is set on the PV and save it to BlockDeviceOptions
blkDevOpts.setBlockDeviceClaimFromPV(pv)
//Initiate clean up only when reclaim policy is not retain.
//TODO: this part of the code could be eliminated by setting up
// BDC owner reference to PVC.
klog.Infof("Release the Block Device Claim %v for PV %v", blkDevOpts.bdcName, pv.Name)
if err := p.deleteBlockDeviceClaim(blkDevOpts); err != nil {
klog.Infof("clean up volume %v failed: %v", pv.Name, err)
return err
}
return nil
}