Skip to content

Commit cde185a

Browse files
authored
refactor: SetRouteConditionResolvedRefs function (#144)
Signed-off-by: ashing <[email protected]>
1 parent 806c514 commit cde185a

File tree

2 files changed

+58
-23
lines changed

2 files changed

+58
-23
lines changed

internal/controller/httproute_controller.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,7 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
139139
msg string
140140
}
141141

142-
resolveRefStatus := status{
143-
status: true,
144-
msg: "backendRefs are resolved",
145-
}
142+
// Only keep acceptStatus since we're using error objects directly now
146143
acceptStatus := status{
147144
status: true,
148145
msg: "Route is accepted",
@@ -187,19 +184,15 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
187184
acceptStatus.msg = err.Error()
188185
}
189186

187+
// Store the backend reference error for later use
188+
var backendRefErr error
190189
if err := r.processHTTPRouteBackendRefs(tctx); err != nil {
191-
resolveRefStatus = status{
192-
status: false,
193-
msg: err.Error(),
194-
}
190+
backendRefErr = err
195191
}
196192

197193
// If the backend reference error is because of an invalid kind, use this error first
198194
if httpRouteErr != nil && IsInvalidKindError(httpRouteErr) {
199-
resolveRefStatus = status{
200-
status: false,
201-
msg: httpRouteErr.Error(),
202-
}
195+
backendRefErr = httpRouteErr
203196
}
204197
ProcessBackendTrafficPolicy(r.Client, r.Log, tctx)
205198

@@ -230,7 +223,8 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
230223
parentStatus.Conditions = MergeCondition(parentStatus.Conditions, condition)
231224
}
232225
SetRouteConditionAccepted(&parentStatus, hr.GetGeneration(), acceptStatus.status, acceptStatus.msg)
233-
SetRouteConditionResolvedRefs(&parentStatus, hr.GetGeneration(), resolveRefStatus.status, resolveRefStatus.msg)
226+
SetRouteConditionResolvedRefs(&parentStatus, hr.GetGeneration(), backendRefErr)
227+
234228
hr.Status.Parents = append(hr.Status.Parents, parentStatus)
235229
}
236230
if err := r.Status().Update(ctx, hr); err != nil {
@@ -448,7 +442,11 @@ func (r *HTTPRouteReconciler) processHTTPRouteBackendRefs(tctx *provider.Transla
448442

449443
var service corev1.Service
450444
if err := r.Get(tctx, serviceNS, &service); err != nil {
451-
terr = err
445+
if client.IgnoreNotFound(err) == nil {
446+
terr = NewBackendNotFoundError(namespace, name)
447+
} else {
448+
terr = err
449+
}
452450
continue
453451
}
454452
if service.Spec.Type == corev1.ServiceTypeExternalName {

internal/controller/utils.go

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,19 +242,31 @@ func SetRouteConditionAccepted(routeParentStatus *gatewayv1.RouteParentStatus, g
242242
}
243243
}
244244

245-
func SetRouteConditionResolvedRefs(routeParentStatus *gatewayv1.RouteParentStatus, generation int64, status bool, message string) {
246-
reason := string(gatewayv1.RouteReasonResolvedRefs)
247-
// check if the error message contains InvalidKind
248-
if !status && strings.Contains(message, string(gatewayv1.RouteReasonInvalidKind)) {
249-
reason = string(gatewayv1.RouteReasonInvalidKind)
250-
}
251-
if !status && strings.Contains(message, "Service") && strings.Contains(message, "not found") {
252-
reason = string(gatewayv1.RouteReasonBackendNotFound)
245+
// SetRouteConditionResolvedRefs sets the ResolvedRefs condition with proper reason based on error type
246+
func SetRouteConditionResolvedRefs(routeParentStatus *gatewayv1.RouteParentStatus, generation int64, err error) {
247+
var (
248+
reason string
249+
status = metav1.ConditionTrue
250+
message = "backendRefs are resolved"
251+
)
252+
253+
if err != nil {
254+
status = metav1.ConditionFalse
255+
message = err.Error()
256+
reason = string(gatewayv1.RouteReasonResolvedRefs)
257+
258+
if IsInvalidKindError(err) {
259+
reason = string(gatewayv1.RouteReasonInvalidKind)
260+
} else if IsBackendNotFoundError(err) {
261+
reason = string(gatewayv1.RouteReasonBackendNotFound)
262+
}
263+
} else {
264+
reason = string(gatewayv1.RouteReasonResolvedRefs)
253265
}
254266

255267
condition := metav1.Condition{
256268
Type: string(gatewayv1.RouteConditionResolvedRefs),
257-
Status: ConditionStatus(status),
269+
Status: status,
258270
Reason: reason,
259271
ObservedGeneration: generation,
260272
Message: message,
@@ -923,6 +935,31 @@ func IsInvalidKindError(err error) bool {
923935
return ok
924936
}
925937

938+
// BackendNotFoundError represents an error when a backend service is not found
939+
type BackendNotFoundError struct {
940+
Name string
941+
Namespace string
942+
}
943+
944+
// Error implements the error interface
945+
func (e *BackendNotFoundError) Error() string {
946+
return fmt.Sprintf("Service %s/%s not found", e.Namespace, e.Name)
947+
}
948+
949+
// NewBackendNotFoundError creates a new BackendNotFoundError
950+
func NewBackendNotFoundError(namespace, name string) *BackendNotFoundError {
951+
return &BackendNotFoundError{
952+
Name: name,
953+
Namespace: namespace,
954+
}
955+
}
956+
957+
// IsBackendNotFoundError checks if the error is a BackendNotFoundError
958+
func IsBackendNotFoundError(err error) bool {
959+
_, ok := err.(*BackendNotFoundError)
960+
return ok
961+
}
962+
926963
// filterHostnames accepts a list of gateways and an HTTPRoute, and returns a copy of the HTTPRoute with only the hostnames that match the listener hostnames of the gateways.
927964
// If the HTTPRoute hostnames do not intersect with the listener hostnames of the gateways, it returns an ErrNoMatchingListenerHostname error.
928965
func filterHostnames(gateways []RouteParentRefContext, httpRoute *gatewayv1.HTTPRoute) (*gatewayv1.HTTPRoute, error) {

0 commit comments

Comments
 (0)