Skip to content

Commit bd55eae

Browse files
Improve error message when adoption-fields annotation is missing
When using `adoption-policy: adopt` without the `adoption-fields` annotation, `ExtractAdoptionFields` calls `json.Unmarshal` on an empty string, producing a generic "unexpected end of JSON input" error that gives no indication of the actual problem. This change adds an explicit empty-string check that returns a descriptive error explaining that the `adoption-fields` annotation is required, and wraps JSON parse errors with context about which annotation failed. Fixes aws-controllers-k8s/community#2824
1 parent a52a686 commit bd55eae

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

pkg/runtime/util.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,18 @@ func NeedAdoption(res acktypes.AWSResource) bool {
209209

210210
func ExtractAdoptionFields(res acktypes.AWSResource) (map[string]string, error) {
211211
fields := getAdoptionFields(res)
212+
if fields == "" {
213+
return nil, fmt.Errorf(
214+
"services.k8s.aws/adoption-fields annotation is required when " +
215+
"adoption-policy is set to 'adopt'; please provide the resource " +
216+
"identifier fields as a JSON object (e.g. '{\"name\": \"my-resource\"}')",
217+
)
218+
}
212219

213220
extractedFields := &map[string]string{}
214221
err := json.Unmarshal([]byte(fields), extractedFields)
215222
if err != nil {
216-
return nil, err
223+
return nil, fmt.Errorf("failed to parse services.k8s.aws/adoption-fields annotation as JSON: %w", err)
217224
}
218225

219226
return *extractedFields, nil

pkg/runtime/util_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package runtime
1515

1616
import (
17+
"strings"
1718
"testing"
1819

1920
"github.com/stretchr/testify/require"
@@ -118,3 +119,33 @@ func TestExtractAdoptionFields(t *testing.T) {
118119
require.NoError(err)
119120
require.Equal(expected, actual)
120121
}
122+
123+
func TestExtractAdoptionFields_EmptyFields(t *testing.T) {
124+
require := require.New(t)
125+
126+
// No adoption-fields annotation at all
127+
res := &mocks.AWSResource{}
128+
res.On("MetaObject").Return(&metav1.ObjectMeta{
129+
Annotations: map[string]string{},
130+
})
131+
132+
_, err := ExtractAdoptionFields(res)
133+
require.Error(err)
134+
require.True(strings.Contains(err.Error(), "services.k8s.aws/adoption-fields annotation is required"))
135+
require.True(strings.Contains(err.Error(), "adoption-policy is set to 'adopt'"))
136+
}
137+
138+
func TestExtractAdoptionFields_InvalidJSON(t *testing.T) {
139+
require := require.New(t)
140+
141+
res := &mocks.AWSResource{}
142+
res.On("MetaObject").Return(&metav1.ObjectMeta{
143+
Annotations: map[string]string{
144+
ackv1alpha1.AnnotationAdoptionFields: `not valid json`,
145+
},
146+
})
147+
148+
_, err := ExtractAdoptionFields(res)
149+
require.Error(err)
150+
require.True(strings.Contains(err.Error(), "failed to parse services.k8s.aws/adoption-fields annotation as JSON"))
151+
}

0 commit comments

Comments
 (0)