Skip to content

Commit 3252612

Browse files
committed
fixup! fixup! fixup! fixup! fixup! Network: gateway routes
1 parent 778ff1c commit 3252612

File tree

7 files changed

+297
-0
lines changed

7 files changed

+297
-0
lines changed

apis/networking/v1alpha1/routeconfiguration_types.go

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package v1alpha1
1616

1717
import (
18+
corev1 "k8s.io/api/core/v1"
1819
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1920
"k8s.io/apimachinery/pkg/runtime/schema"
2021
)
@@ -46,6 +47,9 @@ type Route struct {
4647
Dev *string `json:"dev,omitempty"`
4748
// Onlink enables the onlink falg inside the route.
4849
Onlink *bool `json:"onlink,omitempty"`
50+
// TargetRef is the reference to the target object of the route.
51+
// It is optional and it can be used for custom purposes.
52+
TargetRef *corev1.ObjectReference `json:"targetRef,omitempty"`
4953
}
5054

5155
// Rule is the rule of the RouteConfiguration.
@@ -61,6 +65,9 @@ type Rule struct {
6165
// Routes is the list of routes of the Rule.
6266
// +kubebuilder:validation:MinItems=1
6367
Routes []Route `json:"routes"`
68+
// TargetRef is the reference to the target object of the route.
69+
// It is optional and it can be used for custom purposes.
70+
TargetRef *corev1.ObjectReference `json:"targetRef,omitempty"`
6471
}
6572

6673
// Table is the table of the RouteConfiguration.
@@ -70,6 +77,9 @@ type Table struct {
7077
// Rules is the list of rules of the RouteConfiguration.
7178
// +kubebuilder:validation:MinItems=1
7279
Rules []Rule `json:"rules"`
80+
// TargetRef is the reference to the target object of the route.
81+
// It is optional and it can be used for custom purposes.
82+
TargetRef *corev1.ObjectReference `json:"targetRef,omitempty"`
7383
}
7484

7585
// RouteConfigurationSpec defines the desired state of RouteConfiguration.

apis/networking/v1alpha1/zz_generated.deepcopy.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_routeconfigurations.yaml

