fix: export collector config to ignore empty string (#1674) · GoogleCloudPlatform/prometheus-engine@c8695de (original) (raw)

Original file line number Diff line number Diff line change
@@ -276,18 +276,18 @@ func (r *collectionReconciler) ensureCollectorConfig(ctx context.Context, spec *
276 276 return fmt.Errorf("generate Prometheus config: %w", err)
277 277 }
278 278
279 -var credentialsFile string
280 -if spec.Credentials != nil {
281 -credentialsFile = path.Join(secretsDir, pathForSelector(r.opts.PublicNamespace, &monitoringv1.SecretOrConfigMap{Secret: spec.Credentials}))
282 - }
283 -
284 279 cfg.GoogleCloud = &GoogleCloudConfig{
285 280 Export: &GoogleCloudExportConfig{
286 -Compression: ptr.To(string(compression)),
287 -CredentialsFile: ptr.To(credentialsFile),
288 -Match: spec.Filter.MatchOneOf,
281 +Match: spec.Filter.MatchOneOf,
289 282 },
290 283 }
284 +if string(compression) != "" {
285 +cfg.GoogleCloud.Export.Compression = ptr.To(string(compression))
286 + }
287 +if spec.Credentials != nil {
288 +credentialsFile := path.Join(secretsDir, pathForSelector(r.opts.PublicNamespace, &monitoringv1.SecretOrConfigMap{Secret: spec.Credentials}))
289 +cfg.GoogleCloud.Export.CredentialsFile = ptr.To(credentialsFile)
290 + }
291 291
292 292 cfgEncoded, err := yaml.Marshal(cfg)
293 293 if err != nil {
Original file line number Diff line number Diff line change
@@ -110,11 +110,12 @@ func TestCollectionReconcile(t *testing.T) {
110 110 Interval: "10s",
111 111 },
112 112 }
113 -
113 + exampleCollectorConfigMapWithoutScrapeConfig := "global: {}\ngoogle_cloud:\n export: {}\n"
114 114 testCases := []struct {
115 -desc string
116 -input monitoringv1.MonitoringCRD
117 -expected monitoringv1.MonitoringCRD
115 +desc string
116 +input monitoringv1.MonitoringCRD
117 +expected monitoringv1.MonitoringCRD
118 +expectedCollectorConfigMap *string
118 119 }{
119 120 {
120 121 desc: "podmonitoring: no update",
@@ -413,6 +414,7 @@ func TestCollectionReconcile(t *testing.T) {
413 414 },
414 415 },
415 416 },
417 +expectedCollectorConfigMap: &exampleCollectorConfigMapWithoutScrapeConfig,
416 418 },
417 419 }
418 420
@@ -466,6 +468,21 @@ func TestCollectionReconcile(t *testing.T) {
466 468 t.Fatal(err)
467 469 }
468 470
471 +if tc.expectedCollectorConfigMap != nil {
472 +collectorConfigMap := &corev1.ConfigMap{
473 +ObjectMeta: metav1.ObjectMeta{
474 +Namespace: opts.OperatorNamespace,
475 +Name: NameCollector,
476 + },
477 + }
478 +if err := kubeClient.Get(ctx, client.ObjectKeyFromObject(collectorConfigMap), collectorConfigMap); err != nil {
479 +t.Fatal(err)
480 + }
481 +if diff := cmp.Diff(*tc.expectedCollectorConfigMap, collectorConfigMap.Data["config.yaml"]); diff != "" {
482 +t.Fatalf("unexpected collector configmap (-want, +got): %s", diff)
483 + }
484 + }
485 +
469 486 if err := kubeClient.Get(ctx, client.ObjectKeyFromObject(tc.input), tc.input); err != nil {
470 487 t.Fatal(err)
471 488 }