Merge pull request #126777 from carlory/fix-image-plugin

Fix GetVolumeName for image volumeplugin
This commit is contained in:
Kubernetes Prow Robot 2025-01-20 08:46:36 -08:00 committed by GitHub
commit 3305fcccf7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 66 additions and 5 deletions

View File

@ -17,6 +17,8 @@ limitations under the License.
package image
import (
"fmt"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/kubernetes/pkg/volume"
@ -28,7 +30,6 @@ import (
// feature "ImageVolume"
// See: https://kep.k8s.io/4639
type imagePlugin struct {
spec *volume.Spec
volume.MetricsNil
}
@ -44,9 +45,16 @@ func ProbeVolumePlugins() []volume.VolumePlugin {
return []volume.VolumePlugin{p}
}
func (o *imagePlugin) Init(volume.VolumeHost) error { return nil }
func (o *imagePlugin) GetPluginName() string { return pluginName }
func (o *imagePlugin) GetVolumeName(spec *volume.Spec) (string, error) { return o.spec.Name(), nil }
func (o *imagePlugin) Init(volume.VolumeHost) error { return nil }
func (o *imagePlugin) GetPluginName() string { return pluginName }
func (o *imagePlugin) GetVolumeName(spec *volume.Spec) (string, error) {
volumeSource := getVolumeSource(spec)
if volumeSource == nil {
return "", fmt.Errorf("the volumeSpec does not reference an Image volume type")
}
return volumeSource.Reference, nil
}
func (o *imagePlugin) CanSupport(spec *volume.Spec) bool {
return spec != nil && spec.Volume != nil && spec.Volume.Image != nil
@ -61,7 +69,7 @@ func (o *imagePlugin) NewUnmounter(name string, podUID types.UID) (volume.Unmoun
}
func (o *imagePlugin) ConstructVolumeSpec(volumeName, mountPath string) (volume.ReconstructedVolume, error) {
return volume.ReconstructedVolume{Spec: o.spec}, nil
return volume.ReconstructedVolume{}, nil
}
func (o *imagePlugin) GetAttributes() volume.Attributes {
@ -81,3 +89,10 @@ func (o *imagePlugin) SupportsMountOption() bool
func (o *imagePlugin) SupportsSELinuxContextMount(spec *volume.Spec) (bool, error) { return false, nil }
func (o *imagePlugin) TearDown() error { return nil }
func (o *imagePlugin) TearDownAt(string) error { return nil }
func getVolumeSource(spec *volume.Spec) *v1.ImageVolumeSource {
if spec == nil || spec.Volume == nil {
return nil
}
return spec.Volume.Image
}

View File

@ -0,0 +1,46 @@
/*
Copyright 2024 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 image
import (
"testing"
v1 "k8s.io/api/core/v1"
"k8s.io/kubernetes/pkg/volume"
)
func TestCanSupport(t *testing.T) {
pluginMgr := volume.VolumePluginMgr{}
err := pluginMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, nil /* host */)
if err != nil {
t.Fatalf("Failed to init plugins: %v", err)
}
plugin, err := pluginMgr.FindPluginByName(pluginName)
if err != nil {
t.Fatal("Can't find the plugin by name")
}
if plugin.GetPluginName() != pluginName {
t.Errorf("Wrong name: %s", plugin.GetPluginName())
}
if !plugin.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{Image: &v1.ImageVolumeSource{Reference: ""}}}}) {
t.Errorf("Expected true")
}
if plugin.CanSupport(&volume.Spec{}) {
t.Errorf("Expected false")
}
}