Skip to content
This repository has been archived by the owner on Jan 22, 2021. It is now read-only.

Commit

Permalink
using aggregation from AEM for selecting the value
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaysngupta committed Nov 4, 2019
1 parent ef6a298 commit 311b1f6
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export AZURE_TENANT_ID=""
export AZURE_CLIENT_ID=""
export AZURE_CLIENT_SECRET=""
export SUBSCRIPTION_ID=""
export KUBE_CONFIG_FILE=""
2 changes: 1 addition & 1 deletion pkg/azure/externalmetrics/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package externalmetrics

type AzureExternalMetricResponse struct {
Total float64
Value float64
}

type AzureExternalMetricClient interface {
Expand Down
28 changes: 19 additions & 9 deletions pkg/azure/externalmetrics/monitor_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,33 @@ func (c *monitorClient) GetAzureMetric(azMetricRequest AzureExternalMetricReques
return AzureExternalMetricResponse{}, err
}

total := extractValue(metricResult)

klog.V(2).Infof("found metric value: %f", total)
value := extractValue(azMetricRequest, metricResult)

// TODO set Value based on aggregations type
return AzureExternalMetricResponse{
Total: total,
Value: value,
}, nil
}

func extractValue(metricResult insights.Response) float64 {
//TODO extract value based on aggregation type
//TODO check for nils
func extractValue(azMetricRequest AzureExternalMetricRequest, metricResult insights.Response) float64 {
metricVals := *metricResult.Value
Timeseries := *metricVals[0].Timeseries
data := *Timeseries[0].Data
total := *data[len(data)-1].Total

return total
switch insights.AggregationType(azMetricRequest.Aggregation) {
case insights.Average:
klog.V(2).Infof("metric type: %s %f", azMetricRequest.Aggregation, *data[len(data)-1].Average)
return *data[len(data)-1].Average
case insights.Total:
klog.V(2).Infof("metric type: %s %f", azMetricRequest.Aggregation, *data[len(data)-1].Total)
return *data[len(data)-1].Total
case insights.Maximum:
klog.V(2).Infof("metric type: %s %f", azMetricRequest.Aggregation, *data[len(data)-1].Maximum)
return *data[len(data)-1].Maximum
case insights.Minimum:
klog.V(2).Infof("metric type: %s %f", azMetricRequest.Aggregation, *data[len(data)-1].Minimum)
return *data[len(data)-1].Minimum
default:
return 0
}
}
52 changes: 36 additions & 16 deletions pkg/azure/externalmetrics/monitor_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestAzureMonitorIfFailedResponseGetError(t *testing.T) {

client := newMonitorClient("", monitorClient)

request := newAzureMonitorMetricRequest()
request := newAzureMonitorMetricRequest(insights.Average)
_, err := client.GetAzureMetric(request)

if err == nil {
Expand All @@ -44,28 +44,46 @@ func TestAzureMonitorIfFailedResponseGetError(t *testing.T) {
}

func TestAzureMonitorIfValidRequestGetResult(t *testing.T) {
response := makeAzureMonitorResponse(15)
monitorClient := newFakeMonitorClient(response, nil)
aggregateList := []insights.AggregationType{
insights.Average,
insights.Minimum,
insights.Maximum,
insights.Total,
}

client := newMonitorClient("", monitorClient)
for _, agg := range aggregateList {
response := makeAzureMonitorResponse(agg, 15)
monitorClient := newFakeMonitorClient(response, nil)

request := newAzureMonitorMetricRequest()
metricResponse, err := client.GetAzureMetric(request)
client := newMonitorClient("", monitorClient)

if err != nil {
t.Errorf("error after processing got: %v, want nil", err)
}
request := newAzureMonitorMetricRequest(agg)
metricResponse, err := client.GetAzureMetric(request)

if metricResponse.Total != 15 {
t.Errorf("metricResponse.Total = %v, want = %v", metricResponse.Total, 15)
if err != nil {
t.Errorf("error after processing got: %v, want nil", err)
}

if metricResponse.Value != 15 {
t.Errorf("metricresponse.Value = %v, want = %v", metricResponse.Value, 15)
}
}
}

func makeAzureMonitorResponse(value float64) insights.Response {
func makeAzureMonitorResponse(aggregateType insights.AggregationType, value float64) insights.Response {
// create metric value
mv := insights.MetricValue{
Total: &value,
mv := insights.MetricValue{}
switch aggregateType {
case insights.Average:
mv.Average = &value
case insights.Minimum:
mv.Minimum = &value
case insights.Maximum:
mv.Maximum = &value
case insights.Total:
mv.Total = &value
}

metricValues := []insights.MetricValue{}
metricValues = append(metricValues, mv)

Expand All @@ -77,8 +95,10 @@ func makeAzureMonitorResponse(value float64) insights.Response {
timeseries = append(timeseries, te)

// create metric
aType := string(aggregateType)
metric := insights.Metric{
Timeseries: &timeseries,
Type: &aType,
}
metrics := []insights.Metric{}
metrics = append(metrics, metric)
Expand All @@ -90,7 +110,7 @@ func makeAzureMonitorResponse(value float64) insights.Response {
return response
}

func newAzureMonitorMetricRequest() AzureExternalMetricRequest {
func newAzureMonitorMetricRequest(aggregationType insights.AggregationType) AzureExternalMetricRequest {
return AzureExternalMetricRequest{
ResourceGroup: "ResourceGroup",
ResourceName: "ResourceName",
Expand All @@ -99,7 +119,7 @@ func newAzureMonitorMetricRequest() AzureExternalMetricRequest {
SubscriptionID: "SubscriptionID",
MetricName: "MetricName",
Filter: "Filter",
Aggregation: "Aggregation",
Aggregation: string(aggregationType),
Timespan: "PT10",
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ func (c *servicebusClient) GetAzureMetric(azMetricRequest AzureExternalMetricReq

// TODO set Value based on aggregations type
return AzureExternalMetricResponse{
Total: activeMessageCount,
Value: activeMessageCount,
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func TestIfValidRequestGetResult(t *testing.T) {
t.Errorf("error after processing got: %v, want nil", err)
}

if metricResponse.Total != 15 {
t.Errorf("metricResponse.Total = %v, want = %v", metricResponse.Total, 15)
if metricResponse.Value != 15 {
t.Errorf("metricResponse.Total = %v, want = %v", metricResponse.Value, 15)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/provider_external.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (p *AzureProvider) GetExternalMetric(namespace string, metricSelector label

externalmetric := external_metrics.ExternalMetricValue{
MetricName: info.Metric,
Value: *resource.NewQuantity(int64(metricValue.Total), resource.DecimalSI),
Value: *resource.NewQuantity(int64(metricValue.Value), resource.DecimalSI),
Timestamp: metav1.Now(),
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/provider_external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ type fakeAzureExternalClientFactory struct {
func (f fakeAzureExternalClientFactory) GetAzureExternalMetricClient(clientType string) (client externalmetrics.AzureExternalMetricClient, err error) {
fakeClient := fakeAzureMonitorClient{
err: nil,
result: externalmetrics.AzureExternalMetricResponse{Total: 15},
result: externalmetrics.AzureExternalMetricResponse{Value: 15},
}

return fakeClient, nil
Expand Down
31 changes: 31 additions & 0 deletions script/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

set -aueo pipefail

source .env

GOBIN=$(pwd)/bin

echo -e "Cleanup: delete $GOBIN"
rm -rf "$GOBIN"

echo -e "Compiling..."
go install -v .
RESULT=$?
if [ "$RESULT" -eq "0" ]; then
chmod -R 777 bin
echo -e "Build SUCCEEDED"
else
echo -e "Build FAILED"
exit 1
fi

# Run
./bin/azure-k8s-metrics-adapter \
--authentication-kubeconfig="$KUBE_CONFIG_FILE" \
--authorization-kubeconfig="$KUBE_CONFIG_FILE" \
--lister-kubeconfig="$KUBE_CONFIG_FILE" \
--secure-port=6443 \
--logtostderr=true \
--v 5 #\
#--secure-port 80

0 comments on commit 311b1f6

Please sign in to comment.