From 90f7d61e14634a9978e2f0358ed83737ef9be0d3 Mon Sep 17 00:00:00 2001 From: Zhang Jiajun Date: Fri, 20 Apr 2018 22:36:41 +0800 Subject: [PATCH] [REV] add null valuue check for delete L4 rule of openresty resty api --- pkg/entrance/plugin/openresty/model.go | 26 +++---- .../plugin/openresty/openresty_plugin.go | 70 ++++++++++++++----- 2 files changed, 65 insertions(+), 31 deletions(-) diff --git a/pkg/entrance/plugin/openresty/model.go b/pkg/entrance/plugin/openresty/model.go index 8feb8ab4b..a2f2f310e 100644 --- a/pkg/entrance/plugin/openresty/model.go +++ b/pkg/entrance/plugin/openresty/model.go @@ -45,23 +45,19 @@ func (this *NginxUpstream) AddNode(node NginxNode) { this.Servers = append(this.Servers, node) } -type NginxHttpServer struct { - Name string `json:"name"` - Domain string `json:"domain"` - Port int32 `json:"port"` - Path string `json:"path"` - Protocol string `json:"protocol"` // http and https only - Cert string `json:"cert"` - Key string `json:"key"` - Options map[string]string `json:"options"` - Upstream string `json:"upstream"` - TransferHTTP bool `json:"transferHTTP"` -} - -type NginxStreamServer struct { +type NginxServer struct { Name string `json:"name"` + Domain string `json:"domain"` Port int32 `json:"port"` + Path string `json:"path"` + Protocol string `json:"protocol"` // http and https only + Cert string `json:"cert"` + Key string `json:"key"` Options map[string]string `json:"options"` Upstream string `json:"upstream"` - Protocol string `json:"protocol"` // tcp and udp only + ToHTTPS bool `json:"toHTTPS"` } + +type Options struct { + Protocol string `json:"protocol"` // http/https/tcp/udp +} \ No newline at end of file diff --git a/pkg/entrance/plugin/openresty/openresty_plugin.go b/pkg/entrance/plugin/openresty/openresty_plugin.go index 0e8e716a4..5f7a08235 100644 --- a/pkg/entrance/plugin/openresty/openresty_plugin.go +++ b/pkg/entrance/plugin/openresty/openresty_plugin.go @@ -192,7 +192,7 @@ func (this *openresty) doEach(method string, url string, body interface{}) (e er return errors.New(errMsg) } - logrus.Info(method, " ", url) + logrus.Debug(method, " ", url) return nil } @@ -446,7 +446,7 @@ func (this *openresty) UpdateRule(rules ...*object.RuleObject) error { key = pair.PrivateKey } - openrestyRule := NginxHttpServer{ + openrestyRule := NginxServer{ rule.Name, rule.DomainName, int32(port), @@ -476,7 +476,12 @@ func (this *openresty) UpdateRule(rules ...*object.RuleObject) error { func (this *openresty) DeleteRule(rules ...*object.RuleObject) error { var errs []error for _, rule := range(rules) { - err := this.doEach(DELETE, this.urlServer(rule.Name+"?protocol=http"), nil) + protocol := "http" + if rule.HTTPS { + protocol = "https" + } + + err := this.doEach(DELETE, this.urlServer(rule.Name), Options{protocol}) if err != nil { errs = append(errs, err) @@ -514,12 +519,16 @@ func (this *openresty) UpdateVirtualService(services ...*object.VirtualServiceOb continue } - openrestyRule := NginxStreamServer{ - service.Name, - service.Port, - map[string]string{}, - upstreamName, - service.Protocol, + if service.Protocol == "" { + service.Protocol = "tcp" + } + + openrestyRule := NginxServer{ + Name: service.Name, + Port: service.Port, + Options: map[string]string{}, + Upstream: upstreamName, + Protocol: service.Protocol, } // build json data and request api @@ -539,8 +548,12 @@ func (this *openresty) UpdateVirtualService(services ...*object.VirtualServiceOb func (this *openresty) DeleteVirtualService(services ...*object.VirtualServiceObject) error { var errs []error for _, service := range services { - // build json data and request api - err := this.doEach(DELETE, this.urlServer(service.Name)+"?protocol=tcp", nil) + + if service.Protocol == "" { + service.Protocol = "tcp" + } + + err := this.doEach(DELETE, this.urlServer(service.Name), Options{service.Protocol}) if err != nil { errs = append(errs, err) logrus.Error(err) @@ -566,12 +579,37 @@ func (this *openresty) Stop() error {return nil} func (this *openresty) GetName() string {return "openresty"} func (this *openresty) GetPluginStatus() bool { - err := this.doEach(GET, "/health", nil) - if err != nil { - logrus.Error(err) - return false + health := true + method := GET + + for _, endpoint := range this.getHealthEndpoints() { + request, err := http.NewRequest(method, endpoint.Addr + "/health", nil) + if err != nil { + health = false + logrus.Debug(method, fmt.Sprintf(" %s %s", endpoint.Addr, err.Error())) + continue + } + + response, err := this.client.Do(request) + if err != nil { + health = false + logrus.Debug(method, fmt.Sprintf(" %s %s", endpoint.Addr, err.Error())) + continue + } + + if response.StatusCode != HTTP_OK { + health = false + b, err := ioutil.ReadAll(response.Body) + if err != nil { + logrus.Debug(method, fmt.Sprintf(" %s %s", endpoint.Addr, err.Error())) + } else { + logrus.Debug(method, fmt.Sprintf(" %s %s", endpoint.Addr, string(b))) + } + continue + } } - return true + + return health } //Check check openresty plugin optins