Skip to content

Commit

Permalink
chore(HTTPRoute): change level from ERROR to DEBUG for missing backen…
Browse files Browse the repository at this point in the history
…d and enhance status (#6746)
  • Loading branch information
programmer04 authored Nov 29, 2024
1 parent 6533c42 commit 1f1d0e5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ Adding a new version? You'll need three changes:
- `konghq.com/publish-service`
- `konghq.com/tags`
[#6729](https://github.com/Kong/kubernetes-ingress-controller/pull/6729)
- Log `Object requested backendRef to target, but it does not exist, skipping...`
as `DEBUG` instead of `ERROR`, enhance `HTTPRoute` status with detailed message.
[#6746](https://github.com/Kong/kubernetes-ingress-controller/pull/6746)

### Fixed

Expand Down
34 changes: 22 additions & 12 deletions internal/controllers/gateway/httproute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ func (r *HTTPRouteReconciler) setRouteConditionResolvedRefsCondition(
) (map[string]*gatewayapi.RouteParentStatus, bool, error) {
var changed bool
resolvedRefsStatus := metav1.ConditionFalse
reason, err := r.getHTTPRouteRuleReason(ctx, *httpRoute)
reason, msg, err := r.getHTTPRouteRuleReason(ctx, *httpRoute)
if err != nil {
return nil, false, err
}
Expand All @@ -635,6 +635,7 @@ func (r *HTTPRouteReconciler) setRouteConditionResolvedRefsCondition(
ObservedGeneration: httpRoute.Generation,
LastTransitionTime: metav1.Now(),
Reason: string(reason),
Message: msg,
}
for _, parentStatus := range parentStatuses {
var conditionFound bool
Expand All @@ -658,43 +659,52 @@ func (r *HTTPRouteReconciler) setRouteConditionResolvedRefsCondition(
return parentStatuses, changed, nil
}

func (r *HTTPRouteReconciler) getHTTPRouteRuleReason(ctx context.Context, httpRoute gatewayapi.HTTPRoute) (gatewayapi.RouteConditionReason, error) {
func (r *HTTPRouteReconciler) getHTTPRouteRuleReason(ctx context.Context, httpRoute gatewayapi.HTTPRoute) (reason gatewayapi.RouteConditionReason, msg string, err error) {
for _, rule := range httpRoute.Spec.Rules {
for _, backendRef := range rule.BackendRefs {
backendNamespace := httpRoute.Namespace
if backendRef.Namespace != nil && *backendRef.Namespace != "" {
backendNamespace = string(*backendRef.Namespace)
}

backendRefGK := string(*backendRef.Kind)
if gr := string(*backendRef.Group); gr != "" {
backendRefGK = gr + "/" + backendRefGK
}
targetNN := k8stypes.NamespacedName{Namespace: backendNamespace, Name: string(backendRef.Name)}

// Check if the BackendRef GroupKind is supported
if !util.IsBackendRefGroupKindSupported(backendRef.Group, backendRef.Kind) {
return gatewayapi.RouteReasonInvalidKind, nil
return gatewayapi.RouteReasonInvalidKind, fmt.Sprintf("target %s has unsupported type %s", targetNN, backendRefGK), nil
}

// Check if all the objects referenced actually exist
// Only services are currently supported as BackendRef objects
service := &corev1.Service{}
err := r.Client.Get(ctx, k8stypes.NamespacedName{Namespace: backendNamespace, Name: string(backendRef.Name)}, service)
if err != nil {
if err := r.Client.Get(ctx, targetNN, service); err != nil {
if !apierrors.IsNotFound(err) {
return "", err
return "", "", err
}
return gatewayapi.RouteReasonBackendNotFound, nil
return gatewayapi.RouteReasonBackendNotFound, fmt.Sprintf("target %s of type %s does not exist", targetNN, backendRefGK), nil
}

// Check if the object referenced is in another namespace,
// and if there is grant for that reference
if httpRoute.Namespace != backendNamespace {
differentNamespaceMsg := fmt.Sprintf("%s is in a different namespace than the HTTPRoute (namespace %s)", targetNN, httpRoute.Namespace)
if !r.enableReferenceGrant {
return gatewayapi.RouteReasonRefNotPermitted, nil
return gatewayapi.RouteReasonRefNotPermitted,
differentNamespaceMsg + " install ReferenceGrant CRD and configure a proper grant",
nil
}

referenceGrantList := &gatewayapi.ReferenceGrantList{}
if err := r.Client.List(ctx, referenceGrantList, client.InNamespace(backendNamespace)); err != nil {
return "", err
return "", "", err
}
notGrantedMsg := differentNamespaceMsg + " and no ReferenceGrant allowing reference is configured"
if len(referenceGrantList.Items) == 0 {
return gatewayapi.RouteReasonRefNotPermitted, nil
return gatewayapi.RouteReasonRefNotPermitted, notGrantedMsg, nil
}
var isGranted bool
for _, grant := range referenceGrantList.Items {
Expand All @@ -704,12 +714,12 @@ func (r *HTTPRouteReconciler) getHTTPRouteRuleReason(ctx context.Context, httpRo
}
}
if !isGranted {
return gatewayapi.RouteReasonRefNotPermitted, nil
return gatewayapi.RouteReasonRefNotPermitted, notGrantedMsg, nil
}
}
}
}
return gatewayapi.RouteReasonResolvedRefs, nil
return gatewayapi.RouteReasonResolvedRefs, "", nil
}

// SetLogger sets the logger.
Expand Down
5 changes: 3 additions & 2 deletions internal/dataplane/translator/backendref.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/kongstate"
"github.com/kong/kubernetes-ingress-controller/v3/internal/gatewayapi"
"github.com/kong/kubernetes-ingress-controller/v3/internal/logging"
"github.com/kong/kubernetes-ingress-controller/v3/internal/store"
"github.com/kong/kubernetes-ingress-controller/v3/internal/util"
)
Expand Down Expand Up @@ -57,9 +58,9 @@ func backendRefsToKongStateBackends(
}
if err != nil {
if errors.As(err, &store.NotFoundError{}) {
logger.Error(err, "Object requested backendRef to target, but it does not exist, skipping...")
logger.V(logging.DebugLevel).Info("Object requested backendRef to target, but it does not exist, skipping...")
} else {
logger.Error(err, "Object requested backendRef to target, but an error occurred, skipping...")
logger.Error(err, "Object requested backendRef to target, but an unexpected error occurred, skipping...")
}
continue
}
Expand Down

1 comment on commit 1f1d0e5

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Go Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 1f1d0e5 Previous: 83870ed Ratio
BenchmarkGetPluginRelations 17416 ns/op 7296 B/op 66 allocs/op 7135 ns/op 7296 B/op 66 allocs/op 2.44
BenchmarkGetPluginRelations - ns/op 17416 ns/op 7135 ns/op 2.44

This comment was automatically generated by workflow using github-action-benchmark.

CC: @Kong/k8s-maintainers

Please sign in to comment.