-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Add support for HTTPRoutePolicy in HTTPRoute reconciliation #93
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
890787b
feat: Add support for HTTPRoutePolicy in HTTPRoute reconciliation
dspo acee110
e2e
dspo e4b2523
Update HTTPRoutePolicy and related components
dspo 13f3912
Refactor and update types and references
dspo 4aafb68
Merge branch origin/release-v2-dev
dspo 1ad0626
conflicts
dspo 5c8aa78
Refactor HTTPRoutePolicy status update and ancestor management
dspo 7752729
Merge branch origin/release-v2-dev
dspo d56a253
Update HTTPRoutePolicy and Reconciler for Improved Handling
dspo 4f25e92
Refactor HTTPRoutePolicy handling and update tests
dspo 9d83358
Reorganize import statements for consistency.
dspo bd32c55
resolve comments
dspo e1186f1
enhance policy handling
dspo 216bf0e
Merge remote-tracking branch 'origin/release-v2-dev' into feat/httpro…
dspo e20bac6
resolve comments
dspo b2ea254
resolve lint
dspo 9050538
Update test to use Eventually for status check
dspo 80820d6
fix e2e
dspo cb69e36
Merge branch origin main
dspo ec310a4
fix e2e
dspo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,128 @@ | ||||||||||||||||
| package controller | ||||||||||||||||
|
|
||||||||||||||||
| import ( | ||||||||||||||||
| "context" | ||||||||||||||||
| "time" | ||||||||||||||||
|
|
||||||||||||||||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||||||||||||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||||||||||||||||
| gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" | ||||||||||||||||
| "sigs.k8s.io/gateway-api/apis/v1alpha2" | ||||||||||||||||
|
|
||||||||||||||||
| "github.com/api7/api7-ingress-controller/api/v1alpha1" | ||||||||||||||||
| "github.com/api7/api7-ingress-controller/internal/controller/indexer" | ||||||||||||||||
| "github.com/api7/api7-ingress-controller/internal/provider" | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| func (r *HTTPRouteReconciler) processHTTPRoutePolicies(tctx *provider.TranslateContext, httpRoute *gatewayv1.HTTPRoute) error { | ||||||||||||||||
| // list HTTPRoutePolices which sectionName is not specified | ||||||||||||||||
| var ( | ||||||||||||||||
| checker = conflictChecker{ | ||||||||||||||||
| httpRoute: httpRoute, | ||||||||||||||||
| policies: make(map[targetRefKey][]v1alpha1.HTTPRoutePolicy), | ||||||||||||||||
| } | ||||||||||||||||
| listForAllRules v1alpha1.HTTPRoutePolicyList | ||||||||||||||||
| key = indexer.GenHTTPRoutePolicyIndexKey(v1alpha1.GroupVersion.Group, "HTTPRoute", httpRoute.GetNamespace(), httpRoute.GetName(), "") | ||||||||||||||||
| ) | ||||||||||||||||
| if err := r.List(context.Background(), &listForAllRules, client.MatchingFields{indexer.PolicyTargetRefs: key}); err != nil { | ||||||||||||||||
| return err | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| for _, item := range listForAllRules.Items { | ||||||||||||||||
| checker.append("", item) | ||||||||||||||||
| tctx.HTTPRoutePolicies["*"] = append(tctx.HTTPRoutePolicies["*"], item) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| for _, rule := range httpRoute.Spec.Rules { | ||||||||||||||||
| if rule.Name == nil { | ||||||||||||||||
| continue | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| var ( | ||||||||||||||||
| ruleName = string(*rule.Name) | ||||||||||||||||
| listForSectionRules v1alpha1.HTTPRoutePolicyList | ||||||||||||||||
| key = indexer.GenHTTPRoutePolicyIndexKey(v1alpha1.GroupVersion.Group, "HTTPRoute", httpRoute.GetNamespace(), httpRoute.GetName(), ruleName) | ||||||||||||||||
| ) | ||||||||||||||||
| if err := r.List(context.Background(), &listForSectionRules, client.MatchingFields{indexer.PolicyTargetRefs: key}); err != nil { | ||||||||||||||||
| continue | ||||||||||||||||
| } | ||||||||||||||||
| for _, item := range listForSectionRules.Items { | ||||||||||||||||
| checker.append(ruleName, item) | ||||||||||||||||
| tctx.HTTPRoutePolicies[ruleName] = append(tctx.HTTPRoutePolicies[ruleName], item) | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| var ( | ||||||||||||||||
| status = true | ||||||||||||||||
| reason = string(v1alpha2.PolicyReasonAccepted) | ||||||||||||||||
| message string | ||||||||||||||||
| ) | ||||||||||||||||
| if checker.conflict { | ||||||||||||||||
| status = false | ||||||||||||||||
| reason = string(v1alpha2.PolicyReasonConflicted) | ||||||||||||||||
| message = "HTTPRoutePolicy conflict with others target to the HTTPRoute" | ||||||||||||||||
|
|
||||||||||||||||
| // clear HTTPRoutePolices from TranslateContext | ||||||||||||||||
| tctx.HTTPRoutePolicies = make(map[string][]v1alpha1.HTTPRoutePolicy) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| for _, policies := range checker.policies { | ||||||||||||||||
| for i := range policies { | ||||||||||||||||
| policy := policies[i] | ||||||||||||||||
| r.modifyHTTPRoutePolicyStatus(httpRoute, &policy, status, reason, message) | ||||||||||||||||
| tctx.StatusUpdaters = append(tctx.StatusUpdaters, &policy) | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| return nil | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func (r *HTTPRouteReconciler) modifyHTTPRoutePolicyStatus(httpRoute *gatewayv1.HTTPRoute, policy *v1alpha1.HTTPRoutePolicy, status bool, reason, message string) { | ||||||||||||||||
| condition := metav1.Condition{ | ||||||||||||||||
| Type: string(v1alpha2.PolicyConditionAccepted), | ||||||||||||||||
| Status: metav1.ConditionTrue, | ||||||||||||||||
| ObservedGeneration: policy.GetGeneration(), | ||||||||||||||||
| LastTransitionTime: metav1.Time{Time: time.Now()}, | ||||||||||||||||
| Reason: reason, | ||||||||||||||||
| Message: message, | ||||||||||||||||
| } | ||||||||||||||||
| if !status { | ||||||||||||||||
| condition.Status = metav1.ConditionFalse | ||||||||||||||||
| } | ||||||||||||||||
| _ = SetAncestors(&policy.Status, httpRoute.Spec.ParentRefs, condition) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| type conflictChecker struct { | ||||||||||||||||
| httpRoute *gatewayv1.HTTPRoute | ||||||||||||||||
| policies map[targetRefKey][]v1alpha1.HTTPRoutePolicy | ||||||||||||||||
| conflict bool | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| type targetRefKey struct { | ||||||||||||||||
| Group gatewayv1.Group | ||||||||||||||||
| Namespace gatewayv1.Namespace | ||||||||||||||||
| Name gatewayv1.ObjectName | ||||||||||||||||
| SectionName gatewayv1.SectionName | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func (c *conflictChecker) append(sectionName string, policy v1alpha1.HTTPRoutePolicy) { | ||||||||||||||||
| key := targetRefKey{ | ||||||||||||||||
| Group: gatewayv1.GroupName, | ||||||||||||||||
| Namespace: gatewayv1.Namespace(c.httpRoute.GetNamespace()), | ||||||||||||||||
| Name: gatewayv1.ObjectName(c.httpRoute.GetName()), | ||||||||||||||||
| SectionName: gatewayv1.SectionName(sectionName), | ||||||||||||||||
| } | ||||||||||||||||
| c.policies[key] = append(c.policies[key], policy) | ||||||||||||||||
|
|
||||||||||||||||
| if !c.conflict { | ||||||||||||||||
| Loop: | ||||||||||||||||
| for _, items := range c.policies { | ||||||||||||||||
| for _, item := range items { | ||||||||||||||||
| if item.Spec.Priority != policy.Spec.Priority || *item.Spec.Priority != *policy.Spec.Priority { | ||||||||||||||||
|
||||||||||||||||
| if item.Spec.Priority != policy.Spec.Priority || *item.Spec.Priority != *policy.Spec.Priority { | |
| if item.Spec.Priority == nil || policy.Spec.Priority == nil { | |
| if item.Spec.Priority != policy.Spec.Priority { // One is nil, the other is not | |
| c.conflict = true | |
| break Loop | |
| } | |
| } else if *item.Spec.Priority != *policy.Spec.Priority { |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.