Skip to content
Merged
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
36 changes: 31 additions & 5 deletions internal/controller/atom_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"fmt"
"time"

"github.com/pkg/errors"

policyv1 "k8s.io/api/policy/v1"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand All @@ -50,13 +52,13 @@ import (
)

const (
reconciledConditionType = "Reconciled"
reconciledConditionReasonSucces = "Succes"
reconciledConditionReasonError = "Error"
reconciledConditionType = "Reconciled"
reconciledConditionReasonSuccess = "Success"
reconciledConditionReasonError = "Error"
)

const (
appLabelKey = "app"
appLabelKey = "pdok.nl/app"
appName = "atom-service"
configFileName = "values.yaml"
atomPortName = "atom-service"
Expand Down Expand Up @@ -132,6 +134,14 @@ func (r *AtomReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resul
return result, client.IgnoreNotFound(err)
}

// Recover from a panic so we can add the error to the status of the Atom
defer func() {
if rec := recover(); rec != nil {
err = recoveredPanicToError(rec)
r.logAndUpdateStatusError(ctx, atom, err)
}
}()

// Check TTL expiry
if ttlExpired(atom) {
err = r.Client.Delete(ctx, atom)
Expand All @@ -150,7 +160,6 @@ func (r *AtomReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resul
r.logAndUpdateStatusFinished(ctx, atom, operationResults)

return result, err

}

func (r *AtomReconciler) createOrUpdateAllForAtom(ctx context.Context, atom *pdoknlv3.Atom, ownerInfo *smoothoperatorv1.OwnerInfo) (operationResults map[string]controllerutil.OperationResult, err error) {
Expand Down Expand Up @@ -276,3 +285,20 @@ func ttlExpired(atom *pdoknlv3.Atom) bool {

return false
}

func recoveredPanicToError(rec any) (err error) {
switch x := rec.(type) {
case string:
err = errors.New(x)
case error:
err = x
default:
err = errors.New("unknown panic")
}

// Add stack
// TODO - this doesn't seem to work, see if there is a better method to add the stack
err = errors.WithStack(err)

return
}
5 changes: 4 additions & 1 deletion internal/controller/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
)

func (r *AtomReconciler) logAndUpdateStatusError(ctx context.Context, atom *pdoknlv3.Atom, err error) {
lgr := logf.FromContext(ctx)
lgr.Error(err, "reconcile error")

r.updateStatus(ctx, atom, []metav1.Condition{{
Type: reconciledConditionType,
Status: metav1.ConditionFalse,
Expand All @@ -31,7 +34,7 @@ func (r *AtomReconciler) logAndUpdateStatusFinished(ctx context.Context, atom *p
r.updateStatus(ctx, atom, []metav1.Condition{{
Type: reconciledConditionType,
Status: metav1.ConditionTrue,
Reason: reconciledConditionReasonSucces,
Reason: reconciledConditionReasonSuccess,
ObservedGeneration: atom.Generation,
LastTransitionTime: metav1.NewTime(time.Now()),
}}, operationResults)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: default
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: default
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand All @@ -23,7 +23,7 @@ spec:
selector:
matchLabels:
test: test
app: atom-service
pdok.nl/app: atom-service
revisionHistoryLimit: 1
template:
metadata:
Expand All @@ -33,7 +33,7 @@ spec:
priority.version-checker.io/atom-service: "8"
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
spec:
containers:
- name: atom-service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ metadata:
uptime.pdok.nl/tags: public-stats,atom
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: default
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: default
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: default
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: default
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: default
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: default
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand All @@ -18,4 +18,4 @@ spec:
selector:
matchLabels:
test: test
app: atom-service
pdok.nl/app: atom-service
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: default
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand All @@ -20,4 +20,4 @@ spec:
protocol: TCP
selector:
test: test
app: atom-service
pdok.nl/app: atom-service
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: default
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: default
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand All @@ -23,7 +23,7 @@ spec:
selector:
matchLabels:
test: test
app: atom-service
pdok.nl/app: atom-service
revisionHistoryLimit: 1
template:
metadata:
Expand All @@ -33,7 +33,7 @@ spec:
priority.version-checker.io/atom-service: "8"
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
spec:
containers:
- name: atom-service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ metadata:
uptime.pdok.nl/tags: public-stats,atom
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: default
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: default
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: default
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: default
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand All @@ -18,4 +18,4 @@ spec:
selector:
matchLabels:
test: test
app: atom-service
pdok.nl/app: atom-service
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: default
labels:
test: test
app: atom-service
pdok.nl/app: atom-service
ownerReferences:
- apiVersion: pdok.nl/v3
kind: Atom
Expand All @@ -20,4 +20,4 @@ spec:
protocol: TCP
selector:
test: test
app: atom-service
pdok.nl/app: atom-service
Loading