Skip to content

Commit

Permalink
Fall back to default with empty string as key
Browse files Browse the repository at this point in the history
Signed-off-by: Baiju Muthukadan <baiju.m.mail@gmail.com>
  • Loading branch information
baijum committed Aug 24, 2022
1 parent 96161c4 commit a974e46
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion apis/core/v1alpha1/field_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (`<namespace>.<FieldExport-resource-name>`) for the FieldExport target
Key *string `json:"key,omitempty"`
}

// FieldExportSpec defines the desired state of the FieldExport.
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/services.k8s.aws_fieldexports.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ spec:
the output path for a field export.
properties:
key:
description: Key overrides the default value (`<namespace>.<FieldExport-resource-name>`)
for the FieldExport target
type: string
kind:
description: FieldExportOutputType represents all types that can
Expand Down
4 changes: 2 additions & 2 deletions pkg/runtime/field_export_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
30 changes: 30 additions & 0 deletions pkg/runtime/field_export_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a974e46

Please sign in to comment.