Skip to content

Commit 51acea2

Browse files
adityathebemoshloop
authored andcommitted
fix(reconciler): avoid panic converting unstructured objects
1 parent 86b4141 commit 51acea2

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

reconciler.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package kopper
22

33
import (
44
gocontext "context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"time"
@@ -94,7 +95,15 @@ func (r *Reconciler[T, PT]) Reconcile(ctx gocontext.Context, req ctrl.Request) (
9495
resourceName := fmt.Sprintf("%s[%s/%s:%s]", r.gvk.Kind, req.Namespace, req.Name, raw.GetUID())
9596

9697
obj := PT(new(T))
97-
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(raw.Object, obj); err != nil {
98+
payload, err := json.Marshal(raw.Object)
99+
if err != nil {
100+
logger.Errorf("[kopper] malformed resource %s: %v", resourceName, err)
101+
r.Events.Event(raw, "Warning", "MalformedResource",
102+
fmt.Sprintf("Resource spec does not match expected schema: %v", err))
103+
return ctrl.Result{}, fmt.Errorf("failed to marshal unstructured resource: %w", err)
104+
}
105+
106+
if err := json.Unmarshal(payload, obj); err != nil {
98107
logger.Errorf("[kopper] malformed resource %s: %v", resourceName, err)
99108
r.Events.Event(raw, "Warning", "MalformedResource",
100109
fmt.Sprintf("Resource spec does not match expected schema: %v", err))

0 commit comments

Comments
 (0)