Skip to content

Commit

Permalink
Fix json: cannot unmarshal string into Go value of type request.PodRe…
Browse files Browse the repository at this point in the history
…sponse

#33
  • Loading branch information
halfcrazy committed May 22, 2019
1 parent cb176ab commit 031924d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
39 changes: 23 additions & 16 deletions pkg/daemon/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package daemon

import (
"fmt"
"net/http"
"strings"
"time"

"github.com/alauda/kube-ovn/pkg/request"
"github.com/alauda/kube-ovn/pkg/util"
"github.com/emicklei/go-restful"
"k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog"
"net/http"
"strings"
"time"
)

type cniServerHandler struct {
Expand All @@ -27,17 +28,19 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon
podRequest := request.PodRequest{}
err := req.ReadEntity(&podRequest)
if err != nil {
klog.Errorf("parse add request failed %v", err)
resp.WriteHeaderAndEntity(http.StatusBadRequest, err)
errMsg := fmt.Errorf("parse add request failed %v", err)
klog.Error(errMsg)
resp.WriteHeaderAndEntity(http.StatusBadRequest, request.PodResponse{Err: errMsg.Error()})
return
}
klog.Infof("add port request %v", podRequest)
var macAddr, ipAddr, cidr, gw, 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 {
klog.Errorf("get pod %s/%s failed %v", podRequest.PodNamespace, podRequest.PodName, err)
resp.WriteHeaderAndEntity(http.StatusInternalServerError, err)
errMsg := fmt.Errorf("get pod %s/%s failed %v", podRequest.PodNamespace, podRequest.PodName, err)
klog.Error(errMsg)
resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.PodResponse{Err: errMsg.Error()})
return
}
macAddr = pod.Annotations[util.MacAddressAnnotation]
Expand All @@ -56,16 +59,18 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon
}

if macAddr == "" || ipAddr == "" || cidr == "" || gw == "" {
klog.Errorf("no available ip for pod %s/%s", podRequest.PodNamespace, podRequest.PodName)
resp.WriteHeaderAndEntity(http.StatusInternalServerError, fmt.Sprintf("no available ip for pod %s/%s", podRequest.PodNamespace, podRequest.PodName))
errMsg := fmt.Errorf("no available ip for pod %s/%s", podRequest.PodNamespace, podRequest.PodName)
klog.Error(errMsg)
resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.PodResponse{Err: errMsg.Error()})
return
}

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)
if err != nil {
klog.Errorf("configure nic failed %v", err)
resp.WriteHeaderAndEntity(http.StatusInternalServerError, err)
errMsg := fmt.Errorf("configure nic failed %v", err)
klog.Error(errMsg)
resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.PodResponse{Err: errMsg.Error()})
return
}
resp.WriteHeaderAndEntity(http.StatusOK, request.PodResponse{IpAddress: strings.Split(ipAddr, "/")[0], MacAddress: macAddr, CIDR: cidr, Gateway: gw})
Expand All @@ -75,15 +80,17 @@ func (csh cniServerHandler) handleDel(req *restful.Request, resp *restful.Respon
podRequest := request.PodRequest{}
err := req.ReadEntity(&podRequest)
if err != nil {
klog.Errorf("parse del request failed %v", err)
resp.WriteHeaderAndEntity(http.StatusBadRequest, err)
errMsg := fmt.Errorf("parse del request failed %v", err)
klog.Error(errMsg)
resp.WriteHeaderAndEntity(http.StatusBadRequest, request.PodResponse{Err: errMsg.Error()})
return
}
klog.Infof("delete port request %v", podRequest)
err = csh.deleteNic(podRequest.NetNs, podRequest.PodName, podRequest.PodNamespace, podRequest.ContainerID)
if err != nil {
klog.Errorf("del nic failed %v", err)
resp.WriteHeaderAndEntity(http.StatusInternalServerError, err)
errMsg := fmt.Errorf("del nic failed %v", err)
klog.Error(errMsg)
resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.PodResponse{Err: errMsg.Error()})
return
}
resp.WriteHeader(http.StatusNoContent)
Expand Down
13 changes: 8 additions & 5 deletions pkg/request/cniserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package request
import (
"context"
"fmt"
"github.com/parnurzeal/gorequest"
"net"
"net/http"

"github.com/parnurzeal/gorequest"
)

// CniServerClient is the client to visit cniserver
Expand All @@ -28,6 +29,7 @@ type PodResponse struct {
CIDR string `json:"cidr"`
Gateway string `json:"gateway"`
Mtu int `json:"mtu"`
Err string `json:"error"`
}

// NewCniServerClient return a new cniserverclient
Expand All @@ -42,24 +44,25 @@ func NewCniServerClient(socketAddress string) CniServerClient {
// Add pod request
func (csc CniServerClient) Add(podRequest PodRequest) (*PodResponse, error) {
resp := PodResponse{}
res, body, errors := csc.Post("http://dummy/api/v1/add").Send(podRequest).EndStruct(&resp)
res, _, errors := csc.Post("http://dummy/api/v1/add").Send(podRequest).EndStruct(&resp)
if len(errors) != 0 {
return nil, errors[0]
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("request ip return %d %s", res.StatusCode, body)
return nil, fmt.Errorf("request ip return %d %s", res.StatusCode, resp.Err)
}
return &resp, nil
}

// Del pod request
func (csc CniServerClient) Del(podRequest PodRequest) error {
res, body, errors := csc.Post("http://dummy/api/v1/del").Send(podRequest).End()
resp := PodResponse{}
res, _, errors := csc.Post("http://dummy/api/v1/del").Send(podRequest).EndStruct(&resp)
if len(errors) != 0 {
return errors[0]
}
if res.StatusCode != 204 {
return fmt.Errorf("delete ip return %d %s", res.StatusCode, body)
return fmt.Errorf("delete ip return %d %s", res.StatusCode, resp.Err)
}
return nil
}

0 comments on commit 031924d

Please sign in to comment.