Skip to content

Commit

Permalink
change GetFullQualifiedPluginNameForVolume to distinuish different dr…
Browse files Browse the repository at this point in the history
…ivers

(Merged upstream in 1.19 as kubernetes#96477)

Bug: 172255810
Change-Id: Iefc0ad09fd82db35ac3f745e6c7358ceb83ff422
(cherry picked from commit 32bc48488074c9a0d6f49dfb1f500e3385e33d1c)
  • Loading branch information
JornShen authored and mattcary committed Nov 17, 2020
1 parent 8f3003d commit f979aa3
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pkg/volume/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ go_library(
"//pkg/api/legacyscheme:go_default_library",
"//pkg/api/v1/pod:go_default_library",
"//pkg/apis/core/v1/helper:go_default_library",
"//pkg/features:go_default_library",
"//pkg/securitycontext:go_default_library",
"//pkg/util/resizefs:go_default_library",
"//pkg/volume:go_default_library",
Expand All @@ -38,6 +39,7 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/strategicpatch:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
"//staging/src/k8s.io/component-base/metrics:go_default_library",
"//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library",
Expand All @@ -54,6 +56,7 @@ go_test(
"atomic_writer_test.go",
"attach_limit_test.go",
"device_util_linux_test.go",
"metrics_test.go",
"nested_volumes_test.go",
"resize_util_test.go",
"util_test.go",
Expand Down
11 changes: 9 additions & 2 deletions pkg/volume/util/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
"fmt"
"time"

utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"
"k8s.io/kubernetes/pkg/features"
"k8s.io/kubernetes/pkg/volume"
)

Expand Down Expand Up @@ -115,8 +117,13 @@ func OperationCompleteHook(plugin, operationName string) func(*error) {
// between metrics emitted for CSI volumes which may be handled by different
// CSI plugin drivers.
func GetFullQualifiedPluginNameForVolume(pluginName string, spec *volume.Spec) string {
if spec != nil && spec.PersistentVolume != nil && spec.PersistentVolume.Spec.CSI != nil {
return fmt.Sprintf("%s:%s", pluginName, spec.PersistentVolume.Spec.CSI.Driver)
if spec != nil {
if spec.Volume != nil && spec.Volume.CSI != nil && utilfeature.DefaultFeatureGate.Enabled(features.CSIInlineVolume) {
return fmt.Sprintf("%s:%s", pluginName, spec.Volume.CSI.Driver)
}
if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.CSI != nil {
return fmt.Sprintf("%s:%s", pluginName, spec.PersistentVolume.Spec.CSI.Driver)
}
}
return pluginName
}
Expand Down
91 changes: 91 additions & 0 deletions pkg/volume/util/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright 2020 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 util

import (
"fmt"
"testing"

v1 "k8s.io/api/core/v1"
"k8s.io/kubernetes/pkg/volume"
)

func TestGetFullQualifiedPluginNameForVolume(t *testing.T) {
var (
fakePluginName = "kubernetes.io/fakePlugin"
fakeInlineCSIDriverName = "fake.inline.csi.driver"
fakeCSIDriverName = "fake.csi.driver"
)

testCase := []struct {
name string
pluginName string
spec *volume.Spec
wantFullName string
}{
{
name: "get full qualified plugin name without volume spec",
pluginName: fakePluginName,
spec: nil,
wantFullName: fakePluginName,
},
{
name: "get full qualified plugin name without using CSI plugin",
pluginName: fakePluginName,
spec: &volume.Spec{},
wantFullName: fakePluginName,
},
{
name: "get full qualified plugin name with CSI ephemeral volume",
pluginName: fakePluginName,
spec: &volume.Spec{
Volume: &v1.Volume{
VolumeSource: v1.VolumeSource{
CSI: &v1.CSIVolumeSource{
Driver: fakeInlineCSIDriverName,
},
},
},
},
wantFullName: fmt.Sprintf("%s:%s", fakePluginName, fakeInlineCSIDriverName),
},
{
name: "get full qualified plugin name with CSI PV",
pluginName: fakePluginName,
spec: &volume.Spec{
PersistentVolume: &v1.PersistentVolume{
Spec: v1.PersistentVolumeSpec{
PersistentVolumeSource: v1.PersistentVolumeSource{
CSI: &v1.CSIPersistentVolumeSource{
Driver: fakeCSIDriverName,
},
},
},
},
},
wantFullName: fmt.Sprintf("%s:%s", fakePluginName, fakeCSIDriverName),
},
}

for _, test := range testCase {
t.Run(test.name, func(t *testing.T) {
if fullPluginName := GetFullQualifiedPluginNameForVolume(test.pluginName, test.spec); fullPluginName != test.wantFullName {
t.Errorf("Case name: %s, GetFullQualifiedPluginNameForVolume, pluginName:%s, spec: %v, return:%s, want:%s", test.name, test.pluginName, test.spec, fullPluginName, test.wantFullName)
}
})
}
}

0 comments on commit f979aa3

Please sign in to comment.