@@ -67,8 +67,9 @@ func newObject(objectType client.Object) client.Object {
67
67
}
68
68
69
69
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"
72
73
)
73
74
74
75
// Reconcile implements the reconcile.Reconciler Reconcile method.
@@ -79,30 +80,45 @@ func (r *Implementation) Reconcile(ctx context.Context, req reconcile.Request) (
79
80
80
81
logger .Info ("Reconciling the resource" )
81
82
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
+
82
90
obj := newObject (r .cfg .ObjectType )
83
91
err := r .cfg .Getter .Get (ctx , req .NamespacedName , obj )
84
92
if err != nil {
85
93
if ! apierrors .IsNotFound (err ) {
86
94
logger .Error (err , "Failed to get the resource" )
87
95
return reconcile.Result {}, err
88
96
}
89
- // The resource does not exist
97
+ // The resource does not exist (was deleted).
90
98
obj = nil
91
99
}
92
100
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
-
100
101
var validationError error
101
102
if obj != nil && r .cfg .WebhookValidator != nil {
102
103
validationError = r .cfg .WebhookValidator (obj )
103
104
}
104
105
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
+ }
106
122
107
123
select {
108
124
case <- ctx .Done ():
@@ -115,41 +131,9 @@ func (r *Implementation) Reconcile(ctx context.Context, req reconcile.Request) (
115
131
logger .Error (validationError , webhookValidationErrorLogMsg )
116
132
r .cfg .EventRecorder .Eventf (obj , apiv1 .EventTypeWarning , "Rejected" ,
117
133
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"
124
134
}
125
135
126
136
logger .Info (fmt .Sprintf ("%s the resource" , op ))
127
137
128
138
return reconcile.Result {}, nil
129
139
}
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