Skip to content

refactor: SetRouteConditionResolvedRefs function #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: release-v2-dev
Choose a base branch
from
Open
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
26 changes: 12 additions & 14 deletions internal/controller/httproute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,7 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
msg string
}

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

// Store the backend reference error for later use
var backendRefErr error
if err := r.processHTTPRouteBackendRefs(tctx); err != nil {
resolveRefStatus = status{
status: false,
msg: err.Error(),
}
backendRefErr = err
}

// If the backend reference error is because of an invalid kind, use this error first
if httpRouteErr != nil && IsInvalidKindError(httpRouteErr) {
resolveRefStatus = status{
status: false,
msg: httpRouteErr.Error(),
}
backendRefErr = httpRouteErr
}
ProcessBackendTrafficPolicy(r.Client, r.Log, tctx)

Expand Down Expand Up @@ -230,7 +223,8 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
parentStatus.Conditions = MergeCondition(parentStatus.Conditions, condition)
}
SetRouteConditionAccepted(&parentStatus, hr.GetGeneration(), acceptStatus.status, acceptStatus.msg)
SetRouteConditionResolvedRefs(&parentStatus, hr.GetGeneration(), resolveRefStatus.status, resolveRefStatus.msg)
SetRouteConditionResolvedRefs(&parentStatus, hr.GetGeneration(), backendRefErr)

hr.Status.Parents = append(hr.Status.Parents, parentStatus)
}
if err := r.Status().Update(ctx, hr); err != nil {
Expand Down Expand Up @@ -448,7 +442,11 @@ func (r *HTTPRouteReconciler) processHTTPRouteBackendRefs(tctx *provider.Transla

var service corev1.Service
if err := r.Get(tctx, serviceNS, &service); err != nil {
terr = err
if client.IgnoreNotFound(err) == nil {
terr = NewBackendNotFoundError(namespace, name)
} else {
terr = err
}
continue
}
if service.Spec.Type == corev1.ServiceTypeExternalName {
Expand Down
55 changes: 46 additions & 9 deletions internal/controller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,31 @@ func SetRouteConditionAccepted(routeParentStatus *gatewayv1.RouteParentStatus, g
}
}

func SetRouteConditionResolvedRefs(routeParentStatus *gatewayv1.RouteParentStatus, generation int64, status bool, message string) {
reason := string(gatewayv1.RouteReasonResolvedRefs)
// check if the error message contains InvalidKind
if !status && strings.Contains(message, string(gatewayv1.RouteReasonInvalidKind)) {
reason = string(gatewayv1.RouteReasonInvalidKind)
}
if !status && strings.Contains(message, "Service") && strings.Contains(message, "not found") {
reason = string(gatewayv1.RouteReasonBackendNotFound)
// SetRouteConditionResolvedRefs sets the ResolvedRefs condition with proper reason based on error type
func SetRouteConditionResolvedRefs(routeParentStatus *gatewayv1.RouteParentStatus, generation int64, err error) {
var (
reason string
status = metav1.ConditionTrue
message = "backendRefs are resolved"
)

if err != nil {
status = metav1.ConditionFalse
message = err.Error()
reason = string(gatewayv1.RouteReasonResolvedRefs)

if IsInvalidKindError(err) {
reason = string(gatewayv1.RouteReasonInvalidKind)
} else if IsBackendNotFoundError(err) {
reason = string(gatewayv1.RouteReasonBackendNotFound)
}
} else {
reason = string(gatewayv1.RouteReasonResolvedRefs)
}

condition := metav1.Condition{
Type: string(gatewayv1.RouteConditionResolvedRefs),
Status: ConditionStatus(status),
Status: ConditionStatus(status == metav1.ConditionTrue),
Reason: reason,
ObservedGeneration: generation,
Message: message,
Expand Down Expand Up @@ -923,6 +935,31 @@ func IsInvalidKindError(err error) bool {
return ok
}

// BackendNotFoundError represents an error when a backend service is not found
type BackendNotFoundError struct {
Name string
Namespace string
}

// Error implements the error interface
func (e *BackendNotFoundError) Error() string {
return fmt.Sprintf("Service %s/%s not found", e.Namespace, e.Name)
}

// NewBackendNotFoundError creates a new BackendNotFoundError
func NewBackendNotFoundError(namespace, name string) *BackendNotFoundError {
return &BackendNotFoundError{
Name: name,
Namespace: namespace,
}
}

// IsBackendNotFoundError checks if the error is a BackendNotFoundError
func IsBackendNotFoundError(err error) bool {
_, ok := err.(*BackendNotFoundError)
return ok
}

// 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.
// If the HTTPRoute hostnames do not intersect with the listener hostnames of the gateways, it returns an ErrNoMatchingListenerHostname error.
func filterHostnames(gateways []RouteParentRefContext, httpRoute *gatewayv1.HTTPRoute) (*gatewayv1.HTTPRoute, error) {
Expand Down
Loading