@@ -61,6 +61,106 @@ func (b *RouteMgrImpl) onUpsertedHTTPRoute(routeKey k8stypes.NamespacedName, rou
6161 return b .onInvalidHTTPRouteUpserted (routeKey , route , mapExact , mapPrefix , mapRegex , mapDomainWPathExact )
6262}
6363
64+ func (b * RouteMgrImpl ) onUpsertedTLSRoute (routeKey k8stypes.NamespacedName , route * tree.TLSRoute ,
65+ sni * maps.MapData , acceptedHostnamesForRoute []string ,
66+ ) error {
67+ if route .Valid {
68+ return b .onValidTLSRouteUpserted (routeKey , route , sni , acceptedHostnamesForRoute )
69+ }
70+ return b .onInvalidTLSRouteUpserted (routeKey , route , sni )
71+ }
72+
73+ func (b * RouteMgrImpl ) onValidTLSRouteUpserted (_ k8stypes.NamespacedName ,
74+ tlsRoute * tree.TLSRoute , mapSNI * maps.MapData , acceptedHostnamesForRoute []string ) error {
75+
76+ for _ , tlsRouteRule := range tlsRoute .Rules {
77+ // if !rule.Valid {
78+ // find the old rule in route.TreeStatus.OldTreeResource.Rules, name is optional
79+ // TODO
80+ // }
81+ var routeValue string
82+ var backendNames []string
83+ var backendweights []int32
84+ hostnamesInserted := map [string ]struct {}{}
85+ for _ , backend := range tlsRouteRule .K8sResource .BackendRefs {
86+ checkResult , ok := tlsRouteRule .CheckBackendRef .Get (backend .BackendObjectReference )
87+ if ! ok || ! checkResult .Valid {
88+ b .topManager .logger .LogAttrs (context .Background (), slog .LevelDebug , "Processing TLSRoute [map update] - backend not valid" ,
89+ logging .LogAttrBackendName (string (backend .Name )),
90+ )
91+ continue
92+ }
93+
94+ // backend := rule.K8sResource.BackendRefs[index]
95+ svckey := k8stypes.NamespacedName {
96+ Name : string (backend .Name ),
97+ }
98+ if backend .Namespace == nil {
99+ svckey .Namespace = tlsRoute .K8sResource .Namespace
100+ } else {
101+ svckey .Namespace = string (* backend .Namespace )
102+ }
103+ svcPort := int32 (0 )
104+ if backend .Port != nil {
105+ svcPort = int32 (* backend .Port )
106+ }
107+
108+ backendName , err := b .topManager .getBackendName (svckey , int32 (svcPort ), "" )
109+ if err != nil {
110+ b .topManager .logger .LogAttrs (context .Background (), slog .LevelError , "Processing TLSRoute [map update]" ,
111+ logging .LogAttrError (err ),
112+ )
113+ continue
114+ }
115+ backendNames = append (backendNames , backendName )
116+ weight := int32 (0 )
117+ if backend .Weight != nil {
118+ weight = * backend .Weight
119+ }
120+ backendweights = append (backendweights , weight )
121+ }
122+ if len (backendNames ) == 1 {
123+ routeValue = backendNames [0 ]
124+ } else {
125+ // a: algo, s: suffix, l: list of backend (format depends on algo)
126+ // /wr_a70_b20_c10 {"a":"wr","l":"a:70,b:20,c:10"}
127+ routeValue = `{"a":"wr","l":"`
128+ for i , backendName := range backendNames {
129+ if i > 0 {
130+ routeValue += ","
131+ }
132+ routeValue += fmt .Sprintf ("%s:%d" , backendName , backendweights [i ])
133+ }
134+ routeValue += `"}`
135+ }
136+
137+ for _ , hostname := range acceptedHostnamesForRoute {
138+ if tlsRouteRule .Valid {
139+ mapSNI .AddData (string (hostname ), routeValue )
140+ hostnamesInserted [string (hostname )] = struct {}{}
141+ } else if _ , ok := hostnamesInserted [string (hostname )]; ! ok {
142+ mapSNI .DeleteData (string (hostname ))
143+ }
144+ }
145+ }
146+ return nil
147+ }
148+
149+ func (RouteMgrImpl ) onInvalidTLSRouteUpserted (_ k8stypes.NamespacedName , _ * tree.TLSRoute , _ * maps.MapData ) error {
150+ // TODO we might need to remove it from the maps
151+ return nil
152+ }
153+
154+ func (RouteMgrImpl ) onDeletedTLSRoute (_ k8stypes.NamespacedName , route * tree.TLSRoute ,
155+ mapSNI * maps.MapData ,
156+ ) error {
157+ hostnames := route .K8sResource .Spec .Hostnames
158+ for _ , hostname := range hostnames {
159+ mapSNI .DeleteData (string (hostname ))
160+ }
161+ return nil
162+ }
163+
64164func (RouteMgrImpl ) onDeletedHTTPRoute (_ k8stypes.NamespacedName , route * tree.HTTPRoute ,
65165 // func (b *RouteMgrImpl) onDeletedHTTPRoute(routeKey k8stypes.NamespacedName, route *tree.HTTPRoute,
66166 mapExact , mapPrefix , mapRegex , mapDomainWPathExact * maps.MapData ,
@@ -119,7 +219,7 @@ func (b *RouteMgrImpl) onValidHTTPRouteUpserted(_ k8stypes.NamespacedName, route
119219 // TODO
120220 // }
121221 var routeValue string
122- var backendNames []string
222+ var backendNamesForRule []string
123223 var backendweights []int32
124224 for index , backend := range rule .K8sResource .BackendRefs {
125225 checkResult , ok := rule .CheckBackendRef .Get (backend .BackendObjectReference )
@@ -151,20 +251,20 @@ func (b *RouteMgrImpl) onValidHTTPRouteUpserted(_ k8stypes.NamespacedName, route
151251 )
152252 continue
153253 }
154- backendNames = append (backendNames , backendName )
254+ backendNamesForRule = append (backendNamesForRule , backendName )
155255 weight := int32 (0 )
156256 if backend .Weight != nil {
157257 weight = * backend .Weight
158258 }
159259 backendweights = append (backendweights , weight )
160260 }
161- if len (backendNames ) == 1 {
162- routeValue = backendNames [0 ]
163- } else {
261+ if len (backendNamesForRule ) == 1 {
262+ routeValue = backendNamesForRule [0 ]
263+ } else if len ( backendNamesForRule ) > 1 {
164264 // a: algo, s: suffix, l: list of backend (format depends on algo)
165265 // /wr_a70_b20_c10 {"a":"wr","l":"a:70,b:20,c:10"}
166266 routeValue = `{"a":"wr","l":"`
167- for i , backendName := range backendNames {
267+ for i , backendName := range backendNamesForRule {
168268 if i > 0 {
169269 routeValue += ","
170270 }
0 commit comments