Skip to content

Commit 431db48

Browse files
authored
Changes vmagent remote write (VictoriaMetrics#273)
* fixes sendTimeout for remoteWrite spec at VMAgent moves remoteWrite.labels to spec.remoteWriteSettings.labels - breaking changes
1 parent eea599a commit 431db48

File tree

7 files changed

+125
-59
lines changed

7 files changed

+125
-59
lines changed

api/v1beta1/vmagent_types.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,9 @@ type VMAgentRemoteWriteSettings struct {
304304
// +optional
305305
// +kubebuilder:validation:Pattern:="[0-9]+(ms|s|m|h)"
306306
FlushInterval *string `json:"flushInterval,omitempty"`
307+
// Optional labels in the form 'name=value' to add to all the metrics before sending them
308+
// +optional
309+
Labels map[string]string `json:"label,omitempty"`
307310
}
308311

309312
// VMAgentRemoteWriteSpec defines the remote storage configuration for VmAgent
@@ -317,9 +320,7 @@ type VMAgentRemoteWriteSpec struct {
317320
// Optional bearer auth token to use for -remoteWrite.url
318321
// +optional
319322
BearerTokenSecret *v1.SecretKeySelector `json:"bearerTokenSecret,omitempty"`
320-
// Optional labels in the form 'name=value' to add to all the metrics before sending them
321-
// +optional
322-
Labels map[string]string `json:"label,omitempty"`
323+
323324
// ConfigMap with relabeling config which is applied to metrics before sending them to the corresponding -remoteWrite.url
324325
// +optional
325326
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Key at Configmap with relabelConfig for remoteWrite",xDescriptors="urn:alm:descriptor:io.kubernetes:ConfigMapKeySelector"

api/v1beta1/vmagent_webhook_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestVMAgent_sanityCheck(t *testing.T) {
1919
{
2020
name: "rw empty url",
2121
spec: VMAgentSpec{RemoteWrite: []VMAgentRemoteWriteSpec{
22-
{Labels: map[string]string{}},
22+
{},
2323
}},
2424
wantErr: true,
2525
},

api/v1beta1/zz_generated.deepcopy.go

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

config/crd/bases/operator.victoriametrics.com_vmagents.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,12 +1293,6 @@ spec:
12931293
type: string
12941294
type: object
12951295
type: array
1296-
label:
1297-
additionalProperties:
1298-
type: string
1299-
description: Optional labels in the form 'name=value' to add to
1300-
all the metrics before sending them
1301-
type: object
13021296
sendTimeout:
13031297
description: Timeout for sending a single block of data to -remoteWrite.url
13041298
(default 1m0s)
@@ -1465,6 +1459,12 @@ spec:
14651459
1s)
14661460
pattern: '[0-9]+(ms|s|m|h)'
14671461
type: string
1462+
label:
1463+
additionalProperties:
1464+
type: string
1465+
description: Optional labels in the form 'name=value' to add to
1466+
all the metrics before sending them
1467+
type: object
14681468
maxBlockSize:
14691469
description: The maximum size in bytes of unpacked request to send
14701470
to remote storage

controllers/factory/vmagent.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,18 @@ func BuildRemoteWriteSettings(cr *victoriametricsv1beta1.VMAgent) []string {
867867
if rws.TmpDataPath != nil {
868868
args = append(args, fmt.Sprintf("-remoteWrite.tmpDataPath=%s", *rws.TmpDataPath))
869869
}
870+
if rws.Labels != nil {
871+
lbls := sortMap(rws.Labels)
872+
flagValue := "-remoteWrite.label="
873+
if len(lbls) > 0 {
874+
flagValue += fmt.Sprintf("%s=%s", lbls[0].key, lbls[0].value)
875+
for _, lv := range lbls[1:] {
876+
flagValue += fmt.Sprintf(",%s=%s", lv.key, lv.value)
877+
}
878+
args = append(args, flagValue)
879+
}
880+
881+
}
870882
return args
871883
}
872884

@@ -879,7 +891,6 @@ func BuildRemoteWrites(cr *victoriametricsv1beta1.VMAgent, rwsBasicAuth map[stri
879891
authUser := remoteFlag{flagSetting: "-remoteWrite.basicAuth.username="}
880892
authPassword := remoteFlag{flagSetting: "-remoteWrite.basicAuth.password="}
881893
bearerToken := remoteFlag{flagSetting: "-remoteWrite.bearerToken="}
882-
labels := remoteFlag{flagSetting: "-remoteWrite.label="}
883894
urlRelabelConfig := remoteFlag{flagSetting: "-remoteWrite.urlRelabelConfig="}
884895
sendTimeout := remoteFlag{flagSetting: "-remoteWrite.sendTimeout="}
885896
tlsCAs := remoteFlag{flagSetting: "-remoteWrite.tlsCAFile="}
@@ -960,17 +971,6 @@ func BuildRemoteWrites(cr *victoriametricsv1beta1.VMAgent, rwsBasicAuth map[stri
960971
}
961972
bearerToken.flagSetting += fmt.Sprintf("\"%s\",", strings.Replace(value, `"`, `\"`, -1))
962973

963-
value = ""
964-
if rws.Labels != nil {
965-
labels.isNotNull = true
966-
labels := sortMap(rws.Labels)
967-
for _, v := range labels {
968-
value += fmt.Sprintf("%s=%s,", v.key, v.value)
969-
}
970-
}
971-
// no need to add comma
972-
labels.flagSetting += value
973-
974974
value = ""
975975

976976
if rws.UrlRelabelConfig != nil || len(rws.InlineUrlRelabelConfig) > 0 {
@@ -984,15 +984,14 @@ func BuildRemoteWrites(cr *victoriametricsv1beta1.VMAgent, rwsBasicAuth map[stri
984984
if rws.SendTimeout != nil {
985985
if !sendTimeout.isNotNull {
986986
sendTimeout.isNotNull = true
987-
finalArgs = append(finalArgs, fmt.Sprintf("%s=%s", sendTimeout.flagSetting, *rws.SendTimeout))
988987
}
989988
value = *rws.SendTimeout
990989
}
991990
sendTimeout.flagSetting += fmt.Sprintf("%s,", value)
992991

993992
value = ""
994993
}
995-
remoteArgs = append(remoteArgs, url, authUser, authPassword, bearerToken, labels, urlRelabelConfig, tlsInsecure, sendTimeout)
994+
remoteArgs = append(remoteArgs, url, authUser, authPassword, bearerToken, urlRelabelConfig, tlsInsecure, sendTimeout)
996995
remoteArgs = append(remoteArgs, tlsServerName, tlsKeys, tlsCerts, tlsCAs)
997996

998997
for _, remoteArgType := range remoteArgs {

controllers/factory/vmagent_test.go

Lines changed: 93 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/stretchr/testify/assert"
12+
"k8s.io/utils/pointer"
1213

1314
victoriametricsv1beta1 "github.com/VictoriaMetrics/operator/api/v1beta1"
1415
"github.com/VictoriaMetrics/operator/controllers/factory/k8stools"
@@ -567,28 +568,15 @@ func TestBuildRemoteWrites(t *testing.T) {
567568
args args
568569
want []string
569570
}{
570-
{
571-
name: "test labels",
572-
args: args{
573-
cr: &victoriametricsv1beta1.VMAgent{
574-
Spec: victoriametricsv1beta1.VMAgentSpec{RemoteWrite: []victoriametricsv1beta1.VMAgentRemoteWriteSpec{
575-
{
576-
URL: "localhost:8429",
577-
Labels: map[string]string{"label1": "value1", "label2": "value2"},
578-
},
579-
}},
580-
},
581-
},
582-
want: []string{"-remoteWrite.url=localhost:8429", "-remoteWrite.label=label1=value1,label2=value2"},
583-
},
571+
584572
{
585573
name: "test with tls config full",
586574
args: args{
587575
cr: &victoriametricsv1beta1.VMAgent{
588576
Spec: victoriametricsv1beta1.VMAgentSpec{RemoteWrite: []victoriametricsv1beta1.VMAgentRemoteWriteSpec{
589577
{
590-
URL: "localhost:8429",
591-
Labels: map[string]string{"label1": "value1", "label2": "value2"},
578+
URL: "localhost:8429",
579+
592580
TLSConfig: &victoriametricsv1beta1.TLSConfig{
593581
CA: victoriametricsv1beta1.SecretOrConfigMap{Secret: &corev1.SecretKeySelector{
594582
LocalObjectReference: corev1.LocalObjectReference{
@@ -613,16 +601,16 @@ func TestBuildRemoteWrites(t *testing.T) {
613601
}},
614602
},
615603
},
616-
want: []string{"-remoteWrite.label=label1=value1,label2=value2", "-remoteWrite.tlsCAFile=/etc/vmagent-tls/certs_tls-secret_ca,/path/to_ca", "-remoteWrite.tlsCertFile=,/etc/vmagent-tls/certs_tls-secret_ca", "-remoteWrite.url=localhost:8429,localhost:8429"},
604+
want: []string{"-remoteWrite.tlsCAFile=/etc/vmagent-tls/certs_tls-secret_ca,/path/to_ca", "-remoteWrite.tlsCertFile=,/etc/vmagent-tls/certs_tls-secret_ca", "-remoteWrite.url=localhost:8429,localhost:8429"},
617605
},
618606
{
619607
name: "test insecure with key only",
620608
args: args{
621609
cr: &victoriametricsv1beta1.VMAgent{
622610
Spec: victoriametricsv1beta1.VMAgentSpec{RemoteWrite: []victoriametricsv1beta1.VMAgentRemoteWriteSpec{
623611
{
624-
URL: "localhost:8429",
625-
Labels: map[string]string{"label1": "value1", "label2": "value2"},
612+
URL: "localhost:8429",
613+
626614
TLSConfig: &victoriametricsv1beta1.TLSConfig{
627615
KeySecret: &corev1.SecretKeySelector{
628616
LocalObjectReference: corev1.LocalObjectReference{
@@ -636,24 +624,24 @@ func TestBuildRemoteWrites(t *testing.T) {
636624
}},
637625
},
638626
},
639-
want: []string{"-remoteWrite.url=localhost:8429", "-remoteWrite.tlsInsecureSkipVerify=true", "-remoteWrite.tlsKeyFile=/etc/vmagent-tls/certs_tls-secret_key", "-remoteWrite.label=label1=value1,label2=value2"},
627+
want: []string{"-remoteWrite.url=localhost:8429", "-remoteWrite.tlsInsecureSkipVerify=true", "-remoteWrite.tlsKeyFile=/etc/vmagent-tls/certs_tls-secret_key"},
640628
},
641629
{
642630
name: "test insecure",
643631
args: args{
644632
cr: &victoriametricsv1beta1.VMAgent{
645633
Spec: victoriametricsv1beta1.VMAgentSpec{RemoteWrite: []victoriametricsv1beta1.VMAgentRemoteWriteSpec{
646634
{
647-
URL: "localhost:8429",
648-
Labels: map[string]string{"label1": "value1", "label2": "value2"},
635+
URL: "localhost:8429",
636+
649637
TLSConfig: &victoriametricsv1beta1.TLSConfig{
650638
InsecureSkipVerify: true,
651639
},
652640
},
653641
}},
654642
},
655643
},
656-
want: []string{"-remoteWrite.url=localhost:8429", "-remoteWrite.tlsInsecureSkipVerify=true", "-remoteWrite.label=label1=value1,label2=value2"},
644+
want: []string{"-remoteWrite.url=localhost:8429", "-remoteWrite.tlsInsecureSkipVerify=true"},
657645
},
658646
{
659647
name: "test inline relabeling",
@@ -671,8 +659,8 @@ func TestBuildRemoteWrites(t *testing.T) {
671659
},
672660
},
673661
{
674-
URL: "remote-1:8429",
675-
Labels: map[string]string{"label1": "value1", "label2": "value2"},
662+
URL: "remote-1:8429",
663+
676664
TLSConfig: &victoriametricsv1beta1.TLSConfig{
677665
InsecureSkipVerify: true,
678666
},
@@ -693,7 +681,26 @@ func TestBuildRemoteWrites(t *testing.T) {
693681
},
694682
},
695683
},
696-
want: []string{"-remoteWrite.label=label1=value1,label2=value2", "-remoteWrite.url=localhost:8429,remote-1:8429,remote-1:8429", "-remoteWrite.tlsInsecureSkipVerify=true,true,true", "-remoteWrite.urlRelabelConfig=/etc/vm/relabeling/url_rebaling-0.yaml,,/etc/vm/relabeling/url_rebaling-2.yaml"},
684+
want: []string{"-remoteWrite.url=localhost:8429,remote-1:8429,remote-1:8429", "-remoteWrite.tlsInsecureSkipVerify=true,true,true", "-remoteWrite.urlRelabelConfig=/etc/vm/relabeling/url_rebaling-0.yaml,,/etc/vm/relabeling/url_rebaling-2.yaml"},
685+
},
686+
{
687+
name: "test sendTimeout",
688+
args: args{
689+
cr: &victoriametricsv1beta1.VMAgent{
690+
Spec: victoriametricsv1beta1.VMAgentSpec{RemoteWrite: []victoriametricsv1beta1.VMAgentRemoteWriteSpec{
691+
{
692+
URL: "localhost:8429",
693+
694+
SendTimeout: pointer.String("10s"),
695+
},
696+
{
697+
URL: "localhost:8431",
698+
SendTimeout: pointer.String("15s"),
699+
},
700+
}},
701+
},
702+
},
703+
want: []string{"-remoteWrite.url=localhost:8429,localhost:8431", "-remoteWrite.sendTimeout=10s,15s"},
697704
},
698705
}
699706
for _, tt := range tests {
@@ -702,7 +709,7 @@ func TestBuildRemoteWrites(t *testing.T) {
702709
got := BuildRemoteWrites(tt.args.cr, tt.args.rwsBasicAuth, tt.args.rwsTokens)
703710
sort.Strings(got)
704711
if !reflect.DeepEqual(got, tt.want) {
705-
t.Errorf("BuildRemoteWrites() = %v, want %v", got, tt.want)
712+
t.Errorf("BuildRemoteWrites() = \n%v\n, want \n%v\n", got, tt.want)
706713
}
707714
})
708715
}
@@ -970,3 +977,62 @@ func Test_buildConfigReloaderArgs(t *testing.T) {
970977
})
971978
}
972979
}
980+
981+
func TestBuildRemoteWriteSettings(t *testing.T) {
982+
type args struct {
983+
cr *victoriametricsv1beta1.VMAgent
984+
}
985+
tests := []struct {
986+
name string
987+
args args
988+
want []string
989+
}{
990+
{
991+
name: "test simple ok",
992+
args: args{
993+
cr: &victoriametricsv1beta1.VMAgent{},
994+
},
995+
},
996+
{
997+
name: "test labels",
998+
args: args{
999+
cr: &victoriametricsv1beta1.VMAgent{
1000+
Spec: victoriametricsv1beta1.VMAgentSpec{
1001+
RemoteWriteSettings: &victoriametricsv1beta1.VMAgentRemoteWriteSettings{
1002+
Labels: map[string]string{
1003+
"label-1": "value1",
1004+
"label-2": "value2",
1005+
},
1006+
},
1007+
},
1008+
},
1009+
},
1010+
want: []string{"-remoteWrite.label=label-1=value1,label-2=value2"},
1011+
},
1012+
{
1013+
name: "test label",
1014+
args: args{
1015+
cr: &victoriametricsv1beta1.VMAgent{
1016+
Spec: victoriametricsv1beta1.VMAgentSpec{
1017+
RemoteWriteSettings: &victoriametricsv1beta1.VMAgentRemoteWriteSettings{
1018+
ShowURL: pointer.Bool(true),
1019+
Labels: map[string]string{
1020+
"label-1": "value1",
1021+
},
1022+
},
1023+
},
1024+
},
1025+
},
1026+
want: []string{"-remoteWrite.label=label-1=value1", "-remoteWrite.showURL=true"},
1027+
},
1028+
}
1029+
for _, tt := range tests {
1030+
t.Run(tt.name, func(t *testing.T) {
1031+
got := BuildRemoteWriteSettings(tt.args.cr)
1032+
sort.Strings(got)
1033+
if !reflect.DeepEqual(got, tt.want) {
1034+
t.Errorf("BuildRemoteWriteSettings() = \n%v\n, want \n%v\n", got, tt.want)
1035+
}
1036+
})
1037+
}
1038+
}

docs/api.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ VMAgentRemoteWriteSettings - defines global settings for all remoteWrite urls.
211211
| showURL | Whether to show -remoteWrite.url in the exported metrics. It is hidden by default, since it can contain sensitive auth info | *bool | false |
212212
| tmpDataPath | Path to directory where temporary data for remote write component is stored (default vmagent-remotewrite-data) | *string | false |
213213
| flushInterval | Interval for flushing the data to remote storage. (default 1s) | *string | false |
214+
| label | Optional labels in the form 'name=value' to add to all the metrics before sending them | map[string]string | false |
214215

215216
[Back to TOC](#table-of-contents)
216217

@@ -223,7 +224,6 @@ VMAgentRemoteWriteSpec defines the remote storage configuration for VmAgent
223224
| url | URL of the endpoint to send samples to. | string | true |
224225
| basicAuth | BasicAuth allow an endpoint to authenticate over basic authentication | *[BasicAuth](#basicauth) | false |
225226
| bearerTokenSecret | Optional bearer auth token to use for -remoteWrite.url | *[v1.SecretKeySelector](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#secretkeyselector-v1-core) | false |
226-
| label | Optional labels in the form 'name=value' to add to all the metrics before sending them | map[string]string | false |
227227
| urlRelabelConfig | ConfigMap with relabeling config which is applied to metrics before sending them to the corresponding -remoteWrite.url | *v1.ConfigMapKeySelector | false |
228228
| tlsConfig | TLSConfig describes tls configuration for remote write target | *[TLSConfig](#tlsconfig) | false |
229229
| sendTimeout | Timeout for sending a single block of data to -remoteWrite.url (default 1m0s) | *string | false |

0 commit comments

Comments
 (0)