Repository: VictoriaMetrics/operator

Issue:
Incorrect generation of vmagent config from VMScrapeConfig (kubernetesSDConfigs/attach_metadata)
```
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMScrapeConfig
metadata:
  name: pods
spec:
  kubernetesSDConfigs:
  - attach_metadata:
      node: true
    role: pod
```

and incorrect result in `/etc/vmagent/config_out/vmagent.env.yaml`:
```
- job_name: scrapeConfig/vm-stack/pods
  kubernetes_sd_configs:
  - role: pod
    attach_metadata: true
```

should be:
```
- job_name: scrapeConfig/vm-stack/pods
  kubernetes_sd_configs:
  - role: pod
    attach_metadata:
      node: true
```

Version: `victoriametrics/operator:v0.60.2`

Hints from issue comments:
@AndrewChubatiuk: fix was relased in 0.61.2

Solution patch:
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index 6690916d..db7b3ea0 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -13,6 +13,8 @@ aliases:
 
 ## tip
 
+* BUGFIX: [vmagent](https://docs.victoriametrics.com/operator/resources/vmagent/): properly add `attach_metadata` section to scrape configuration. See [#1476](https://github.com/VictoriaMetrics/operator/issues/1476).
+
 Dependency: [vmoperator](https://docs.victoriametrics.com/operator/): Updated default versions for VM apps to [v1.122.0](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.122.0) version
 
 ## [v0.61.1](https://github.com/VictoriaMetrics/operator/releases/tag/v0.61.1)
diff --git a/internal/controller/operator/factory/vmagent/scrapeconfig.go b/internal/controller/operator/factory/vmagent/scrapeconfig.go
index 36578fda..056e4a5a 100644
--- a/internal/controller/operator/factory/vmagent/scrapeconfig.go
+++ b/internal/controller/operator/factory/vmagent/scrapeconfig.go
@@ -223,10 +223,7 @@ func generateScrapeConfig(
 			if config.AttachMetadata.Node != nil {
 				switch config.Role {
 				case "pod", "endpoints", "endpointslice":
-					configs[i] = append(configs[i], yaml.MapItem{
-						Key:   "attach_metadata",
-						Value: *config.AttachMetadata.Node,
-					})
+					configs[i] = addAttachMetadata(configs[i], &config.AttachMetadata)
 				}
 			}
 



Test patch:
diff --git a/internal/controller/operator/factory/vmagent/scrapeconfig_test.go b/internal/controller/operator/factory/vmagent/scrapeconfig_test.go
index 7fd5cd83..38260f24 100644
--- a/internal/controller/operator/factory/vmagent/scrapeconfig_test.go
+++ b/internal/controller/operator/factory/vmagent/scrapeconfig_test.go
@@ -312,7 +312,8 @@ kubernetes_sd_configs:
   role: pod
   tls_config:
     insecure_skip_verify: true
-  attach_metadata: true
+  attach_metadata:
+    node: true
   selectors:
   - role: pod
     label: app/instance