Skip to content

Commit 979af1f

Browse files
committed
feat: Add migrated label to metrics
1 parent c68077f commit 979af1f

File tree

10 files changed

+100
-9
lines changed

10 files changed

+100
-9
lines changed

cmd/csi-resizer/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/kubernetes-csi/external-resizer/pkg/controller"
3737
"github.com/kubernetes-csi/external-resizer/pkg/resizer"
3838
"github.com/kubernetes-csi/external-resizer/pkg/util"
39+
csitrans "k8s.io/csi-translation-lib"
3940

4041
"k8s.io/apimachinery/pkg/util/wait"
4142
"k8s.io/client-go/informers"
@@ -127,6 +128,17 @@ func main() {
127128
}
128129
klog.V(2).Infof("CSI driver name: %q", driverName)
129130

131+
translator := csitrans.New()
132+
if translator.IsMigratedCSIDriverByName(driverName) {
133+
metricsManager = metrics.NewCSIMetricsManagerWithOptions(driverName, metrics.WithMigration())
134+
migratedCsiClient, err := csi.New(*csiAddress, *timeout, metricsManager)
135+
if err != nil {
136+
klog.Fatal(err.Error())
137+
}
138+
csiClient.CloseConnection()
139+
csiClient = migratedCsiClient
140+
}
141+
130142
csiResizer, err := resizer.NewResizerFromClient(
131143
csiClient,
132144
*timeout,

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/googleapis/gnostic v0.5.3 // indirect
1111
github.com/hashicorp/golang-lru v0.5.4 // indirect
1212
github.com/imdario/mergo v0.3.11 // indirect
13-
github.com/kubernetes-csi/csi-lib-utils v0.9.0
13+
github.com/kubernetes-csi/csi-lib-utils v0.9.1
1414
github.com/prometheus/client_golang v1.9.0 // indirect
1515
golang.org/x/crypto v0.0.0-20201217014255-9d1352758620 // indirect
1616
golang.org/x/net v0.0.0-20201216054612-986b41b23924 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
303303
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
304304
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
305305
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
306-
github.com/kubernetes-csi/csi-lib-utils v0.9.0 h1:TbuDmxoVqM+fvVkzG/7sShyX/8jUln0ElLHuETcsQJI=
307-
github.com/kubernetes-csi/csi-lib-utils v0.9.0/go.mod h1:8E2jVUX9j3QgspwHXa6LwyN7IHQDjW9jX3kwoWnSC+M=
306+
github.com/kubernetes-csi/csi-lib-utils v0.9.1 h1:sGq6ifVujfMSkfTsMZip44Ttv8SDXvsBlFk9GdYl/b8=
307+
github.com/kubernetes-csi/csi-lib-utils v0.9.1/go.mod h1:8E2jVUX9j3QgspwHXa6LwyN7IHQDjW9jX3kwoWnSC+M=
308308
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
309309
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
310310
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=

pkg/csi/client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ type Client interface {
4949
// Expand expands the volume to a new size at least as big as requestBytes.
5050
// It returns the new size and whether the volume need expand operation on the node.
5151
Expand(ctx context.Context, volumeID string, requestBytes int64, secrets map[string]string, capability *csi.VolumeCapability) (int64, bool, error)
52+
53+
//CloseConnection closes the gRPC connection established by the client
54+
CloseConnection()
5255
}
5356

5457
// New creates a new CSI client.
@@ -134,3 +137,7 @@ func (c *client) Expand(
134137
}
135138
return resp.CapacityBytes, resp.NodeExpansionRequired, nil
136139
}
140+
141+
func (c *client) CloseConnection() {
142+
c.conn.Close()
143+
}

pkg/csi/mock_client.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/container-storage-interface/spec/lib/go/csi"
8+
"github.com/kubernetes-csi/external-resizer/vendor/github.com/kubernetes-csi/csi-lib-utils/connection"
89
)
910