+126
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,53 @@ spec:
9191
description: Src is the source of the RouteConfiguration.
9292
pattern: ^(([1-9]{0,1}[0-9]{0,2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]{0,1}[0-9]{0,2}|2[0-4][0-9]|25[0-5])$
9393
type: string
94+
targetRef:
95+
description: TargetRef is the reference to the target
96+
object of the route. It is optional and it can be
97+
used for custom purposes.
98+
properties:
99+
apiVersion:
100+
description: API version of the referent.
101+
type: string
102+
fieldPath:
103+
description: 'If referring to a piece of an object
104+
instead of an entire object, this string should
105+
contain a valid JSON/Go field access statement,
106+
such as desiredState.manifest.containers[2].
107+
For example, if the object reference is to a
108+
container within a pod, this would take on a
109+
value like: "spec.containers{name}" (where "name"
110+
refers to the name of the container that triggered
111+
the event) or if no container name is specified
112+
"spec.containers[2]" (container with index 2
113+
in this pod). This syntax is chosen only to
114+
have some well-defined way of referencing a
115+
part of an object. TODO: this design is not
116+
final and this field is subject to change in
117+
the future.'
118+
type: string
119+
kind:
120+
description: 'Kind of the referent. More info:
121+
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
122+
type: string
123+
name:
124+
description: 'Name of the referent. More info:
125+
https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names'
126+
type: string
127+
namespace:
128+
description: 'Namespace of the referent. More
129+
info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/'
130+
type: string
131+
resourceVersion:
132+
description: 'Specific resourceVersion to which
133+
this reference is made, if any. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency'
134+
type: string
135+
uid:
136+
description: 'UID of the referent. More info:
137+
https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids'
138+
type: string
139+
type: object
140+
x-kubernetes-map-type: atomic
94141
required:
95142
- dst
96143
type: object
@@ -100,11 +147,90 @@ spec:
100147
description: Src is the source of the Rule.
101148
pattern: ^(([1-9]{0,1}[0-9]{0,2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]{0,1}[0-9]{0,2}|2[0-4][0-9]|25[0-5])\/([0-9]|[1-2][0-9]|3[0-2])$
102149
type: string
150+
targetRef:
151+
description: TargetRef is the reference to the target object
152+
of the route. It is optional and it can be used for custom
153+
purposes.
154+
properties:
155+
apiVersion:
156+
description: API version of the referent.
157+
type: string
158+
fieldPath:
159+
description: 'If referring to a piece of an object instead
160+
of an entire object, this string should contain a
161+
valid JSON/Go field access statement, such as desiredState.manifest.containers[2].
162+
For example, if the object reference is to a container
163+
within a pod, this would take on a value like: "spec.containers{name}"
164+
(where "name" refers to the name of the container
165+
that triggered the event) or if no container name
166+
is specified "spec.containers[2]" (container with
167+
index 2 in this pod). This syntax is chosen only to
168+
have some well-defined way of referencing a part of
169+
an object. TODO: this design is not final and this
170+
field is subject to change in the future.'
171+
type: string
172+
kind:
173+
description: 'Kind of the referent. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
174+
type: string
175+
name:
176+
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names'
177+
type: string
178+
namespace:
179+
description: 'Namespace of the referent. More info:
180+
https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/'
181+
type: string
182+
resourceVersion:
183+
description: 'Specific resourceVersion to which this
184+
reference is made, if any. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency'
185+
type: string
186+
uid:
187+
description: 'UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids'
188+
type: string
189+
type: object
190+
x-kubernetes-map-type: atomic
103191
required:
104192
- routes
105193
type: object
106194
minItems: 1
107195
type: array
196+
targetRef:
197+
description: TargetRef is the reference to the target object of
198+
the route. It is optional and it can be used for custom purposes.
199+
properties:
200+
apiVersion:
201+
description: API version of the referent.
202+
type: string
203+
fieldPath:
204+
description: 'If referring to a piece of an object instead
205+
of an entire object, this string should contain a valid
206+
JSON/Go field access statement, such as desiredState.manifest.containers[2].
207+
For example, if the object reference is to a container within
208+
a pod, this would take on a value like: "spec.containers{name}"
209+
(where "name" refers to the name of the container that triggered
210+
the event) or if no container name is specified "spec.containers[2]"
211+
(container with index 2 in this pod). This syntax is chosen
212+
only to have some well-defined way of referencing a part
213+
of an object. TODO: this design is not final and this field
214+
is subject to change in the future.'
215+
type: string
216+
kind:
217+
description: 'Kind of the referent. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
218+
type: string
219+
name:
220+
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names'
221+
type: string
222+
namespace:
223+
description: 'Namespace of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/'
224+
type: string
225+
resourceVersion:
226+
description: 'Specific resourceVersion to which this reference
227+
is made, if any. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency'
228+
type: string
229+
uid:
230+
description: 'UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids'
231+
type: string
232+
type: object
233+
x-kubernetes-map-type: atomic
108234
required:
109235
- name
110236
- rules

pkg/liqo-controller-manager/internal-network/route/k8s.go

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ func addPodToRoute(pod *corev1.Pod, internalnode *networkingv1alpha1.InternalNod
9292
Dev: &internalnode.Spec.Interface.Gateway.Name,
9393
Gw: ptr.To(networkingv1alpha1.IP(fabric.GeneveNodeInterfaceIP)),
9494
Onlink: ptr.To(true),
95+
TargetRef: &corev1.ObjectReference{
96+
Kind: pod.GetObjectKind().GroupVersionKind().Kind,
97+
Name: pod.GetName(),
98+
Namespace: pod.GetNamespace(),
99+
UID: pod.GetUID(),
100+
},
95101
}
96102
rule.Routes = append(rule.Routes, route)
97103
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright 2019-2024 The Liqo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package route
16+
17+
import (
18+
"context"
19+
20+
corev1 "k8s.io/api/core/v1"
21+
apierrors "k8s.io/apimachinery/pkg/api/errors"
22+
"k8s.io/apimachinery/pkg/labels"
23+
"k8s.io/klog/v2"
24+
"sigs.k8s.io/controller-runtime/pkg/client"
25+
"sigs.k8s.io/controller-runtime/pkg/event"
26+
"sigs.k8s.io/controller-runtime/pkg/handler"
27+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
28+
"sigs.k8s.io/controller-runtime/pkg/source"
29+
30+
networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1"
31+
"github.com/liqotech/liqo/pkg/utils/getters"
32+
)
33+
34+
// NewLeftoverPodsSource returns a new LeftoversPodSource.
35+
func NewLeftoverPodsSource(src <-chan event.GenericEvent) *source.Channel {
36+
return &source.Channel{
37+
Source: src,
38+
}
39+
}
40+
41+
// NewLeftoverPodsEventHandler returns a new LeftoverPodsEventHandler.
42+
func NewLeftoverPodsEventHandler(opts *Options) handler.EventHandler {
43+
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, o client.Object) []reconcile.Request {
44+
pod, ok := o.(*corev1.Pod)
45+
if !ok {
46+
klog.Errorf("unable to cast object %s to pod", o.GetName())
47+
return nil
48+
}
49+
return []reconcile.Request{
50+
{
51+
NamespacedName: client.ObjectKey{
52+
Name: pod.Spec.NodeName,
53+
Namespace: opts.Namespace,
54+
},
55+
},
56+
}
57+
})
58+
}
59+
60+
// CheckLeftoverRoutes lists all currently existing routeconfigurations and adds their
61+
// pod to the queue. This will detect routes that exist with no
62+
// corresponding pod; these routes need to be deleted. We only need to
63+
// do this once on startup, because in steady-state these are detected (but
64+
// some stragglers could have been left behind if this controller
65+
// reboots).
66+
func (r *PodReconciler) CheckLeftoverRoutes(ctx context.Context) error {
67+
routecfglist, err := getters.ListRouteConfigurationsByLabel(ctx, r.Client, r.Options.Namespace, labels.Everything())
68+
if err != nil {
69+
return err
70+
}
71+
return r.processRouteConfigurations(ctx, routecfglist)
72+
}
73+
74+
func (r *PodReconciler) processRouteConfigurations(ctx context.Context, routecfglist *networkingv1alpha1.RouteConfigurationList) error {
75+
for i := range routecfglist.Items {
76+
if routecfglist.Items[i].Spec.Table.Rules == nil || len(routecfglist.Items[i].Spec.Table.Rules) == 0 {
77+
continue
78+
}
79+
if err := r.processRules(ctx, &routecfglist.Items[i]); err != nil {
80+
return err
81+
}
82+
}
83+
return nil
84+
}
85+
86+
func (r *PodReconciler) processRules(ctx context.Context, routecfg *networkingv1alpha1.RouteConfiguration) error {
87+
for j := range routecfg.Spec.Table.Rules {
88+
if err := r.processRoutes(ctx, &routecfg.Spec.Table.Rules[j]); err != nil {
89+
return err
90+
}
91+
}
92+
return nil
93+
}
94+
95+
func (r *PodReconciler) processRoutes(ctx context.Context, rule *networkingv1alpha1.Rule) error {
96+
for k := range rule.Routes {
97+
if rule.Routes[k].TargetRef == nil {
98+
continue
99+
}
100+
if err := r.processRoute(ctx, &rule.Routes[k]); err != nil {
101+
return err
102+
}
103+
}
104+
return nil
105+
}
106+
107+
func (r *PodReconciler) processRoute(ctx context.Context, route *networkingv1alpha1.Route) error {
108+
pod := &corev1.Pod{}
109+
if err := r.Get(ctx, client.ObjectKey{Name: route.TargetRef.Name, Namespace: route.TargetRef.Namespace}, pod); err != nil {
110+
if !apierrors.IsNotFound(err) {
111+
return err
112+
}
113+
klog.Infof("pod %s not found, adding to queue", route.TargetRef.Name)
114+
}
115+
return nil
116+
}

0 commit comments

Comments
 (0)