Skip to content

Commit

Permalink
fix libovsdb issues (#2070)
Browse files Browse the repository at this point in the history
1. avoid tcp connection stuck by setting TCP_USER_TIMEOUT socket option;
2. fix connect/reconnect timeout;
3. ensure cache is consistent before reading data.
  • Loading branch information
zhangzujian committed Nov 27, 2022
1 parent 9c292c0 commit 7344578
Show file tree
Hide file tree
Showing 39 changed files with 248 additions and 206 deletions.
14 changes: 8 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ require (
github.com/Wifx/gonetworkmanager v0.4.0
github.com/alauda/felix v3.6.6-0.20201207121355-187332daf314+incompatible
github.com/bhendo/go-powershell v0.0.0-20190719160123-219e7fb4e41e
github.com/cenkalti/backoff/v4 v4.1.3
github.com/cenkalti/backoff/v4 v4.2.0
github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08
github.com/containernetworking/cni v1.1.2
github.com/containernetworking/plugins v1.1.1
github.com/coreos/go-iptables v0.6.0
github.com/emicklei/go-restful/v3 v3.9.0
github.com/evanphx/json-patch v5.6.0+incompatible
github.com/go-logr/stdr v1.2.2
github.com/greenpau/ovsdb v1.0.2
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.3.0
github.com/mdlayher/arp v0.0.0-20220512170110-6706a2966875
Expand All @@ -28,10 +27,10 @@ require (
github.com/osrg/gobgp/v3 v3.6.0
github.com/ovn-org/libovsdb v0.0.0-20221101143603-8f21d188c3a5
github.com/parnurzeal/gorequest v0.2.16
github.com/prometheus/client_golang v1.13.0
github.com/prometheus/client_golang v1.14.0
github.com/sirupsen/logrus v1.9.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.8.1
github.com/vishvananda/netlink v1.2.1-beta.2
golang.org/x/sys v0.2.0
golang.org/x/time v0.2.0
Expand All @@ -52,6 +51,7 @@ require (
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/cenk/hub v1.0.1 // indirect
github.com/cenkalti/hub v1.0.1 // indirect
github.com/cenkalti/rpc2 v0.0.0-20210604223624-c1acbc6ec984 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
Expand All @@ -68,6 +68,7 @@ require (
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
Expand Down Expand Up @@ -95,7 +96,7 @@ require (
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mdlayher/ethernet v0.0.0-20220221185849-529eae5b6118 // indirect
github.com/mdlayher/packet v1.0.0 // indirect
github.com/mdlayher/socket v0.2.3 // indirect
Expand All @@ -116,7 +117,7 @@ require (
github.com/projectcalico/go-yaml v0.0.0-20161201183616-955bc3e451ef // indirect
github.com/projectcalico/go-yaml-wrapper v0.0.0-20161127220527-598e54215bee // indirect
github.com/projectcalico/libcalico-go v0.0.0-20190305235709-3d935c3b8b86 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
Expand Down Expand Up @@ -161,6 +162,7 @@ replace (
github.com/openshift/api => github.com/openshift/api v0.0.0-20210105115604-44119421ec6b
github.com/openshift/client-go => github.com/openshift/client-go v0.0.0-20210112165513-ebc401615f47
github.com/openshift/library-go => github.com/mhenriks/library-go v0.0.0-20210511195009-51ba86622560
github.com/ovn-org/libovsdb => github.com/kubeovn/libovsdb v0.0.0-20221125061852-8b910935f8e4
gopkg.in/k8snetworkplumbingwg/multus-cni.v3 => github.com/k8snetworkplumbingwg/multus-cni v0.0.0-20220818150730-3d9cec4ec9d5
k8s.io/api => k8s.io/api v0.24.4
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.24.4
Expand Down
25 changes: 15 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,11 @@ github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b/go.mod h1:obH5gd0Bsq
github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE=
github.com/campoy/embedmd v1.0.0/go.mod h1:oxyr9RCiSXg0M3VJ3ks0UGfp98BpSSGr0kpiX3MzVl8=
github.com/cenk/hub v1.0.1 h1:RBwXNOF4a8KjD8BJ08XqN8KbrqaGiQLDrgvUGJSHuPA=
github.com/cenk/hub v1.0.1/go.mod h1:rJM1LNAW0ppT8FMMuPK6c2NP/R2nH/UthtuRySSaf6Y=
github.com/cenkalti/backoff v0.0.0-20181003080854-62661b46c409/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4=
github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/cenkalti/backoff/v4 v4.2.0 h1:HN5dHm3WBOgndBH6E8V0q2jIYIR3s9yglV8k/+MN3u4=
github.com/cenkalti/backoff/v4 v4.2.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cenkalti/hub v1.0.1 h1:UMtjc6dHSaOQTO15SVA50MBIR9zQwvsukQupDrkIRtg=
github.com/cenkalti/hub v1.0.1/go.mod h1:tcYwtS3a2d9NO/0xDXVJWx3IedurUjYCqFCmpi0lpHs=
github.com/cenkalti/rpc2 v0.0.0-20210604223624-c1acbc6ec984 h1:CNwZyGS6KpfaOWbh2yLkSy3rSTUh3jub9CzpFpP6PVQ=
Expand Down Expand Up @@ -817,6 +818,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubeovn/felix v0.0.0-20220325073257-c8a0f705d139 h1:MaLC8/dohKHU8nkfglfE2oikefB6urJG75yZDOcKTRU=
github.com/kubeovn/felix v0.0.0-20220325073257-c8a0f705d139/go.mod h1:ulxnUH9cbIOtCH+exhJPeV2mleh+bDv67WKsl/MVU/g=
github.com/kubeovn/libovsdb v0.0.0-20221125061852-8b910935f8e4 h1:S/R2b2/S7L3l68Y2YwV/0zDGEVDargfRVqYu6989fIw=
github.com/kubeovn/libovsdb v0.0.0-20221125061852-8b910935f8e4/go.mod h1:N20zsElkDpTm57hVDosiZVghprt9Y4Vfqsi1HBXOzr4=
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0 h1:nHHjmvjitIiyPlUHk/ofpgvBcNcawJLtf4PYHORLjAA=
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0/go.mod h1:YBCo4DoEeDndqvAn6eeu0vWM7QdXmHEeI9cFWplmBys=
github.com/kylelemons/godebug v0.0.0-20160406211939-eadb3ce320cb/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
Expand Down Expand Up @@ -859,8 +862,9 @@ github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
github.com/mattn/go-shellwords v1.0.3/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
github.com/mattn/go-shellwords v1.0.6/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/maxbrunsfeld/counterfeiter/v6 v6.2.2/go.mod h1:eD9eIE7cdwcMi9rYluz88Jz2VyhSmden33/aXg4oVIY=
github.com/mdlayher/arp v0.0.0-20220512170110-6706a2966875 h1:ql8x//rJsHMjS+qqEag8n3i4azw1QneKh5PieH9UEbY=
github.com/mdlayher/arp v0.0.0-20220512170110-6706a2966875/go.mod h1:kfOoFJuHWp76v1RgZCb9/gVUc7XdY877S2uVYbNliGc=
Expand Down Expand Up @@ -1013,8 +1017,6 @@ github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFSt
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
github.com/osrg/gobgp/v3 v3.6.0 h1:mKJoFUt+5RaGujNfus3XELmBGntSeniJJNGfCh/5XVc=
github.com/osrg/gobgp/v3 v3.6.0/go.mod h1:fKQPuk7+4qMiDT5viZTXT/aSEn8yYDkEs5p3NjmU2bw=
github.com/ovn-org/libovsdb v0.0.0-20221101143603-8f21d188c3a5 h1:3kWiEqfdhvdVuyL7ZSxkn8sg9xwIoh7SuhWboAIUDq0=
github.com/ovn-org/libovsdb v0.0.0-20221101143603-8f21d188c3a5/go.mod h1:S/+Hux9//oB7yLaPsUKnXTzZc6S1C4a9HP0UifXfKz0=
github.com/parnurzeal/gorequest v0.2.16 h1:T/5x+/4BT+nj+3eSknXmCTnEVGSzFzPGdpqmUVVZXHQ=
github.com/parnurzeal/gorequest v0.2.16/go.mod h1:3Kh2QUMJoqw3icWAecsyzkpY7UzRfDhbRdTjtNwNiUE=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
Expand Down Expand Up @@ -1065,15 +1067,16 @@ github.com/prometheus/client_golang v1.2.1/go.mod h1:XMU6Z2MjaRKVu/dC1qupJI9SiNk
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
github.com/prometheus/client_golang v1.12.1/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY=
github.com/prometheus/client_golang v1.13.0 h1:b71QUfeo5M8gq2+evJdTPfZhYMAU0uKPkyPJ7TPsloU=
github.com/prometheus/client_golang v1.13.0/go.mod h1:vTeo+zgvILHsnnj/39Ou/1fPN5nJFOEMgftOUOmlvYQ=
github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw=
github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y=
github.com/prometheus/client_model v0.0.0-20171117100541-99fa1f4be8e5/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4=
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
github.com/prometheus/common v0.0.0-20180110214958-89604d197083/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
Expand Down Expand Up @@ -1198,8 +1201,9 @@ github.com/stretchr/objx v0.0.0-20180129172003-8a3f7159479f/go.mod h1:HFkY916IF+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v0.0.0-20180303142811-b89eecf5ca5d/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand All @@ -1209,8 +1213,9 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/subosito/gotenv v1.4.1 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs=
github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
Expand Down
7 changes: 0 additions & 7 deletions pkg/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"flag"
"fmt"
"os"
"strconv"
"time"

attachnetclientset "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned"
Expand Down Expand Up @@ -92,7 +91,6 @@ type Configuration struct {

GCInterval int
InspectInterval int
Verbosity int
}

// ParseFlags parses cmd args then init kubeclient and conf
Expand Down Expand Up @@ -230,11 +228,6 @@ func ParseFlags() (*Configuration, error) {
return nil, fmt.Errorf("no host nic for vlan")
}

var err error
if config.Verbosity, err = strconv.Atoi(klogFlags.Lookup("v").Value.String()); err != nil {
return nil, fmt.Errorf("failed to parse log verbosity level: %v", err)
}

if config.DefaultGateway == "" {
gw, err := util.GetGwByCidr(config.DefaultCIDR)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func NewController(config *Configuration) *Controller {
}

var err error
if controller.ovnClient, err = ovs.NewOvnClient(config.OvnNbAddr, config.OvnTimeout, config.Verbosity); err != nil {
if controller.ovnClient, err = ovs.NewOvnClient(config.OvnNbAddr, config.OvnTimeout); err != nil {
klog.Fatal(err)
}

Expand Down
12 changes: 9 additions & 3 deletions pkg/ovs/ovn-nb-logical_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ import (
)

func (c OvnClient) GetLogicalRouter(name string, ignoreNotFound bool) (*ovnnb.LogicalRouter, error) {
predicate := func(model *ovnnb.LogicalRouter) bool {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()

api, err := c.ovnNbClient.WherePredict(ctx, func(model *ovnnb.LogicalRouter) bool {
return model.Name == name
})
if err != nil {
return nil, err
}
// Logical_Router has no indexes defined in the schema

var result []*ovnnb.LogicalRouter
if err := c.ovnNbClient.WhereCache(predicate).List(context.TODO(), &result); err != nil || len(result) == 0 {
if err = api.List(context.TODO(), &result); err != nil || len(result) == 0 {
if ignoreNotFound && (err == client.ErrNotFound || len(result) == 0) {
return nil, nil
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/ovs/ovn-nb-logical_router_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import (
)

func (c OvnClient) GetLogicalRouterPort(name string, ignoreNotFound bool) (*ovnnb.LogicalRouterPort, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()

lrp := &ovnnb.LogicalRouterPort{Name: name}
if err := c.ovnNbClient.Get(context.TODO(), lrp); err != nil {
if err := c.ovnNbClient.Get(ctx, lrp); err != nil {
if ignoreNotFound && err == client.ErrNotFound {
return nil, nil
}
Expand Down
32 changes: 22 additions & 10 deletions pkg/ovs/ovn-nb-logical_router_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,37 @@ import (
)

func (c OvnClient) GetLogicalRouterRouteByOpts(key, value string) ([]ovnnb.LogicalRouterStaticRoute, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()

api, err := c.ovnNbClient.WherePredict(ctx, func(r *ovnnb.LogicalRouterStaticRoute) bool {
return r.Options[key] == value
})
if err != nil {
return nil, err
}

var lrRouteList []ovnnb.LogicalRouterStaticRoute
err := c.ovnNbClient.WhereCache(
func(r *ovnnb.LogicalRouterStaticRoute) bool {
return r.Options[key] == value
}).List(context.TODO(), &lrRouteList)
if err != nil && err != client.ErrNotFound {
if err = api.List(context.TODO(), &lrRouteList); err != nil && err != client.ErrNotFound {
return nil, err
}

return lrRouteList, nil
}

func (c OvnClient) GetLogicalRouterPoliciesByExtID(key, value string) ([]ovnnb.LogicalRouterPolicy, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()

api, err := c.ovnNbClient.WherePredict(ctx, func(p *ovnnb.LogicalRouterPolicy) bool {
return p.ExternalIDs[key] == value
})
if err != nil {
return nil, err
}

var lrPolicyList []ovnnb.LogicalRouterPolicy
err := c.ovnNbClient.WhereCache(
func(p *ovnnb.LogicalRouterPolicy) bool {
return p.ExternalIDs[key] == value
}).List(context.TODO(), &lrPolicyList)
if err != nil && err != client.ErrNotFound {
if err = api.List(context.TODO(), &lrPolicyList); err != nil && err != client.ErrNotFound {
return nil, err
}

Expand Down
33 changes: 26 additions & 7 deletions pkg/ovs/ovn-nb-logical_switch_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import (
)

func (c OvnClient) GetLogicalSwitchPort(name string, ignoreNotFound bool) (*ovnnb.LogicalSwitchPort, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()

lsp := &ovnnb.LogicalSwitchPort{Name: name}
if err := c.ovnNbClient.Get(context.TODO(), lsp); err != nil {
if err := c.ovnNbClient.Get(ctx, lsp); err != nil {
if ignoreNotFound && err == client.ErrNotFound {
return nil, nil
}
Expand All @@ -24,19 +27,29 @@ func (c OvnClient) GetLogicalSwitchPort(name string, ignoreNotFound bool) (*ovnn
}

func (c OvnClient) ListPodLogicalSwitchPorts(key string) ([]ovnnb.LogicalSwitchPort, error) {
lspList := make([]ovnnb.LogicalSwitchPort, 0, 1)
if err := c.ovnNbClient.WhereCache(func(lsp *ovnnb.LogicalSwitchPort) bool {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()

api, err := c.ovnNbClient.WherePredict(ctx, func(lsp *ovnnb.LogicalSwitchPort) bool {
return len(lsp.ExternalIDs) != 0 && lsp.ExternalIDs["pod"] == key
}).List(context.TODO(), &lspList); err != nil {
})
if err != nil {
return nil, err
}

var lspList []ovnnb.LogicalSwitchPort
if err = api.List(context.TODO(), &lspList); err != nil {
return nil, fmt.Errorf("failed to list logical switch ports of Pod %s: %v", key, err)
}

return lspList, nil
}

func (c OvnClient) ListLogicalSwitchPorts(needVendorFilter bool, externalIDs map[string]string) ([]ovnnb.LogicalSwitchPort, error) {
lspList := make([]ovnnb.LogicalSwitchPort, 0)
if err := c.ovnNbClient.WhereCache(func(lsp *ovnnb.LogicalSwitchPort) bool {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()

api, err := c.ovnNbClient.WherePredict(ctx, func(lsp *ovnnb.LogicalSwitchPort) bool {
if lsp.Type != "" {
return false
}
Expand All @@ -54,7 +67,13 @@ func (c OvnClient) ListLogicalSwitchPorts(needVendorFilter bool, externalIDs map
}
}
return true
}).List(context.TODO(), &lspList); err != nil {
})
if err != nil {
return nil, err
}

var lspList []ovnnb.LogicalSwitchPort
if err = api.List(context.TODO(), &lspList); err != nil {
klog.Errorf("failed to list logical switch ports: %v", err)
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/ovs/ovn-nb-port_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import (
)

func (c OvnClient) GetPortGroup(name string, ignoreNotFound bool) (*ovnnb.PortGroup, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()

pg := &ovnnb.PortGroup{Name: name}
if err := c.ovnNbClient.Get(context.TODO(), pg); err != nil {
if err := c.ovnNbClient.Get(ctx, pg); err != nil {
if ignoreNotFound && err == client.ErrNotFound {
return nil, nil
}
Expand Down
18 changes: 12 additions & 6 deletions pkg/ovs/ovn.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type OvnClient struct {

type ovnNbClient struct {
client.Client
Timeout int
Timeout time.Duration
}

const (
Expand Down Expand Up @@ -76,18 +76,24 @@ func NewLegacyClient(ovnNbAddr string, ovnNbTimeout int, ovnSbAddr, clusterRoute
}

// TODO: support sb/ic-nb client
func NewOvnClient(ovnNbAddr string, ovnNbTimeout, verbosity int) (*OvnClient, error) {
nbClient, err := ovsclient.NewNbClient(ovnNbAddr, ovnNbTimeout, verbosity)
func NewOvnClient(ovnNbAddr string, ovnNbTimeout int) (*OvnClient, error) {
nbClient, err := ovsclient.NewNbClient(ovnNbAddr)
if err != nil {
klog.Errorf("failed to create OVN NB client: %v", err)
return nil, err
}

return &OvnClient{ovnNbClient: ovnNbClient{Client: nbClient, Timeout: ovnNbTimeout}}, nil
c := &OvnClient{
ovnNbClient: ovnNbClient{
Client: nbClient,
Timeout: time.Duration(ovnNbTimeout) * time.Second,
},
}
return c, nil
}

func Transact(c client.Client, method string, operations []ovsdb.Operation, timeout int) error {
ctx, cancel := context.WithTimeout(context.TODO(), time.Duration(timeout)*time.Second)
func Transact(c client.Client, method string, operations []ovsdb.Operation, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
defer cancel()

start := time.Now()
Expand Down
Loading

0 comments on commit 7344578

Please sign in to comment.