Skip to content

Commit

Permalink
fix: custom resource test, issue TencentBlueKing#269
Browse files Browse the repository at this point in the history
  • Loading branch information
DeveloperJim committed Nov 22, 2019
1 parent eb3f0e7 commit e61d063
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
3 changes: 2 additions & 1 deletion bcs-mesos/bcs-mesos-driver/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type MesosDriverOptionsOut struct {
SchedDiscvSvr string `json:"sched_regdiscv" value:"127.0.0.1:2181" usage:"the address to discove schdulers"`
Cluster string `json:"cluster" value:"" usage:"the cluster ID under bcs"`
AdmissionWebhook bool `json:"admission_webhook" value:"false" usage:"whether admission webhook"`
KubeConfig string `json:"kubeconfig" value:"" usage:"kube config for custom resource feature"`
}

//MesosDriverOption is option in flags
Expand All @@ -53,7 +54,7 @@ func NewMesosDriverOption(opOut *MesosDriverOptionsOut) *MesosDriverOption {
RegDiscvSvr: opOut.BCSZk,
SchedDiscvSvr: opOut.SchedDiscvSvr,
AdmissionWebhook: opOut.AdmissionWebhook,

KubeConfig: opOut.KubeConfig,
ServCert: &config.CertConfig{
CAFile: opOut.CAFile,
CertFile: opOut.ServerCertFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"path/filepath"
"strings"

Expand All @@ -35,7 +36,8 @@ const (
//default kube custom resource definition url
defaultCRDURL = "/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions"
//default custom resource definition apiVersion we use
defaultAPIVersion = "apiextensions.k8s.io/v1beta1"
defaultAPIVersion = "apiextensions.k8s.io/v1beta1"
defaultMesosVersion = "v4"
)

//kubeProxy proxy for custom resource
Expand All @@ -56,6 +58,7 @@ func (proxy *kubeProxy) init() error {
return fmt.Errorf("bcs-mesos-driver create CustomResource transport failed")
}
proxy.crsProxy = &httputil.ReverseProxy{
Director: func(req *http.Request) {},
Transport: httpRoundTripper,
}
proxy.crdsProxy = &httputil.ReverseProxy{
Expand Down Expand Up @@ -88,15 +91,28 @@ func (proxy *kubeProxy) apiVersionReqConvert(req *http.Request) {
blog.Errorf("bcs-mesos-driver new custom resource definition Request json Marshal failed, %s. URL: %s", err.Error(), req.URL.Path)
return
}
req.Body = ioutil.NopCloser(bytes.NewBuffer(newBody))
buffer := bytes.NewBuffer(newBody)
req.Body = ioutil.NopCloser(buffer)
req.ContentLength = int64(buffer.Len())
req.GetBody = func() (io.ReadCloser, error) {
r := bytes.NewReader(newBody)
return ioutil.NopCloser(r), nil
}
blog.Infof("bcs-mesos-driver custom resource definition [%s] Request convert success.", req.URL.Path)
}

func (proxy *kubeProxy) apiVersionResConvert(req *http.Response) error {
func (proxy *kubeProxy) apiVersionResConvert(resp *http.Response) error {
allBytes, _ := ioutil.ReadAll(resp.Body)
if len(allBytes) == 0 {
blog.Errorf("mesos-driver response for %s is Empty", resp.Request.URL.String())
return nil
}
tmpStr := string(allBytes)
newStr := strings.Replace(tmpStr, defaultAPIVersion, defaultMesosVersion, 1)
buffer := bytes.NewBuffer([]byte(newStr))
resp.Body = ioutil.NopCloser(buffer)
resp.ContentLength = int64(buffer.Len())
blog.Infof("mesos-driver convert custom resource definition [%s] response success", resp.Request.URL.String())
return nil
}

Expand Down Expand Up @@ -139,11 +155,14 @@ func (s *Scheduler) customResourceForwarding(req *restful.Request, resp *restful
}
//change Path & Host
rawRequest := req.Request
original := rawRequest.URL.String()
mesosURL := req.PathParameter("uri")
kubeURL := filepath.Join("apis", mesosURL)
rawRequest.URL.Host = s.localProxy.config.Host
kubeURL := filepath.Join("/apis", mesosURL)
tmpURL, _ := url.Parse(s.localProxy.config.Host)
rawRequest.URL.Scheme = tmpURL.Scheme
rawRequest.URL.Host = tmpURL.Host
rawRequest.URL.Path = kubeURL
blog.Infof("bcs-mesos-driver custom resource forwarding: %s", rawRequest.URL.String())
blog.Infof("bcs-mesos-driver custom resource forwarding from %s to %s", original, rawRequest.URL.String())
s.localProxy.crsProxy.ServeHTTP(resp, rawRequest)
}

Expand All @@ -154,8 +173,11 @@ func (s *Scheduler) customResourceDefinitionForwarding(req *restful.Request, res
kubeURL = filepath.Join(defaultCRDURL, name)
}
rawRequest := req.Request
rawRequest.URL.Host = s.localProxy.config.Host
original := rawRequest.URL.String()
tmpURL, _ := url.Parse(s.localProxy.config.Host)
rawRequest.URL.Scheme = tmpURL.Scheme
rawRequest.URL.Host = tmpURL.Host
rawRequest.URL.Path = kubeURL
blog.Infof("bcs-mesos-driver CustomResourceDefinition forwarding: %s", rawRequest.URL.String())
blog.Infof("bcs-mesos-driver CustomResourceDefinition forwarding from %s to %s", original, rawRequest.URL.String())
s.localProxy.crdsProxy.ServeHTTP(resp, rawRequest)
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ func NewScheduler() *Scheduler {
client: httpclient.NewHttpClient(),
rwHost: new(sync.RWMutex),
}

s.initKube()
s.initActions()

return s
}

Expand All @@ -69,6 +65,9 @@ func (s *Scheduler) InitConfig(conf *config.MesosDriverConfig) {

s.client.SetHeader("Content-Type", "application/json")
s.client.SetHeader("Accept", "application/json")

s.initKube()
s.initActions()
}

//Actions all http action implementation
Expand Down

0 comments on commit e61d063

Please sign in to comment.