Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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: 15 additions & 11 deletions api/dashboard/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions internal/controller/httproute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,14 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}
}

var httpRouteErr error
if err := r.processHTTPRoute(tctx, hr); err != nil {
acceptStatus.status = false
acceptStatus.msg = err.Error()
httpRouteErr = err
// When encountering a backend reference error, it should not affect the acceptance status
if !strings.Contains(err.Error(), string(gatewayv1.RouteReasonInvalidKind)) {
acceptStatus.status = false
acceptStatus.msg = err.Error()
}
}

if err := r.processHTTPRoutePolicies(tctx, hr); err != nil {
Expand All @@ -187,6 +192,13 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}
}

// If the backend reference error is because of an invalid kind, use this error first
if httpRouteErr != nil && strings.Contains(httpRouteErr.Error(), string(gatewayv1.RouteReasonInvalidKind)) {
Copy link

Copilot AI May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding logging for the backend reference error (httpRouteErr) before setting the resolveRefStatus so that the issue is more easily traceable in production environments.

Suggested change
if httpRouteErr != nil && strings.Contains(httpRouteErr.Error(), string(gatewayv1.RouteReasonInvalidKind)) {
if httpRouteErr != nil && strings.Contains(httpRouteErr.Error(), string(gatewayv1.RouteReasonInvalidKind)) {
r.Log.Error(httpRouteErr, "Backend reference error due to invalid kind encountered while processing HTTPRoute")

Copilot uses AI. Check for mistakes.
resolveRefStatus = status{
status: false,
msg: httpRouteErr.Error(),
}
}
ProcessBackendTrafficPolicy(r.Client, r.Log, tctx)

if err := r.Provider.Update(ctx, tctx, hr); err != nil {
Expand Down Expand Up @@ -492,7 +504,7 @@ func (r *HTTPRouteReconciler) processHTTPRoute(tctx *provider.TranslateContext,
kind = strings.ToLower(string(*backend.Kind))
}
if kind != "service" {
terror = fmt.Errorf("kind %s is not supported", kind)
terror = fmt.Errorf("%s %s", string(gatewayv1.RouteReasonInvalidKind), kind)
continue
}

Expand Down
8 changes: 7 additions & 1 deletion internal/controller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,16 @@ func SetRouteConditionResolvedRefs(routeParentStatus *gatewayv1.RouteParentStatu
conditionStatus = metav1.ConditionFalse
}

reason := string(gatewayv1.RouteReasonResolvedRefs)
// check if the error message contains InvalidKind
if !status && strings.Contains(message, string(gatewayv1.RouteReasonInvalidKind)) {
reason = string(gatewayv1.RouteReasonInvalidKind)
}

condition := metav1.Condition{
Type: string(gatewayv1.RouteConditionResolvedRefs),
Status: conditionStatus,
Reason: string(gatewayv1.RouteReasonResolvedRefs),
Reason: reason,
ObservedGeneration: generation,
Message: message,
LastTransitionTime: metav1.Now(),
Expand Down
34 changes: 28 additions & 6 deletions internal/provider/adc/translator/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,18 @@ func (t *Translator) translateEndpointSlice(portName *string, weight int, endpoi
return nodes
}

func (t *Translator) translateBackendRef(tctx *provider.TranslateContext, ref gatewayv1.BackendRef) adctypes.UpstreamNodes {
func (t *Translator) translateBackendRef(tctx *provider.TranslateContext, ref gatewayv1.BackendRef) (adctypes.UpstreamNodes, error) {
if ref.Kind != nil && *ref.Kind != "Service" {
return adctypes.UpstreamNodes{}, fmt.Errorf("kind %s is not supported", *ref.Kind)
Copy link

Copilot AI May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enhance the error message here by including additional context (e.g., backend reference name or namespace) to improve debugging when an unsupported kind is encountered.

Suggested change
return adctypes.UpstreamNodes{}, fmt.Errorf("kind %s is not supported", *ref.Kind)
key := types.NamespacedName{
Namespace: string(*ref.Namespace),
Name: string(ref.Name),
}
return adctypes.UpstreamNodes{}, fmt.Errorf("kind %s is not supported for backend reference %s", *ref.Kind, key)

Copilot uses AI. Check for mistakes.
}

key := types.NamespacedName{
Namespace: string(*ref.Namespace),
Name: string(ref.Name),
}
service, ok := tctx.Services[key]
if !ok {
return adctypes.UpstreamNodes{}
return adctypes.UpstreamNodes{}, fmt.Errorf("service %s not found", key)
}

weight := 1
Expand All @@ -333,7 +337,7 @@ func (t *Translator) translateBackendRef(tctx *provider.TranslateContext, ref ga
Port: port,
Weight: weight,
},
}
}, nil
}

var portName *string
Expand All @@ -345,12 +349,12 @@ func (t *Translator) translateBackendRef(tctx *provider.TranslateContext, ref ga
}
}
if portName == nil {
return adctypes.UpstreamNodes{}
return adctypes.UpstreamNodes{}, nil
}
}

endpointSlices := tctx.EndpointSlices[key]
return t.translateEndpointSlice(portName, weight, endpointSlices)
return t.translateEndpointSlice(portName, weight, endpointSlices), nil
}

