Skip to content

Commit

Permalink
feat: add ip info to ip crd
Browse files Browse the repository at this point in the history
  • Loading branch information
oilbeater committed Jul 26, 2019
1 parent ad7b5c2 commit 31ad00b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/apis/kubeovn/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
68 changes: 63 additions & 5 deletions pkg/daemon/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
13 changes: 13 additions & 0 deletions yamls/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 31ad00b

Please sign in to comment.