Skip to content

Commit cdbf329

Browse files
committed
save temp
Signed-off-by: clyi <[email protected]>
1 parent ee6f907 commit cdbf329

17 files changed

+367
-25
lines changed

Makefile

+1-2
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,8 @@ build-go-arm:
134134
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build $(GO_BUILD_FLAGS) -buildmode=pie -o $(CURDIR)/dist/images/kube-ovn-controller -v ./cmd/controller
135135

136136
.PHONY: build-kube-ovn
137-
build-kube-ovn: build-debug build-go
137+
build-kube-ovn: build-go
138138
docker build -t $(REGISTRY)/kube-ovn:$(RELEASE_TAG) --build-arg VERSION=$(RELEASE_TAG) -f dist/images/Dockerfile dist/images/
139-
docker build -t $(REGISTRY)/kube-ovn:$(LEGACY_TAG) --build-arg VERSION=$(LEGACY_TAG) -f dist/images/Dockerfile dist/images/
140139

141140
.PHONY: build-kube-ovn-dpdk
142141
build-kube-ovn-dpdk: build-go

dist/images/Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# syntax = docker/dockerfile:experimental
22
ARG VERSION
33
ARG BASE_TAG=$VERSION
4-
FROM kubeovn/kube-ovn-base:$BASE_TAG AS setcap
4+
FROM yichanglu/kube-ovn-base:$BASE_TAG AS setcap
55

66
COPY *.sh /kube-ovn/
77
COPY kubectl-ko /kube-ovn/kubectl-ko
@@ -22,7 +22,7 @@ RUN ln -s /kube-ovn/kube-ovn-cmd /kube-ovn/kube-ovn-monitor && \
2222
setcap CAP_NET_RAW,CAP_NET_BIND_SERVICE+eip /kube-ovn/kube-ovn-controller && \
2323
setcap CAP_NET_ADMIN,CAP_NET_RAW,CAP_NET_BIND_SERVICE,CAP_SYS_ADMIN+eip /kube-ovn/kube-ovn-daemon
2424

25-
FROM kubeovn/kube-ovn-base:$BASE_TAG
25+
FROM yichanglu/kube-ovn-base:$BASE_TAG
2626

