From 127c84510252668464e30ba77d91164579dc9ab0 Mon Sep 17 00:00:00 2001 From: Haines Chan Date: Thu, 8 Nov 2018 15:31:25 +0800 Subject: [PATCH] Rename back and bug fix --- cmd/octopus/octopus.go | 2 +- deployment/anchor.yaml | 2 +- internal/app/anchor.go | 13 ++++++------- pkg/runtime/k8s/types.go | 6 +++--- pkg/store/etcd/etcd.go | 20 +++----------------- 5 files changed, 14 insertions(+), 29 deletions(-) diff --git a/cmd/octopus/octopus.go b/cmd/octopus/octopus.go index 12fd5a8..2435113 100644 --- a/cmd/octopus/octopus.go +++ b/cmd/octopus/octopus.go @@ -136,7 +136,7 @@ func cmdAdd(args *skel.CmdArgs) error { } // 3. Get annotations from k8s_client via K8S_POD_NAME and K8S_POD_NAMESPACE. - _, annot, err := k8s.GetK8sPodInfo(k8sClient, string(k8sArgs.PodName), string(k8sArgs.PodNamespace)) + _, annot, err := k8s.GetK8sPodInfo(k8sClient, string(k8sArgs.K8S_POD_NAME), string(k8sArgs.K8S_POD_NAMESPACE)) if err != nil { return fmt.Errorf("failed to read annotaions for pod " + err.Error()) } diff --git a/deployment/anchor.yaml b/deployment/anchor.yaml index ecf2c1b..eab5e60 100644 --- a/deployment/anchor.yaml +++ b/deployment/anchor.yaml @@ -37,7 +37,7 @@ data: "kubeconfig": "__KUBECONFIG_FILEPATH__" }, "ipam": { - "type": "anchor-ipam", + "type": "anchor", "etcd_endpoints": "__ETCD_ENDPOINTS__", "etcd_key_file": "__ETCD_KEY_FILE__", "etcd_cert_file": "__ETCD_CERT_FILE__", diff --git a/internal/app/anchor.go b/internal/app/anchor.go index 771d7cd..1600167 100644 --- a/internal/app/anchor.go +++ b/internal/app/anchor.go @@ -101,8 +101,7 @@ func newAllocator(args *skel.CmdArgs, conf *config.IPAMConf) (*anchor.Allocator, } // 3. Get annotations from k8s_client via K8S_POD_NAME and K8S_POD_NAMESPACE. - label, annot, err := k8s.GetK8sPodInfo(runtime, string(k8sArgs.PodName), - string(k8sArgs.PodNamespace)) + label, annot, err := k8s.GetK8sPodInfo(runtime, string(k8sArgs.K8S_POD_NAME), string(k8sArgs.K8S_POD_NAMESPACE)) if err != nil { return nil, err } @@ -117,13 +116,13 @@ func newAllocator(args *skel.CmdArgs, conf *config.IPAMConf) (*anchor.Allocator, // It is friendly to show which controller the pods controled by. // TODO: maybe it is meaningless. the pod name starts with the controller name. - controllerName, _ := k8s.ResourceControllerName(runtime, string(k8sArgs.PodName), string(k8sArgs.PodNamespace)) + controllerName, _ := k8s.ResourceControllerName(runtime, string(k8sArgs.K8S_POD_NAME), string(k8sArgs.K8S_POD_NAMESPACE)) if controllerName != "" { customized["cni.anchor.org/controller"] = controllerName } - return anchor.NewAllocator(store, string(k8sArgs.PodName), - string(k8sArgs.PodNamespace), customized) + return anchor.NewAllocator(store, string(k8sArgs.K8S_POD_NAME), + string(k8sArgs.K8S_POD_NAMESPACE), customized) } func newCleaner(args *skel.CmdArgs, ipamConf *config.IPAMConf) (*anchor.Cleaner, error) { @@ -148,6 +147,6 @@ func newCleaner(args *skel.CmdArgs, ipamConf *config.IPAMConf) (*anchor.Cleaner, } return anchor.NewCleaner(store, - string(k8sArgs.PodName), - string(k8sArgs.PodNamespace)) + string(k8sArgs.K8S_POD_NAME), + string(k8sArgs.K8S_POD_NAMESPACE)) } diff --git a/pkg/runtime/k8s/types.go b/pkg/runtime/k8s/types.go index 3e49423..805a9d2 100644 --- a/pkg/runtime/k8s/types.go +++ b/pkg/runtime/k8s/types.go @@ -34,7 +34,7 @@ type Kubernetes struct { type Args struct { types.CommonArgs IP net.IP - PodName types.UnmarshallableString - PodNamespace types.UnmarshallableString - PodInfraContainerID types.UnmarshallableString + K8S_POD_NAME types.UnmarshallableString + K8S_POD_NAMESPACE types.UnmarshallableString + K8S_POD_INFRA_CONTAINER_ID types.UnmarshallableString } diff --git a/pkg/store/etcd/etcd.go b/pkg/store/etcd/etcd.go index af68dcd..d6615cd 100644 --- a/pkg/store/etcd/etcd.go +++ b/pkg/store/etcd/etcd.go @@ -12,7 +12,6 @@ import ( "crypto/tls" "fmt" "net" - "reflect" "strings" "time" @@ -99,30 +98,17 @@ func (e *Etcd) Close() error { // RetrieveGateway retrieves gateway for subnet. func (e *Etcd) RetrieveGateway(subnet *net.IPNet) net.IP { - resp, err := e.kv.Get(context.TODO(), gatewayPrefix, clientv3.WithPrefix()) + resp, err := e.kv.Get(context.TODO(), gatewayPrefix + subnet.String()) if err != nil { return nil } + return net.ParseIP(string(resp.Kvs[0].Value)) - for _, item := range resp.Kvs { - parts := strings.Split(string(item.Value), ",") - _, n, err := net.ParseCIDR(parts[0]) - if err != nil { - // ommit the error. - continue - } - // TODO: if go-cmp better? - if reflect.DeepEqual(subnet, n) { - gw := net.ParseIP(parts[1]) - return gw - } - } - return nil } // RetrieveAllocated retrieves allocated IPs in subnet for namespace. func (e *Etcd) RetrieveAllocated(namespace string, subnet *net.IPNet) (*utils.RangeSet, error) { - resp, err := e.kv.Get(context.TODO(), userPrefix+namespace) + resp, err := e.kv.Get(context.TODO(), userPrefix + namespace) if err != nil { return nil, err }