diff --git a/apis/core/v1alpha1/field_export.go b/apis/core/v1alpha1/field_export.go index 3b8a050..df1e53e 100644 --- a/apis/core/v1alpha1/field_export.go +++ b/apis/core/v1alpha1/field_export.go @@ -24,7 +24,8 @@ type FieldExportTarget struct { // Namespace is marked as optional, so we cannot compose `NamespacedName` Namespace *string `json:"namespace,omitempty"` Kind FieldExportOutputType `json:"kind"` - Key *string `json:"key,omitempty"` + // Key overrides the default value (`.`) for the FieldExport target + Key *string `json:"key,omitempty"` } // FieldExportSpec defines the desired state of the FieldExport. diff --git a/config/crd/bases/services.k8s.aws_fieldexports.yaml b/config/crd/bases/services.k8s.aws_fieldexports.yaml index b04e05f..a435de8 100644 --- a/config/crd/bases/services.k8s.aws_fieldexports.yaml +++ b/config/crd/bases/services.k8s.aws_fieldexports.yaml @@ -67,6 +67,8 @@ spec: the output path for a field export. properties: key: + description: Key overrides the default value (`.`) + for the FieldExport target type: string kind: description: FieldExportOutputType represents all types that can diff --git a/pkg/runtime/field_export_reconciler.go b/pkg/runtime/field_export_reconciler.go index 68c77b6..2c971c6 100644 --- a/pkg/runtime/field_export_reconciler.go +++ b/pkg/runtime/field_export_reconciler.go @@ -296,7 +296,7 @@ func (r *fieldExportReconciler) writeToConfigMap( ) error { // Construct the data key key := fmt.Sprintf("%s.%s", desired.Namespace, desired.Name) - if desired.Spec.To.Key != nil { + if desired.Spec.To.Key != nil && strings.TrimSpace(*desired.Spec.To.Key) != "" { key = *desired.Spec.To.Key } @@ -343,7 +343,7 @@ func (r *fieldExportReconciler) writeToSecret( ) error { // Construct the data key key := fmt.Sprintf("%s.%s", desired.Namespace, desired.Name) - if desired.Spec.To.Key != nil { + if desired.Spec.To.Key != nil && strings.TrimSpace(*desired.Spec.To.Key) != "" { key = *desired.Spec.To.Key } diff --git a/pkg/runtime/field_export_reconciler_test.go b/pkg/runtime/field_export_reconciler_test.go index d7ce419..d11d67f 100644 --- a/pkg/runtime/field_export_reconciler_test.go +++ b/pkg/runtime/field_export_reconciler_test.go @@ -512,6 +512,36 @@ func TestSync_SetKeyNameExplicitly(t *testing.T) { assertPatchedSecretWithKey(true, t, ctx, kc, "new-key") } +func TestSync_SetKeyNameExplicitlyWithEmptyString(t *testing.T) { + // Setup + require := require.New(t) + // Mock resource creation + r, kc, apiReader := mockFieldExportReconciler() + descriptor, res, _ := mockDescriptorAndAWSResource() + manager := mockManager() + fieldExport := fieldExportWithKey(FieldExportNamespace, FieldExportName, ackv1alpha1.FieldExportOutputTypeSecret, "") + sourceResource, _, _ := mockSourceResource() + ctx := context.TODO() + statusWriter := &ctrlrtclientmock.StatusWriter{} + + //Mock behavior setup + setupMockClientForFieldExport(kc, statusWriter, ctx, fieldExport) + setupMockApiReaderForFieldExport(apiReader, ctx, res) + setupMockManager(manager, ctx, res) + setupMockDescriptor(descriptor, res) + setupMockUnstructuredConverter() + + // Call + latest, err := r.Sync(ctx, sourceResource, *fieldExport) + + //Assertions + require.Nil(err) + require.NotNil(latest.Status) + require.Len(latest.Status.Conditions, 0) + assertPatchedConfigMap(false, t, ctx, kc) + assertPatchedSecret(true, t, ctx, kc) +} + // Assertions func assertPatchedConfigMap(expected bool, t *testing.T, ctx context.Context, kc *ctrlrtclientmock.Client) {