Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 33 additions & 26 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
version: "2"
run:
timeout: 5m
allow-parallel-runners: true

issues:
# don't skip warning about doc comments
# don't exclude the default set of lint
exclude-use-default: false
# restore some of the defaults
# (fill in the rest as needed)
exclude-rules:
- path: "api/*"
linters:
- lll
- path: "internal/*"
linters:
- dupl
- lll
linters:
disable-all: true
default: none
enable:
- dupl
- errcheck
- exportloopref
- ginkgolinter
- goconst
- gocyclo
- gofmt
- goimports
- gosimple
- govet
- ineffassign
- lll
Expand All @@ -36,12 +17,38 @@ linters:
- prealloc
- revive
- staticcheck
- typecheck
- unconvert
- unparam
- unused

linters-settings:
revive:
settings:
revive:
rules:
- name: comment-spacings
exclusions:
generated: lax
rules:
- name: comment-spacings
- linters:
- lll
path: api/*
- linters:
- dupl
- lll
path: internal/*
- linters:
- staticcheck
text: "ST1001:"
path: test/.*
paths:
- third_party$
- builtin$
- examples$
formatters:
enable:
- gofmt
- goimports
exclusions:
generated: lax
paths:
- third_party$
- builtin$
- examples$
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
KUSTOMIZE_VERSION ?= v5.4.2
CONTROLLER_TOOLS_VERSION ?= v0.15.0
ENVTEST_VERSION ?= release-0.18
GOLANGCI_LINT_VERSION ?= v1.59.1
GOLANGCI_LINT_VERSION ?= v2.1.5

.PHONY: kustomize
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
Expand All @@ -305,7 +305,7 @@ $(ENVTEST): $(LOCALBIN)
.PHONY: golangci-lint
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
$(GOLANGCI_LINT): $(LOCALBIN)
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | sh -s $(GOLANGCI_LINT_VERSION)

gofmt: ## Apply go fmt
@gofmt -w -r 'interface{} -> any' .
Expand Down
4 changes: 2 additions & 2 deletions api/dashboard/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,9 +730,9 @@ func ComposeConsumerName(namespace, name string) string {
buf := bytes.NewBuffer(p)

// TODO If APISIX modifies the consumer name schema, we can drop this.
buf.WriteString(strings.Replace(namespace, "-", "_", -1))
buf.WriteString(strings.ReplaceAll(namespace, "-", "_"))
buf.WriteString("_")
buf.WriteString(strings.Replace(name, "-", "_", -1))
buf.WriteString(strings.ReplaceAll(name, "-", "_"))

return buf.String()
}
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/consumer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (r *ConsumerReconciler) checkGatewayRef(object client.Object) bool {
return false
}
gatewayClass := &gatewayv1.GatewayClass{}
if err := r.Client.Get(context.Background(), client.ObjectKey{Name: string(gateway.Spec.GatewayClassName)}, gatewayClass); err != nil {
if err := r.Get(context.Background(), client.ObjectKey{Name: string(gateway.Spec.GatewayClassName)}, gatewayClass); err != nil {
r.Log.Error(err, "failed to get gateway class", "gateway", gateway.GetName(), "gatewayclass", gateway.Spec.GatewayClassName)
return false
}
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (r *GatewayReconciler) listGatewayForGatewayClass(ctx context.Context, gate
func (r *GatewayReconciler) checkGatewayClass(obj client.Object) bool {
gateway := obj.(*gatewayv1.Gateway)
gatewayClass := &gatewayv1.GatewayClass{}
if err := r.Client.Get(context.Background(), client.ObjectKey{Name: string(gateway.Spec.GatewayClassName)}, gatewayClass); err != nil {
if err := r.Get(context.Background(), client.ObjectKey{Name: string(gateway.Spec.GatewayClassName)}, gatewayClass); err != nil {
r.Log.Error(err, "failed to get gateway class", "gateway", gateway.GetName(), "gatewayclass", gateway.Spec.GatewayClassName)
return false
}
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/ingress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (r *IngressReconciler) getIngressClass(obj client.Object) (*networkingv1.In

// if it does not match, check if the ingress class is controlled by us
ingressClass := networkingv1.IngressClass{}
if err := r.Client.Get(context.Background(), client.ObjectKey{Name: *ingress.Spec.IngressClassName}, &ingressClass); err != nil {
if err := r.Get(context.Background(), client.ObjectKey{Name: *ingress.Spec.IngressClassName}, &ingressClass); err != nil {
return nil, err
}

Expand Down
3 changes: 1 addition & 2 deletions internal/provider/adc/translator/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package translator
import (
"encoding/json"

"github.com/api7/api7-ingress-controller/api/adc"
adctypes "github.com/api7/api7-ingress-controller/api/adc"
"github.com/api7/api7-ingress-controller/api/v1alpha1"
"github.com/api7/api7-ingress-controller/internal/provider"
Expand All @@ -22,7 +21,7 @@ func (t *Translator) TranslateConsumerV1alpha1(tctx *provider.TranslateContext,
}
credentials := make([]adctypes.Credential, 0, len(consumerV.Spec.Credentials))
for _, credentialSpec := range consumerV.Spec.Credentials {
credential := adc.Credential{}
credential := adctypes.Credential{}
credential.Name = credentialSpec.Name
credential.Type = credentialSpec.Type
if credentialSpec.SecretRef != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/adc/translator/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ func (t *Translator) fillPluginFromHTTPRequestMirrorFilter(plugins adctypes.Plug
}

var (
port int = 80
ns string = namespace
port = 80
ns = namespace
)
if reqMirror.BackendRef.Port != nil {
port = int(*reqMirror.BackendRef.Port)
Expand Down
5 changes: 3 additions & 2 deletions internal/provider/adc/translator/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ func (t *Translator) TranslateIngress(tctx *provider.TranslateContext, obj *netw

uris := []string{path.Path}
if path.PathType != nil {
if *path.PathType == networkingv1.PathTypePrefix {
switch *path.PathType {
case networkingv1.PathTypePrefix:
// As per the specification of Ingress path matching rule:
// if the last element of the path is a substring of the
// last element in request path, it is not a match, e.g. /foo/bar
Expand All @@ -188,7 +189,7 @@ func (t *Translator) TranslateIngress(tctx *provider.TranslateContext, obj *netw
prefix += "/*"
}
uris = append(uris, prefix)
} else if *path.PathType == networkingv1.PathTypeImplementationSpecific {
case networkingv1.PathTypeImplementationSpecific:
uris = []string{"/*"}
}
}
Expand Down
12 changes: 5 additions & 7 deletions internal/provider/adc/translator/policies.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package translator

import (
"github.com/api7/api7-ingress-controller/api/adc"
adctypes "github.com/api7/api7-ingress-controller/api/adc"
"github.com/api7/api7-ingress-controller/api/v1alpha1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"

adctypes "github.com/api7/api7-ingress-controller/api/adc"
)

func convertBackendRef(namespace, name, kind string) gatewayv1.BackendRef {
Expand Down Expand Up @@ -53,13 +51,13 @@ func (t *Translator) attachBackendTrafficPolicyToUpstream(policy *v1alpha1.Backe
}
if policy.Spec.Timeout != nil {
upstream.Timeout = &adctypes.Timeout{
Connect: int(policy.Spec.Timeout.Connect.Duration.Seconds()),
Read: int(policy.Spec.Timeout.Read.Duration.Seconds()),
Send: int(policy.Spec.Timeout.Send.Duration.Seconds()),
Connect: int(policy.Spec.Timeout.Connect.Seconds()),
Read: int(policy.Spec.Timeout.Read.Seconds()),
Send: int(policy.Spec.Timeout.Send.Seconds()),
}
}
if policy.Spec.LoadBalancer != nil {
upstream.Type = adc.UpstreamType(policy.Spec.LoadBalancer.Type)
upstream.Type = adctypes.UpstreamType(policy.Spec.LoadBalancer.Type)
upstream.HashOn = policy.Spec.LoadBalancer.HashOn
upstream.Key = policy.Spec.LoadBalancer.Key
}
Expand Down
12 changes: 6 additions & 6 deletions internal/provider/controlplane/translator/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ func (t *Translator) fillPluginFromHTTPRequestMirrorFilter(plugins v1.Plugins, n
}

var (
port int = 80
ns string = namespace
port = 80
ns = namespace
)
if reqMirror.BackendRef.Port != nil {
port = int(*reqMirror.BackendRef.Port)
Expand Down Expand Up @@ -273,8 +273,8 @@ func (t *Translator) TranslateHTTPRoute(tctx *provider.TranslateContext, httpRou
backend.Namespace = &namespace
}
upstream := t.translateBackendRef(tctx, backend.BackendRef)
upstream.Labels["name"] = string(backend.BackendRef.Name)
upstream.Labels["namespace"] = string(*backend.BackendRef.Namespace)
upstream.Labels["name"] = string(backend.Name)
upstream.Labels["namespace"] = string(*backend.Namespace)
upstreams = append(upstreams, upstream)
if len(upstream.Nodes) == 0 {
upstream.Nodes = v1.UpstreamNodes{
Expand Down Expand Up @@ -389,7 +389,7 @@ func (t *Translator) translateGatewayHTTPRouteMatch(match *gatewayv1.HTTPRouteMa
}
}

if match.Headers != nil && len(match.Headers) > 0 {
if len(match.Headers) > 0 {
for _, header := range match.Headers {
name := strings.ToLower(string(header.Name))
name = strings.ReplaceAll(name, "-", "_")
Expand Down Expand Up @@ -420,7 +420,7 @@ func (t *Translator) translateGatewayHTTPRouteMatch(match *gatewayv1.HTTPRouteMa
}
}

if match.QueryParams != nil && len(match.QueryParams) > 0 {
if len(match.QueryParams) > 0 {
for _, query := range match.QueryParams {
var this []v1.StringOrSlice
this = append(this, v1.StringOrSlice{
Expand Down
4 changes: 2 additions & 2 deletions internal/types/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type TimeDuration struct {
}

func (d *TimeDuration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.Duration.String())
return json.Marshal(d.String())
}

func (d *TimeDuration) UnmarshalJSON(data []byte) error {
Expand All @@ -55,7 +55,7 @@ func (d *TimeDuration) UnmarshalJSON(data []byte) error {
}

func (d *TimeDuration) MarshalYAML() (any, error) {
return d.Duration.String(), nil
return d.String(), nil
}

func (d *TimeDuration) UnmarshalYAML(unmarshal func(any) error) error {
Expand Down
14 changes: 9 additions & 5 deletions pkg/dashboard/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var (
// ErrRouteNotFound means the [route, ssl, upstream] was not found.
ErrNotFound = cache.ErrNotFound

_errReadOnClosedResBody = errors.New("http: read on closed response body")
errReadOnClosedResBody = errors.New("http: read on closed response body")
)

// ClusterOptions contains parameters to customize APISIX client.
Expand Down Expand Up @@ -128,10 +128,14 @@ func newCluster(ctx context.Context, o *ClusterOptions) (Cluster, error) {
if err != nil {
return nil, err
}
if u.Port() == "" {
if u.Scheme == "http" {

switch u.Scheme {
case "http":
if u.Port() == "" {
u.Host = u.Host + ":80"
} else if u.Scheme == "https" {
}
case "https":
if u.Port() == "" {
u.Host = u.Host + ":443"
}
}
Expand Down Expand Up @@ -698,7 +702,7 @@ func (c *cluster) deleteResource(ctx context.Context, url, resource string) erro
func drainBody(r io.ReadCloser, url string) {
_, err := io.Copy(io.Discard, r)
if err != nil {
if err.Error() != _errReadOnClosedResBody.Error() {
if err.Error() != errReadOnClosedResBody.Error() {
log.Warnw("failed to drain body (read)",
zap.String("url", url),
zap.Error(err),
Expand Down
2 changes: 1 addition & 1 deletion pkg/dashboard/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (srv *fakeAPISIXConsumerSrv) ServeHTTP(w http.ResponseWriter, r *http.Reque
Value: srv.consumer[key],
},
}
resp.fakeGetCreateItem.Value = srv.consumer[key]
resp.Value = srv.consumer[key]
w.WriteHeader(http.StatusOK)
data, _ := json.Marshal(resp)
_, _ = w.Write(data)
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/adminapi/dashboard_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"time"

"github.com/api7/api7-ingress-controller/test/e2e/scaffold"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/ginkgo/v2" //nolint:staticcheck
. "github.com/onsi/gomega" //nolint:staticcheck

v1 "github.com/api7/api7-ingress-controller/api/dashboard/v1"
)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/framework/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func (f *Framework) deploy() {
install.Namespace = f.kubectlOpts.Namespace
install.ReleaseName = "api7ee3"

chartPath, err := install.ChartPathOptions.LocateChart("api7/api7ee3", cli.New())
chartPath, err := install.LocateChart("api7/api7ee3", cli.New())
f.GomegaT.Expect(err).ShouldNot(HaveOccurred(), "locate helm chart")

chart, err := loader.Load(chartPath)
Expand Down
1 change: 1 addition & 0 deletions test/e2e/framework/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
mysqlDSN = "mysql://root:changeme@tcp(mysql:3306)/api7ee"
)

//nolint:unused
func getDSN() string {
switch _db {
case postgres:
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/gruntwork-io/terratest/modules/logger"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/ginkgo/v2" //nolint:staticcheck
. "github.com/onsi/gomega" //nolint:staticcheck
clientv3 "go.etcd.io/etcd/client/v3"
"gorm.io/gorm"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/framework/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/Masterminds/sprig/v3"
"github.com/gruntwork-io/terratest/modules/k8s"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega" //nolint:staticcheck
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/framework/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/Masterminds/sprig/v3"
"github.com/gruntwork-io/terratest/modules/k8s"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega" //nolint:staticcheck
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down
Loading
Loading