Skip to content

Commit

Permalink
Add windows host-gw
Browse files Browse the repository at this point in the history
    - patch for flannel-io#921
  • Loading branch information
thxCode committed Sep 11, 2018
1 parent bda9bea commit 940745c
Show file tree
Hide file tree
Showing 5 changed files with 1,037 additions and 5 deletions.
1 change: 0 additions & 1 deletion backend/hostgw/hostgw.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !windows

package hostgw

Expand Down
221 changes: 217 additions & 4 deletions backend/hostgw/hostgw_windows.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build windows

// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,14 +11,229 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build windows

package hostgw

import (
"fmt"
"strconv"
"sync"
"time"

"github.com/Microsoft/hcsshim"
"github.com/coreos/flannel/backend"
"github.com/coreos/flannel/network/netroute"
"github.com/coreos/flannel/network/netsh"
"github.com/coreos/flannel/pkg/ip"
"github.com/coreos/flannel/subnet"
log "github.com/golang/glog"
"github.com/juju/errors"
"golang.org/x/net/context"
"k8s.io/apimachinery/pkg/util/json"
utilexec "k8s.io/utils/exec"
)

func init() {
log.Infof("hostgw is not supported on this platform")
backend.Register("host-gw", New)
}

type HostgwBackend struct {
sm subnet.Manager
extIface *backend.ExternalInterface
}

func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
if !extIface.ExtAddr.Equal(extIface.IfaceAddr) {
return nil, fmt.Errorf("your PublicIP differs from interface IP, meaning that probably you're on a NAT, which is not supported by host-gw backend")
}

be := &HostgwBackend{
sm: sm,
extIface: extIface,
}

return be, nil
}

