Skip to content

Commit afcfcf2

Browse files
committed
Improve reconcile
1 parent 54b5de5 commit afcfcf2

File tree

1 file changed

+27
-43
lines changed

1 file changed

+27
-43
lines changed

internal/reconciler/implementation.go

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ func newObject(objectType client.Object) client.Object {
6767
}
6868

6969
const (
70-
webhookValidationErrorLogMsg = "Rejected the resource because the Gateway API webhook failed to reject it with" +
71-
" a validation error; make sure the webhook is installed and running correctly"
70+
webhookValidationErrorLogMsg = "Rejected the resource because the Gateway API webhook failed to reject it with " +
71+
"a validation error; make sure the webhook is installed and running correctly; " +
72+
"NKG will delete any existing NGINX configuration that corresponds to the resource"
7273
)
7374

7475
// Reconcile implements the reconcile.Reconciler Reconcile method.
@@ -79,30 +80,45 @@ func (r *Implementation) Reconcile(ctx context.Context, req reconcile.Request) (
7980

8081
logger.Info("Reconciling the resource")
8182

83+
if r.cfg.NamespacedNameFilter != nil {
84+
if allow, msg := r.cfg.NamespacedNameFilter(req.NamespacedName); !allow {
85+
logger.Info(msg)
86+
return reconcile.Result{}, nil
87+
}
88+
}
89+
8290
obj := newObject(r.cfg.ObjectType)
8391
err := r.cfg.Getter.Get(ctx, req.NamespacedName, obj)
8492
if err != nil {
8593
if !apierrors.IsNotFound(err) {
8694
logger.Error(err, "Failed to get the resource")
8795
return reconcile.Result{}, err
8896
}
89-
// The resource does not exist
97+
// The resource does not exist (was deleted).
9098
obj = nil
9199
}
92100

93-
if r.cfg.NamespacedNameFilter != nil {
94-
if allow, msg := r.cfg.NamespacedNameFilter(req.NamespacedName); !allow {
95-
logger.Info(msg)
96-
return reconcile.Result{}, nil
97-
}
98-
}
99-
100101
var validationError error
101102
if obj != nil && r.cfg.WebhookValidator != nil {
102103
validationError = r.cfg.WebhookValidator(obj)
103104
}
104105

105-
e := generateEvent(r.cfg.ObjectType, req.NamespacedName, obj, validationError)
106+
var e interface{}
107+
var op string
108+
109+
if obj == nil || validationError != nil {
110+
// In case of a validation error, we handle the resource as if it was deleted.
111+
e = &events.DeleteEvent{
112+
Type: r.cfg.ObjectType,
113+
NamespacedName: req.NamespacedName,
114+
}
115+
op = "Deleted"
116+
} else {
117+
e = &events.UpsertEvent{
118+
Resource: obj,
119+
}
120+
op = "Upserted"
121+
}
106122

107123
select {
108124
case <-ctx.Done():
@@ -115,41 +131,9 @@ func (r *Implementation) Reconcile(ctx context.Context, req reconcile.Request) (
115131
logger.Error(validationError, webhookValidationErrorLogMsg)
116132
r.cfg.EventRecorder.Eventf(obj, apiv1.EventTypeWarning, "Rejected",
117133
webhookValidationErrorLogMsg+"; validation error: %v", validationError)
118-
return reconcile.Result{}, nil
119-
}
120-
121-
op := "Upserted"
122-
if _, deleted := e.(*events.DeleteEvent); deleted {
123-
op = "Deleted"
124134
}
125135

126136
logger.Info(fmt.Sprintf("%s the resource", op))
127137

128138
return reconcile.Result{}, nil
129139
}
130-
131-
func generateEvent(
132-
objType client.Object,
133-
nsName types.NamespacedName,
134-
obj client.Object,
135-
validationErr error,
136-
) interface{} {
137-
if obj == nil {
138-
return &events.DeleteEvent{
139-
Type: objType,
140-
NamespacedName: nsName,
141-
}
142-
}
143-
144-
if validationErr != nil {
145-
// If the resource is invalid, we will delete it from the NGINX configuration.
146-
return &events.DeleteEvent{
147-
Type: objType,
148-
NamespacedName: nsName,
149-
}
150-
}
151-
152-
return &events.UpsertEvent{
153-
Resource: obj,
154-
}
155-
}

0 commit comments

Comments
 (0)