Skip to content

Commit 786b9ef

Browse files
author
Doyoon Kim
authored
Refactor event handling and remove service reconciler (#360)
1 parent 07701cc commit 786b9ef

File tree

13 files changed

+518
-862
lines changed

13 files changed

+518
-862
lines changed

controllers/eventhandlers/endpoints.go

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package eventhandlers
2+
3+
import (
4+
"context"
5+
"github.com/aws/aws-application-networking-k8s/pkg/apis/applicationnetworking/v1alpha1"
6+
"github.com/aws/aws-application-networking-k8s/pkg/k8s"
7+
"github.com/aws/aws-application-networking-k8s/pkg/model/core"
8+
"github.com/aws/aws-application-networking-k8s/pkg/utils/gwlog"
9+
corev1 "k8s.io/api/core/v1"
10+
"k8s.io/apimachinery/pkg/api/errors"
11+
"k8s.io/apimachinery/pkg/types"
12+
"sigs.k8s.io/controller-runtime/pkg/client"
13+
gateway_api_v1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
14+
gateway_api "sigs.k8s.io/gateway-api/apis/v1beta1"
15+
mcs_api "sigs.k8s.io/mcs-api/pkg/apis/v1alpha1"
16+
)
17+
18+
type resourceMapper struct {
19+
log gwlog.Logger
20+
client client.Client
21+
}
22+
23+
const (
24+
coreGroupName = "" // empty means core by definition
25+
serviceKind = "Service"
26+
serviceImportKind = "ServiceImport"
27+
)
28+
29+
func (r *resourceMapper) ServiceToRoutes(ctx context.Context, svc *corev1.Service, routeType core.RouteType) []core.Route {
30+
if svc == nil {
31+
return nil
32+
}
33+
return r.backendRefToRoutes(ctx, svc, coreGroupName, serviceKind, routeType)
34+
}
35+
36+
func (r *resourceMapper) ServiceImportToRoutes(ctx context.Context, svc *mcs_api.ServiceImport, routeType core.RouteType) []core.Route {
37+
if svc == nil {
38+
return nil
39+
}
40+
return r.backendRefToRoutes(ctx, svc, mcs_api.GroupName, serviceImportKind, routeType)
41+
}
42+
43+
func (r *resourceMapper) ServiceToServiceExport(ctx context.Context, svc *corev1.Service) *mcs_api.ServiceExport {
44+
if svc == nil {
45+
return nil
46+
}
47+
svcExport := &mcs_api.ServiceExport{}
48+
if err := r.client.Get(ctx, k8s.NamespacedName(svc), svcExport); err != nil {
49+
return nil
50+
}
51+
return svcExport
52+
}
53+
54+
func (r *resourceMapper) EndpointsToService(ctx context.Context, ep *corev1.Endpoints) *corev1.Service {
55+
if ep == nil {
56+
return nil
57+
}
58+
svc := &corev1.Service{}
59+
if err := r.client.Get(ctx, k8s.NamespacedName(ep), svc); err != nil {
60+
return nil
61+
}
62+
return svc
63+
}
64+
65+
func (r *resourceMapper) TargetGroupPolicyToService(ctx context.Context, tgp *v1alpha1.TargetGroupPolicy) *corev1.Service {
66+
if tgp == nil {
67+
return nil
68+
}
69+
policyName := k8s.NamespacedName(tgp).String()
70+
71+
targetRef := tgp.Spec.TargetRef
72+
if targetRef == nil {
73+
r.log.Infow("TargetGroupPolicy does not have targetRef, skipping",
74+
"policyName", policyName)
75+
return nil
76+
}
77+
if targetRef.Group != coreGroupName || targetRef.Kind != serviceKind {
78+
r.log.Infow("Detected non-Service TargetGroupPolicy attachment, skipping",
79+
"policyName", policyName, "targetRef", targetRef)
80+
return nil
81+
}
82+
namespace := tgp.Namespace
83+
if targetRef.Namespace != nil && namespace != string(*targetRef.Namespace) {
84+
r.log.Infow("Detected cross namespace TargetGroupPolicy attachment, skipping",
85+
"policyName", policyName, "targetRef", targetRef)
86+
return nil
87+
}
88+
89+
svcName := types.NamespacedName{
90+
Namespace: namespace,
91+
Name: string(targetRef.Name),
92+
}
93+
svc := &corev1.Service{}
94+
if err := r.client.Get(ctx, svcName, svc); err != nil {
95+
if errors.IsNotFound(err) {
96+
r.log.Debugw("TargetGroupPolicy is referring to non-existent service, skipping",
97+
"policyName", policyName, "serviceName", svcName.String())
98+
} else {
99+
// Still gracefully skipping the event but errors other than NotFound are bad sign.
100+
r.log.Errorw("Failed to query targetRef of TargetGroupPolicy",
101+
"policyName", policyName, "serviceName", svcName.String(), "reason", err.Error())
102+
}
103+
return nil
104+
}
105+
r.log.Debugw("TargetGroupPolicy change on Service detected",
106+
"policyName", policyName, "serviceName", svcName.String())
107+
108+
return svc
109+
}
110+
111+
func (r *resourceMapper) backendRefToRoutes(ctx context.Context, obj client.Object, group, kind string, routeType core.RouteType) []core.Route {
112+
if obj == nil {
113+
return nil
114+
}
115+
var routes []core.Route
116+
switch routeType {
117+
case core.HttpRouteType:
118+
routeList := &gateway_api.HTTPRouteList{}
119+
r.client.List(ctx, routeList)
120+
for _, k8sRoute := range routeList.Items {
121+
routes = append(routes, core.NewHTTPRoute(k8sRoute))
122+
}
123+
case core.GrpcRouteType:
124+
routeList := &gateway_api_v1alpha2.GRPCRouteList{}
125+
r.client.List(ctx, routeList)
126+
for _, k8sRoute := range routeList.Items {
127+
routes = append(routes, core.NewGRPCRoute(k8sRoute))
128+
}
129+
default:
130+
return nil
131+
}
132+
133+
var filteredRoutes []core.Route
134+
for _, route := range routes {
135+
if r.isBackendRefUsedByRoute(route, obj, group, kind) {
136+
filteredRoutes = append(filteredRoutes, route)
137+
}
138+
}
139+
return filteredRoutes
140+
}
141+
142+
func (r *resourceMapper) isBackendRefUsedByRoute(route core.Route, obj k8s.NamespacedAndNamed, group, kind string) bool {
143+
for _, rule := range route.Spec().Rules() {
144+
for _, backendRef := range rule.BackendRefs() {
145+
isGroupEqual := backendRef.Group() != nil && string(*backendRef.Group()) == group
146+
isKindEqual := backendRef.Kind() != nil && string(*backendRef.Kind()) == kind
147+
isNameEqual := string(backendRef.Name()) == obj.GetName()
148+
149+
namespace := route.Namespace()
150+
if backendRef.Namespace() != nil {
151+
namespace = string(*backendRef.Namespace())
152+
}
153+
isNamespaceEqual := namespace == obj.GetNamespace()
154+
155+
if isGroupEqual && isKindEqual && isNameEqual && isNamespaceEqual {
156+
return true
157+
}
158+
}
159+
}
160+
return false
161+
}

0 commit comments

Comments
 (0)