Skip to content

Commit d1a375d

Browse files
committed
Merge branch origin/release-v2-dev
2 parents d1c824e + bcb11a7 commit d1a375d

File tree

9 files changed

+391
-189
lines changed

9 files changed

+391
-189
lines changed

internal/controller/httproute_controller.go

Lines changed: 39 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -75,67 +75,12 @@ func (r *HTTPRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
7575
Watches(&v1alpha1.BackendTrafficPolicy{},
7676
handler.EnqueueRequestsFromMapFunc(r.listHTTPRoutesForBackendTrafficPolicy),
7777
builder.WithPredicates(
78-
predicate.Funcs{
79-
GenericFunc: func(e event.GenericEvent) bool {
80-
return false
81-
},
82-
DeleteFunc: func(e event.DeleteEvent) bool {
83-
return true
84-
},
85-
CreateFunc: func(e event.CreateEvent) bool {
86-
return true
87-
},
88-
UpdateFunc: func(e event.UpdateEvent) bool {
89-
oldObj, ok := e.ObjectOld.(*v1alpha1.BackendTrafficPolicy)
90-
newObj, ok2 := e.ObjectNew.(*v1alpha1.BackendTrafficPolicy)
91-
if !ok || !ok2 {
92-
return false
93-
}
94-
oldRefs := oldObj.Spec.TargetRefs
95-
newRefs := newObj.Spec.TargetRefs
96-
97-
oldRefMap := make(map[string]v1alpha1.BackendPolicyTargetReferenceWithSectionName)
98-
for _, ref := range oldRefs {
99-
key := fmt.Sprintf("%s/%s/%s", ref.Group, ref.Kind, ref.Name)
100-
oldRefMap[key] = ref
101-
}
102-
103-
for _, ref := range newRefs {
104-
key := fmt.Sprintf("%s/%s/%s", ref.Group, ref.Kind, ref.Name)
105-
delete(oldRefMap, key)
106-
}
107-
if len(oldRefMap) > 0 {
108-
targetRefs := make([]v1alpha1.BackendPolicyTargetReferenceWithSectionName, 0, len(oldRefs))
109-
for _, ref := range oldRefMap {
110-
targetRefs = append(targetRefs, ref)
111-
}
112-
dump := oldObj.DeepCopy()
113-
dump.Spec.TargetRefs = targetRefs
114-
r.genericEvent <- event.GenericEvent{
115-
Object: dump,
116-
}
117-
}
118-
return true
119-
},
120-
},
78+
BackendTrafficPolicyPredicateFunc(r.genericEvent),
12179
),
12280
).
12381
Watches(&v1alpha1.HTTPRoutePolicy{},
12482
handler.EnqueueRequestsFromMapFunc(r.listHTTPRouteByHTTPRoutePolicy),
125-
builder.WithPredicates(
126-
predicate.Funcs{
127-
CreateFunc: func(e event.CreateEvent) bool {
128-
return true
129-
},
130-
DeleteFunc: func(e event.DeleteEvent) bool {
131-
return true
132-
},
133-
UpdateFunc: httpRoutePolicyPredicateOnUpdate(r.genericEvent, "HTTPRoute"),
134-
GenericFunc: func(e event.GenericEvent) bool {
135-
return false
136-
},
137-
},
138-
),
83+
builder.WithPredicates(httpRoutePolicyPredicateFuncs(r.genericEvent)),
13984
).
14085
WatchesRawSource(
14186
source.Channel(
@@ -425,52 +370,15 @@ func (r *HTTPRouteReconciler) listHTTPRouteByHTTPRoutePolicy(ctx context.Context
425370
}
426371

427372
func (r *HTTPRouteReconciler) listHTTPRouteForGenericEvent(ctx context.Context, obj client.Object) (requests []reconcile.Request) {
428-
var namespacedNameMap = make(map[types.NamespacedName]struct{})
429-
430-
switch v := obj.(type) {
373+
switch obj.(type) {
431374
case *v1alpha1.BackendTrafficPolicy:
432-
httprouteAll := []gatewayv1.HTTPRoute{}
433-
for _, ref := range v.Spec.TargetRefs {
434-
httprouteList := &gatewayv1.HTTPRouteList{}
435-
if err := r.List(ctx, httprouteList, client.MatchingFields{
436-
indexer.ServiceIndexRef: indexer.GenIndexKey(v.GetNamespace(), string(ref.Name)),
437-
}); err != nil {
438-
r.Log.Error(err, "failed to list HTTPRoutes for BackendTrafficPolicy", "namespace", v.GetNamespace(), "ref", ref.Name)
439-
return nil
440-
}
441-
httprouteAll = append(httprouteAll, httprouteList.Items...)
442-
}
443-
for _, hr := range httprouteAll {
444-
key := types.NamespacedName{
445-
Namespace: hr.Namespace,
446-
Name: hr.Name,
447-
}
448-
if _, ok := namespacedNameMap[key]; !ok {
449-
namespacedNameMap[key] = struct{}{}
450-
requests = append(requests, reconcile.Request{
451-
NamespacedName: client.ObjectKey{
452-
Namespace: hr.Namespace,
453-
Name: hr.Name,
454-
},
455-
})
456-
}
457-
}
375+
return r.listHTTPRoutesForBackendTrafficPolicy(ctx, obj)
458376
case *v1alpha1.HTTPRoutePolicy:
459-
for _, ref := range v.Spec.TargetRefs {
460-
namespacedName := types.NamespacedName{Namespace: v.GetNamespace(), Name: string(ref.Name)}
461-
if _, ok := namespacedNameMap[namespacedName]; !ok {
462-
namespacedNameMap[namespacedName] = struct{}{}
463-
if err := r.Get(ctx, namespacedName, new(gatewayv1.HTTPRoute)); err != nil {
464-
r.Log.Error(err, "failed to Get HTTPRoute", "namespace", namespacedName.Namespace, "name", namespacedName.Name)
465-
continue
466-
}
467-
requests = append(requests, reconcile.Request{NamespacedName: namespacedName})
468-
}
469-
}
377+
return r.listHTTPRouteByHTTPRoutePolicy(ctx, obj)
470378
default:
471379
r.Log.Error(fmt.Errorf("unexpected object type"), "failed to convert object to BackendTrafficPolicy or HTTPRoutePolicy")
380+
return nil
472381
}
473-
return requests
474382
}
475383

476384
func (r *HTTPRouteReconciler) processHTTPRouteBackendRefs(tctx *provider.TranslateContext) error {
@@ -590,36 +498,41 @@ func (r *HTTPRouteReconciler) processHTTPRoute(tctx *provider.TranslateContext,
590498
return terror
591499
}
592500

593-
func httpRoutePolicyPredicateOnUpdate(c chan event.GenericEvent, kind string) func(e event.UpdateEvent) bool {
594-
return func(e event.UpdateEvent) bool {
595-
oldPolicy, ok0 := e.ObjectOld.(*v1alpha1.HTTPRoutePolicy)
596-
newPolicy, ok1 := e.ObjectNew.(*v1alpha1.HTTPRoutePolicy)
597-
if !ok0 || !ok1 {
598-
return false
599-
}
600-
var discardsRefs = make(map[string]v1alpha2.LocalPolicyTargetReferenceWithSectionName)
601-
for _, ref := range oldPolicy.Spec.TargetRefs {
602-
if string(ref.Kind) != kind {
603-
continue
501+
func httpRoutePolicyPredicateFuncs(channel chan event.GenericEvent) predicate.Predicate {
502+
return predicate.Funcs{
503+
CreateFunc: func(e event.CreateEvent) bool {
504+
return true
505+
},
506+
DeleteFunc: func(e event.DeleteEvent) bool {
507+
return true
508+
},
509+
UpdateFunc: func(e event.UpdateEvent) bool {
510+
oldPolicy, ok0 := e.ObjectOld.(*v1alpha1.HTTPRoutePolicy)
511+
newPolicy, ok1 := e.ObjectNew.(*v1alpha1.HTTPRoutePolicy)
512+
if !ok0 || !ok1 {
513+
return false
604514
}
605-
key := indexer.GenHTTPRoutePolicyIndexKey(string(ref.Group), string(ref.Kind), e.ObjectOld.GetNamespace(), string(ref.Name), "")
606-
discardsRefs[key] = ref
607-
}
608-
for _, ref := range newPolicy.Spec.TargetRefs {
609-
if string(ref.Kind) != kind {
610-
continue
515+
var discardsRefs = make(map[string]v1alpha2.LocalPolicyTargetReferenceWithSectionName)
516+
for _, ref := range oldPolicy.Spec.TargetRefs {
517+
key := indexer.GenHTTPRoutePolicyIndexKey(string(ref.Group), string(ref.Kind), e.ObjectOld.GetNamespace(), string(ref.Name), "")
518+
discardsRefs[key] = ref
611519
}
612-
key := indexer.GenHTTPRoutePolicyIndexKey(string(ref.Group), string(ref.Kind), e.ObjectOld.GetNamespace(), string(ref.Name), "")
613-
delete(discardsRefs, key)
614-
}
615-
if len(discardsRefs) > 0 {
616-
dump := oldPolicy.DeepCopy()
617-
dump.Spec.TargetRefs = make([]v1alpha2.LocalPolicyTargetReferenceWithSectionName, 0, len(discardsRefs))
618-
for _, ref := range discardsRefs {
619-
dump.Spec.TargetRefs = append(dump.Spec.TargetRefs, ref)
520+
for _, ref := range newPolicy.Spec.TargetRefs {
521+
key := indexer.GenHTTPRoutePolicyIndexKey(string(ref.Group), string(ref.Kind), e.ObjectOld.GetNamespace(), string(ref.Name), "")
522+
delete(discardsRefs, key)
620523
}
621-
c <- event.GenericEvent{Object: dump}
622-
}
623-
return true
524+
if len(discardsRefs) > 0 {
525+
dump := oldPolicy.DeepCopy()
526+
dump.Spec.TargetRefs = make([]v1alpha2.LocalPolicyTargetReferenceWithSectionName, 0, len(discardsRefs))
527+
for _, ref := range discardsRefs {
528+
dump.Spec.TargetRefs = append(dump.Spec.TargetRefs, ref)
529+
}
530+
channel <- event.GenericEvent{Object: dump}
531+
}
532+
return true
533+
},
534+
GenericFunc: func(e event.GenericEvent) bool {
535+
return false
536+
},
624537
}
625538
}

internal/controller/httproutepolicy.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,14 @@ func (r *HTTPRouteReconciler) processHTTPRoutePolicies(tctx *provider.TranslateC
8989
for _, policies := range checker.policies {
9090
for i := range policies {
9191
policy := policies[i]
92-
r.modifyHTTPRoutePolicyStatus(httpRoute, &policy, status, reason, message)
92+
modifyHTTPRoutePolicyStatus(httpRoute.Spec.ParentRefs, &policy, status, reason, message)
9393
tctx.StatusUpdaters = append(tctx.StatusUpdaters, &policy)
9494
}
9595
}
9696

9797
return nil
9898
}
9999

100-
func (r *HTTPRouteReconciler) modifyHTTPRoutePolicyStatus(httpRoute *gatewayv1.HTTPRoute, policy *v1alpha1.HTTPRoutePolicy, status bool, reason, message string) {
101-
condition := metav1.Condition{
102-
Type: string(v1alpha2.PolicyConditionAccepted),
103-
Status: metav1.ConditionTrue,
104-
ObservedGeneration: policy.GetGeneration(),
105-
LastTransitionTime: metav1.Time{Time: time.Now()},
106-
Reason: reason,
107-
Message: message,
108-
}
109-
if !status {
110-
condition.Status = metav1.ConditionFalse
111-
}
112-
_ = SetAncestors(&policy.Status, httpRoute.Spec.ParentRefs, condition)
113-
}
114-
115100
func (r *IngressReconciler) processHTTPRoutePolicies(tctx *provider.TranslateContext, ingress *networkingv1.Ingress) error {
116101
var (
117102
checker = conflictChecker{
@@ -131,16 +116,44 @@ func (r *IngressReconciler) processHTTPRoutePolicies(tctx *provider.TranslateCon
131116
tctx.HTTPRoutePolicies["*"] = append(tctx.HTTPRoutePolicies["*"], item)
132117
}
133118

119+
var (
120+
status = true
121+
reason = string(v1alpha2.PolicyReasonAccepted)
122+
message string
123+
)
134124
if checker.conflict {
125+
status = false
126+
reason = string(v1alpha2.PolicyReasonConflicted)
127+
message = "HTTPRoutePolicy conflict with others target to the Ingress"
128+
135129
// clear HTTPRoutePolicies from TranslateContext
136130
tctx.HTTPRoutePolicies = make(map[string][]v1alpha1.HTTPRoutePolicy)
137131
}
138132

139-
// todo: handle HTTPRoutePolicy status
133+
for i := range list.Items {
134+
policy := list.Items[i]
135+
modifyHTTPRoutePolicyStatus(tctx.RouteParentRefs, &policy, status, reason, message)
136+
tctx.StatusUpdaters = append(tctx.StatusUpdaters, &policy)
137+
}
140138

141139
return nil
142140
}
143141

142+
func modifyHTTPRoutePolicyStatus(parentRefs []gatewayv1.ParentReference, policy *v1alpha1.HTTPRoutePolicy, status bool, reason, message string) {
143+
condition := metav1.Condition{
144+
Type: string(v1alpha2.PolicyConditionAccepted),
145+
Status: metav1.ConditionTrue,
146+
ObservedGeneration: policy.GetGeneration(),
147+
LastTransitionTime: metav1.Time{Time: time.Now()},
148+
Reason: reason,
149+
Message: message,
150+
}
151+
if !status {
152+
condition.Status = metav1.ConditionFalse
153+
}
154+
_ = SetAncestors(&policy.Status, parentRefs, condition)
155+
}
156+
144157
type conflictChecker struct {
145158
object client.Object
146159
policies map[targetRefKey][]v1alpha1.HTTPRoutePolicy

internal/controller/indexer/indexer.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,6 @@ func IngressSecretIndexFunc(rawObj client.Object) []string {
256256
return secrets
257257
}
258258

259-
func IngressHTTPRouteIndexFunc(rawObj client.Object) []string {
260-
return []string{GenHTTPRoutePolicyIndexKey(networkingv1.GroupName, "Ingress", rawObj.GetNamespace(), rawObj.GetName(), "")}
261-
}
262-
263259
func GenIndexKeyWithGK(group, kind, namespace, name string) string {
264260
gvk := schema.GroupKind{
265261
Group: group,

0 commit comments

Comments
 (0)