Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pkg/condition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,21 @@ func WithReferencesResolvedCondition(
return ko
}

// WithTerminalCondition returns a new AWSResource with the
// ConditionTypeTerminal set based on the err parameter
func WithTerminalCondition(
resource acktypes.AWSResource,
err error,
) acktypes.AWSResource {
ko := resource.DeepCopy()

if err != nil {
errString := err.Error()
SetTerminal(ko, corev1.ConditionTrue, nil, &errString)
}
return ko
}

// LateInitializationInProgress return true if ConditionTypeLateInitialized has "False" status
// False status means that resource has LateInitializationConfig but has not been completely
// late initialized yet.
Expand Down
4 changes: 2 additions & 2 deletions pkg/runtime/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func (r *resourceReconciler) handlePopulation(
if adoptionPolicy == AdoptionPolicy_AdoptOrCreate {
return desired, nil
}
return desired, ackerr.NewTerminalError(err)
return ackcondition.WithTerminalCondition(desired, err), ackerr.Terminal
}

populated := desired.DeepCopy()
Expand Down Expand Up @@ -554,7 +554,7 @@ func (r *resourceReconciler) Sync(
if needAdoption {
populated, err := r.handlePopulation(ctx, desired)
if err != nil {
return nil, err
return populated, err
}
if adoptionPolicy == AdoptionPolicy_AdoptOrCreate {
// here we assume the spec fields are provided in the spec.
Expand Down
24 changes: 11 additions & 13 deletions pkg/runtime/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,30 +208,28 @@ func NeedAdoption(res acktypes.AWSResource) bool {
}

func ExtractAdoptionFields(res acktypes.AWSResource) (map[string]string, error) {
fields := getAdoptionFields(res)

extractedFields := &map[string]string{}
err := json.Unmarshal([]byte(fields), extractedFields)
fields, ok := getAdoptionFields(res)
if !ok {
return nil, fmt.Errorf("%s annotation is not defined. Cannot extract resource identifiers", ackv1alpha1.AnnotationAdoptionFields)
}
extractedFields := map[string]string{}
err := json.Unmarshal([]byte(fields), &extractedFields)
if err != nil {
return nil, err
return nil, fmt.Errorf("error parsing content of %s annotation: %w", ackv1alpha1.AnnotationAdoptionFields, err)
}

return *extractedFields, nil
return extractedFields, nil
}

func getAdoptionFields(res acktypes.AWSResource) string {
func getAdoptionFields(res acktypes.AWSResource) (string, bool) {
mo := res.MetaObject()
if mo == nil {
// Should never happen... if it does, it's buggy code.
panic("ExtractRequiredFields received resource with nil RuntimeObject")
}

for k, v := range mo.GetAnnotations() {
if k == ackv1alpha1.AnnotationAdoptionFields {
return v
}
}
return ""
fields, ok := mo.GetAnnotations()[ackv1alpha1.AnnotationAdoptionFields]
return fields, ok
}

// patchObject performs a patch operation using context.WithoutCancel to prevent
Expand Down
31 changes: 31 additions & 0 deletions pkg/runtime/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package runtime

import (
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -118,3 +120,32 @@ func TestExtractAdoptionFields(t *testing.T) {
require.NoError(err)
require.Equal(expected, actual)
}

func TestExtractAdoptionFields_NoAdoptionFields(t *testing.T) {
require := require.New(t)

res := &mocks.AWSResource{}
res.On("MetaObject").Return(&metav1.ObjectMeta{
Annotations: map[string]string{},
})

_, err := ExtractAdoptionFields(res)
require.Error(err)
require.Equal(err, fmt.Errorf("%s annotation is not defined. Cannot extract resource identifiers", ackv1alpha1.AnnotationAdoptionFields))
}

func TestExtractAdoptionFields_InvalidAdoptionFields(t *testing.T) {
require := require.New(t)

res := &mocks.AWSResource{}
res.On("MetaObject").Return(&metav1.ObjectMeta{
Annotations: map[string]string{
ackv1alpha1.AnnotationAdoptionFields: "misconfigured",
},
})

_, err := ExtractAdoptionFields(res)
require.Error(err)
expectedErr := fmt.Sprintf("error parsing content of %s annotation", ackv1alpha1.AnnotationAdoptionFields)
require.True(strings.Contains(err.Error(), expectedErr))
}