Kaynağa Gözat

subnet: Move etcd files into their own package.

Kube backend is in it's own package so etcdv2 should be too
This makes it easier to see
- what being tested and what's not
- What interfaces are etcd specific (e.g. registry)
Tom Denham 8 yıl önce
ebeveyn
işleme
afc5e0d997

+ 1 - 1
Makefile

@@ -8,7 +8,7 @@ TAG?=$(shell git describe --tags --dirty)
 ARCH?=amd64
 
 # These variables can be overridden by setting an environment variable.
-TEST_PACKAGES?=pkg/ip subnet
+TEST_PACKAGES?=pkg/ip subnet subnet/etcdv2
 TEST_PACKAGES_EXPANDED=$(TEST_PACKAGES:%=github.com/coreos/flannel/%)
 PACKAGES?=$(TEST_PACKAGES) network
 PACKAGES_EXPANDED=$(PACKAGES:%=github.com/coreos/flannel/%)

+ 14 - 13
main.go

@@ -29,30 +29,31 @@ import (
 
 	"github.com/coreos/flannel/network"
 	"github.com/coreos/flannel/subnet"
+	"github.com/coreos/flannel/subnet/etcdv2"
 	"github.com/coreos/flannel/subnet/kube"
 	"github.com/coreos/flannel/version"
 
 	// Backends need to be imported for their init() to get executed and them to register
+	_ "github.com/coreos/flannel/backend/alivpc"
 	_ "github.com/coreos/flannel/backend/alloc"
 	_ "github.com/coreos/flannel/backend/awsvpc"
 	_ "github.com/coreos/flannel/backend/gce"
 	_ "github.com/coreos/flannel/backend/hostgw"
 	_ "github.com/coreos/flannel/backend/udp"
 	_ "github.com/coreos/flannel/backend/vxlan"
-	_ "github.com/coreos/flannel/backend/alivpc"
 )
 
 type CmdLineOpts struct {
-	etcdEndpoints  string
-	etcdPrefix     string
-	etcdKeyfile    string
-	etcdCertfile   string
-	etcdCAFile     string
-	etcdUsername   string
-	etcdPassword   string
-	help           bool
-	version        bool
-	kubeSubnetMgr  bool
+	etcdEndpoints string
+	etcdPrefix    string
+	etcdKeyfile   string
+	etcdCertfile  string
+	etcdCAFile    string
+	etcdUsername  string
+	etcdPassword  string
+	help          bool
+	version       bool
+	kubeSubnetMgr bool
 }
 
 var opts CmdLineOpts
@@ -75,7 +76,7 @@ func newSubnetManager() (subnet.Manager, error) {
 		return kube.NewSubnetManager()
 	}
 