2727
COPY --chmod=0644 logrotate/* /etc/logrotate.d/
2828
COPY grace_stop_ovn_controller /usr/share/ovn/scripts/grace_stop_ovn_controller

dist/images/Dockerfile.base

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ RUN cd /usr/src/ && git clone -b branch-24.03 --depth=1 https://github.com/ovn-o
6565
# support dedicated BFD LRP
6666
curl -s https://github.com/kubeovn/ovn/commit/40345aa35d03c93cde877ccfa8111346291ebc7c.patch | git apply && \
6767
# skip node local dns ip conntrack when set acl
68-
curl -s https://github.com/kubeovn/ovn/commit/e7d3ba53cdcbc524bb29c54ddb07b83cc4258ed7.patch | git apply
68+
curl -s https://github.com/kubeovn/ovn/commit/e7d3ba53cdcbc524bb29c54ddb07b83cc4258ed7.patch | git apply && \
69+
# select local backend first
70+
curl -s https://github.com/kubeovn/ovn/commit/e5a123631df32895f6a1fd3796d073f3afe03d44.patch | git apply
6971

7072
RUN apt install -y build-essential fakeroot \
7173
autoconf automake bzip2 debhelper-compat dh-exec dh-python dh-sequence-python3 dh-sequence-sphinxdoc \

dist/images/install.sh

+11-8
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ OVSDB_INACTIVITY_TIMEOUT=${OVSDB_INACTIVITY_TIMEOUT:-10}
4747
ENABLE_LIVE_MIGRATION_OPTIMIZE=${ENABLE_LIVE_MIGRATION_OPTIMIZE:-true}
4848

4949
# debug
50-
DEBUG_WRAPPER=${DEBUG_WRAPPER:-}
50+
DEBUG_WRAPPER=${DEBUG_WRAPPER:-true}
5151
RUN_AS_USER=65534 # run as nobody
5252
if [ "$ENABLE_OVN_IPSEC" = "true" -o -n "$DEBUG_WRAPPER" ]; then
5353
RUN_AS_USER=0
5454
fi
5555

56+
RUN_AS_USER=0
57+
5658
KUBELET_DIR=${KUBELET_DIR:-/var/lib/kubelet}
5759
LOG_DIR=${LOG_DIR:-/var/log}
5860

@@ -3665,6 +3667,7 @@ rules:
36653667
- ovn-eips/status
36663668
- nodes
36673669
- pods
3670+
- vips
36683671
verbs:
36693672
- get
36703673
- list
@@ -3956,7 +3959,7 @@ spec:
39563959
- /kube-ovn/start-db.sh
39573960
securityContext:
39583961
runAsUser: ${RUN_AS_USER}
3959-
privileged: false
3962+
privileged: true
39603963
capabilities:
39613964
add:
39623965
- NET_BIND_SERVICE
@@ -4302,7 +4305,7 @@ spec:
43024305
- /kube-ovn/start-ovs.sh
43034306
securityContext:
43044307
runAsUser: ${RUN_AS_USER}
4305-
privileged: false
4308+
privileged: true
43064309
capabilities:
43074310
add:
43084311
- NET_ADMIN
@@ -4729,7 +4732,7 @@ spec:
47294732
- --image=$REGISTRY/kube-ovn:$VERSION
47304733
securityContext:
47314734
runAsUser: ${RUN_AS_USER}
4732-
privileged: false
4735+
privileged: true
47334736
capabilities:
47344737
add:
47354738
- NET_BIND_SERVICE
@@ -4919,7 +4922,7 @@ spec:
49194922
- --set-vxlan-tx-off=$SET_VXLAN_TX_OFF
49204923
securityContext:
49214924
runAsUser: 0
4922-
privileged: false
4925+
privileged: true
49234926
capabilities:
49244927
add:
49254928
- NET_ADMIN
@@ -5131,7 +5134,7 @@ spec:
51315134
imagePullPolicy: $IMAGE_PULL_POLICY
51325135
securityContext:
51335136
runAsUser: ${RUN_AS_USER}
5134-
privileged: false
5137+
privileged: true
51355138
capabilities:
51365139
add:
51375140
- NET_BIND_SERVICE
@@ -5281,7 +5284,7 @@ spec:
52815284
- --log_file_max_size=200
52825285
securityContext:
52835286
runAsUser: ${RUN_AS_USER}
5284-
privileged: false
5287+
privileged: true
52855288
capabilities:
52865289
add:
52875290
- NET_BIND_SERVICE
@@ -5502,7 +5505,7 @@ spec:
55025505
- --alsologtostderr=true
55035506
securityContext:
55045507
runAsUser: ${RUN_AS_USER}
5505-
privileged: false
5508+
privileged: true
55065509
capabilities:
55075510
add:
55085511
- NET_BIND_SERVICE

mocks/pkg/ovs/interface.go

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

pkg/apis/kubeovn/v1/subnet.go

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ type SubnetSpec struct {
8585
EnableLb *bool `json:"enableLb,omitempty"`
8686
EnableEcmp bool `json:"enableEcmp,omitempty"`
8787
EnableMulticastSnoop bool `json:"enableMulticastSnoop,omitempty"`
88+
IsMetalLBAddressPool bool `json:"metallbAddressPool,omitempty"`
8889

8990
RouteTable string `json:"routeTable,omitempty"`
9091
NamespaceSelectors []metav1.LabelSelector `json:"namespaceSelectors,omitempty"`

pkg/controller/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ type Configuration struct {
9393
EnableEcmp bool
9494
EnableKeepVMIP bool
9595
EnableLbSvc bool
96+
EnableLbSvcPolicyLocal bool
9697
EnableMetrics bool
9798
EnableANP bool
9899
EnableOVNIPSec bool
@@ -175,6 +176,7 @@ func ParseFlags() (*Configuration, error) {
175176
argEnableEcmp = pflag.Bool("enable-ecmp", false, "Enable ecmp route for centralized subnet")
176177
argKeepVMIP = pflag.Bool("keep-vm-ip", true, "Whether to keep ip for kubevirt pod when pod is rebuild")
177178
argEnableLbSvc = pflag.Bool("enable-lb-svc", false, "Whether to support loadbalancer service")
179+
argEnableLbSvcPolicyLocal = pflag.Bool("is-external-lb", true, "Whether to support external loadbalancer")
178180
argEnableMetrics = pflag.Bool("enable-metrics", true, "Whether to support metrics query")
179181
argEnableANP = pflag.Bool("enable-anp", false, "Enable support for admin network policy and baseline admin network policy")
180182
argEnableOVNIPSec = pflag.Bool("enable-ovn-ipsec", false, "Whether to enable ovn ipsec")
@@ -271,6 +273,7 @@ func ParseFlags() (*Configuration, error) {
271273
GCInterval: *argGCInterval,
272274
InspectInterval: *argInspectInterval,
273275
EnableLbSvc: *argEnableLbSvc,
276+
EnableLbSvcPolicyLocal: *argEnableLbSvcPolicyLocal,
274277
EnableMetrics: *argEnableMetrics,
275278
EnableOVNIPSec: *argEnableOVNIPSec,
276279
EnableLiveMigrationOptimize: *argEnableLiveMigrationOptimize,

pkg/controller/endpoint.go

+51-7
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func (c *Controller) handleUpdateEndpoint(key string) error {
7575
vip, vpcName, subnetName string
7676
ok bool
7777
ignoreHealthCheck = true
78+
isPreferLocalBackend = false
7879
)
7980

8081
if vip, ok = svc.Annotations[util.SwitchLBRuleVipsAnnotation]; ok {
@@ -93,6 +94,20 @@ func (c *Controller) handleUpdateEndpoint(key string) error {
9394
return nil
9495
}
9596

97+
// 注意这些东西只有在ovn lb开关打开的时候才能用
98+
if svc.Spec.Type == v1.ServiceTypeLoadBalancer && svc.Spec.ExternalTrafficPolicy == v1.ServiceExternalTrafficPolicyTypeLocal {
99+
if externalIP := util.GetLoadBalancerIP(*svc); err == nil && externalIP != "" {
100+
lbVips = append(lbVips, externalIP)
101+
} else if err != nil {
102+
klog.Errorf("failed to get external load balancer IP for service %s/%s: %v", namespace, name, err)
103+
return err
104+
}
105+
isPreferLocalBackend = true
106+
} else if svc.Spec.Type == v1.ServiceTypeClusterIP && svc.Spec.InternalTrafficPolicy != nil && *svc.Spec.InternalTrafficPolicy == v1.ServiceInternalTrafficPolicyLocal {
107+
isPreferLocalBackend = true
108+
}
109+
110+
klog.Errorf("clyi lbVips %v", lbVips)
96111
if pods, err = c.podsLister.Pods(namespace).List(labels.Set(svc.Spec.Selector).AsSelector()); err != nil {
97112
klog.Errorf("failed to get pods for service %s in namespace %s: %v", name, namespace, err)
98113
return err
@@ -157,19 +172,40 @@ func (c *Controller) handleUpdateEndpoint(key string) error {
157172
backends []string
158173
ipPortMapping, externals map[string]string
159174
)
160-
161-
if !ignoreHealthCheck {
175+
isGenIPPortMapping := !ignoreHealthCheck || isPreferLocalBackend
176+
if isGenIPPortMapping {
162177
if checkIP, err = c.getHealthCheckVip(subnetName, lbVip); err != nil {
163178
klog.Error(err)
164179
return err
165180
}
181+
182+
subnet, err := c.subnetsLister.Get(subnetName)
183+
if err != nil {
184+
klog.Errorf("failed to get subnet %s: %v", subnetName, err)
185+
return err
186+
}
187+
188+
if subnet.Spec.IsMetalLBAddressPool {
189+
vipName := fmt.Sprintf("%s.%s", svc.Name, svc.Namespace)
190+
vip := &kubeovnv1.Vip{
191+
ObjectMeta: metav1.ObjectMeta{
192+
Name: vipName,
193+
},
194+
Spec: kubeovnv1.VipSpec{
195+
Subnet: subnetName,
196+
},
197+
}
198+
if _, err = c.config.KubeOvnClient.KubeovnV1().Vips().Create(context.Background(), vip, metav1.CreateOptions{}); err != nil {
199+
klog.Errorf("failed to create vip %s, %v", vipName, err)
200+
return err
201+
}
202+
}
203+
166204
externals = map[string]string{
167205
util.SwitchLBRuleSubnet: subnetName,
168206
}
169207
}
170-
171-
ipPortMapping, backends = getIPPortMappingBackend(ep, pods, port, lbVip, checkIP, ignoreHealthCheck)
172-
208+
ipPortMapping, backends = getIPPortMappingBackend(ep, pods, port, lbVip, checkIP, isGenIPPortMapping)
173209
// for performance reason delete lb with no backends
174210
if len(backends) != 0 {
175211
vip = util.JoinHostPort(lbVip, port.Port)
@@ -178,6 +214,14 @@ func (c *Controller) handleUpdateEndpoint(key string) error {
178214
klog.Errorf("failed to add vip %s with backends %s to LB %s: %v", lbVip, backends, lb, err)
179215
return err
180216
}
217+
218+
if isPreferLocalBackend {
219+
if err = c.OVNNbClient.LoadBalancerUpdateIPPortMapping(lb, vip, ipPortMapping); err != nil {
220+
klog.Errorf("failed to update ip port mapping %s for vip %s to LB %s: %v", ipPortMapping, vip, lb, err)
221+
return err
222+
}
223+
}
224+
181225
if !ignoreHealthCheck && len(ipPortMapping) != 0 {
182226
klog.Infof("add health check ip port mapping %v to LB %s", ipPortMapping, lb)
183227
if err = c.OVNNbClient.LoadBalancerAddHealthCheck(lb, vip, ignoreHealthCheck, ipPortMapping, externals); err != nil {
@@ -321,7 +365,7 @@ func (c *Controller) getHealthCheckVip(subnetName, lbVip string) (string, error)
321365
return checkIP, nil
322366
}
323367

324-
func getIPPortMappingBackend(endpoints *v1.Endpoints, pods []*v1.Pod, servicePort v1.ServicePort, serviceIP, checkVip string, ignoreHealthCheck bool) (map[string]string, []string) {
368+
func getIPPortMappingBackend(endpoints *v1.Endpoints, pods []*v1.Pod, servicePort v1.ServicePort, serviceIP, checkVip string, isGenIPPortMapping bool) (map[string]string, []string) {
325369
var (
326370
ipPortMapping = map[string]string{}
327371
backends = []string{}
@@ -341,7 +385,7 @@ func getIPPortMappingBackend(endpoints *v1.Endpoints, pods []*v1.Pod, servicePor
341385
}
342386

343387
for _, address := range subset.Addresses {
344-
if !ignoreHealthCheck && address.TargetRef.Name != "" {
388+
if isGenIPPortMapping && address.TargetRef.Name != "" {
345389
ipName := fmt.Sprintf("%s.%s", address.TargetRef.Name, endpoints.Namespace)
346390
ipPortMapping[address.IP] = fmt.Sprintf(util.HealthCheckNamedVipTemplate, ipName, checkVip)
347391
}

pkg/controller/init.go

+6
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ func (c *Controller) initLB(name, protocol string, sessionAffinity bool) error {
251251
}
252252
}
253253

254+
err = c.OVNNbClient.SetLoadBalancerPreferLocalBackend(name, c.config.EnableLbSvcPolicyLocal)
255+
if err != nil {
256+
klog.Errorf("failed to set prefer local backend for load balancer %s: %v", name, err)
257+
return err
258+
}
259+
254260
return nil
255261
}
256262

pkg/controller/subnet.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,11 @@ func (c *Controller) handleAddOrUpdateSubnet(key string) error {
681681

682682
needRouter := subnet.Spec.Vlan == "" || subnet.Spec.LogicalGateway ||
683683
(subnet.Status.U2OInterconnectionIP != "" && subnet.Spec.U2OInterconnection)
684+
685+
if subnet.Spec.Vlan != "" && subnet.Spec.IsMetalLBAddressPool {
686+
needRouter = true
687+
}
688+
684689
// 1. overlay subnet, should add lrp, lrp ip is subnet gw
685690
// 2. underlay subnet use logical gw, should add lrp, lrp ip is subnet gw
686691
randomAllocateGW := !subnet.Spec.LogicalGateway && vpc.Spec.EnableExternal && subnet.Name == c.config.ExternalGatewaySwitch
@@ -1852,8 +1857,11 @@ func (c *Controller) reconcileSubnetSpecialIPs(subnet *kubeovnv1.Subnet) (bool,
18521857
if subnet.Spec.Vlan != "" && !subnet.Spec.LogicalGateway {
18531858
u2oInterconnName := fmt.Sprintf(util.U2OInterconnName, subnet.Spec.Vpc, subnet.Name)
18541859
u2oInterconnLrpName := fmt.Sprintf("%s-%s", subnet.Spec.Vpc, subnet.Name)
1860+
1861+
needAllocateU2OIP := false
1862+
needAllocateU2OIP = subnet.Spec.U2OInterconnection || subnet.Spec.IsMetalLBAddressPool
18551863
var v4ip, v6ip string
1856-
if subnet.Spec.U2OInterconnection {
1864+
if needAllocateU2OIP {
18571865
v4ip, v6ip, _, err = c.acquireU2OIP(subnet, u2oInterconnName, u2oInterconnLrpName)
18581866
if err != nil {
18591867
return isU2OIPChanged, isMcastQuerierIPChanged, err
@@ -1862,7 +1870,7 @@ func (c *Controller) reconcileSubnetSpecialIPs(subnet *kubeovnv1.Subnet) (bool,
18621870
if v4ip != "" || v6ip != "" {
18631871
isU2OIPChanged = true
18641872
}
1865-
} else if subnet.Status.U2OInterconnectionIP != "" {
1873+
} else if !needAllocateU2OIP && subnet.Status.U2OInterconnectionIP != "" {
18661874
err = c.releaseU2OIP(subnet, u2oInterconnName)
18671875
if err != nil {
18681876
return isU2OIPChanged, isMcastQuerierIPChanged, err

pkg/daemon/controller.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ type Controller struct {
5858
nodesLister listerv1.NodeLister
5959
nodesSynced cache.InformerSynced
6060

61+
virtualIpsLister kubeovnlister.VipLister
62+
virtualIpsSynced cache.InformerSynced
63+
6164
recorder record.EventRecorder
6265

6366
protocol string
@@ -89,6 +92,8 @@ func NewController(config *Configuration, stopCh <-chan struct{}, podInformerFac
8992
ovnEipInformer := kubeovnInformerFactory.Kubeovn().V1().OvnEips()
9093
podInformer := podInformerFactory.Core().V1().Pods()
9194
nodeInformer := nodeInformerFactory.Core().V1().Nodes()
95+
virtualIPInformer := kubeovnInformerFactory.Kubeovn().V1().Vips()
96+
// serviceInformer := kubeovnInformerFactory.Kubeovn().V1().Services()
9297

9398
controller := &Controller{
9499
config: config,
@@ -105,6 +110,9 @@ func NewController(config *Configuration, stopCh <-chan struct{}, podInformerFac
105110
subnetsSynced: subnetInformer.Informer().HasSynced,
106111
subnetQueue: newTypedRateLimitingQueue[*subnetEvent]("Subnet", nil),
107112

113+
virtualIpsLister: virtualIPInformer.Lister(),
114+
virtualIpsSynced: virtualIPInformer.Informer().HasSynced,
115+
108116
ovnEipsLister: ovnEipInformer.Lister(),
109117
ovnEipsSynced: ovnEipInformer.Informer().HasSynced,
110118

@@ -135,7 +143,7 @@ func NewController(config *Configuration, stopCh <-chan struct{}, podInformerFac
135143

136144
if !cache.WaitForCacheSync(stopCh,
137145
controller.providerNetworksSynced, controller.vlansSynced, controller.subnetsSynced,
138-
controller.podsSynced, controller.nodesSynced) {
146+
controller.podsSynced, controller.nodesSynced, controller.virtualIpsSynced) {
139147
util.LogFatalAndExit(nil, "failed to wait for caches to sync")
140148
}
141149

0 commit comments

Comments
 (0)