mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 14:07:14 +00:00
refactor(flexvolume): simplify capabilities handling
This commit is contained in:
parent
470eb922ab
commit
22d5e4810b
@ -42,6 +42,7 @@ go_test(
|
|||||||
"attacher_test.go",
|
"attacher_test.go",
|
||||||
"common_test.go",
|
"common_test.go",
|
||||||
"detacher_test.go",
|
"detacher_test.go",
|
||||||
|
"driver-call_test.go",
|
||||||
"flexvolume_test.go",
|
"flexvolume_test.go",
|
||||||
"mounter_test.go",
|
"mounter_test.go",
|
||||||
"plugin_test.go",
|
"plugin_test.go",
|
||||||
|
@ -58,9 +58,6 @@ const (
|
|||||||
optionKeyPodUID = "kubernetes.io/pod.uid"
|
optionKeyPodUID = "kubernetes.io/pod.uid"
|
||||||
|
|
||||||
optionKeyServiceAccountName = "kubernetes.io/serviceAccount.name"
|
optionKeyServiceAccountName = "kubernetes.io/serviceAccount.name"
|
||||||
|
|
||||||
attachCapability = "attach"
|
|
||||||
selinuxRelabelCapability = "selinuxRelabel"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -83,11 +80,6 @@ type DriverCall struct {
|
|||||||
args []string
|
args []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type driverCapabilities struct {
|
|
||||||
attach bool
|
|
||||||
selinuxRelabel bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (plugin *flexVolumePlugin) NewDriverCall(command string) *DriverCall {
|
func (plugin *flexVolumePlugin) NewDriverCall(command string) *DriverCall {
|
||||||
return plugin.NewDriverCallWithTimeout(command, 0)
|
return plugin.NewDriverCallWithTimeout(command, 0)
|
||||||
}
|
}
|
||||||
@ -210,7 +202,19 @@ type DriverStatus struct {
|
|||||||
// Returns capabilities of the driver.
|
// Returns capabilities of the driver.
|
||||||
// By default we assume all the capabilities are supported.
|
// By default we assume all the capabilities are supported.
|
||||||
// If the plugin does not support a capability, it can return false for that capability.
|
// If the plugin does not support a capability, it can return false for that capability.
|
||||||
Capabilities map[string]bool
|
Capabilities *DriverCapabilities `json:",omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DriverCapabilities struct {
|
||||||
|
Attach bool `json:"attach"`
|
||||||
|
SELinuxRelabel bool `json:"selinuxRelabel"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultCapabilities() *DriverCapabilities {
|
||||||
|
return &DriverCapabilities{
|
||||||
|
Attach: true,
|
||||||
|
SELinuxRelabel: true,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// isCmdNotSupportedErr checks if the error corresponds to command not supported by
|
// isCmdNotSupportedErr checks if the error corresponds to command not supported by
|
||||||
@ -226,7 +230,9 @@ func isCmdNotSupportedErr(err error) bool {
|
|||||||
// handleCmdResponse processes the command output and returns the appropriate
|
// handleCmdResponse processes the command output and returns the appropriate
|
||||||
// error code or message.
|
// error code or message.
|
||||||
func handleCmdResponse(cmd string, output []byte) (*DriverStatus, error) {
|
func handleCmdResponse(cmd string, output []byte) (*DriverStatus, error) {
|
||||||
var status DriverStatus
|
status := DriverStatus{
|
||||||
|
Capabilities: defaultCapabilities(),
|
||||||
|
}
|
||||||
if err := json.Unmarshal(output, &status); err != nil {
|
if err := json.Unmarshal(output, &status); err != nil {
|
||||||
glog.Errorf("Failed to unmarshal output for command: %s, output: %q, error: %s", cmd, string(output), err.Error())
|
glog.Errorf("Failed to unmarshal output for command: %s, output: %q, error: %s", cmd, string(output), err.Error())
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -241,23 +247,3 @@ func handleCmdResponse(cmd string, output []byte) (*DriverStatus, error) {
|
|||||||
|
|
||||||
return &status, nil
|
return &status, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getDriverCapabilities returns the reported capabilities as returned by driver's init() function
|
|
||||||
func (ds *DriverStatus) getDriverCapabilities() *driverCapabilities {
|
|
||||||
driverCaps := &driverCapabilities{
|
|
||||||
attach: true,
|
|
||||||
selinuxRelabel: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if driver supports SELinux Relabeling of mounted volume
|
|
||||||
if dcap, ok := ds.Capabilities[selinuxRelabelCapability]; ok {
|
|
||||||
driverCaps.selinuxRelabel = dcap
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check whether the plugin is attachable.
|
|
||||||
if dcap, ok := ds.Capabilities[attachCapability]; ok {
|
|
||||||
driverCaps.attach = dcap
|
|
||||||
}
|
|
||||||
|
|
||||||
return driverCaps
|
|
||||||
}
|
|
||||||
|
32
pkg/volume/flexvolume/driver-call_test.go
Normal file
32
pkg/volume/flexvolume/driver-call_test.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2017 The Kubernetes 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 flexvolume
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHandleResponseDefaults(t *testing.T) {
|
||||||
|
ds, err := handleCmdResponse("test", []byte(`{"status": "Success"}`))
|
||||||
|
if err != nil {
|
||||||
|
t.Error("error: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if *ds.Capabilities != *defaultCapabilities() {
|
||||||
|
t.Error("wrong default capabilities: ", *ds.Capabilities)
|
||||||
|
}
|
||||||
|
}
|
@ -47,7 +47,7 @@ func (f *mounterDefaults) GetAttributes() volume.Attributes {
|
|||||||
return volume.Attributes{
|
return volume.Attributes{
|
||||||
ReadOnly: f.readOnly,
|
ReadOnly: f.readOnly,
|
||||||
Managed: !f.readOnly,
|
Managed: !f.readOnly,
|
||||||
SupportsSELinux: f.flexVolume.plugin.capabilities.selinuxRelabel,
|
SupportsSELinux: f.flexVolume.plugin.capabilities.SELinuxRelabel,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,8 +42,8 @@ type flexVolumePlugin struct {
|
|||||||
runner exec.Interface
|
runner exec.Interface
|
||||||
|
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
capabilities *driverCapabilities
|
|
||||||
unsupportedCommands []string
|
unsupportedCommands []string
|
||||||
|
capabilities DriverCapabilities
|
||||||
}
|
}
|
||||||
|
|
||||||
type flexVolumeAttachablePlugin struct {
|
type flexVolumeAttachablePlugin struct {
|
||||||
@ -65,19 +65,16 @@ func NewFlexVolumePlugin(pluginDir, name string) (volume.VolumePlugin, error) {
|
|||||||
unsupportedCommands: []string{},
|
unsupportedCommands: []string{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve driver reported capabilities
|
// Initialize the plugin and probe the capabilities
|
||||||
call := flexPlugin.NewDriverCall(initCmd)
|
call := flexPlugin.NewDriverCall(initCmd)
|
||||||
ds, err := call.Run()
|
ds, err := call.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
flexPlugin.capabilities = *ds.Capabilities
|
||||||
|
|
||||||
driverCaps := ds.getDriverCapabilities()
|
if flexPlugin.capabilities.Attach {
|
||||||
flexPlugin.capabilities = driverCaps
|
// Plugin supports attach/detach, so return flexVolumeAttachablePlugin
|
||||||
|
|
||||||
// Check whether the plugin is attachable.
|
|
||||||
if driverCaps.attach {
|
|
||||||
// Plugin supports attach/detach by default, so return flexVolumeAttachablePlugin
|
|
||||||
return &flexVolumeAttachablePlugin{flexVolumePlugin: flexPlugin}, nil
|
return &flexVolumeAttachablePlugin{flexVolumePlugin: flexPlugin}, nil
|
||||||
} else {
|
} else {
|
||||||
return flexPlugin, nil
|
return flexPlugin, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user