Skip to content

Commit

Permalink
Merge pull request #19 from nerdalert/brent/unused-code-removal
Browse files Browse the repository at this point in the history
Removed some unused code #5 and added demo/test script
  • Loading branch information
nerdalert committed Jul 20, 2015
2 parents 82f4932 + 46787bb commit e105aae
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 115 deletions.
Binary file modified binaries/docker-ovs-plugin-0.1-Linux-x86_64
Binary file not shown.
26 changes: 0 additions & 26 deletions plugin/ovs/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,6 @@ const (
containerEthName = "eth"
)

var bridgeNetworks []*net.IPNet

func init() {
// Here we don't follow the convention of using the 1st IP of the range for the gateway.
// This is to use the same gateway IPs as the /24 ranges, which predate the /16 ranges.
// In theory this shouldn't matter - in practice there's bound to be a few scripts relying
// on the internal addressing or other stupid things like that.
// They shouldn't, but hey, let's not break them unless we really have to.
// Don't use 172.16.0.0/16, it conflicts with EC2 DNS 172.16.0.23

// 172.[17-31].42.1/16
mask := []byte{255, 255, 0, 0}
for i := 17; i < 32; i++ {
bridgeNetworks = append(bridgeNetworks, &net.IPNet{IP: []byte{172, byte(i), 42, 1}, Mask: mask})
}
// 10.[0-255].42.1/16
for i := 0; i < 256; i++ {
bridgeNetworks = append(bridgeNetworks, &net.IPNet{IP: []byte{10, byte(i), 42, 1}, Mask: mask})
}
// 192.168.[42-44].1/24
mask[2] = 255
for i := 42; i < 45; i++ {
bridgeNetworks = append(bridgeNetworks, &net.IPNet{IP: []byte{192, 168, byte(i), 1}, Mask: mask})
}
}

type Driver interface {
Listen(string) error
}
Expand Down
90 changes: 1 addition & 89 deletions plugin/ovs/utils.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
package ovs

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net"
"regexp"
"time"

log "github.com/Sirupsen/logrus"
"github.com/docker/libnetwork/netutils"
"github.com/vishvananda/netlink"
)

// utils
const (
ipv4NumBlock = `(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)`
ipv4Address = `(` + ipv4NumBlock + `\.){3}` + ipv4NumBlock

// This is not an IPv6 address verifier as it will accept a super-set of IPv6, and also
// will *not match* IPv4-Embedded IPv6 Addresses (RFC6052), but that and other variants
// -- e.g. other link-local types -- either won't work in containers or are unnecessary.
// For readability and sufficiency for Docker purposes this seemed more reasonable than a
// 1000+ character regexp with exact and complete IPv6 validation
ipv6Address = `([0-9A-Fa-f]{0,4}:){2,7}([0-9A-Fa-f]{0,4})`
)

var nsRegexp = regexp.MustCompile(`^\s*nameserver\s*((` + ipv4Address + `)|(` + ipv6Address + `))\s*$`)

// Generate a mac addr
func makeMac(ip net.IP) string {
hw := make(net.HardwareAddr, 6)
hw[0] = 0x7a
Expand All @@ -37,75 +18,6 @@ func makeMac(ip net.IP) string {
return hw.String()
}

// getNameserversAsCIDR returns nameservers (if any) listed in
// /etc/resolv.conf as CIDR blocks (e.g., "1.2.3.4/32")
// This function's output is intended for net.ParseCIDR
func getNameserversAsCIDR(resolvConf []byte) []string {
nameservers := []string{}
for _, nameserver := range getNameservers(resolvConf) {
nameservers = append(nameservers, nameserver+"/32")
}
return nameservers
}

func findBridgeCIDR() (*net.IPNet, error) {

// We don't check for an error here, because we don't really care if we
// can't read /etc/resolv.conf. So instead we skip the append if resolvConf
// is nil. It either doesn't exist, or we can't read it for some reason.
nameservers := []string{}
if resolvConf, _ := readResolvConf(); resolvConf != nil {
nameservers = append(nameservers, getNameserversAsCIDR(resolvConf)...)
}

// Try to automatically elect appropriate bridge IPv4 settings.
for _, n := range bridgeNetworks {
if err := netutils.CheckNameserverOverlaps(nameservers, n); err == nil {
if err := netutils.CheckRouteOverlaps(n); err == nil {
return n, nil
}
}
}

return nil, errors.New("cannot find available address for bridge")

}

// GetNameservers returns nameservers (if any) listed in /etc/resolv.conf
func getNameservers(resolvConf []byte) []string {
nameservers := []string{}
for _, line := range getLines(resolvConf, []byte("#")) {
var ns = nsRegexp.FindSubmatch(line)
if len(ns) > 0 {
nameservers = append(nameservers, string(ns[1]))
}
}
return nameservers
}

// getLines parses input into lines and strips away comments.
func getLines(input []byte, commentMarker []byte) [][]byte {
lines := bytes.Split(input, []byte("\n"))
var output [][]byte
for _, currentLine := range lines {
var commentIndex = bytes.Index(currentLine, commentMarker)
if commentIndex == -1 {
output = append(output, currentLine)
} else {
output = append(output, currentLine[:commentIndex])
}
}
return output
}

func readResolvConf() ([]byte, error) {
resolv, err := ioutil.ReadFile("/etc/resolv.conf")
if err != nil {
return nil, err
}
return resolv, nil
}

// Return the IPv4 address of a network interface
func getIfaceAddr(name string) (*net.IPNet, error) {
iface, err := netlink.LinkByName(name)
Expand Down
34 changes: 34 additions & 0 deletions scripts/release-the-whales.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

# This can be used for manually testing load on the plugin until we get CI setup.
# Can also be useful for demoing. Both Docker and the network plugin need to be running.
# Two options, create specified number of containers and then delete them all:
# 1) Launch a specified number of busybox containers by running --> ./release-the-whales.sh 25
# 2) Delete all busybox containers by running --> ./release-the-whales.sh clean

image_name="busybox"

delcon() {
# if no containers matching $image_name exist you will get an error of:
# docker: "rm" requires a minimum of 1 argument.
echo "Deleting all Busybox containers..."
docker rm -f $(docker ps -a | grep ${image_name} | awk '{print $1}')
}

if [ "$1" -eq "$1" ] 2>/dev/null; then
echo "Release the whales!"
for i in `seq 1 $1`;
do
echo "Launching container #$i"
docker run -itd --name=container-${i} busybox
done
fi

if [[ $1 = "clean" ]]; then
delcon
elif [[ $1 = "" ]]; then
echo "Supports 2 options:"
echo "==================="
echo "1) Launch (n) of busybox containers. Example:[ release-the-whales.sh 20 ]"
echo "2) Delete all containers matching [ $image_name ] Example:[ release-the-whales.sh clean ]"
fi

0 comments on commit e105aae

Please sign in to comment.