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
9 changes: 4 additions & 5 deletions internal/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func NewResource(ctx context.Context, slice *apiv1.ResourceSlice, index int) (*R
if str, ok := anno[reconcileIntervalKey]; ok {
reconcileInterval, err := time.ParseDuration(str)
if anno[reconcileIntervalKey] != "" && err != nil {
logger.V(0).Info("invalid reconcile interval - ignoring")
return nil, fmt.Errorf("invalid reconcile interval, %s", str)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's safe to make these terminal errors but we should add an integration test in the reconciliation controller to prove that producing invalid resources doesn't cause a deadlock state e.g. synthesis is blocked because the previous synthesis is invalid

}
res.ReconcileInterval = &metav1.Duration{Duration: reconcileInterval}
}
Expand All @@ -259,18 +259,17 @@ func NewResource(ctx context.Context, slice *apiv1.ResourceSlice, index int) (*R
if js, ok := anno[overridesKey]; ok {
err = json.Unmarshal([]byte(js), &res.Overrides)
if err != nil {
logger.Error(err, "invalid override json")
return nil, fmt.Errorf("invalid override json, %s", err)
}
}

const readinessGroupKey = "eno.azure.io/readiness-group"
if str, ok := anno[readinessGroupKey]; ok {
rg, err := strconv.Atoi(str)
if err != nil {
logger.V(0).Info("invalid readiness group - ignoring")
} else {
res.ReadinessGroup = rg
return nil, fmt.Errorf("invalid readiness group, %s", str)
}
res.ReadinessGroup = rg
}

for key, value := range anno {
Expand Down
80 changes: 64 additions & 16 deletions internal/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,22 +262,6 @@ var newResourceTests = []struct {
}, r.parsed)
},
},
{
Name: "invalid-override-json",
Manifest: `{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "foo",
"annotations": {
"eno.azure.io/overrides": "not json"
}
}
}`,
Assert: func(t *testing.T, r *Resource) {
assert.Len(t, r.Overrides, 0)
},
},
{
Name: "labels",
Manifest: `{
Expand Down Expand Up @@ -328,6 +312,70 @@ func TestNewResource(t *testing.T) {
}
}

func TestNewResourceFailures(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
manifest string
wantErr string
}{
{
name: "invalid-override-json",
manifest: `{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "foo",
"annotations": {
"eno.azure.io/overrides": "[{\"path\":\".foo\", invalid json"
}
}
}`,
wantErr: "invalid override json",
},
{
name: "bad-duration-reconcile-interval",
manifest: `{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "foo",
"annotations": {
"eno.azure.io/reconcile-interval": "not-a-duration"
}
}
}`,
wantErr: "invalid reconcile interval",
},
{
name: "not-a-number-readiness-group",
manifest: `{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "foo",
"annotations": {
"eno.azure.io/readiness-group": "not-a-number"
}
}
}`,
wantErr: "invalid readiness group",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
_, err := NewResource(ctx, &apiv1.ResourceSlice{
Spec: apiv1.ResourceSliceSpec{
Resources: []apiv1.Manifest{{Manifest: tc.manifest}},
},
}, 0)
require.Error(t, err)
assert.Contains(t, err.Error(), tc.wantErr)
})
}
}

func TestResourceOrdering(t *testing.T) {
resources := []*Resource{
{ManifestHash: []byte("a")},
Expand Down
Loading