Skip to content

Commit b531385

Browse files
Matthew Wongironcladlou
Matthew Wong
authored andcommitted
UPSTREAM: 50843: FlexVolume: Add ability to control 'SupportsSELinux' during driver's init phase
:100644 100644 86644fc... 0267fb8... M pkg/volume/flexvolume/driver-call.go :100644 100644 a8586b4... a3996d4... M pkg/volume/flexvolume/mounter-defaults.go :100644 100644 d79c63c... c4e1e4b... M pkg/volume/flexvolume/plugin.go
1 parent 7c3ffec commit b531385

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

pkg/volume/flexvolume/driver-call.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ const (
5959

6060
optionKeyServiceAccountName = "kubernetes.io/serviceAccount.name"
6161

62-
attachCapability = "attach"
62+
attachCapability = "attach"
63+
selinuxRelabelCapability = "selinuxRelabel"
6364
)
6465

6566
const (
@@ -82,6 +83,11 @@ type DriverCall struct {
8283
args []string
8384
}
8485

86+
type driverCapabilities struct {
87+
attach bool
88+
selinuxRelabel bool
89+
}
90+
8591
func (plugin *flexVolumePlugin) NewDriverCall(command string) *DriverCall {
8692
return plugin.NewDriverCallWithTimeout(command, 0)
8793
}
@@ -235,3 +241,23 @@ func handleCmdResponse(cmd string, output []byte) (*DriverStatus, error) {
235241

236242
return &status, nil
237243
}
244+
245+
// getDriverCapabilities returns the reported capabilities as returned by driver's init() function
246+
func (ds *DriverStatus) getDriverCapabilities() *driverCapabilities {
247+
driverCaps := &driverCapabilities{
248+
attach: true,
249+
selinuxRelabel: true,
250+
}
251+
252+
// Check if driver supports SELinux Relabeling of mounted volume
253+
if dcap, ok := ds.Capabilities[selinuxRelabelCapability]; ok {
254+
driverCaps.selinuxRelabel = dcap
255+
}
256+
257+
// Check whether the plugin is attachable.
258+
if dcap, ok := ds.Capabilities[attachCapability]; ok {
259+
driverCaps.attach = dcap
260+
}
261+
262+
return driverCaps
263+
}

pkg/volume/flexvolume/mounter-defaults.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (f *mounterDefaults) GetAttributes() volume.Attributes {
4747
return volume.Attributes{
4848
ReadOnly: f.readOnly,
4949
Managed: !f.readOnly,
50-
SupportsSELinux: true,
50+
SupportsSELinux: f.flexVolume.plugin.capabilities.selinuxRelabel,
5151
}
5252
}
5353

pkg/volume/flexvolume/plugin.go

+12-25
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type flexVolumePlugin struct {
4242
runner exec.Interface
4343

4444
sync.Mutex
45+
capabilities *driverCapabilities
4546
unsupportedCommands []string
4647
}
4748

@@ -64,44 +65,30 @@ func NewFlexVolumePlugin(pluginDir, name string) (volume.VolumePlugin, error) {
6465
unsupportedCommands: []string{},
6566
}
6667

67-
// Check whether the plugin is attachable.
68-
ok, err := isAttachable(flexPlugin)
68+
// Retrieve driver reported capabilities
69+
call := flexPlugin.NewDriverCall(initCmd)
70+
ds, err := call.Run()
6971
if err != nil {
7072
return nil, err
7173
}
7274

73-
if ok {
74-
// Plugin supports attach/detach, so return flexVolumeAttachablePlugin
75+
driverCaps := ds.getDriverCapabilities()
76+
flexPlugin.capabilities = driverCaps
77+
78+
// Check whether the plugin is attachable.
79+
if driverCaps.attach {
80+
// Plugin supports attach/detach by default, so return flexVolumeAttachablePlugin
7581
return &flexVolumeAttachablePlugin{flexVolumePlugin: flexPlugin}, nil
7682
} else {
7783
return flexPlugin, nil
7884
}
7985
}
8086

81-
func isAttachable(plugin *flexVolumePlugin) (bool, error) {
82-
call := plugin.NewDriverCall(initCmd)
83-
res, err := call.Run()
84-
if err != nil {
85-
return false, err
86-
}
87-
88-
// By default all plugins are attachable, unless they report otherwise.
89-
cap, ok := res.Capabilities[attachCapability]
90-
if ok {
91-
// cap is false, so plugin does not support attach/detach calls.
92-
return cap, nil
93-
}
94-
95-
return true, nil
96-
}
97-
9887
// Init is part of the volume.VolumePlugin interface.
9988
func (plugin *flexVolumePlugin) Init(host volume.VolumeHost) error {
10089
plugin.host = host
101-
// call the init script
102-
call := plugin.NewDriverCall(initCmd)
103-
_, err := call.Run()
104-
return err
90+
// Hardwired 'success' as any errors from calling init() will be caught by NewFlexVolumePlugin()
91+
return nil
10592
}
10693

10794
func (plugin *flexVolumePlugin) getExecutable() string {

0 commit comments

Comments
 (0)