diff --git a/internal/controller/operator/factory/vmagent/vmagent_scrapeconfig_test.go b/internal/controller/operator/factory/vmagent/vmagent_scrapeconfig_test.go index 6059b2b1..b40bd302 100644 --- a/internal/controller/operator/factory/vmagent/vmagent_scrapeconfig_test.go +++ b/internal/controller/operator/factory/vmagent/vmagent_scrapeconfig_test.go @@ -11,6 +11,7 @@ import ( vmv1beta1 "github.com/VictoriaMetrics/operator/api/operator/v1beta1" "github.com/VictoriaMetrics/operator/internal/config" + "github.com/VictoriaMetrics/operator/internal/controller/operator/factory/build" "github.com/VictoriaMetrics/operator/internal/controller/operator/factory/k8stools" "github.com/stretchr/testify/assert" "gopkg.in/yaml.v2" @@ -975,12 +976,211 @@ scrape_configs: target_label: node - target_label: job replacement: default/test-good +`, + }, + { + name: "with changed default config value", + args: args{ + cr: &vmv1beta1.VMAgent{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + Namespace: "default", + }, + Spec: vmv1beta1.VMAgentSpec{ + ServiceScrapeNamespaceSelector: &metav1.LabelSelector{}, + ServiceScrapeSelector: &metav1.LabelSelector{}, + PodScrapeSelector: &metav1.LabelSelector{}, + PodScrapeNamespaceSelector: &metav1.LabelSelector{}, + NodeScrapeNamespaceSelector: &metav1.LabelSelector{}, + NodeScrapeSelector: &metav1.LabelSelector{}, + StaticScrapeNamespaceSelector: &metav1.LabelSelector{}, + StaticScrapeSelector: &metav1.LabelSelector{}, + ProbeNamespaceSelector: &metav1.LabelSelector{}, + ProbeSelector: &metav1.LabelSelector{}, + }, + }, + c: func() *config.BaseOperatorConf { + cfg := *config.MustGetBaseConfig() + cfg.VMServiceScrapeDefault.EnforceEndpointslices = true + return &cfg + }(), + }, + predefinedObjects: []runtime.Object{ + &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: "default", + }, + }, + &vmv1beta1.VMServiceScrape{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "default", + Name: "test-vms", + }, + Spec: vmv1beta1.VMServiceScrapeSpec{ + Selector: metav1.LabelSelector{}, + JobLabel: "app", + NamespaceSelector: vmv1beta1.NamespaceSelector{}, + Endpoints: []vmv1beta1.Endpoint{ + { + EndpointScrapeParams: vmv1beta1.EndpointScrapeParams{ + Path: "/metrics", + }, + Port: "8085", + EndpointAuth: vmv1beta1.EndpointAuth{ + BearerTokenSecret: &corev1.SecretKeySelector{ + Key: "bearer", + LocalObjectReference: corev1.LocalObjectReference{ + Name: "access-creds", + }, + }, + }, + }, + { + EndpointScrapeParams: vmv1beta1.EndpointScrapeParams{ + Path: "/metrics-2", + }, + Port: "8083", + }, + }, + }, + }, + &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: "access-creds", + Namespace: "default", + }, + Data: map[string][]byte{ + "cid": []byte(`some-client-id`), + "cs": []byte(`some-client-secret`), + "username": []byte(`some-username`), + "password": []byte(`some-password`), + "ca": []byte(`some-ca-cert`), + "cert": []byte(`some-cert`), + "key": []byte(`some-key`), + "bearer": []byte(`some-bearer`), + }, + }, + }, + wantConfig: `global: + scrape_interval: 30s + external_labels: + prometheus: default/test +scrape_configs: +- job_name: serviceScrape/default/test-vms/0 + kubernetes_sd_configs: + - role: endpointslices + namespaces: + names: + - default + honor_labels: false + metrics_path: /metrics + relabel_configs: + - action: keep + source_labels: + - __meta_kubernetes_endpointslice_port_name + regex: "8085" + - source_labels: + - __meta_kubernetes_endpointslice_address_target_kind + - __meta_kubernetes_endpointslice_address_target_name + separator: ; + regex: Node;(.*) + replacement: ${1} + target_label: node + - source_labels: + - __meta_kubernetes_endpointslice_address_target_kind + - __meta_kubernetes_endpointslice_address_target_name + separator: ; + regex: Pod;(.*) + replacement: ${1} + target_label: pod + - source_labels: + - __meta_kubernetes_pod_name + target_label: pod + - source_labels: + - __meta_kubernetes_pod_container_name + target_label: container + - source_labels: + - __meta_kubernetes_namespace + target_label: namespace + - source_labels: + - __meta_kubernetes_service_name + target_label: service + - source_labels: + - __meta_kubernetes_service_name + target_label: job + replacement: ${1} + - source_labels: + - __meta_kubernetes_service_label_app + target_label: job + regex: (.+) + replacement: ${1} + - target_label: endpoint + replacement: "8085" + bearer_token: some-bearer +- job_name: serviceScrape/default/test-vms/1 + kubernetes_sd_configs: + - role: endpointslices + namespaces: + names: + - default + honor_labels: false + metrics_path: /metrics-2 + relabel_configs: + - action: keep + source_labels: + - __meta_kubernetes_endpointslice_port_name + regex: "8083" + - source_labels: + - __meta_kubernetes_endpointslice_address_target_kind + - __meta_kubernetes_endpointslice_address_target_name + separator: ; + regex: Node;(.*) + replacement: ${1} + target_label: node + - source_labels: + - __meta_kubernetes_endpointslice_address_target_kind + - __meta_kubernetes_endpointslice_address_target_name + separator: ; + regex: Pod;(.*) + replacement: ${1} + target_label: pod + - source_labels: + - __meta_kubernetes_pod_name + target_label: pod + - source_labels: + - __meta_kubernetes_pod_container_name + target_label: container + - source_labels: + - __meta_kubernetes_namespace + target_label: namespace + - source_labels: + - __meta_kubernetes_service_name + target_label: service + - source_labels: + - __meta_kubernetes_service_name + target_label: job + replacement: ${1} + - source_labels: + - __meta_kubernetes_service_label_app + target_label: job + regex: (.+) + replacement: ${1} + - target_label: endpoint + replacement: "8083" `, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { testClient := k8stools.GetTestClientWithObjects(tt.predefinedObjects) + cfgO := *config.MustGetBaseConfig() + if tt.args.c != nil { + *config.MustGetBaseConfig() = *tt.args.c + defer func() { + *config.MustGetBaseConfig() = cfgO + }() + } + build.AddDefaults(testClient.Scheme()) if _, err := createOrUpdateConfigurationSecret(context.TODO(), tt.args.cr, testClient); (err != nil) != tt.wantErr { t.Errorf("CreateOrUpdateConfigurationSecret() error = %v, wantErr %v", err, tt.wantErr) } diff --git a/internal/controller/operator/factory/vmagent/vmagent_test.go b/internal/controller/operator/factory/vmagent/vmagent_test.go index ce174ec3..3e27a120 100644 --- a/internal/controller/operator/factory/vmagent/vmagent_test.go +++ b/internal/controller/operator/factory/vmagent/vmagent_test.go @@ -51,8 +51,12 @@ func TestCreateOrUpdateVMAgent(t *testing.T) { RemoteWrite: []vmv1beta1.VMAgentRemoteWriteSpec{ {URL: "http://remote-write"}, }, - StatefulMode: true, - IngestOnlyMode: true, + CommonApplicationDeploymentParams: vmv1beta1.CommonApplicationDeploymentParams{ + ReplicaCount: ptr.To(int32(1)), + }, + CommonDefaultableParams: vmv1beta1.CommonDefaultableParams{}, + StatefulMode: true, + IngestOnlyMode: true, StatefulStorage: &vmv1beta1.StorageSpec{ VolumeClaimTemplate: vmv1beta1.EmbeddedPersistentVolumeClaim{ Spec: corev1.PersistentVolumeClaimSpec{ @@ -126,6 +130,9 @@ func TestCreateOrUpdateVMAgent(t *testing.T) { Namespace: "default", }, Spec: vmv1beta1.VMAgentSpec{ + CommonApplicationDeploymentParams: vmv1beta1.CommonApplicationDeploymentParams{ + ReplicaCount: ptr.To(int32(0)), + }, RemoteWrite: []vmv1beta1.VMAgentRemoteWriteSpec{ {URL: "http://remote-write"}, }, @@ -404,6 +411,9 @@ func TestCreateOrUpdateVMAgent(t *testing.T) { RemoteWrite: []vmv1beta1.VMAgentRemoteWriteSpec{ {URL: "http://remote-write"}, }, + CommonApplicationDeploymentParams: vmv1beta1.CommonApplicationDeploymentParams{ + ReplicaCount: ptr.To(int32(1)), + }, StatefulMode: true, ServiceSpec: &vmv1beta1.AdditionalServiceSpec{ EmbeddedObjectMetadata: vmv1beta1.EmbeddedObjectMetadata{ @@ -523,6 +533,9 @@ func TestCreateOrUpdateVMAgent(t *testing.T) { RemoteWrite: []vmv1beta1.VMAgentRemoteWriteSpec{ {URL: "http://remote-write"}, }, + CommonApplicationDeploymentParams: vmv1beta1.CommonApplicationDeploymentParams{ + ReplicaCount: ptr.To(int32(1)), + }, StatefulMode: true, IngestOnlyMode: true, StatefulStorage: &vmv1beta1.StorageSpec{ @@ -602,6 +615,8 @@ func TestCreateOrUpdateVMAgent(t *testing.T) { tt.args.cr.Annotations["operator.victoriametrics/last-applied-spec"] = string(jsonSpec) } errC := make(chan error) + build.AddDefaults(fclient.Scheme()) + fclient.Scheme().Default(tt.args.cr) go func() { err := CreateOrUpdateVMAgent(context.TODO(), tt.args.cr, fclient) select { @@ -619,8 +634,10 @@ func TestCreateOrUpdateVMAgent(t *testing.T) { Namespace: "default", Name: fmt.Sprintf("vmagent-%s-%d", tt.args.cr.Name, i), }, &sts); err != nil { + return false, nil } + sts.Status.ObservedGeneration = sts.Generation sts.Status.ReadyReplicas = ptr.Deref(tt.args.cr.Spec.ReplicaCount, 0) sts.Status.UpdatedReplicas = ptr.Deref(tt.args.cr.Spec.ReplicaCount, 0) sts.Status.CurrentReplicas = ptr.Deref(tt.args.cr.Spec.ReplicaCount, 0) @@ -638,12 +655,18 @@ func TestCreateOrUpdateVMAgent(t *testing.T) { err := wait.PollUntilContextTimeout(context.Background(), 20*time.Millisecond, time.Second, false, func(ctx context.Context) (done bool, err error) { var sts appsv1.StatefulSet if err := fclient.Get(ctx, types.NamespacedName{Namespace: "default", Name: fmt.Sprintf("vmagent-%s", tt.args.cr.Name)}, &sts); err != nil { + + println("updating to err ", err.Error()) return false, nil } + println("updating to ,t ", *tt.args.cr.Spec.ReplicaCount) sts.Status.ReadyReplicas = ptr.Deref(tt.args.cr.Spec.ReplicaCount, 0) sts.Status.UpdatedReplicas = ptr.Deref(tt.args.cr.Spec.ReplicaCount, 0) sts.Status.CurrentReplicas = ptr.Deref(tt.args.cr.Spec.ReplicaCount, 0) - fclient.Status().Update(ctx, &sts) + err = fclient.Status().Update(ctx, &sts) + if err != nil { + return false, err + } return true, nil }) if err != nil {