Skip to content

Commit

Permalink
Move metrics initialization logic out of constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
verult committed Dec 5, 2020
1 parent a6c7600 commit 71e12cc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 62 deletions.
31 changes: 26 additions & 5 deletions cmd/csi-resizer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"os"
"time"

"github.com/kubernetes-csi/csi-lib-utils/metrics"
"github.com/kubernetes-csi/external-resizer/pkg/csi"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -112,20 +114,33 @@ func main() {

mux := http.NewServeMux()

csiResizer, err := resizer.NewResizer(
*csiAddress,
metricsManager := metrics.NewCSIMetricsManager("" /* driverName */)

csiClient, err := csi.New(*csiAddress, *timeout, metricsManager)
if err != nil {
klog.Fatal(err.Error())
}

driverName, err := getDriverName(csiClient, *timeout)
if err != nil {
klog.Fatal(fmt.Errorf("get driver name failed: %v", err))
}
klog.V(2).Infof("CSI driver name: %q", driverName)

csiResizer, err := resizer.NewResizerFromClient(
csiClient,
*timeout,
kubeClient,
informerFactory,
mux,
addr,
*metricsPath)
driverName)
if err != nil {
klog.Fatal(err.Error())
}

// Start HTTP server for metrics + leader election healthz
if addr != "" {
metricsManager.RegisterToServer(mux, *metricsPath)
metricsManager.SetDriverName(driverName)
go func() {
klog.Infof("ServeMux listening at %q", addr)
err := http.ListenAndServe(addr, mux)
Expand Down Expand Up @@ -167,3 +182,9 @@ func main() {
}
}
}

func getDriverName(client csi.Client, timeout time.Duration) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return client.GetDriverName(ctx)
}
45 changes: 1 addition & 44 deletions pkg/resizer/csi_resizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"time"

csilib "github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-csi/csi-lib-utils/metrics"
"github.com/kubernetes-csi/external-resizer/pkg/csi"
"github.com/kubernetes-csi/external-resizer/pkg/util"
v1 "k8s.io/api/core/v1"
Expand All @@ -40,48 +39,12 @@ var (
resizeNotSupportErr = errors.New("CSI driver neither supports controller resize nor node resize")
)

// NewResizer creates a new resizer responsible for resizing CSI volumes.
func NewResizer(
address string,
timeout time.Duration,
k8sClient kubernetes.Interface,
informerFactory informers.SharedInformerFactory,
metricsServer metrics.Server,
metricsAddress, metricsPath string) (Resizer, error) {
metricsManager := metrics.NewCSIMetricsManager("" /* driverName */)
csiClient, err := csi.New(address, timeout, metricsManager)
if err != nil {
return nil, err
}
return NewResizerFromClient(
csiClient,
timeout,
k8sClient,
informerFactory,
metricsManager,
metricsServer,
metricsAddress,
metricsPath)
}

func NewResizerFromClient(
csiClient csi.Client,
timeout time.Duration,
k8sClient kubernetes.Interface,
informerFactory informers.SharedInformerFactory,
metricsManager metrics.CSIMetricsManager,
metricsServer metrics.Server,
metricsAddress, metricsPath string) (Resizer, error) {
driverName, err := getDriverName(csiClient, timeout)
if err != nil {
return nil, fmt.Errorf("get driver name failed: %v", err)
}

klog.V(2).Infof("CSI driver name: %q", driverName)
if metricsAddress != "" {
metricsManager.RegisterToServer(metricsServer, metricsPath)
metricsManager.SetDriverName(driverName)
}
driverName string) (Resizer, error) {

supportControllerService, err := supportsPluginControllerService(csiClient, timeout)
if err != nil {
Expand Down Expand Up @@ -271,12 +234,6 @@ func GetVolumeCapabilities(pvSpec v1.PersistentVolumeSpec) (*csilib.VolumeCapabi
return cap, nil
}

func getDriverName(client csi.Client, timeout time.Duration) (string, error) {
ctx, cancel := timeoutCtx(timeout)
defer cancel()
return client.GetDriverName(ctx)
}

func supportsPluginControllerService(client csi.Client, timeout time.Duration) (bool, error) {
ctx, cancel := timeoutCtx(timeout)
defer cancel()
Expand Down
17 changes: 4 additions & 13 deletions pkg/resizer/csi_resizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

csilib "github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-csi/csi-lib-utils/metrics"
"github.com/kubernetes-csi/external-resizer/pkg/csi"
"github.com/kubernetes-csi/external-resizer/pkg/util"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -62,11 +61,9 @@ func TestNewResizer(t *testing.T) {
},
} {
client := csi.NewMockClient("mock", c.SupportsNodeResize, c.SupportsControllerResize, c.SupportsPluginControllerService)
metricsManager := metrics.NewCSIMetricsManager("" /* driverName */)
metricsAddress := ""
metricsPath := ""
driverName := "mock-driver"
k8sClient, informerFactory := fakeK8s()
resizer, err := NewResizerFromClient(client, 0, k8sClient, informerFactory, metricsManager, nil /* metricsServer */, metricsAddress, metricsPath)
resizer, err := NewResizerFromClient(client, 0, k8sClient, informerFactory, driverName)
if err != c.Error {
t.Errorf("Case %d: Unexpected error: wanted %v, got %v", i, c.Error, err)
}
Expand Down Expand Up @@ -157,10 +154,7 @@ func TestResizeMigratedPV(t *testing.T) {
driverName := tc.driverName
client := csi.NewMockClient(driverName, true, true, true)
k8sClient, informerFactory := fakeK8s()
metricsManager := metrics.NewCSIMetricsManager("" /* driverName */)
metricsAddress := ""
metricsPath := ""
resizer, err := NewResizerFromClient(client, 0, k8sClient, informerFactory, metricsManager, nil /* metricsServer */, metricsAddress, metricsPath)
resizer, err := NewResizerFromClient(client, 0, k8sClient, informerFactory, driverName)
if err != nil {
t.Fatalf("Failed to create resizer: %v", err)
}
Expand Down Expand Up @@ -364,10 +358,7 @@ func TestCanSupport(t *testing.T) {
driverName := tc.driverName
client := csi.NewMockClient(driverName, true, true, true)
k8sClient, informerFactory := fakeK8s()
metricsManager := metrics.NewCSIMetricsManager("" /* driverName */)
metricsAddress := ""
metricsPath := ""
resizer, err := NewResizerFromClient(client, 0, k8sClient, informerFactory, metricsManager, nil /* metricsServer */, metricsAddress, metricsPath)
resizer, err := NewResizerFromClient(client, 0, k8sClient, informerFactory, driverName)
if err != nil {
t.Fatalf("Failed to create resizer: %v", err)
}
Expand Down

0 comments on commit 71e12cc

Please sign in to comment.