Skip to content

Commit

Permalink
Support CNI VESION command (#1596)
Browse files Browse the repository at this point in the history
  • Loading branch information
lrh3321 committed Jun 9, 2022
1 parent 34dc29d commit 4053d46
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
25 changes: 15 additions & 10 deletions cmd/cni/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cni

import (
"encoding/json"
"errors"
"fmt"
"net"
"runtime"
Expand All @@ -16,6 +15,7 @@ import (
kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/kubeovn/kube-ovn/pkg/request"
"github.com/kubeovn/kube-ovn/pkg/util"
"github.com/kubeovn/kube-ovn/versions"
)

func CmdMain() {
Expand All @@ -24,7 +24,8 @@ func CmdMain() {
// must ensure that the goroutine does not jump from OS thread to thread
runtime.LockOSThread()

skel.PluginMain(cmdAdd, nil, cmdDel, version.All, "")
about := fmt.Sprintf("CNI kube-ovn plugin %s", versions.VERSION)
skel.PluginMain(cmdAdd, nil, cmdDel, version.All, about)
}

func cmdAdd(args *skel.CmdArgs) error {
Expand Down Expand Up @@ -61,7 +62,7 @@ func cmdAdd(args *skel.CmdArgs) error {
VhostUserSocketName: netConf.VhostUserSocketName,
})
if err != nil {
return err
return types.NewError(types.ErrTryAgainLater, "RPC failed", err.Error())
}

result := generateCNIResult(response)
Expand Down Expand Up @@ -142,7 +143,7 @@ func cmdDel(args *skel.CmdArgs) error {
netConf.Provider = util.OvnProvider
}

return client.Del(request.CniRequest{
err = client.Del(request.CniRequest{
CniType: netConf.Type,
PodName: podName,
PodNamespace: podNamespace,
Expand All @@ -153,6 +154,10 @@ func cmdDel(args *skel.CmdArgs) error {
DeviceID: netConf.DeviceID,
VhostUserSocketVolumeName: netConf.VhostUserSocketVolumeName,
})
if err != nil {
return types.NewError(types.ErrTryAgainLater, "RPC failed", err.Error())
}
return nil
}

type ipamConf struct {
Expand All @@ -163,7 +168,7 @@ type ipamConf struct {
func loadNetConf(bytes []byte) (*netConf, string, error) {
n := &netConf{}
if err := json.Unmarshal(bytes, n); err != nil {
return nil, "", fmt.Errorf("failed to load netconf: %v", err)
return nil, "", types.NewError(types.ErrDecodingFailure, "failed to load netconf", err.Error())
}

if n.Type != util.CniTypeName && n.IPAM != nil {
Expand All @@ -172,7 +177,7 @@ func loadNetConf(bytes []byte) (*netConf, string, error) {
}

if n.ServerSocket == "" {
return nil, "", fmt.Errorf("server_socket is required in cni.conf, %+v", n)
return nil, "", types.NewError(types.ErrInvalidNetworkConfig, "Invalid Configuration", fmt.Sprintf("server_socket is required in cni.conf, %+v", n))
}

if n.Provider == "" {
Expand All @@ -185,7 +190,7 @@ func loadNetConf(bytes []byte) (*netConf, string, error) {

func parseValueFromArgs(key, argString string) (string, error) {
if argString == "" {
return "", errors.New("CNI_ARGS is required")
return "", types.NewError(types.ErrInvalidNetworkConfig, "Invalid Configuration", "CNI_ARGS is required")
}
args := strings.Split(argString, ";")
for _, arg := range args {
Expand All @@ -196,7 +201,7 @@ func parseValueFromArgs(key, argString string) (string, error) {
}
}
}
return "", fmt.Errorf("%s is required in CNI_ARGS", key)
return "", types.NewError(types.ErrInvalidNetworkConfig, "Invalid Configuration", fmt.Sprintf("%s is required in CNI_ARGS", key))
}

func assignV4Address(ipAddress, gateway string, mask *net.IPNet) (*current.IPConfig, *types.Route) {
Expand All @@ -208,7 +213,7 @@ func assignV4Address(ipAddress, gateway string, mask *net.IPNet) (*current.IPCon
var route *types.Route
if gw := net.ParseIP(gateway); gw != nil {
route = &types.Route{
Dst: net.IPNet{IP: net.ParseIP("0.0.0.0").To4(), Mask: net.CIDRMask(0, 32)},
Dst: net.IPNet{IP: net.IPv4zero.To4(), Mask: net.CIDRMask(0, 32)},
GW: net.ParseIP(gateway).To4(),
}
}
Expand All @@ -225,7 +230,7 @@ func assignV6Address(ipAddress, gateway string, mask *net.IPNet) (*current.IPCon
var route *types.Route
if gw := net.ParseIP(gateway); gw != nil {
route = &types.Route{
Dst: net.IPNet{IP: net.ParseIP("::").To16(), Mask: net.CIDRMask(0, 128)},
Dst: net.IPNet{IP: net.IPv6zero, Mask: net.CIDRMask(0, 128)},
GW: net.ParseIP(gateway).To16(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/bhendo/go-powershell v0.0.0-20190719160123-219e7fb4e41e
github.com/cenkalti/backoff/v4 v4.1.3
github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08
github.com/containernetworking/cni v1.0.1
github.com/containernetworking/cni v1.1.1
github.com/containernetworking/plugins v1.1.1
github.com/coreos/go-iptables v0.6.0
github.com/emicklei/go-restful/v3 v3.7.4
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,9 @@ github.com/containerd/zfs v1.0.0/go.mod h1:m+m51S1DvAP6r3FcmYCp54bQ34pyOwTieQDNR
github.com/containernetworking/cni v0.7.1/go.mod h1:LGwApLUm2FpoOfxTDEeq8T9ipbpZ61X79hmU3w8FmsY=
github.com/containernetworking/cni v0.8.0/go.mod h1:LGwApLUm2FpoOfxTDEeq8T9ipbpZ61X79hmU3w8FmsY=
github.com/containernetworking/cni v0.8.1/go.mod h1:LGwApLUm2FpoOfxTDEeq8T9ipbpZ61X79hmU3w8FmsY=
github.com/containernetworking/cni v1.0.1 h1:9OIL/sZmMYDBe+G8svzILAlulUpaDTUjeAbtH/JNLBo=
github.com/containernetworking/cni v1.0.1/go.mod h1:AKuhXbN5EzmD4yTNtfSsX3tPcmtrBI6QcRV0NiNt15Y=
github.com/containernetworking/cni v1.1.1 h1:ky20T7c0MvKvbMOwS/FrlbNwjEoqJEUUYfsL4b0mc4k=
github.com/containernetworking/cni v1.1.1/go.mod h1:sDpYKmGVENF3s6uvMvGgldDWeG8dMxakj/u+i9ht9vw=
github.com/containernetworking/plugins v0.8.6/go.mod h1:qnw5mN19D8fIwkqW7oHHYDHVlzhJpcY6TQxn/fUyDDM=
github.com/containernetworking/plugins v0.9.1/go.mod h1:xP/idU2ldlzN6m4p5LmGiwRDjeJr6FLK6vuiUwoH7P8=
github.com/containernetworking/plugins v1.1.0/go.mod h1:Sr5TH/eBsGLXK/h71HeLfX19sZPp3ry5uHSkI4LPxV8=
Expand Down Expand Up @@ -1021,8 +1022,9 @@ github.com/onsi/ginkgo v1.14.1/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9k
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.0.0 h1:CcuG/HvWNkkaqCUpJifQY8z7qEMBJya6aLPx6ftGyjQ=
github.com/onsi/ginkgo/v2 v2.0.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.1.3 h1:e/3Cwtogj0HA+25nMP1jCMDIf8RtRYbGwGGuBIFztkc=
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/gomega v0.0.0-20151007035656-2152b45fa28a/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
Expand Down

0 comments on commit 4053d46

Please sign in to comment.