Skip to content

Commit 1ae4654

Browse files
author
Jelle Dijkstra
committed
Panic recovery and label prefixes
1 parent 23bf4a5 commit 1ae4654

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

internal/controller/shared_controller.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"strconv"
77
"time"
88

9+
"github.com/pkg/errors"
10+
911
"sigs.k8s.io/controller-runtime/pkg/client"
1012

1113
"github.com/pdok/smooth-operator/model"
@@ -21,7 +23,8 @@ import (
2123
)
2224

2325
const (
24-
AppLabelKey = "app"
26+
AppLabelKey = "pdok.nl/app"
27+
InspireLabelKey = "pdok.nl/inspire"
2528
)
2629

2730
func ttlExpired[O pdoknlv3.WMSWFS](obj O) bool {
@@ -68,7 +71,7 @@ func addCommonLabels[O pdoknlv3.WMSWFS](obj O, labels map[string]string) map[str
6871
inspire = any(obj).(*pdoknlv3.WMS).Spec.Service.Inspire != nil
6972
}
7073

71-
labels["inspire"] = strconv.FormatBool(inspire)
74+
labels[InspireLabelKey] = strconv.FormatBool(inspire)
7275

7376
return labels
7477
}
@@ -249,3 +252,20 @@ func createOrUpdateOrDeletePodDisruptionBudget[O pdoknlv3.WMSWFS, R Reconciler](
249252
}
250253
return nil
251254
}
255+
256+
func recoveredPanicToError(rec any) (err error) {
257+
switch x := rec.(type) {
258+
case string:
259+
err = errors.New(x)
260+
case error:
261+
err = x
262+
default:
263+
err = errors.New("unknown panic")
264+
}
265+
266+
// Add stack
267+
// TODO - this doesn't seem to work, see if there is a better method to add the stack
268+
err = errors.WithStack(err)
269+
270+
return
271+
}

internal/controller/wfs_controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ func (r *WFSReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result
9999
return result, err
100100
}
101101

102+
// Recover from a panic so we can add the error to the status of the Atom
103+
defer func() {
104+
if rec := recover(); rec != nil {
105+
err = recoveredPanicToError(rec)
106+
logAndUpdateStatusError(ctx, r, wfs, err)
107+
}
108+
}()
109+
102110
// Check TTL, delete if expired
103111
if ttlExpired(wfs) {
104112
err = r.Client.Delete(ctx, wfs)

internal/controller/wms_controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ func (r *WMSReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result
110110
return result, client.IgnoreNotFound(err)
111111
}
112112

113+
// Recover from a panic so we can add the error to the status of the Atom
114+
defer func() {
115+
if rec := recover(); rec != nil {
116+
err = recoveredPanicToError(rec)
117+
logAndUpdateStatusError(ctx, r, wms, err)
118+
}
119+
}()
120+
113121
// Check TTL, delete if expired
114122
if ttlExpired(wms) {
115123
err = r.Client.Delete(ctx, wms)

0 commit comments

Comments
 (0)