1011
func NewMockClient(
@@ -28,6 +29,7 @@ type MockClient struct {
2829
supportsPluginControllerService bool
2930
expandCalled int
3031
expansionFailed bool
32+
checkMigratedLabel bool
3133
usedSecrets map[string]string
3234
usedCapability *csi.VolumeCapability
3335
}
@@ -52,6 +54,10 @@ func (c *MockClient) SetExpansionFailed() {
5254
c.expansionFailed = true
5355
}
5456

57+
func (c *MockClient) SetCheckMigratedLabel() {
58+
c.checkMigratedLabel = true
59+
}
60+
5561
func (c *MockClient) Expand(
5662
ctx context.Context,
5763
volumeID string,
@@ -63,6 +69,14 @@ func (c *MockClient) Expand(
6369
c.expandCalled++
6470
return requestBytes, c.supportsNodeResize, fmt.Errorf("expansion failed")
6571
}
72+
if c.checkMigratedLabel {
73+
additionalInfo := ctx.Value(connection.AdditionalInfoKey)
74+
additionalInfoVal := additionalInfo.(connection.AdditionalInfo)
75+
migrated := additionalInfoVal.Migrated
76+
if migrated != "true" {
77+
fmt.Errorf("Expected value of migrated label: true, Actual value: %v", migrated)
78+
}
79+
}
6680
c.expandCalled++
6781
c.usedSecrets = secrets
6882
c.usedCapability = capability
@@ -81,3 +95,7 @@ func (c *MockClient) GetCapability() *csi.VolumeCapability {
8195
func (c *MockClient) GetSecrets() map[string]string {
8296
return c.usedSecrets
8397
}
98+
99+
func (c *MockClient) CloseConnection() {
100+
101+
}

pkg/resizer/csi_resizer.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
"strconv"
2324
"time"
2425

2526
csilib "github.com/container-storage-interface/spec/lib/go/csi"
27+
"github.com/kubernetes-csi/csi-lib-utils/connection"
2628
"github.com/kubernetes-csi/external-resizer/pkg/csi"
2729
"github.com/kubernetes-csi/external-resizer/pkg/util"
2830
v1 "k8s.io/api/core/v1"
@@ -125,6 +127,7 @@ func (r *csiResizer) Resize(pv *v1.PersistentVolume, requestSize resource.Quanti
125127
var volumeID string
126128
var source *v1.CSIPersistentVolumeSource
127129
var pvSpec v1.PersistentVolumeSpec
130+
var migrated bool
128131
if pv.Spec.CSI != nil {
129132
// handle CSI volume
130133
source = pv.Spec.CSI
@@ -138,6 +141,7 @@ func (r *csiResizer) Resize(pv *v1.PersistentVolume, requestSize resource.Quanti
138141
if err != nil {
139142
return oldSize, false, fmt.Errorf("failed to translate persistent volume: %v", err)
140143
}
144+
migrated = true
141145
source = csiPV.Spec.CSI
142146
pvSpec = csiPV.Spec
143147
volumeID = source.VolumeHandle
@@ -167,8 +171,10 @@ func (r *csiResizer) Resize(pv *v1.PersistentVolume, requestSize resource.Quanti
167171
}
168172

169173
ctx, cancel := timeoutCtx(r.timeout)
174+
resizeCtx := context.WithValue(ctx, connection.AdditionalInfoKey, connection.AdditionalInfo{Migrated: strconv.FormatBool(migrated)})
175+
170176
defer cancel()
171-
newSizeBytes, nodeResizeRequired, err := r.client.Expand(ctx, volumeID, requestSize.Value(), secrets, capability)
177+
newSizeBytes, nodeResizeRequired, err := r.client.Expand(resizeCtx, volumeID, requestSize.Value(), secrets, capability)
172178
if err != nil {
173179
return oldSize, nodeResizeRequired, err
174180
}

pkg/resizer/csi_resizer_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,14 @@ func TestResizeMigratedPV(t *testing.T) {
126126
driverName string
127127
pv *v1.PersistentVolume
128128
nodeResizeRequired bool
129+
expectMigrated bool
129130
err error
130131
}{
131132
{
132133
name: "Test AWS EBS CSI Driver",
133134
driverName: "ebs.csi.aws.com",
134135
pv: createInTreeEBSPV(1),
136+
expectMigrated: true,
135137
nodeResizeRequired: true,
136138
},
137139
{
@@ -153,6 +155,7 @@ func TestResizeMigratedPV(t *testing.T) {
153155
t.Run(tc.name, func(t *testing.T) {
154156
driverName := tc.driverName
155157
client := csi.NewMockClient(driverName, true, true, true)
158+
client.SetCheckMigratedLabel()
156159
k8sClient, informerFactory := fakeK8s()
157160
resizer, err := NewResizerFromClient(client, 0, k8sClient, informerFactory, driverName)
158161
if err != nil {

vendor/github.com/kubernetes-csi/csi-lib-utils/connection/connection.go

Lines changed: 33 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/kubernetes-csi/csi-lib-utils/metrics/metrics.go

Lines changed: 16 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ github.com/hashicorp/golang-lru/simplelru
5454
github.com/imdario/mergo
5555
# github.com/json-iterator/go v1.1.10
5656
github.com/json-iterator/go
57-
# github.com/kubernetes-csi/csi-lib-utils v0.9.0
57+
# github.com/kubernetes-csi/csi-lib-utils v0.9.1
5858
## explicit
5959
github.com/kubernetes-csi/csi-lib-utils/connection
6060
github.com/kubernetes-csi/csi-lib-utils/leaderelection

0 commit comments

Comments
 (0)