-
Notifications
You must be signed in to change notification settings - Fork 113
/
osdiskexcludefilter.go
137 lines (121 loc) · 4.52 KB
/
osdiskexcludefilter.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
/*
Copyright 2018 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 filter
import (
"strings"
"github.com/openebs/node-disk-manager/blockdevice"
"github.com/openebs/node-disk-manager/cmd/ndm_daemonset/controller"
"github.com/openebs/node-disk-manager/pkg/mount"
"github.com/openebs/node-disk-manager/pkg/util"
"k8s.io/klog"
)
const (
osDiskExcludeFilterKey = "os-disk-exclude-filter"
)
var (
defaultMountFilePath = "/proc/self/mounts"
mountPoints = []string{"/", "/etc/hosts"}
hostMountFilePath = "/host/proc/1/mounts" // hostMountFilePath is the file path mounted inside container
oSDiskExcludeFilterName = "os disk exclude filter" // filter name
oSDiskExcludeFilterState = defaultEnabled // filter state
)
// oSDiskExcludeFilterRegister contains registration process of oSDiskExcludeFilter
var oSDiskExcludeFilterRegister = func() {
ctrl := <-controller.ControllerBroadcastChannel
if ctrl == nil {
return
}
if ctrl.NDMConfig != nil {
for _, filterConfig := range ctrl.NDMConfig.FilterConfigs {
if filterConfig.Key == osDiskExcludeFilterKey {
oSDiskExcludeFilterName = filterConfig.Name
oSDiskExcludeFilterState = util.CheckTruthy(filterConfig.State)
mountPoints = strings.Split(filterConfig.Exclude, ",")
break
}
}
}
var fi controller.FilterInterface = newNonOsDiskFilter(ctrl)
newRegisterFilter := ®isterFilter{
name: oSDiskExcludeFilterName,
state: oSDiskExcludeFilterState,
fi: fi,
controller: ctrl,
}
newRegisterFilter.register()
}
// oSDiskExcludeFilter controller and path of os disk
type oSDiskExcludeFilter struct {
controller *controller.Controller
excludeDevPaths []string
}
// newOsDiskFilter returns new pointer osDiskFilter
func newNonOsDiskFilter(ctrl *controller.Controller) *oSDiskExcludeFilter {
return &oSDiskExcludeFilter{
controller: ctrl,
}
}
// Start set os disk devPath in nonOsDiskFilter pointer
func (odf *oSDiskExcludeFilter) Start() {
/*
process1 check for mentioned mountpoint in host's /proc/1/mounts file
host's /proc/1/mounts file mounted inside container it checks for
every mentioned mountpoints if it is able to get parent disk's devpath
it adds it in Controller struct and make isOsDiskFilterSet true
*/
for _, mountPoint := range mountPoints {
mountPointUtil := mount.NewMountUtil(hostMountFilePath, "", mountPoint)
if devPath, err := mountPointUtil.GetDiskPath(); err != nil {
klog.Errorf("unable to configure os disk filter for mountpoint: %s, error: %v", mountPoint, err)
} else {
odf.excludeDevPaths = append(odf.excludeDevPaths, devPath)
}
}
/*
process2 check for mountpoints in /proc/self/mounts file if it is able to get
disk's path of it adds it in Controller struct and make isOsDiskFilterSet true
*/
for _, mountPoint := range mountPoints {
mountPointUtil := mount.NewMountUtil(defaultMountFilePath, "", mountPoint)
if devPath, err := mountPointUtil.GetDiskPath(); err != nil {
klog.Errorf("unable to configure os disk filter for mountpoint: %s, error: %v", mountPoint, err)
} else {
odf.excludeDevPaths = append(odf.excludeDevPaths, devPath)
}
}
}
// Include contains nothing by default it returns false
func (odf *oSDiskExcludeFilter) Include(blockDevice *blockdevice.BlockDevice) bool {
return true
}
// Exclude returns true if disk devpath does not match with excludeDevPaths
func (odf *oSDiskExcludeFilter) Exclude(blockDevice *blockdevice.BlockDevice) bool {
// The partitionRegex is chosen depending on whether the device uses
// the p[0-9] partition naming structure or not.
var partitionRegex string
for _, excludeDevPath := range odf.excludeDevPaths {
if util.IsMatchRegex(".+[0-9]+$", excludeDevPath) {
// matches loop0, loop0p1, nvme3n0p1
partitionRegex = "(p[0-9]+)?$"
} else {
// matches sda, sda1
partitionRegex = "[0-9]*$"
}
regex := "^" + excludeDevPath + partitionRegex
klog.Infof("applying os-filter regex %s on %s", regex, blockDevice.DevPath)
if util.IsMatchRegex(regex, blockDevice.DevPath) {
return false
}
}
return true
}