Skip to content

Commit c8695de

Browse files
authored
fix: export collector config to ignore empty string (#1674)
Compression and CredentialsFile could be empty string, in these cases they should not be populated into collector configmap.
2 parents 22ea449 + 3190e05 commit c8695de

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

pkg/operator/collection.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,18 +276,18 @@ func (r *collectionReconciler) ensureCollectorConfig(ctx context.Context, spec *
276276
return fmt.Errorf("generate Prometheus config: %w", err)
277277
}
278278

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-
284279
cfg.GoogleCloud = &GoogleCloudConfig{
285280
Export: &GoogleCloudExportConfig{
286-
Compression: ptr.To(string(compression)),
287-
CredentialsFile: ptr.To(credentialsFile),
288-
Match: spec.Filter.MatchOneOf,
281+
Match: spec.Filter.MatchOneOf,
289282
},
290283
}
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+
}
291291

292292
cfgEncoded, err := yaml.Marshal(cfg)
293293
if err != nil {

pkg/operator/collection_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ func TestCollectionReconcile(t *testing.T) {
110110
Interval: "10s",
111111
},
112112
}
113-
113+
exampleCollectorConfigMapWithoutScrapeConfig := "global: {}\ngoogle_cloud:\n export: {}\n"
114114
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
118119
}{
119120
{
120121
desc: "podmonitoring: no update",
@@ -413,6 +414,7 @@ func TestCollectionReconcile(t *testing.T) {
413414
},
414415
},
415416
},
417+
expectedCollectorConfigMap: &exampleCollectorConfigMapWithoutScrapeConfig,
416418
},
417419
}
418420

@@ -466,6 +468,21 @@ func TestCollectionReconcile(t *testing.T) {
466468
t.Fatal(err)
467469
}
468470

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+
469486
if err := kubeClient.Get(ctx, client.ObjectKeyFromObject(tc.input), tc.input); err != nil {
470487
t.Fatal(err)
471488
}

0 commit comments

Comments
 (0)