Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Flannel to support Windows for host-gw and VXLAN #832

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ cover.out
.editorconfig
.idea/

default.etcd/
flannel.exe
2 changes: 2 additions & 0 deletions backend/alivpc/alivpc.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
25 changes: 25 additions & 0 deletions backend/alivpc/alivpc_unspecified.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// +build !linux

// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// 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.

package alivpc

import (
log "github.com/golang/glog"
)

func init() {
log.Infof("AliVpc is not supported on this platform")
}
2 changes: 2 additions & 0 deletions backend/awsvpc/awsvpc.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
25 changes: 25 additions & 0 deletions backend/awsvpc/awsvpc_unspecified.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// +build !linux

// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// 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.

package awsvpc

import (
log "github.com/golang/glog"
)

func init() {
log.Infof("AWS VPC is not supported on this platform")
}
2 changes: 2 additions & 0 deletions backend/awsvpc/filter.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 2 additions & 0 deletions backend/gce/api.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 2 additions & 0 deletions backend/gce/gce.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
48 changes: 48 additions & 0 deletions backend/gce/gce_unspecified.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// +build !linux

// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// 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.

// This work borrows from the https://github.com/kelseyhightower/flannel-route-manager
// project which has the following license agreement.

// Copyright (c) 2014 Kelsey Hightower

// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package gce

import (
log "github.com/golang/glog"
)

func init() {
log.Infof("GCE is not supported on this platform")
}
2 changes: 2 additions & 0 deletions backend/gce/metadata.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 2 additions & 0 deletions backend/hostgw/hostgw.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 2 additions & 0 deletions backend/hostgw/hostgw_network.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
208 changes: 208 additions & 0 deletions backend/hostgw/hostgw_network_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
// +build windows

// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// 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.

package hostgw

import (
"sync"
"time"

log "github.com/golang/glog"
"golang.org/x/net/context"

"github.com/coreos/flannel/backend"
"github.com/coreos/flannel/subnet"

netroute "github.com/rakelkar/gonetsh/netroute"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be added to the glide.* files

)

type network struct {
name string
extIface *backend.ExternalInterface
linkIndex int
rl []netroute.Route
lease *subnet.Lease
sm subnet.Manager
}

func (n *network) Lease() *subnet.Lease {
return n.lease
}

func (n *network) MTU() int {
return n.extIface.Iface.MTU
}

func (n *network) Run(ctx context.Context) {
wg := sync.WaitGroup{}

log.Info("Watching for new subnet leases")
evts := make(chan []subnet.Event)
wg.Add(1)
go func() {
subnet.WatchLeases(ctx, n.sm, n.lease, evts)
wg.Done()
}()

n.rl = make([]netroute.Route, 0, 10)
wg.Add(1)
go func() {
n.routeCheck(ctx)
wg.Done()
}()

defer wg.Wait()

for {
select {
case evtBatch := <-evts:
n.handleSubnetEvents(evtBatch)

case <-ctx.Done():
return
}
}
}
func (n *network) handleSubnetEvents(batch []subnet.Event) {
nr := netroute.New()
defer nr.Exit()

for _, evt := range batch {
switch evt.Type {
case subnet.EventAdded:
log.Infof("Subnet added: %v via %v", evt.Lease.Subnet, evt.Lease.Attrs.PublicIP)

if evt.Lease.Attrs.BackendType != "host-gw" {
log.Warningf("Ignoring non-host-gw subnet: type=%v", evt.Lease.Attrs.BackendType)
continue
}

route := netroute.Route{
DestinationSubnet: evt.Lease.Subnet.ToIPNet(),
GatewayAddress: evt.Lease.Attrs.PublicIP.ToIP(),
LinkIndex: n.linkIndex,
}

existingRoutes, _ := nr.GetNetRoutes(route.LinkIndex, route.DestinationSubnet)

if existingRoutes != nil && len(existingRoutes) > 0 {
if existingRoutes[0].Equal(route) {
continue
}

log.Warningf("Replacing existing route to %v via %v with %v via %v.", evt.Lease.Subnet, existingRoutes[0].GatewayAddress, evt.Lease.Subnet, evt.Lease.Attrs.PublicIP)
err := nr.RemoveNetRoute(route.LinkIndex, route.DestinationSubnet, existingRoutes[0].GatewayAddress)
if err != nil {
log.Errorf("Error removing route: %v", err)
continue
}
}

err := nr.NewNetRoute(route.LinkIndex, route.DestinationSubnet, route.GatewayAddress)
if err != nil {
log.Errorf("Error creating route: %v", err)
}

n.addToRouteList(route)

case subnet.EventRemoved:
log.Info("Subnet removed: ", evt.Lease.Subnet)

if evt.Lease.Attrs.BackendType != "host-gw" {
log.Warningf("Ignoring non-host-gw subnet: type=%v", evt.Lease.Attrs.BackendType)
continue
}

route := netroute.Route{
DestinationSubnet: evt.Lease.Subnet.ToIPNet(),
GatewayAddress: evt.Lease.Attrs.PublicIP.ToIP(),
LinkIndex: n.linkIndex,
}

existingRoutes, _ := nr.GetNetRoutes(route.LinkIndex, route.DestinationSubnet)

if existingRoutes != nil {
err := nr.RemoveNetRoute(route.LinkIndex, route.DestinationSubnet, route.GatewayAddress)
if err != nil {
log.Errorf("Error removing route: %v", err)
}
}

n.removeFromRouteList(route)

default:
log.Error("Internal error: unknown event type: ", int(evt.Type))
}
}
}

func (n *network) addToRouteList(route netroute.Route) {
for _, r := range n.rl {
if r.Equal(route) {
return
}
}
n.rl = append(n.rl, route)
}

func (n *network) removeFromRouteList(route netroute.Route) {
for index, r := range n.rl {
if r.Equal(route) {
n.rl = append(n.rl[:index], n.rl[index+1:]...)
return
}
}
}

func (n *network) routeCheck(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case <-time.After(routeCheckRetries * time.Second):
n.checkSubnetExistInRoutes()
}
}
}

func (n *network) checkSubnetExistInRoutes() {
nr := netroute.New()
defer nr.Exit()

currentRoutes, err := nr.GetNetRoutesAll()
if err != nil {
log.Errorf("Error enumerating routes", err)
return
}
for _, r := range n.rl {
exist := false
for _, currentRoute := range currentRoutes {
if r.Equal(currentRoute) {
exist = true
break
}
}

if !exist {
err := nr.NewNetRoute(r.LinkIndex, r.DestinationSubnet, r.GatewayAddress)
if err != nil {
log.Errorf("Error recovering route to %v via %v on %v (%v).", r.DestinationSubnet, r.GatewayAddress, r.LinkIndex, err)
continue
}
log.Errorf("Recovered route to %v via %v on %v.", r.DestinationSubnet, r.GatewayAddress, r.LinkIndex)
}
}
}
25 changes: 25 additions & 0 deletions backend/hostgw/hostgw_unspecified.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// +build !linux,!windows

// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// 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.

package hostgw

import (
log "github.com/golang/glog"
)

func init() {
log.Infof("hostgw is not supported on this platform")
}
Loading