func (be *HostgwBackend) RegisterNetwork(ctx context.Context, wg sync.WaitGroup, config *subnet.Config) (backend.Network, error) {
// 1. Parse configuration
cfg := struct {
Name string
DNSServerList string
}{}
if len(config.Backend) > 0 {
if err := json.Unmarshal(config.Backend, &cfg); err != nil {
return nil, errors.Annotate(err, "error decoding windows host-gw backend config")
}
}
if len(cfg.Name) == 0 {
cfg.Name = "cbr0"
}
log.Infof("HOST-GW config: Name=%s DNSServerList=%s", cfg.Name, cfg.DNSServerList)

n := &backend.RouteNetwork{
SimpleNetwork: backend.SimpleNetwork{
ExtIface: be.extIface,
},
SM: be.sm,
BackendType: "host-gw",
Mtu: be.extIface.Iface.MTU,
LinkIndex: be.extIface.Iface.Index,
}
n.GetRoute = func(lease *subnet.Lease) *netroute.Route {
return &netroute.Route{
Dst: lease.Subnet.ToIPNet(),
Gw: lease.Attrs.PublicIP.ToIP(),
LinkIndex: n.LinkIndex,
}
}

// 2. Acquire the lease form subnet manager
attrs := subnet.LeaseAttrs{
PublicIP: ip.FromIP(be.extIface.ExtAddr),
BackendType: "host-gw",
}

l, err := be.sm.AcquireLease(ctx, &attrs)
switch err {
case nil:
n.SubnetLease = l

case context.Canceled, context.DeadlineExceeded:
return nil, err

default:
return nil, errors.Annotate(err, "failed to acquire lease")
}

// 3. Check if the network exists and has the expected settings
createNetwork := true
netshHelper := netsh.NewHelper(utilexec.New())

addressPrefix := n.SubnetLease.Subnet.String()
networkGatewayAddress := n.SubnetLease.Subnet.IP + 1
podGatewayAddress := n.SubnetLease.Subnet.IP + 2
hnsNetworkName := cfg.Name

hnsNetwork, err := hcsshim.GetHNSNetworkByName(hnsNetworkName)
if err == nil && hnsNetwork.DNSServerList == cfg.DNSServerList {
for _, subnet := range hnsNetwork.Subnets {
if subnet.AddressPrefix == addressPrefix && subnet.GatewayAddress == networkGatewayAddress.String() {
createNetwork = false
log.Infof("Found existing HNSNetwork %s", hnsNetworkName)
break
}
}
}

// 4. Create a new HNSNetwork
if createNetwork {
if hnsNetwork != nil {
if _, err := hnsNetwork.Delete(); err != nil {
return nil, errors.Annotatef(err, "failed to delete existing HNSNetwork %s", hnsNetworkName)
}
log.Infof("Deleted stale HNSNetwork %s", hnsNetworkName)
}

request := map[string]interface{}{
"Name": hnsNetworkName,
"Type": "l2bridge",
"Subnets": []interface{}{
map[string]interface{}{
"AddressPrefix": addressPrefix,
"GatewayAddress": networkGatewayAddress,
},
},
"DNSServerList": cfg.DNSServerList,
}
jsonRequest, err := json.Marshal(request)
if err != nil {
return nil, errors.Annotatef(err, "failed to marshal %+v", request)
}

log.Infof("Attempting to create HNSNetwork %s", string(jsonRequest))
newHnsNetwork, err := hcsshim.HNSNetworkRequest("POST", "", string(jsonRequest))
if err != nil {
return nil, errors.Annotatef(err, "failed to create HNSNetwork %s", hnsNetworkName)
}

// Wait for the network to populate Management IP
for len(newHnsNetwork.ManagementIP) == 0 {
time.Sleep(1 * time.Second)
newHnsNetwork, _ = hcsshim.HNSNetworkRequest("GET", newHnsNetwork.Id, "")
}

// Wait for the interface with the management IP
for {
if _, err = netshHelper.GetInterfaceByIP(newHnsNetwork.ManagementIP); err != nil {
time.Sleep(1 * time.Second)
continue
}
break
}

log.Infof("Created HNSNetwork %s", hnsNetworkName)
hnsNetwork = newHnsNetwork
}

// 5. Ensure a 1.2 endpoint on this network in the host compartment
bridgeHNSEndpointName := hnsNetworkName + "_ep"
createBridgeHNSEndpoint := true
hnsEndpoint, err := hcsshim.GetHNSEndpointByName(bridgeHNSEndpointName)
if err == nil && hnsEndpoint.IPAddress.String() == podGatewayAddress.String() {
log.Infof("Found existing bridge HNSEndpoint %s", bridgeHNSEndpointName)
createBridgeHNSEndpoint = false
}

// 6. Create a bridge HNSEndpoint
if createBridgeHNSEndpoint {
if hnsEndpoint != nil {
if _, err = hnsEndpoint.Delete(); err != nil {
return nil, errors.Annotatef(err, "failed to delete existing bridge HNSEndpoint %s", bridgeHNSEndpointName)
}
log.Infof("Deleted stale bridge HNSEndpoint %s", bridgeHNSEndpointName)
}

hnsEndpoint = &hcsshim.HNSEndpoint{
Id: "",
Name: bridgeHNSEndpointName,
IPAddress: podGatewayAddress.ToIP(),
VirtualNetwork: hnsNetwork.Id,
}

log.Infof("Attempting to create bridge HNSEndpoint %+v", hnsEndpoint)
if hnsEndpoint, err = hnsEndpoint.Create(); err != nil {
return nil, errors.Annotatef(err, "failed to create bridge HNSEndpoint %s", bridgeHNSEndpointName)
}

log.Infof("Created bridge HNSEndpoint %s", bridgeHNSEndpointName)
}

if err = hnsEndpoint.HostAttach(1); err != nil {
return nil, errors.Annotatef(err, "failed to hot attach bridge HNSEndpoint %s to host compartment", bridgeHNSEndpointName)
}
log.Infof("Attached bridge endpoint %s to host", bridgeHNSEndpointName)

// 7. Enable forwarding on the host interface and endpoint
for _, interfaceIpAddress := range []string{hnsNetwork.ManagementIP, hnsEndpoint.IPAddress.String()} {
netInterface, err := netshHelper.GetInterfaceByIP(interfaceIpAddress)
if err != nil {
return nil, errors.Annotatef(err, "failed to find interface for IP Address %s", interfaceIpAddress)
}
log.Infof("Found %v NetshInterface with IP %s", netInterface, interfaceIpAddress)

// When a new hns network is created, the interface is modified, esp the name, index
if hnsNetwork.ManagementIP == netInterface.IpAddress {
n.LinkIndex = netInterface.Idx
n.Name = netInterface.Name
}

interfaceIdx := strconv.Itoa(netInterface.Idx)
if err := netshHelper.EnableForwarding(interfaceIdx); err != nil {
return nil, errors.Annotatef(err, "failed to enable forwarding on %s index %d", netInterface.Name, interfaceIdx)
}
log.Infof("Enabled forwarding on %s index %d", netInterface.Name, interfaceIdx)
}

return n, nil
}
Loading

0 comments on commit 940745c

Please sign in to comment.