mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 04:06:03 +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",
|
||||
"common_test.go",
|
||||
"detacher_test.go",
|
||||
"driver-call_test.go",
|
||||
"flexvolume_test.go",
|
||||
"mounter_test.go",
|
||||
"plugin_test.go",
|
||||
|
@ -58,9 +58,6 @@ const (
|
||||
optionKeyPodUID = "kubernetes.io/pod.uid"
|
||||
|
||||
optionKeyServiceAccountName = "kubernetes.io/serviceAccount.name"
|
||||
|
||||
attachCapability = "attach"
|
||||
selinuxRelabelCapability = "selinuxRelabel"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -83,11 +80,6 @@ type DriverCall struct {
|
||||
args []string
|
||||
}
|
||||
|
||||
type driverCapabilities struct {
|
||||
attach bool
|
||||
selinuxRelabel bool
|
||||
}
|
||||
|
||||
func (plugin *flexVolumePlugin) NewDriverCall(command string) *DriverCall {
|
||||
return plugin.NewDriverCallWithTimeout(command, 0)
|
||||
}
|
||||
@ -210,7 +202,19 @@ type DriverStatus struct {
|
||||
// Returns capabilities of the driver.
|
||||
// By default we assume all the capabilities are supported.
|
||||
// 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
|
||||
@ -226,7 +230,9 @@ func isCmdNotSupportedErr(err error) bool {
|
||||
// handleCmdResponse processes the command output and returns the appropriate
|
||||
// error code or message.
|
||||
func handleCmdResponse(cmd string, output []byte) (*DriverStatus, error) {
|
||||
var status DriverStatus
|
||||
status := DriverStatus{
|
||||
Capabilities: defaultCapabilities(),
|
||||
}
|
||||
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())
|
||||
return nil, err
|
||||
@ -241,23 +247,3 @@ func handleCmdResponse(cmd string, output []byte) (*DriverStatus, error) {
|
||||
|
||||
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{
|
||||
ReadOnly: 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
|
||||
|
||||
sync.Mutex
|
||||
capabilities *driverCapabilities
|
||||
unsupportedCommands []string
|
||||
capabilities DriverCapabilities
|
||||
}
|
||||
|
||||
type flexVolumeAttachablePlugin struct {
|
||||
@ -65,19 +65,16 @@ func NewFlexVolumePlugin(pluginDir, name string) (volume.VolumePlugin, error) {
|
||||
unsupportedCommands: []string{},
|
||||
}
|
||||
|
||||
// Retrieve driver reported capabilities
|
||||
// Initialize the plugin and probe the capabilities
|
||||
call := flexPlugin.NewDriverCall(initCmd)
|
||||
ds, err := call.Run()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
flexPlugin.capabilities = *ds.Capabilities
|
||||
|
||||
driverCaps := ds.getDriverCapabilities()
|
||||
flexPlugin.capabilities = driverCaps
|
||||
|
||||
// Check whether the plugin is attachable.
|
||||
if driverCaps.attach {
|
||||
// Plugin supports attach/detach by default, so return flexVolumeAttachablePlugin
|
||||
if flexPlugin.capabilities.Attach {
|
||||
// Plugin supports attach/detach, so return flexVolumeAttachablePlugin
|
||||
return &flexVolumeAttachablePlugin{flexVolumePlugin: flexPlugin}, nil
|
||||
} else {
|
||||
return flexPlugin, nil
|
||||
|
Loading…
Reference in New Issue
Block a user