You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by to...@apache.org on 2021/04/01 01:36:05 UTC

[apisix-ingress-controller] branch master updated: fix: go lint errors (#325)

This is an automated email from the ASF dual-hosted git repository.

tokers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-ingress-controller.git


The following commit(s) were added to refs/heads/master by this push:
     new 9f0f246  fix: go lint errors (#325)
9f0f246 is described below

commit 9f0f2460e930984c1cf80a45fbddb5e0b4f7164f
Author: Alex Zhang <to...@apache.org>
AuthorDate: Thu Apr 1 09:35:56 2021 +0800

    fix: go lint errors (#325)
---
 .golangci.yml                           | 25 +++++++++++++++++
 cmd/ingress/ingress_test.go             | 10 +------
 pkg/apisix/cache/memdb_test.go          | 11 +++++++-
 pkg/config/config_test.go               |  2 +-
 pkg/ingress/apisix/tls.go               |  6 ++--
 pkg/ingress/controller/apisix_tls.go    |  4 +--
 pkg/ingress/controller/controller.go    |  1 -
 pkg/ingress/controller/ingress.go       | 49 ---------------------------------
 pkg/ingress/controller/secret.go        |  8 ++----
 pkg/kube/ingress.go                     | 16 +++++------
 pkg/kube/translation/apisix_upstream.go |  2 +-
 pkg/log/options.go                      |  1 -
 pkg/types/apisix/v1/types.go            |  2 +-
 pkg/types/timeduration.go               |  6 ++--
 14 files changed, 56 insertions(+), 87 deletions(-)

diff --git a/.golangci.yml b/.golangci.yml
new file mode 100644
index 0000000..7422131
--- /dev/null
+++ b/.golangci.yml
@@ -0,0 +1,25 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+issues:
+    exclude-rules:
+    - linters:
+      - staticcheck
+      text: SA1019
+linters-settings:
+  govet:
+    disable:
+    - unsafeptr
diff --git a/cmd/ingress/ingress_test.go b/cmd/ingress/ingress_test.go
index 139f708..4594b72 100644
--- a/cmd/ingress/ingress_test.go
+++ b/cmd/ingress/ingress_test.go
@@ -55,14 +55,6 @@ func (fws *fakeWriteSyncer) Write(p []byte) (int, error) {
 	return fws.buf.Write(p)
 }
 
-func (fws *fakeWriteSyncer) bytes() (p []byte) {
-	s := fws.buf.Bytes()
-	p = make([]byte, len(s))
-	copy(p, s)
-	fws.buf.Reset()
-	return
-}
-
 func getRandomListen() string {
 	port := rand.Intn(10000) + 10000
 	return fmt.Sprintf("127.0.0.1:%d", port)
@@ -155,7 +147,7 @@ func TestNewIngressCommandEffectiveLog(t *testing.T) {
 	assert.Equal(t, cfg.HTTPListen, listen)
 	assert.Equal(t, cfg.EnableProfiling, true)
 	assert.Equal(t, cfg.Kubernetes.Kubeconfig, "/foo/bar/baz")
-	assert.Equal(t, cfg.Kubernetes.ResyncInterval, types.TimeDuration{24 * time.Hour})
+	assert.Equal(t, cfg.Kubernetes.ResyncInterval, types.TimeDuration{Duration: 24 * time.Hour})
 	assert.Equal(t, cfg.APISIX.AdminKey, "0x123")
 	assert.Equal(t, cfg.APISIX.BaseURL, "http://apisixgw.default.cluster.local/apisix")
 }
diff --git a/pkg/apisix/cache/memdb_test.go b/pkg/apisix/cache/memdb_test.go
index 47e7914..5f2c9c7 100644
--- a/pkg/apisix/cache/memdb_test.go
+++ b/pkg/apisix/cache/memdb_test.go
@@ -37,6 +37,7 @@ func TestMemDBCacheRoute(t *testing.T) {
 	assert.Nil(t, c.InsertRoute(r1), "inserting route 1")
 
 	r, err := c.GetRoute("abc")
+	assert.Nil(t, err)
 	assert.Equal(t, r1, r)
 
 	r2 := &v1.Route{
@@ -57,6 +58,7 @@ func TestMemDBCacheRoute(t *testing.T) {
 	assert.Nil(t, c.InsertRoute(r3), "inserting route r3")
 
 	r, err = c.GetRoute("ghi")
+	assert.Nil(t, err)
 	assert.Equal(t, r3, r)
 
 	assert.Nil(t, c.DeleteRoute(r3), "delete route r3")
@@ -92,6 +94,7 @@ func TestMemDBCacheService(t *testing.T) {
 	assert.Nil(t, c.InsertService(s1), "inserting service 1")
 
 	s, err := c.GetService("abc")
+	assert.Nil(t, err)
 	assert.Equal(t, s1, s)
 
 	s2 := &v1.Service{
@@ -108,6 +111,7 @@ func TestMemDBCacheService(t *testing.T) {
 	assert.Nil(t, c.InsertService(s3), "inserting service 3")
 
 	s, err = c.GetService("ghi")
+	assert.Nil(t, err)
 	assert.Equal(t, s3, s)
 
 	assert.Nil(t, c.DeleteService(s3), "delete service 3")
@@ -140,6 +144,7 @@ func TestMemDBCacheSSL(t *testing.T) {
 	assert.Nil(t, c.InsertSSL(s1), "inserting ssl 1")
 
 	s, err := c.GetSSL("abc")
+	assert.Nil(t, err)
 	assert.Equal(t, s1, s)
 
 	s2 := &v1.Ssl{
@@ -154,6 +159,7 @@ func TestMemDBCacheSSL(t *testing.T) {
 	assert.Nil(t, c.InsertSSL(s3), "inserting ssl 3")
 
 	s, err = c.GetSSL("ghi")
+	assert.Nil(t, err)
 	assert.Equal(t, s3, s)
 
 	assert.Nil(t, c.DeleteSSL(s3), "delete ssl 3")
@@ -183,9 +189,11 @@ func TestMemDBCacheUpstream(t *testing.T) {
 			Name:     "abc",
 		},
 	}
-	assert.Nil(t, c.InsertUpstream(u1), "inserting upstream 1")
+	err = c.InsertUpstream(u1)
+	assert.Nil(t, err, "inserting upstream 1")
 
 	u, err := c.GetUpstream("abc")
+	assert.Nil(t, err)
 	assert.Equal(t, u1, u)
 
 	u2 := &v1.Upstream{
@@ -204,6 +212,7 @@ func TestMemDBCacheUpstream(t *testing.T) {
 	assert.Nil(t, c.InsertUpstream(u3), "inserting upstream 3")
 
 	u, err = c.GetUpstream("ghi")
+	assert.Nil(t, err)
 	assert.Equal(t, u3, u)
 
 	assert.Nil(t, c.DeleteUpstream(u3), "delete upstream 3")
diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go
index ae7e868..6c814f7 100644
--- a/pkg/config/config_test.go
+++ b/pkg/config/config_test.go
@@ -33,7 +33,7 @@ func TestNewConfigFromFile(t *testing.T) {
 		HTTPListen:      ":9090",
 		EnableProfiling: true,
 		Kubernetes: KubernetesConfig{
-			ResyncInterval:     types.TimeDuration{time.Hour},
+			ResyncInterval:     types.TimeDuration{Duration: time.Hour},
 			Kubeconfig:         "/path/to/foo/baz",
 			AppNamespaces:      []string{""},
 			ElectionID:         "my-election-id",
diff --git a/pkg/ingress/apisix/tls.go b/pkg/ingress/apisix/tls.go
index 856adae..7ef31fa 100644
--- a/pkg/ingress/apisix/tls.go
+++ b/pkg/ingress/apisix/tls.go
@@ -42,10 +42,8 @@ func (as *ApisixTLSCRD) Convert(sc Secreter) (*apisix.Ssl, error) {
 	cert := string(secret.Data["cert"])
 	key := string(secret.Data["key"])
 	status := 1
-	snis := make([]string, 0)
-	for _, host := range as.Spec.Hosts {
-		snis = append(snis, host)
-	}
+	var snis []string
+	snis = append(snis, as.Spec.Hosts...)
 	ssl := &apisix.Ssl{
 		ID:     id,
 		Snis:   snis,
diff --git a/pkg/ingress/controller/apisix_tls.go b/pkg/ingress/controller/apisix_tls.go
index 0ab3617..14a0131 100644
--- a/pkg/ingress/controller/apisix_tls.go
+++ b/pkg/ingress/controller/apisix_tls.go
@@ -175,7 +175,7 @@ func (c *ApisixTLSController) syncHandler(tqo *TlsQueueObj) error {
 func SyncSecretSSL(key string, ssl *v1.Ssl, operator string) {
 	ssls, ok := secretSSLMap.Load(key)
 	if ok {
-		sslMap := ssls.(sync.Map)
+		sslMap := ssls.(*sync.Map)
 		switch operator {
 		case state.Delete:
 			sslMap.Delete(ssl.ID)
@@ -186,7 +186,7 @@ func SyncSecretSSL(key string, ssl *v1.Ssl, operator string) {
 		}
 	} else {
 		if operator != state.Delete {
-			sslMap := sync.Map{}
+			sslMap := &sync.Map{}
 			sslMap.Store(ssl.ID, ssl)
 			secretSSLMap.Store(key, sslMap)
 		}
diff --git a/pkg/ingress/controller/controller.go b/pkg/ingress/controller/controller.go
index a449241..35d46f9 100644
--- a/pkg/ingress/controller/controller.go
+++ b/pkg/ingress/controller/controller.go
@@ -65,7 +65,6 @@ type Controller struct {
 	clientset          kubernetes.Interface
 	crdClientset       crdclientset.Interface
 	metricsCollector   metrics.Collector
-	crdController      *Api6Controller
 	crdInformerFactory externalversions.SharedInformerFactory
 
 	// common informers and listers
diff --git a/pkg/ingress/controller/ingress.go b/pkg/ingress/controller/ingress.go
index badc105..690e819 100644
--- a/pkg/ingress/controller/ingress.go
+++ b/pkg/ingress/controller/ingress.go
@@ -23,7 +23,6 @@ import (
 	"k8s.io/client-go/tools/cache"
 	"k8s.io/client-go/util/workqueue"
 
-	apisixcache "github.com/apache/apisix-ingress-controller/pkg/apisix/cache"
 	"github.com/apache/apisix-ingress-controller/pkg/config"
 	"github.com/apache/apisix-ingress-controller/pkg/kube"
 	"github.com/apache/apisix-ingress-controller/pkg/log"
@@ -175,54 +174,6 @@ func (c *ingressController) sync(ctx context.Context, ev *types.Event) error {
 	return c.controller.syncManifests(ctx, added, updated, deleted)
 }
 
-func (c *ingressController) syncToCluster(ctx context.Context, clusterName string, routes []*apisixv1.Route, upstreams []*apisixv1.Upstream, ev types.EventType) error {
-	// TODO throttle if the number of routes and upstreams are huge.
-	if ev == types.EventDelete {
-		for _, r := range routes {
-			if err := c.controller.apisix.Cluster(clusterName).Route().Delete(ctx, r); err != nil {
-				return err
-			}
-		}
-		for _, u := range upstreams {
-			if err := c.controller.apisix.Cluster(clusterName).Upstream().Delete(ctx, u); err != nil {
-				return err
-			}
-		}
-		return nil
-	}
-	for _, u := range upstreams {
-		old, err := c.controller.apisix.Cluster(clusterName).Upstream().Get(ctx, u.FullName)
-		if err != nil && err != apisixcache.ErrNotFound {
-			return err
-		}
-		if old == nil {
-			_, err = c.controller.apisix.Cluster(clusterName).Upstream().Create(ctx, u)
-		} else {
-			// TODO diff route.
-			_, err = c.controller.apisix.Cluster(clusterName).Upstream().Update(ctx, u)
-		}
-		if err != nil {
-			return err
-		}
-	}
-	for _, r := range routes {
-		old, err := c.controller.apisix.Cluster(clusterName).Route().Get(ctx, r.FullName)
-		if err != nil && err != apisixcache.ErrNotFound {
-			return err
-		}
-		if old == nil {
-			_, err = c.controller.apisix.Cluster(clusterName).Route().Create(ctx, r)
-		} else {
-			// TODO diff route.
-			_, err = c.controller.apisix.Cluster(clusterName).Route().Update(ctx, r)
-		}
-		if err != nil {
-			return err
-		}
-	}
-	return nil
-}
-
 func (c *ingressController) handleSyncErr(obj interface{}, err error) {
 	if err == nil {
 		c.workqueue.Forget(obj)
diff --git a/pkg/ingress/controller/secret.go b/pkg/ingress/controller/secret.go
index 1c271e2..3738c94 100644
--- a/pkg/ingress/controller/secret.go
+++ b/pkg/ingress/controller/secret.go
@@ -145,15 +145,11 @@ func (c *secretController) sync(ctx context.Context, ev *types.Event) error {
 	// FixMe Need to update the status of CRD ApisixTls
 	ssls, ok := secretSSLMap.Load(secretMapkey)
 	if ok {
-		sslMap := ssls.(sync.Map)
+		sslMap := ssls.(*sync.Map)
 		sslMap.Range(func(_, v interface{}) bool {
 			ssl := v.(*apisixv1.Ssl)
 			ssl.FullName = ssl.ID
-			err = state.SyncSsl(ssl, ev.Type.String())
-			if err != nil {
-				return false
-			}
-			return true
+			return state.SyncSsl(ssl, ev.Type.String()) == nil
 		})
 	}
 	return err
diff --git a/pkg/kube/ingress.go b/pkg/kube/ingress.go
index ac39470..ed43046 100644
--- a/pkg/kube/ingress.go
+++ b/pkg/kube/ingress.go
@@ -163,21 +163,21 @@ func (l *ingressLister) ExtensionsV1beta1(namespace, name string) (Ingress, erro
 // MustNewIngress creates a kube.Ingress object according to the
 // type of obj.
 func MustNewIngress(obj interface{}) Ingress {
-	switch obj.(type) {
+	switch ing := obj.(type) {
 	case *networkingv1.Ingress:
 		return &ingress{
 			groupVersion: IngressV1,
-			v1:           obj.(*networkingv1.Ingress),
+			v1:           ing,
 		}
 	case *networkingv1beta1.Ingress:
 		return &ingress{
 			groupVersion: IngressV1beta1,
-			v1beta1:      obj.(*networkingv1beta1.Ingress),
+			v1beta1:      ing,
 		}
 	case *extensionsv1beta1.Ingress:
 		return &ingress{
 			groupVersion:      IngressExtensionsV1beta1,
-			extensionsV1beta1: obj.(*extensionsv1beta1.Ingress),
+			extensionsV1beta1: ing,
 		}
 	default:
 		panic("invalid ingress type")
@@ -188,21 +188,21 @@ func MustNewIngress(obj interface{}) Ingress {
 // type of obj. It returns nil and the error reason when the
 // type assertion fails.
 func NewIngress(obj interface{}) (Ingress, error) {
-	switch obj.(type) {
+	switch ing := obj.(type) {
 	case *networkingv1.Ingress:
 		return &ingress{
 			groupVersion: IngressV1,
-			v1:           obj.(*networkingv1.Ingress),
+			v1:           ing,
 		}, nil
 	case *networkingv1beta1.Ingress:
 		return &ingress{
 			groupVersion: IngressV1beta1,
-			v1beta1:      obj.(*networkingv1beta1.Ingress),
+			v1beta1:      ing,
 		}, nil
 	case *extensionsv1beta1.Ingress:
 		return &ingress{
 			groupVersion:      IngressExtensionsV1beta1,
-			extensionsV1beta1: obj.(*extensionsv1beta1.Ingress),
+			extensionsV1beta1: ing,
 		}, nil
 	default:
 		return nil, errors.New("invalid ingress type")
diff --git a/pkg/kube/translation/apisix_upstream.go b/pkg/kube/translation/apisix_upstream.go
index 6b68495..f9ce843 100644
--- a/pkg/kube/translation/apisix_upstream.go
+++ b/pkg/kube/translation/apisix_upstream.go
@@ -181,7 +181,7 @@ func (t *translator) translateUpstreamActiveHealthCheck(config *configv1.ActiveH
 	active.HTTPPath = config.HTTPPath
 	active.HTTPRequestHeaders = config.RequestHeaders
 
-	if config.StrictTLS == nil || *config.StrictTLS == true {
+	if config.StrictTLS == nil || *config.StrictTLS {
 		active.HTTPSVerifyCert = true
 	}
 
diff --git a/pkg/log/options.go b/pkg/log/options.go
index 695f799..15c88bd 100644
--- a/pkg/log/options.go
+++ b/pkg/log/options.go
@@ -35,7 +35,6 @@ type options struct {
 	writeSyncer zapcore.WriteSyncer
 	outputFile  string
 	logLevel    string
-	context     string
 }
 
 // WithLogLevel sets the log level.
diff --git a/pkg/types/apisix/v1/types.go b/pkg/types/apisix/v1/types.go
index bf58da5..3650a0b 100644
--- a/pkg/types/apisix/v1/types.go
+++ b/pkg/types/apisix/v1/types.go
@@ -166,7 +166,7 @@ type Upstream struct {
 	Metadata `json:",inline" yaml:",inline"`
 
 	Type     string               `json:"type,omitempty" yaml:"type,omitempty"`
-	HashOn   string               `json:"hash_on,omitemtpy" yaml:"hash_on,omitempty"`
+	HashOn   string               `json:"hash_on,omitempty" yaml:"hash_on,omitempty"`
 	Key      string               `json:"key,omitempty" yaml:"key,omitempty"`
 	Checks   *UpstreamHealthCheck `json:"checks,omitempty" yaml:"checks,omitempty"`
 	Nodes    []UpstreamNode       `json:"nodes,omitempty" yaml:"nodes,omitempty"`
diff --git a/pkg/types/timeduration.go b/pkg/types/timeduration.go
index 41127fa..2000692 100644
--- a/pkg/types/timeduration.go
+++ b/pkg/types/timeduration.go
@@ -38,11 +38,11 @@ func (d *TimeDuration) UnmarshalJSON(data []byte) error {
 	if err := json.Unmarshal(data, &value); err != nil {
 		return err
 	}
-	switch value.(type) {
+	switch v := value.(type) {
 	case float64:
-		d.Duration = time.Duration(value.(float64))
+		d.Duration = time.Duration(v)
 	case string:
-		dur, err := time.ParseDuration(value.(string))
+		dur, err := time.ParseDuration(v)
 		if err != nil {
 			return err
 		}