Skip to content

Commit

Permalink
feat: auto assign gw for controller config and expose more cmd args
Browse files Browse the repository at this point in the history
  • Loading branch information
oilbeater committed Jun 4, 2019
1 parent ad64769 commit 5509040
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 18 deletions.
21 changes: 20 additions & 1 deletion docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,28 @@ For high-available ovn db, see [high available](high-available.md)
### Controller Configuration

```bash
# Default Logical Switch
--default-ls: The default logical switch name, default: ovn-default
--default-cidr: Default CIDR for Namespaces with no logical switch annotation, default: 10.16.0.0/16
--default-gateway: The gateway address for default cidr, default: 10.16.0.1
--default-gateway: Default gateway for default-cidr, default the first ip in default-cidr
--node-switch-cidr: The CIDR for the Node switch, default: 100.64.0.0/16
--default-exclude-ips: Exclude ips in default switch, default equals to gateway address

# Node Switch
--node-switch: The name of node gateway switch which help node to access pod network, default: join
--node-switch-cidr: The cidr for node switch, default: 100.64.0.0/16
--node-switch-gateway: The gateway for node switch, default the first ip in node-switch-cidr

# LoadBalancer
--cluster-tcp-loadbalancer: The name for cluster tcp loadbalancer, default cluster-tcp-loadbalancer
--cluster-udp-loadbalancer: The name for cluster udp loadbalancer, default cluster-udp-loadbalancer

# Router
--cluster-router: The router name for cluster router, default: ovn-cluster

# Misc
--worker-num: The parallelism of each worker, default: 3
--kubeconfig: Path to kubeconfig file with authorization and master location information. If not set use the inCluster token
```
## To uninstall
Expand Down
34 changes: 26 additions & 8 deletions pkg/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"flag"
"github.com/alauda/kube-ovn/pkg/util"
"os"

"github.com/spf13/pflag"
Expand Down Expand Up @@ -50,14 +51,14 @@ func ParseFlags() (*Configuration, error) {
argKubeConfigFile = pflag.String("kubeconfig", "", "Path to kubeconfig file with authorization and master location information. If not set use the inCluster token.")

argDefaultLogicalSwitch = pflag.String("default-ls", "ovn-default", "The default logical switch name, default: ovn-default")
argDefaultCIDR = pflag.String("default-cidr", "10.16.0.0/16", "Default cidr for namespace with no logical switch annotation, default: 10.16.0.0/16")
argDefaultGateway = pflag.String("default-gateway", "10.16.0.1", "Default gateway for default subnet, default: 10.16.0.1")
argDefaultCIDR = pflag.String("default-cidr", "10.16.0.0/16", "Default CIDR for namespace with no logical switch annotation, default: 10.16.0.0/16")
argDefaultGateway = pflag.String("default-gateway", "", "Default gateway for default-cidr, default the first ip in default-cidr")
argDefaultExcludeIps = pflag.String("default-exclude-ips", "", "Exclude ips in default switch, default equals to gateway address")

argClusterRouter = pflag.String("cluster-router", "ovn-cluster", "The router name for cluster router, default: cluster-router")
argClusterRouter = pflag.String("cluster-router", "ovn-cluster", "The router name for cluster router, default: ovn-cluster")
argNodeSwitch = pflag.String("node-switch", "join", "The name of node gateway switch which help node to access pod network, default: join")
argNodeSwitchCIDR = pflag.String("node-switch-cidr", "100.64.0.0/16", "The cidr for node switch, default: 100.64.0.0/16")
argNodeSwitchGateway = pflag.String("node-switch-gateway", "100.64.0.1", "The gateway for node switch, default: 100.64.0.1")
argNodeSwitchGateway = pflag.String("node-switch-gateway", "", "The gateway for node switch, default the first ip in node-switch-cidr")

argClusterTcpLoadBalancer = pflag.String("cluster-tcp-loadbalancer", "cluster-tcp-loadbalancer", "The name for cluster tcp loadbalancer")
argClusterUdpLoadBalancer = pflag.String("cluster-udp-loadbalancer", "cluster-udp-loadbalancer", "The name for cluster udp loadbalancer")
Expand All @@ -84,10 +85,6 @@ func ParseFlags() (*Configuration, error) {
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()

if *argDefaultExcludeIps == "" {
argDefaultExcludeIps = argDefaultGateway
}

config := &Configuration{
OvnNbSocket: *argOvnNbSocket,
OvnNbHost: *argOvnNbHost,
Expand All @@ -108,6 +105,27 @@ func ParseFlags() (*Configuration, error) {
PodName: os.Getenv("POD_NAME"),
PodNamespace: os.Getenv("KUBE_NAMESPACE"),
}

if config.DefaultGateway == "" {
gw, err := util.FirstSubnetIP(config.DefaultCIDR)
if err != nil {
return nil, err
}
config.DefaultGateway = gw
}

if config.DefaultExcludeIps == "" {
config.DefaultExcludeIps = config.DefaultGateway
}

if config.NodeSwitchGateway == "" {
gw, err := util.FirstSubnetIP(config.NodeSwitchCIDR)
if err != nil {
return nil, err
}
config.NodeSwitchGateway = gw
}

err := config.initKubeClient()
if err != nil {
return nil, err
Expand Down
21 changes: 21 additions & 0 deletions pkg/util/net.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package util

import (
"bytes"
"encoding/binary"
"fmt"
"math/rand"
"net"
"time"
)

Expand All @@ -13,3 +16,21 @@ func GenerateMac() string {
mac := fmt.Sprintf("%s:%02X:%02X:%02X", prefix, newRand.Intn(255), newRand.Intn(255), newRand.Intn(255))
return mac
}

func Ip2Long(ip string) uint32 {
var long uint32
binary.Read(bytes.NewBuffer(net.ParseIP(ip).To4()), binary.BigEndian, &long)
return long
}

func Long2Ip(ip uint32) string {
return fmt.Sprintf("%d.%d.%d.%d", ip>>24, ip<<8>>24, ip<<16>>24, ip<<24>>24)
}

func FirstSubnetIP(subnet string) (string, error) {
_, cidr, err := net.ParseCIDR(subnet)
if err != nil {
return "", fmt.Errorf("%s is not a valid cidr", subnet)
}
return Long2Ip(Ip2Long(cidr.IP.String()) + 1), nil
}
11 changes: 2 additions & 9 deletions pkg/util/validator.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package util

import (
"bytes"
"encoding/binary"
"fmt"
"net"
"strconv"
Expand All @@ -27,6 +25,7 @@ func ValidateLogicalSwitch(annotations map[string]string) error {
if gateway == nil {
return fmt.Errorf("%s is not a valid gateway", gatewayStr)
}

if !cidr.Contains(gateway) {
return fmt.Errorf("gateway address %s not in cidr range", gatewayStr)
}
Expand All @@ -52,7 +51,7 @@ func ValidateLogicalSwitch(annotations map[string]string) error {
return fmt.Errorf("ip %s in exclude_ips is not a valid address", ip)
}
}
if ip2Long(ips[0]) >= ip2Long(ips[1]) {
if Ip2Long(ips[0]) >= Ip2Long(ips[1]) {
return fmt.Errorf("%s in %s is not a valid ip range", ipr, ExcludeIpsAnnotation)
}
}
Expand Down Expand Up @@ -128,9 +127,3 @@ func ValidatePodNetwork(annotations map[string]string) error {

return nil
}

func ip2Long(ip string) uint32 {
var long uint32
binary.Read(bytes.NewBuffer(net.ParseIP(ip).To4()), binary.BigEndian, &long)
return long
}

0 comments on commit 5509040

Please sign in to comment.