Replace standard json encoding with jsoniter

This commit is contained in:
Manuel de Brito Fontes 2018-09-22 14:25:01 -03:00
parent 0fd87a7e56
commit 91ae204f6c
5 changed files with 25 additions and 7 deletions

View file

@ -131,7 +131,13 @@ func (n *NGINXController) syncIngress(interface{}) error {
upstreams, servers := n.getBackendServers(ings) upstreams, servers := n.getBackendServers(ings)
var passUpstreams []*ingress.SSLPassthroughBackend var passUpstreams []*ingress.SSLPassthroughBackend
hosts := sets.NewString()
for _, server := range servers { for _, server := range servers {
if !hosts.Has(server.Hostname) {
hosts.Insert(server.Hostname)
}
if !server.SSLPassthrough { if !server.SSLPassthrough {
continue continue
} }
@ -184,6 +190,8 @@ func (n *NGINXController) syncIngress(interface{}) error {
return err return err
} }
n.metricCollector.SetHosts(hosts)
glog.Infof("Backend successfully reloaded.") glog.Infof("Backend successfully reloaded.")
n.metricCollector.ConfigSuccess(hash, true) n.metricCollector.ConfigSuccess(hash, true)
n.metricCollector.IncReloadCount() n.metricCollector.IncReloadCount()

View file

@ -17,7 +17,6 @@ limitations under the License.
package controller package controller
import ( import (
"encoding/json"
"io" "io"
"io/ioutil" "io/ioutil"
"net" "net"
@ -26,7 +25,9 @@ import (
"strings" "strings"
"testing" "testing"
jsoniter "github.com/json-iterator/go"
apiv1 "k8s.io/api/core/v1" apiv1 "k8s.io/api/core/v1"
"k8s.io/ingress-nginx/internal/ingress" "k8s.io/ingress-nginx/internal/ingress"
) )
@ -241,7 +242,7 @@ func TestConfigureCertificates(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
var postedServers []ingress.Server var postedServers []ingress.Server
err = json.Unmarshal(b, &postedServers) err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(b, &postedServers)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -17,7 +17,6 @@ limitations under the License.
package template package template
import ( import (
"encoding/json"
"io/ioutil" "io/ioutil"
"net" "net"
"os" "os"
@ -29,6 +28,7 @@ import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
jsoniter "github.com/json-iterator/go"
"k8s.io/ingress-nginx/internal/file" "k8s.io/ingress-nginx/internal/file"
"k8s.io/ingress-nginx/internal/ingress" "k8s.io/ingress-nginx/internal/ingress"
"k8s.io/ingress-nginx/internal/ingress/annotations/authreq" "k8s.io/ingress-nginx/internal/ingress/annotations/authreq"
@ -518,7 +518,7 @@ func TestTemplateWithData(t *testing.T) {
t.Error("unexpected error reading json file: ", err) t.Error("unexpected error reading json file: ", err)
} }
var dat config.TemplateConfig var dat config.TemplateConfig
if err := json.Unmarshal(data, &dat); err != nil { if err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(data, &dat); err != nil {
t.Errorf("unexpected error unmarshalling json: %v", err) t.Errorf("unexpected error unmarshalling json: %v", err)
} }
if dat.ListenPorts == nil { if dat.ListenPorts == nil {
@ -565,7 +565,7 @@ func BenchmarkTemplateWithData(b *testing.B) {
b.Error("unexpected error reading json file: ", err) b.Error("unexpected error reading json file: ", err)
} }
var dat config.TemplateConfig var dat config.TemplateConfig
if err := json.Unmarshal(data, &dat); err != nil { if err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(data, &dat); err != nil {
b.Errorf("unexpected error unmarshalling json: %v", err) b.Errorf("unexpected error unmarshalling json: %v", err)
} }

View file

@ -22,6 +22,8 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/ingress-nginx/internal/ingress" "k8s.io/ingress-nginx/internal/ingress"
"k8s.io/ingress-nginx/internal/ingress/annotations/class" "k8s.io/ingress-nginx/internal/ingress/annotations/class"
"k8s.io/ingress-nginx/internal/ingress/metric/collectors" "k8s.io/ingress-nginx/internal/ingress/metric/collectors"
@ -38,6 +40,9 @@ type Collector interface {
SetSSLExpireTime([]*ingress.Server) SetSSLExpireTime([]*ingress.Server)
// SetHosts sets the hostnames that are being served by the ingress controller
SetHosts(sets.String)
Start() Start()
Stop() Stop()
} }
@ -138,3 +143,7 @@ func (c *collector) Stop() {
func (c *collector) SetSSLExpireTime(servers []*ingress.Server) { func (c *collector) SetSSLExpireTime(servers []*ingress.Server) {
c.ingressController.SetSSLExpireTime(servers) c.ingressController.SetSSLExpireTime(servers)
} }
func (c *collector) SetHosts(hosts sets.String) {
c.socket.SetHosts(hosts)
}

View file

@ -18,12 +18,12 @@ package annotations
import ( import (
"bytes" "bytes"
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"os/exec" "os/exec"
"time" "time"
jsoniter "github.com/json-iterator/go"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/parnurzeal/gorequest" "github.com/parnurzeal/gorequest"
@ -90,7 +90,7 @@ var _ = framework.IngressNginxDescribe("Annotations - influxdb", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var results map[string][]map[string]interface{} var results map[string][]map[string]interface{}
json.Unmarshal([]byte(measurements), &results) jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(measurements), &results)
Expect(len(measurements)).ShouldNot(Equal(0)) Expect(len(measurements)).ShouldNot(Equal(0))
for _, elem := range results["results"] { for _, elem := range results["results"] {