Skip to content

Commit

Permalink
add delete network action
Browse files Browse the repository at this point in the history
  • Loading branch information
swapwz committed Dec 5, 2017
1 parent d4ce146 commit 106a874
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 7 deletions.
Binary file removed example/bridge
Binary file not shown.
11 changes: 11 additions & 0 deletions example/bridge/bridge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"fmt"
"github.com/union-cni/pkg/bridge"
)

func main() {
br, err := bridge.CreateBridge("simple")
fmt.Printf("create simple bridge: %v %v\r\n", br, err)
}
18 changes: 18 additions & 0 deletions example/pod/pod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"fmt"
"os"

"github.com/union-cni/pkg/client"
)

func main() {
cli := client.CreateInsecureClient("127.0.0.1", "8080")
pod, err := cli.GetPod(os.Args[1], os.Args[2])
if err != nil {
fmt.Printf("no such pod")
return
}
fmt.Printf("Annotation %q, err %v\r\n", pod.Annotations, err)
}
26 changes: 26 additions & 0 deletions pkg/veth/veth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"net"
"errors"
"crypto/rand"

"github.com/containernetworking/cni/pkg/types/current"
Expand All @@ -14,6 +15,10 @@ import (
const defaultMTU = 1400
const defaultPrefix = "sim"

var (
ErrLinkNotFound = errors.New("link not found")
)

func makeVethPair(name string, peer string, mtu int) (netlink.Link, error) {
veth := &netlink.Veth {
LinkAttrs: netlink.LinkAttrs {
Expand Down Expand Up @@ -91,6 +96,27 @@ func CreateVethPair(name string, peer string) (netlink.Link, netlink.Link, error
return hostLink, peerLink, nil
}

func DelLink(name string, nspath string) error {
netns, err := ns.GetNS(nspath)
if err != nil {
return err
}
defer netns.Close()
link, err := netlink.LinkByName(name)
if err != nil {
if err.Error() == "Link not found" {
return ErrLinkNotFound
}
return fmt.Errorf("failed to lookup %q: %v", name, err)
}

if err = netlink.LinkDel(link); err != nil {
return fmt.Errorf("failed to delete %q: %v", name, err)
}

return nil
}

// Current namespace is the default namespace
// If namespace == "", means default namespace
func JoinNetNS(name string, nspath string) error {
Expand Down
48 changes: 41 additions & 7 deletions unicni.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"runtime"
"net"
"os"
"errors"
"encoding/json"

"github.com/union-cni/pkg/bridge"
Expand All @@ -28,10 +27,6 @@ const (
dataPortAnnotation = "data_port"
)

var (
ErrLinkNotFound = errors.New("link not found")
)

type NetworkInfo struct {
Crediential string `json:"credential"`
Group string `json:"group"`
Expand Down Expand Up @@ -59,7 +54,7 @@ func init() {
runtime.LockOSThread()
}

func setupNetwork(net *netinfo.NetworkInfo, netns string) (*current.Result, error) {
func createNetwork(net *netinfo.NetworkInfo, netns string) (*current.Result, error) {
// the length of bridge name must be less than 15 characters.
ctrlBrName := net.GetCtrlBridgeName()
ctrlBr, err := bridge.CreateBridge(ctrlBrName)
Expand Down Expand Up @@ -132,6 +127,23 @@ func setupNetwork(net *netinfo.NetworkInfo, netns string) (*current.Result, erro
return result, nil
}

func deleteNetwork(netInfo *netinfo.NetworkInfo, nspath string) error {
ctrlPortName := netInfo.GetCtrlPort()
err := veth.DelLink(ctrlPortName, nspath)
if err != nil && err != veth.ErrLinkNotFound {
fmt.Fprintf(os.Stderr, "[UNION CNI] failed to delete link %s: %v\r\n", ctrlPortName, err)
return err
}

dataPortName := netInfo.GetDataPort()
err = veth.DelLink(dataPortName, nspath)
if err != nil && err != veth.ErrLinkNotFound {
fmt.Fprintf(os.Stderr, "[UNION CNI] failed to delete link %s: %v\r\n", ctrlPortName, err)
return err
}
return nil
}

func cmdAdd(args *skel.CmdArgs) error {
conf := types.NetConf{}
err := json.Unmarshal(args.StdinData, &conf)
Expand All @@ -155,7 +167,7 @@ func cmdAdd(args *skel.CmdArgs) error {
if err != nil {
return err
}
result, err = setupNetwork(netInfo, args.Netns)
result, err = createNetwork(netInfo, args.Netns)
result.Interfaces = []*current.Interface{}
}
return types.PrintResult(result, conf.CNIVersion)
Expand All @@ -164,6 +176,28 @@ func cmdAdd(args *skel.CmdArgs) error {
func cmdDel(args *skel.CmdArgs) error {
// Delete all port related current user's pod
fmt.Fprintf(os.Stderr, "[UNION CNI] action delete.\r\n")
conf := types.NetConf{}
err := json.Unmarshal(args.StdinData, &conf)
if err != nil {
fmt.Fprintf(os.Stderr, "[UNION CNI] failed to load netconf: %v", err)
return err
}

k8sArgs := K8SArgs{}
err = types.LoadArgs(args.Args, &k8sArgs)
if err != nil {
fmt.Fprintf(os.Stderr, "[UNION CNI]: Failed to load args %q: %v\r\n", args.Args, err)
return err
}

// Get annotaions, parse data and control bridge name, and delete all
fmt.Fprintf(os.Stderr, "[UNION CNI] k8s namespace: %s, pod name: %s\r\n", k8sArgs.K8S_POD_NAMESPACE, k8sArgs.K8S_POD_NAME)
if len(k8sArgs.K8S_POD_NAME) != 0 || len(k8sArgs.K8S_POD_NAMESPACE) != 0 {
netInfo, err := netinfo.GetNetInfo(defaultHost, defaultPort, string(k8sArgs.K8S_POD_NAMESPACE), string(k8sArgs.K8S_POD_NAME))
if err == nil {
deleteNetwork(netInfo, args.Netns)
}
}
return nil
}

Expand Down

0 comments on commit 106a874

Please sign in to comment.