Skip to content

Commit 3305fcc

Browse files
authored
Merge pull request kubernetes#126777 from carlory/fix-image-plugin
Fix GetVolumeName for image volumeplugin
2 parents 3a4c2a0 + d9e2787 commit 3305fcc

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

pkg/volume/image/image.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package image
1818

1919
import (
20+
"fmt"
21+
2022
v1 "k8s.io/api/core/v1"
2123
"k8s.io/apimachinery/pkg/types"
2224
"k8s.io/kubernetes/pkg/volume"
@@ -28,7 +30,6 @@ import (
2830
// feature "ImageVolume"
2931
// See: https://kep.k8s.io/4639
3032
type imagePlugin struct {
31-
spec *volume.Spec
3233
volume.MetricsNil
3334
}
3435

@@ -44,9 +45,16 @@ func ProbeVolumePlugins() []volume.VolumePlugin {
4445
return []volume.VolumePlugin{p}
4546
}
4647

47-
func (o *imagePlugin) Init(volume.VolumeHost) error { return nil }
48-
func (o *imagePlugin) GetPluginName() string { return pluginName }
49-
func (o *imagePlugin) GetVolumeName(spec *volume.Spec) (string, error) { return o.spec.Name(), nil }
48+
func (o *imagePlugin) Init(volume.VolumeHost) error { return nil }
49+
func (o *imagePlugin) GetPluginName() string { return pluginName }
50+
func (o *imagePlugin) GetVolumeName(spec *volume.Spec) (string, error) {
51+
volumeSource := getVolumeSource(spec)
52+
if volumeSource == nil {
53+
return "", fmt.Errorf("the volumeSpec does not reference an Image volume type")
54+
}
55+
56+
return volumeSource.Reference, nil
57+
}
5058

5159
func (o *imagePlugin) CanSupport(spec *volume.Spec) bool {
5260
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
6169
}
6270

6371
func (o *imagePlugin) ConstructVolumeSpec(volumeName, mountPath string) (volume.ReconstructedVolume, error) {
64-
return volume.ReconstructedVolume{Spec: o.spec}, nil
72+
return volume.ReconstructedVolume{}, nil
6573
}
6674

6775
func (o *imagePlugin) GetAttributes() volume.Attributes {
@@ -81,3 +89,10 @@ func (o *imagePlugin) SupportsMountOption() bool
8189
func (o *imagePlugin) SupportsSELinuxContextMount(spec *volume.Spec) (bool, error) { return false, nil }
8290
func (o *imagePlugin) TearDown() error { return nil }
8391
func (o *imagePlugin) TearDownAt(string) error { return nil }
92+
93+
func getVolumeSource(spec *volume.Spec) *v1.ImageVolumeSource {
94+
if spec == nil || spec.Volume == nil {
95+
return nil
96+
}
97+
return spec.Volume.Image
98+
}

pkg/volume/image/image_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package image
18+
19+
import (
20+
"testing"
21+
22+
v1 "k8s.io/api/core/v1"
23+
"k8s.io/kubernetes/pkg/volume"
24+
)
25+
26+
func TestCanSupport(t *testing.T) {
27+
pluginMgr := volume.VolumePluginMgr{}
28+
err := pluginMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, nil /* host */)
29+
if err != nil {
30+
t.Fatalf("Failed to init plugins: %v", err)
31+
}
32+
33+
plugin, err := pluginMgr.FindPluginByName(pluginName)
34+
if err != nil {
35+
t.Fatal("Can't find the plugin by name")
36+
}
37+
if plugin.GetPluginName() != pluginName {
38+
t.Errorf("Wrong name: %s", plugin.GetPluginName())
39+
}
40+
if !plugin.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{Image: &v1.ImageVolumeSource{Reference: ""}}}}) {
41+
t.Errorf("Expected true")
42+
}
43+
if plugin.CanSupport(&volume.Spec{}) {
44+
t.Errorf("Expected false")
45+
}
46+
}

0 commit comments

Comments
 (0)