Skip to content

Commit

Permalink
Introduce metrics library
Browse files Browse the repository at this point in the history
  • Loading branch information
saad-ali committed Jan 2, 2020
1 parent 77ba86e commit a98e9b7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ Note that the external-resizer does not scale with more replicas. Only one exter

* `--workers <num>`: Number of simultaneously running `ControllerExpandVolume` operations. Default value is `10`.

* `--metrics-address`: The TCP network address where the prometheus metrics endpoint will run (example: `:8080` which corresponds to port 8080 on local host). The default is empty string, which means metrics endpoint is disabled.

* `--metrics-path`: The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.

#### Other recognized arguments

* `--kubeconfig <path>`: Path to Kubernetes client configuration that the external-resizer uses to connect to Kubernetes API server. When omitted, default token provided by Kubernetes will be used. This option is useful only when the external-resizer does not run as a Kubernetes pod, e.g. for debugging. Either this or `--master` needs to be set if the external-resizer is being run out of cluster.
Expand Down
11 changes: 10 additions & 1 deletion cmd/csi-resizer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ var (
enableLeaderElection = flag.Bool("leader-election", false, "Enable leader election.")
leaderElectionNamespace = flag.String("leader-election-namespace", "", "Namespace where the leader election resource lives. Defaults to the pod namespace if not set.")

metricsAddress = flag.String("metrics-address", "", "The TCP network address where the prometheus metrics endpoint will listen (example: `:8080`). The default is empty string, which means metrics endpoint is disabled.")
metricsPath = flag.String("metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.")

version = "unknown"
)

Expand All @@ -67,7 +70,13 @@ func main() {

informerFactory := informers.NewSharedInformerFactory(kubeClient, *resyncPeriod)

csiResizer, err := resizer.NewResizer(*csiAddress, *csiTimeout, kubeClient, informerFactory)
csiResizer, err := resizer.NewResizer(
*csiAddress,
*csiTimeout,
kubeClient,
informerFactory,
*metricsAddress,
*metricsPath)
if err != nil {
klog.Fatal(err.Error())
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"testing"
"time"

"github.com/kubernetes-csi/csi-lib-utils/metrics"
"github.com/kubernetes-csi/external-resizer/pkg/csi"
"github.com/kubernetes-csi/external-resizer/pkg/resizer"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -114,7 +115,10 @@ func TestController(t *testing.T) {
}
}

csiResizer, err := resizer.NewResizerFromClient(client, 15*time.Second, kubeClient, informerFactory)
metricsManager := metrics.NewCSIMetricsManager("" /* driverName */)
metricsAddress := ""
metricsPath := ""
csiResizer, err := resizer.NewResizerFromClient(client, 15*time.Second, kubeClient, informerFactory, metricsManager, metricsAddress, metricsPath)
if err != nil {
t.Fatalf("Test %s: Unable to create resizer: %v", test.Name, err)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/csi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-csi/csi-lib-utils/connection"
"github.com/kubernetes-csi/csi-lib-utils/metrics"
csirpc "github.com/kubernetes-csi/csi-lib-utils/rpc"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -51,8 +52,8 @@ type Client interface {
}

// New creates a new CSI client.
func New(address string, timeout time.Duration) (Client, error) {
conn, err := connection.Connect(address, connection.OnConnectionLoss(connection.ExitOnConnectionLoss()))
func New(address string, timeout time.Duration, metricsManager metrics.CSIMetricsManager) (Client, error) {
conn, err := connection.Connect(address, metricsManager, connection.OnConnectionLoss(connection.ExitOnConnectionLoss()))
if err != nil {
return nil, fmt.Errorf("failed to connect to CSI driver: %v", err)
}
Expand Down
26 changes: 21 additions & 5 deletions pkg/resizer/csi_resizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import (
"fmt"
"time"

"github.com/kubernetes-csi/csi-lib-utils/metrics"
"github.com/kubernetes-csi/external-resizer/pkg/csi"
"github.com/kubernetes-csi/external-resizer/pkg/util"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
Expand All @@ -45,24 +46,39 @@ func NewResizer(
address string,
timeout time.Duration,
k8sClient kubernetes.Interface,
informerFactory informers.SharedInformerFactory) (Resizer, error) {
csiClient, err := csi.New(address, timeout)
informerFactory informers.SharedInformerFactory,
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)
return NewResizerFromClient(
csiClient,
timeout,
k8sClient,
informerFactory,
metricsManager,
metricsAddress,
metricsPath)
}

func NewResizerFromClient(
csiClient csi.Client,
timeout time.Duration,
k8sClient kubernetes.Interface,
informerFactory informers.SharedInformerFactory) (Resizer, error) {
informerFactory informers.SharedInformerFactory,
metricsManager metrics.CSIMetricsManager,
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)
metricsManager.SetDriverName(driverName)
metricsManager.StartMetricsEndpoint(metricsAddress, metricsPath)

supportControllerService, err := supportsPluginControllerService(csiClient, timeout)
if err != nil {
return nil, fmt.Errorf("failed to check if plugin supports controller service: %v", err)
Expand Down
18 changes: 14 additions & 4 deletions pkg/resizer/csi_resizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"testing"
"time"

"github.com/kubernetes-csi/csi-lib-utils/metrics"
"github.com/kubernetes-csi/external-resizer/pkg/csi"
"github.com/kubernetes-csi/external-resizer/pkg/util"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
Expand Down Expand Up @@ -59,8 +60,11 @@ func TestNewResizer(t *testing.T) {
},
} {
client := csi.NewMockClient("mock", c.SupportsNodeResize, c.SupportsControllerResize, c.SupportsPluginControllerService)
metricsManager := metrics.NewCSIMetricsManager("" /* driverName */)
metricsAddress := ""
metricsPath := ""
k8sClient, informerFactory := fakeK8s()
resizer, err := NewResizerFromClient(client, 0, k8sClient, informerFactory)
resizer, err := NewResizerFromClient(client, 0, k8sClient, informerFactory, metricsManager, metricsAddress, metricsPath)
if err != c.Error {
t.Errorf("Case %d: Unexpected error: wanted %v, got %v", i, c.Error, err)
}
Expand Down Expand Up @@ -166,7 +170,10 @@ func TestResizeMigratedPV(t *testing.T) {
driverName := tc.driverName
client := csi.NewMockClient(driverName, true, true, true)
k8sClient, informerFactory := fakeK8s()
resizer, err := NewResizerFromClient(client, 0, k8sClient, informerFactory)
metricsManager := metrics.NewCSIMetricsManager("" /* driverName */)
metricsAddress := ""
metricsPath := ""
resizer, err := NewResizerFromClient(client, 0, k8sClient, informerFactory, metricsManager, metricsAddress, metricsPath)
if err != nil {
t.Fatalf("Failed to create resizer: %v", err)
}
Expand Down Expand Up @@ -237,7 +244,10 @@ func TestCanSupport(t *testing.T) {
driverName := tc.driverName
client := csi.NewMockClient(driverName, true, true, true)
k8sClient, informerFactory := fakeK8s()
resizer, err := NewResizerFromClient(client, 0, k8sClient, informerFactory)
metricsManager := metrics.NewCSIMetricsManager("" /* driverName */)
metricsAddress := ""
metricsPath := ""
resizer, err := NewResizerFromClient(client, 0, k8sClient, informerFactory, metricsManager, metricsAddress, metricsPath)
if err != nil {
t.Fatalf("Failed to create resizer: %v", err)
}
Expand Down

0 comments on commit a98e9b7

Please sign in to comment.