Skip to content

Commit

Permalink
cloud: remove decoding for cloud output v1
Browse files Browse the repository at this point in the history
  • Loading branch information
yorugac committed Aug 23, 2024
1 parent d9490de commit 0f3957c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 91 deletions.
49 changes: 12 additions & 37 deletions pkg/cloud/aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,18 @@ import (
corev1 "k8s.io/api/core/v1"
)

var aggregationVarNames = map[int][]string{
1: []string{
// cloud output v1: to be removed in the future
"K6_CLOUD_AGGREGATION_MIN_SAMPLES",
"K6_CLOUD_AGGREGATION_PERIOD",
"K6_CLOUD_AGGREGATION_WAIT_PERIOD",
"K6_CLOUD_METRIC_PUSH_INTERVAL",
"K6_CLOUD_MAX_METRIC_SAMPLES_PER_PACKAGE",
"K6_CLOUD_MAX_METRIC_PUSH_CONCURRENCY",
},
2: []string{
// cloud output v2
"K6_CLOUD_API_VERSION",
"K6_CLOUD_AGGREGATION_PERIOD",
"K6_CLOUD_AGGREGATION_WAIT_PERIOD",
"K6_CLOUD_METRIC_PUSH_INTERVAL",
"K6_CLOUD_METRIC_PUSH_CONCURRENCY",
},
var aggregationVarNames = []string{
// cloud output v2
"K6_CLOUD_API_VERSION",
"K6_CLOUD_AGGREGATION_PERIOD",
"K6_CLOUD_AGGREGATION_WAIT_PERIOD",
"K6_CLOUD_METRIC_PUSH_INTERVAL",
"K6_CLOUD_METRIC_PUSH_CONCURRENCY",
}

func EncodeAggregationConfig(testRun *cloudapi.Config) string {
return fmt.Sprintf("%d|%s|%s|%s|%d",
2, // use v2 for all new test runs
2, // version of protocol
testRun.AggregationPeriod.String(),
testRun.AggregationWaitPeriod.String(),
testRun.MetricPushInterval.String(),
Expand All @@ -40,32 +29,18 @@ func EncodeAggregationConfig(testRun *cloudapi.Config) string {
func DecodeAggregationConfig(encoded string) ([]corev1.EnvVar, error) {
values := strings.Split(encoded, "|")

// in order not to break existing deployments,
// let's support decoding of cloud output v1 for some time
var (
apiV1VarNames = len(aggregationVarNames[1])
apiV2VarNames = len(aggregationVarNames[2])
)

if len(values) != apiV1VarNames && len(values) != apiV2VarNames {
if len(values) != len(aggregationVarNames) {
return nil, fmt.Errorf(
"Aggregation vars got corrupted: there are %d values instead of %d or %d. Encoded value: `%s`.",
"Aggregation vars got corrupted: there are %d values instead of %d. Encoded value: `%s`.",
len(values),
apiV1VarNames, apiV2VarNames,
len(aggregationVarNames),
encoded)
}

var varNames []string
if len(values) == apiV1VarNames {
varNames = aggregationVarNames[1]
} else {
varNames = aggregationVarNames[2]
}

vars := make([]corev1.EnvVar, len(values))
for i := range values {
vars[i] = corev1.EnvVar{
Name: varNames[i],
Name: aggregationVarNames[i],
Value: values[i],
}
}
Expand Down
43 changes: 4 additions & 39 deletions pkg/cloud/aggregation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,9 @@ func Test_EncodeAggregationConfig(t *testing.T) {

func Test_DecodeAggregationConfig(t *testing.T) {
var (
// For now, we support both versions in decoding.
v1Encoded = "50|3s|8s|6s|10000|10"
v2Encoded = "2|5s|3s|10s|10"
encoded = "2|5s|3s|10s|10"

v1EnvVars = []corev1.EnvVar{
{
Name: "K6_CLOUD_AGGREGATION_MIN_SAMPLES",
Value: "50",
},
{
Name: "K6_CLOUD_AGGREGATION_PERIOD",
Value: "3s",
},
{
Name: "K6_CLOUD_AGGREGATION_WAIT_PERIOD",
Value: "8s",
},
{
Name: "K6_CLOUD_METRIC_PUSH_INTERVAL",
Value: "6s",
},
{
Name: "K6_CLOUD_MAX_METRIC_SAMPLES_PER_PACKAGE",
Value: "10000",
},
{
Name: "K6_CLOUD_MAX_METRIC_PUSH_CONCURRENCY",
Value: "10",
},
}

v2EnvVars = []corev1.EnvVar{
expected = []corev1.EnvVar{
{
Name: "K6_CLOUD_API_VERSION",
Value: "2",
Expand All @@ -85,16 +56,10 @@ func Test_DecodeAggregationConfig(t *testing.T) {
}
)

envVars, err := DecodeAggregationConfig(v1Encoded)
envVars, err := DecodeAggregationConfig(encoded)
assert.Equal(t, nil, err)

for i, expectedEnvVar := range v1EnvVars {
assert.Equal(t, expectedEnvVar, envVars[i])
}

envVars, err = DecodeAggregationConfig(v2Encoded)
assert.Equal(t, nil, err)
for i, expectedEnvVar := range v2EnvVars {
for i, expectedEnvVar := range expected {
assert.Equal(t, expectedEnvVar, envVars[i])
}
}
27 changes: 12 additions & 15 deletions pkg/resources/jobs/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,20 @@ import (

// these are default values hard-coded in k6
var aggregationEnvVars = []corev1.EnvVar{
corev1.EnvVar{
Name: "K6_CLOUD_AGGREGATION_MIN_SAMPLES",
Value: "50",
}, corev1.EnvVar{
{
Name: "K6_CLOUD_API_VERSION",
Value: "2",
}, {
Name: "K6_CLOUD_AGGREGATION_PERIOD",
Value: "3s",
}, corev1.EnvVar{
Value: "5s",
}, {
Name: "K6_CLOUD_AGGREGATION_WAIT_PERIOD",
Value: "8s",
}, corev1.EnvVar{
Value: "3s",
}, {
Name: "K6_CLOUD_METRIC_PUSH_INTERVAL",
Value: "6s",
}, corev1.EnvVar{
Name: "K6_CLOUD_MAX_METRIC_SAMPLES_PER_PACKAGE",
Value: "10000",
}, corev1.EnvVar{
Name: "K6_CLOUD_MAX_METRIC_PUSH_CONCURRENCY",
Value: "10s",
}, {
Name: "K6_CLOUD_METRIC_PUSH_CONCURRENCY",
Value: "10",
},
}
Expand Down Expand Up @@ -1114,7 +1111,7 @@ func TestNewRunnerJobCloud(t *testing.T) {
// testrunid has to be set hard-coded here.
Status: v1alpha1.TestRunStatus{
TestRunID: "testrunid",
AggregationVars: "50|3s|8s|6s|10000|10",
AggregationVars: "2|5s|3s|10s|10",
},
}

Expand Down

0 comments on commit 0f3957c

Please sign in to comment.