// calculateHTTPRoutePriority calculates the priority of the HTTP route.
Expand Down Expand Up @@ -424,12 +428,17 @@ func (t *Translator) TranslateHTTPRoute(tctx *provider.TranslateContext, httpRou

for ruleIndex, rule := range rules {
upstream := adctypes.NewDefaultUpstream()
var backendErr error
for _, backend := range rule.BackendRefs {
if backend.Namespace == nil {
namespace := gatewayv1.Namespace(httpRoute.Namespace)
backend.Namespace = &namespace
}
upNodes := t.translateBackendRef(tctx, backend.BackendRef)
upNodes, err := t.translateBackendRef(tctx, backend.BackendRef)
if err != nil {
backendErr = err
continue
}
t.AttachBackendTrafficPolicyToUpstream(backend.BackendRef, tctx.BackendTrafficPolicies, upstream)
upstream.Nodes = append(upstream.Nodes, upNodes...)
}
Expand All @@ -443,6 +452,19 @@ func (t *Translator) TranslateHTTPRoute(tctx *provider.TranslateContext, httpRou
service.ID = id.GenID(service.Name)
service.Hosts = hosts
service.Upstream = upstream

if backendErr != nil && len(upstream.Nodes) == 0 {
if service.Plugins == nil {
service.Plugins = make(map[string]any)
}
service.Plugins["fault-injection"] = map[string]any{
"abort": map[string]any{
"http_status": 500,
"body": "No existing backendRef provided",
},
}
}

t.fillPluginsFromHTTPRouteFilters(service.Plugins, httpRoute.GetNamespace(), rule.Filters, rule.Matches, tctx)

matches := rule.Matches
Expand Down
5 changes: 1 addition & 4 deletions test/conformance/conformance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ var skippedTestsForTraditionalRoutes = []string{
tests.HTTPRouteHostnameIntersection.ShortName,
tests.HTTPRouteListenerHostnameMatching.ShortName,

// tests.HTTPRouteMatching.ShortName,
// tests.HTTPRouteMatchingAcrossRoutes.ShortName,

tests.GatewayInvalidTLSConfiguration.ShortName,
tests.HTTPRouteInvalidBackendRefUnknownKind.ShortName,
// tests.HTTPRouteInvalidBackendRefUnknownKind.ShortName,
Copy link

Copilot AI May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding an inline comment explaining why the HTTPRouteInvalidBackendRefUnknownKind test is commented out to clarify that this change is intentional.

Suggested change
// tests.HTTPRouteInvalidBackendRefUnknownKind.ShortName,
// tests.HTTPRouteInvalidBackendRefUnknownKind.ShortName, // Commented out because this test is not applicable to the current implementation. See issue tracker for details.

Copilot uses AI. Check for mistakes.
tests.HTTPRouteInvalidCrossNamespaceParentRef.ShortName,
tests.HTTPRouteInvalidNonExistentBackendRef.ShortName,
tests.HTTPRouteInvalidParentRefNotMatchingSectionName.ShortName,
Expand Down
Loading