From 31ad00bd75a3e1fe4aaa4f7993b56e9ec1b94163 Mon Sep 17 00:00:00 2001 From: MengxinLiu Date: Fri, 26 Jul 2019 14:32:16 +0800 Subject: [PATCH] feat: add ip info to ip crd --- pkg/apis/kubeovn/v1/types.go | 1 + pkg/daemon/handler.go | 68 +++++++++++++++++++++++++++++++++--- yamls/crd.yaml | 13 +++++++ 3 files changed, 77 insertions(+), 5 deletions(-) diff --git a/pkg/apis/kubeovn/v1/types.go b/pkg/apis/kubeovn/v1/types.go index df8ecafceb1..0669ba8f54a 100644 --- a/pkg/apis/kubeovn/v1/types.go +++ b/pkg/apis/kubeovn/v1/types.go @@ -28,6 +28,7 @@ type IP struct { type IPSpec struct { PodName string `json:"podName"` Namespace string `json:"namespace"` + Subnet string `json:"subnet"` NodeName string `json:"nodeName"` IPAddress string `json:"ipAddress"` MacAddress string `json:"macAddress"` diff --git a/pkg/daemon/handler.go b/pkg/daemon/handler.go index 954001308ab..f328dab4b05 100644 --- a/pkg/daemon/handler.go +++ b/pkg/daemon/handler.go @@ -6,21 +6,26 @@ import ( "strings" "time" + kubeovnv1 "github.com/alauda/kube-ovn/pkg/apis/kubeovn/v1" + clientset "github.com/alauda/kube-ovn/pkg/client/clientset/versioned" "github.com/alauda/kube-ovn/pkg/request" "github.com/alauda/kube-ovn/pkg/util" "github.com/emicklei/go-restful" + k8serrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/klog" ) type cniServerHandler struct { - Config *Configuration - KubeClient kubernetes.Interface + Config *Configuration + KubeClient kubernetes.Interface + KubeOvnClient clientset.Interface } func createCniServerHandler(config *Configuration) *cniServerHandler { - csh := &cniServerHandler{KubeClient: config.KubeClient, Config: config} + csh := &cniServerHandler{KubeClient: config.KubeClient, KubeOvnClient: config.KubeOvnClient, Config: config} return csh } @@ -34,7 +39,7 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon return } klog.Infof("add port request %v", podRequest) - var macAddr, ip, ipAddr, cidr, gw, ingress, egress string + var macAddr, ip, ipAddr, cidr, gw, subnet, ingress, egress string for i := 0; i < 10; i++ { pod, err := csh.KubeClient.CoreV1().Pods(podRequest.PodNamespace).Get(podRequest.PodName, v1.GetOptions{}) if err != nil { @@ -46,13 +51,14 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon if err := util.ValidatePodNetwork(pod.Annotations); err != nil { klog.Errorf("validate pod %s/%s failed, %v", podRequest.PodNamespace, podRequest.PodName, err) // wait controller assign an address - time.Sleep(2 * time.Second) + time.Sleep(1 * time.Second) continue } macAddr = pod.Annotations[util.MacAddressAnnotation] ip = pod.Annotations[util.IpAddressAnnotation] cidr = pod.Annotations[util.CidrAnnotation] gw = pod.Annotations[util.GatewayAnnotation] + subnet = pod.Annotations[util.LogicalSwitchAnnotation] ingress = pod.Annotations[util.IngressRateAnnotation] egress = pod.Annotations[util.EgressRateAnnotation] break @@ -65,6 +71,51 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon return } + ipCrd, err := csh.KubeOvnClient.KubeovnV1().IPs().Get(fmt.Sprintf("%s.%s", podRequest.PodName, podRequest.PodNamespace), metav1.GetOptions{}) + if err != nil && k8serrors.IsNotFound(err) { + _, err := csh.KubeOvnClient.KubeovnV1().IPs().Create(&kubeovnv1.IP{ + ObjectMeta: v1.ObjectMeta{ + Name: fmt.Sprintf("%s.%s", podRequest.PodName, podRequest.PodNamespace)}, + Spec: kubeovnv1.IPSpec{ + PodName: podRequest.PodName, + Namespace: podRequest.PodNamespace, + Subnet: subnet, + NodeName: csh.Config.NodeName, + IPAddress: ip, + MacAddress: macAddr, + ContainerID: podRequest.ContainerID, + }, + }) + if err != nil { + errMsg := fmt.Errorf("failed to create ip crd for %s, %v", ip, err) + klog.Error(errMsg) + resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.PodResponse{Err: errMsg.Error()}) + return + } + } else { + if err != nil { + ipCrd.Spec.PodName = podRequest.PodName + ipCrd.Spec.Namespace = podRequest.PodNamespace + ipCrd.Spec.Subnet = subnet + ipCrd.Spec.NodeName = csh.Config.NodeName + ipCrd.Spec.IPAddress = ip + ipCrd.Spec.MacAddress = macAddr + ipCrd.Spec.ContainerID = podRequest.ContainerID + _, err := csh.KubeOvnClient.KubeovnV1().IPs().Update(ipCrd) + if err != nil { + errMsg := fmt.Errorf("failed to create ip crd for %s, %v", ip, err) + klog.Error(errMsg) + resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.PodResponse{Err: errMsg.Error()}) + return + } + } else { + errMsg := fmt.Errorf("failed to get ip crd for %s, %v", ip, err) + klog.Error(errMsg) + resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.PodResponse{Err: errMsg.Error()}) + return + } + } + ipAddr = fmt.Sprintf("%s/%s", ip, strings.Split(cidr, "/")[1]) klog.Infof("create container mac %s, ip %s, cidr %s, gw %s", macAddr, ipAddr, cidr, gw) err = csh.configureNic(podRequest.PodName, podRequest.PodNamespace, podRequest.NetNs, podRequest.ContainerID, macAddr, ipAddr, gw, ingress, egress) @@ -94,5 +145,12 @@ func (csh cniServerHandler) handleDel(req *restful.Request, resp *restful.Respon resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.PodResponse{Err: errMsg.Error()}) return } + err = csh.KubeOvnClient.KubeovnV1().IPs().Delete(fmt.Sprintf("%s.%s", podRequest.PodName, podRequest.PodNamespace), &metav1.DeleteOptions{}) + if err != nil && !k8serrors.IsNotFound(err) { + errMsg := fmt.Errorf("del ipcrd for %s failed %v", fmt.Sprintf("%s.%s", podRequest.PodName, podRequest.PodNamespace), err) + klog.Error(errMsg) + resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.PodResponse{Err: errMsg.Error()}) + return + } resp.WriteHeader(http.StatusNoContent) } diff --git a/yamls/crd.yaml b/yamls/crd.yaml index b031f8f29fe..fe69634fd0e 100644 --- a/yamls/crd.yaml +++ b/yamls/crd.yaml @@ -12,6 +12,19 @@ spec: kind: IP shortNames: - ip + additionalPrinterColumns: + - name: IP + type: string + JSONPath: .spec.ipAddress + - name: Mac + type: string + JSONPath: .spec.macAddress + - name: Node + type: string + JSONPath: .spec.nodeName + - name: Subnet + type: string + JSONPath: .spec.subnet --- apiVersion: apiextensions.k8s.io/v1beta1 kind: CustomResourceDefinition