-	cfg := &subnet.EtcdConfig{
+	cfg := &etcdv2.EtcdConfig{
 		Endpoints: strings.Split(opts.etcdEndpoints, ","),
 		Keyfile:   opts.etcdKeyfile,
 		Certfile:  opts.etcdCertfile,
@@ -85,7 +86,7 @@ func newSubnetManager() (subnet.Manager, error) {
 		Password:  opts.etcdPassword,
 	}
 
-	return subnet.NewLocalManager(cfg)
+	return etcdv2.NewLocalManager(cfg)
 }
 
 func main() {

+ 0 - 19
subnet/clock.go

@@ -1,19 +0,0 @@
-// 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 subnet
-
-import "github.com/jonboulle/clockwork"
-
-var clock clockwork.Clock = clockwork.NewRealClock()

+ 2 - 1
subnet/local_manager.go → subnet/etcdv2/local_manager.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package subnet
+package etcdv2
 
 import (
 	"errors"
@@ -22,6 +22,7 @@ import (
 
 	etcd "github.com/coreos/etcd/client"
 	"github.com/coreos/flannel/pkg/ip"
+	. "github.com/coreos/flannel/subnet"
 	log "github.com/golang/glog"
 	"golang.org/x/net/context"
 )

+ 1 - 1
subnet/mock_etcd.go → subnet/etcdv2/mock_etcd.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package subnet
+package etcdv2
 
 import (
 	"fmt"

+ 1 - 1
subnet/mock_etcd_test.go → subnet/etcdv2/mock_etcd_test.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package subnet
+package etcdv2
 
 import (
 	"fmt"

+ 9 - 5
subnet/mock_registry.go → subnet/etcdv2/mock_registry.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package subnet
+package etcdv2
 
 import (
 	"fmt"
@@ -21,11 +21,15 @@ import (
 	"time"
 
 	etcd "github.com/coreos/etcd/client"
+	"github.com/jonboulle/clockwork"
 	"golang.org/x/net/context"
 
 	"github.com/coreos/flannel/pkg/ip"
+	. "github.com/coreos/flannel/subnet"
 )
 
+var clock clockwork.Clock = clockwork.NewRealClock()
+
 type netwk struct {
 	config        string
 	subnets       []Lease
@@ -168,7 +172,7 @@ func (msr *MockSubnetRegistry) createSubnet(ctx context.Context, network string,
 		Subnet:     sn,
 		Attrs:      *attrs,
 		Expiration: exp,
-		asof:       msr.index,
+		Asof:       msr.index,
 	}
 	n.subnets = append(n.subnets, l)
 
@@ -205,7 +209,7 @@ func (msr *MockSubnetRegistry) updateSubnet(ctx context.Context, network string,
 	}
 
 	sub.Attrs = *attrs
-	sub.asof = msr.index
+	sub.Asof = msr.index
 	sub.Expiration = exp
 	n.subnets[i] = sub
 	n.sendSubnetEvent(sn, event{
@@ -237,7 +241,7 @@ func (msr *MockSubnetRegistry) deleteSubnet(ctx context.Context, network string,
 
 	n.subnets[i] = n.subnets[len(n.subnets)-1]
 	n.subnets = n.subnets[:len(n.subnets)-1]
-	sub.asof = msr.index
+	sub.Asof = msr.index
 	n.sendSubnetEvent(sn, event{
 		Event{
 			Type:    EventRemoved,
@@ -332,7 +336,7 @@ func (msr *MockSubnetRegistry) expireSubnet(network string, sn ip.IP4Net) {
 		msr.index += 1
 		n.subnets[i] = n.subnets[len(n.subnets)-1]
 		n.subnets = n.subnets[:len(n.subnets)-1]
-		sub.asof = msr.index
+		sub.Asof = msr.index
 		n.sendSubnetEvent(sn, event{
 			Event{
 				Type:  EventRemoved,

+ 6 - 2
subnet/mock_subnet.go → subnet/etcdv2/mock_subnet.go

@@ -12,8 +12,12 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package subnet
+package etcdv2
 
-func NewMockManager(registry *MockSubnetRegistry) Manager {
+import (
+	"github.com/coreos/flannel/subnet"
+)
+
+func NewMockManager(registry *MockSubnetRegistry) subnet.Manager {
 	return newLocalManager(registry)
 }

+ 1 - 1
subnet/rand.go → subnet/etcdv2/rand.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package subnet
+package etcdv2
 
 import (
 	"math/rand"

+ 4 - 4
subnet/registry.go → subnet/etcdv2/registry.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package subnet
+package etcdv2
 
 import (
 	"encoding/json"
@@ -29,11 +29,11 @@ import (
 	"golang.org/x/net/context"
 
 	"github.com/coreos/flannel/pkg/ip"
+	. "github.com/coreos/flannel/subnet"
 )
 
 var (
-	subnetRegex *regexp.Regexp = regexp.MustCompile(`(\d+\.\d+.\d+.\d+)-(\d+)`)
-	errTryAgain                = errors.New("try again")
+	errTryAgain = errors.New("try again")
 )
 
 type Registry interface {
@@ -414,7 +414,7 @@ func nodeToLease(node *etcd.Node) (*Lease, error) {
 		Subnet:     *sn,
 		Attrs:      *attrs,
 		Expiration: exp,
-		asof:       node.ModifiedIndex,
+		Asof:       node.ModifiedIndex,
 	}
 
 	return &lease, nil

+ 2 - 1
subnet/registry_test.go → subnet/etcdv2/registry_test.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package subnet
+package etcdv2
 
 import (
 	"fmt"
@@ -24,6 +24,7 @@ import (
 	"golang.org/x/net/context"
 
 	"github.com/coreos/flannel/pkg/ip"
+	. "github.com/coreos/flannel/subnet"
 )
 
 func newTestEtcdRegistry(t *testing.T) (Registry, *mockEtcd) {

+ 3 - 3
subnet/subnet_test.go → subnet/etcdv2/subnet_test.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package subnet
+package etcdv2
 
 import (
 	"encoding/json"
@@ -21,10 +21,10 @@ import (
 	"time"
 
 	etcd "github.com/coreos/etcd/client"
+	"github.com/coreos/flannel/pkg/ip"
+	. "github.com/coreos/flannel/subnet"
 	"github.com/jonboulle/clockwork"
 	"golang.org/x/net/context"
-
-	"github.com/coreos/flannel/pkg/ip"
 )
 
 func newDummyRegistry() *MockSubnetRegistry {

+ 5 - 3
subnet/subnet.go

@@ -19,6 +19,7 @@ import (
 	"errors"
 	"fmt"
 	"net"
+	"regexp"
 	"strconv"
 	"time"
 
@@ -27,8 +28,9 @@ import (
 )
 
 var (
-	ErrLeaseTaken  = errors.New("subnet: lease already taken")
-	ErrNoMoreTries = errors.New("subnet: no more tries")
+	ErrLeaseTaken                 = errors.New("subnet: lease already taken")
+	ErrNoMoreTries                = errors.New("subnet: no more tries")
+	subnetRegex    *regexp.Regexp = regexp.MustCompile(`(\d+\.\d+.\d+.\d+)-(\d+)`)
 )
 
 type LeaseAttrs struct {
@@ -42,7 +44,7 @@ type Lease struct {
 	Attrs      LeaseAttrs
 	Expiration time.Time
 
-	asof uint64
+	Asof uint64
 }
 
 func (l *Lease) Key() string {