Skip to content

Commit

Permalink
Run golang-ci lint for all the modules (#2081)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome authored Jun 4, 2024
1 parent fc2ed96 commit 212c0f6
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 44 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ jobs:
lint:
name: Lint
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
directory: [., tests] # we need to run golangci-lint for every module https://github.com/golangci/golangci-lint/issues/828
steps:
- name: Checkout Repository
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
Expand All @@ -36,6 +40,7 @@ jobs:
uses: golangci/golangci-lint-action@a4f60bb28d35aeee14e6880718e0c85ff1882e64 # v6.0.1
with:
args: --timeout 10m0s
working-directory: ${{ matrix.directory }}

njs-lint:
name: NJS Lint
Expand Down
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ linters-settings:
- name: error-strings
- name: errorf
- name: exported
- name: if-return
- name: increment-decrement
- name: indent-error-flow
- name: package-comments
Expand Down
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ repos:
rev: v1.59.0
hooks:
- id: golangci-lint-full
name: golangci-lint-root
alias: golangci-lint-root

- id: golangci-lint-full
name: golangci-lint-tests
alias: golangci-lint-tests
entry: bash -c 'cd tests && golangci-lint run --fix --config $OLDPWD/.golangci.yml'

# Rules are in .markdownlint-cli2.yaml file
# See https://github.com/DavidAnson/markdownlint/blob/main/doc/Rules.md for rule descriptions
Expand Down
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ GO_LINKER_FlAGS_VARS = -X main.version=${VERSION} -X main.commit=${GIT_COMMIT} -
GO_LINKER_FLAGS_OPTIMIZATIONS = -s -w
GO_LINKER_FLAGS = $(GO_LINKER_FLAGS_OPTIMIZATIONS) $(GO_LINKER_FlAGS_VARS)

# tools versions
GOLANGCI_LINT_VERSION := $(shell awk '/repo:.*golangci-lint/{getline; if ($$1 == "rev:") {sub(/^v/, "", $$2); print $$2}}' $(SELF_DIR).pre-commit-config.yaml)

# variables that can be overridden by the user
PREFIX ?= nginx-gateway-fabric## The name of the NGF image. For example, nginx-gateway-fabric
NGINX_PREFIX ?= $(PREFIX)/nginx## The name of the nginx image. For example: nginx-gateway-fabric/nginx
Expand Down Expand Up @@ -168,9 +171,14 @@ njs-fmt: ## Run prettier against the njs httpmatches module
vet: ## Run go vet against code
go vet ./...

.PHONY: check-golangci-lint
check-golangci-lint:
@golangci-lint --version || (code=$$?; printf "\033[0;31mError\033[0m: there was a problem with golangci-lint. Follow the docs to install it https://golangci-lint.run/welcome/install/\n"; exit $$code)
@golangci-lint --version | grep -q $(GOLANGCI_LINT_VERSION) || (printf "\033[0;33mWarning\033[0m: your golangci-lint version is different from the one specified in .pre-commit-config.yaml. The recommended version is $(GOLANGCI_LINT_VERSION)\n")

.PHONY: lint
lint: ## Run golangci-lint against code
docker run --pull always --rm -v $(CURDIR):/nginx-gateway-fabric -w /nginx-gateway-fabric -v $(shell go env GOCACHE):/cache/go -e GOCACHE=/cache/go -e GOLANGCI_LINT_CACHE=/cache/go -v $(shell go env GOPATH)/pkg:/go/pkg golangci/golangci-lint:latest golangci-lint --color always run
lint: check-golangci-lint ## Run golangci-lint against code
golangci-lint run

.PHONY: unit-test
unit-test: ## Run unit tests for the go code
Expand Down
16 changes: 8 additions & 8 deletions internal/mode/static/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ func (h *eventHandlerImpl) updateStatuses(ctx context.Context, logger logr.Logge
func (h *eventHandlerImpl) parseAndCaptureEvent(ctx context.Context, logger logr.Logger, event interface{}) {
switch e := event.(type) {
case *events.UpsertEvent:
filterKey := objectFilterKey(e.Resource, client.ObjectKeyFromObject(e.Resource))
upFilterKey := objectFilterKey(e.Resource, client.ObjectKeyFromObject(e.Resource))

if filter, ok := h.objectFilters[filterKey]; ok {
if filter, ok := h.objectFilters[upFilterKey]; ok {
filter.upsert(ctx, logger, e.Resource)
if !filter.captureChangeInGraph {
return
Expand All @@ -293,9 +293,9 @@ func (h *eventHandlerImpl) parseAndCaptureEvent(ctx context.Context, logger logr

h.cfg.processor.CaptureUpsertChange(e.Resource)
case *events.DeleteEvent:
filterKey := objectFilterKey(e.Type, e.NamespacedName)
delFilterKey := objectFilterKey(e.Type, e.NamespacedName)

if filter, ok := h.objectFilters[filterKey]; ok {
if filter, ok := h.objectFilters[delFilterKey]; ok {
filter.delete(ctx, logger, e.NamespacedName)
if !filter.captureChangeInGraph {
return
Expand Down Expand Up @@ -359,14 +359,14 @@ func (h *eventHandlerImpl) updateUpstreamServers(
}

for _, u := range conf.Upstreams {
upstream := upstream{
confUpstream := upstream{
name: u.Name,
servers: ngxConfig.ConvertEndpoints(u.Endpoints),
}

if u, ok := prevUpstreams[upstream.name]; ok {
if !serversEqual(upstream.servers, u.Peers) {
upstreams = append(upstreams, upstream)
if u, ok := prevUpstreams[confUpstream.name]; ok {
if !serversEqual(confUpstream.servers, u.Peers) {
upstreams = append(upstreams, confUpstream)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ GW_API_VERSION ?= $(shell sed -n 's/.*ref=v\(.*\)/\1/p' ../config/crd/gateway-ap
GW_API_PREV_VERSION ?= 1.0.0## Supported Gateway API version from previous NGF release
GW_SERVICE_TYPE = NodePort## Service type to use for the gateway
GW_SVC_GKE_INTERNAL = false
K8S_VERSION ?= latest## Kubernetes version to use. Expected format: 1.24 (major.minor) or latest
NGF_VERSION ?= $(shell git describe --tags $(shell git rev-list --tags --max-count=1))## NGF version to be tested (defaults to latest tag)
PULL_POLICY = Never## Pull policy for the images
PROVISIONER_MANIFEST = conformance/provisioner/provisioner.yaml
Expand Down Expand Up @@ -114,7 +113,7 @@ stop-longevity-test: nfr-test ## Stop the longevity test and collects results
--label-filter "nfr" $(GINKGO_FLAGS) ./suite -- --gateway-api-version=$(GW_API_VERSION) \
--gateway-api-prev-version=$(GW_API_PREV_VERSION) --image-tag=$(TAG) --version-under-test=$(NGF_VERSION) \
--plus-enabled=$(PLUS_ENABLED) --ngf-image-repo=$(PREFIX) --nginx-image-repo=$(NGINX_PREFIX) --nginx-plus-image-repo=$(NGINX_PLUS_PREFIX) \
--pull-policy=$(PULL_POLICY) --k8s-version=$(K8S_VERSION) --service-type=$(GW_SERVICE_TYPE) \
--pull-policy=$(PULL_POLICY) --service-type=$(GW_SERVICE_TYPE) \
--is-gke-internal-lb=$(GW_SVC_GKE_INTERNAL)

.PHONY: test
Expand All @@ -124,7 +123,7 @@ test: ## Runs the functional tests on your default k8s cluster
--gateway-api-version=$(GW_API_VERSION) --gateway-api-prev-version=$(GW_API_PREV_VERSION) \
--image-tag=$(TAG) --version-under-test=$(NGF_VERSION) \
--plus-enabled=$(PLUS_ENABLED) --ngf-image-repo=$(PREFIX) --nginx-image-repo=$(NGINX_PREFIX) --nginx-plus-image-repo=$(NGINX_PLUS_PREFIX) \
--pull-policy=$(PULL_POLICY) --k8s-version=$(K8S_VERSION) --service-type=$(GW_SERVICE_TYPE) \
--pull-policy=$(PULL_POLICY) --service-type=$(GW_SERVICE_TYPE) \
--is-gke-internal-lb=$(GW_SVC_GKE_INTERNAL)

.PHONY: test-with-plus
Expand Down
2 changes: 1 addition & 1 deletion tests/framework/ngf.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func InstallGatewayAPI(apiVersion string) ([]byte, error) {
}

// UninstallGatewayAPI uninstalls the specified version of the Gateway API resources.
func UninstallGatewayAPI(apiVersion, k8sVersion string) ([]byte, error) {
func UninstallGatewayAPI(apiVersion string) ([]byte, error) {
apiPath := fmt.Sprintf("%s/v%s/standard-install.yaml", gwInstallBasePath, apiVersion)

output, err := exec.Command("kubectl", "delete", "-f", apiPath).CombinedOutput()
Expand Down
3 changes: 1 addition & 2 deletions tests/framework/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package framework
import (
"bytes"
"fmt"
"log/slog"
"net/http"
"net/url"
"path"
"time"

"log/slog"

"k8s.io/client-go/rest"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
Expand Down
12 changes: 7 additions & 5 deletions tests/framework/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func InstallPrometheus(

scrapeInterval := fmt.Sprintf("%ds", int(cfg.ScrapeInterval.Seconds()))

// nolint:gosec
output, err = exec.Command(
"helm",
"install",
Expand Down Expand Up @@ -134,13 +135,12 @@ const (

// PrometheusInstance represents a Prometheus instance in the cluster.
type PrometheusInstance struct {
apiClient v1.API
podIP string
podName string
podNamespace string
portForward bool
queryTimeout time.Duration

apiClient v1.API
portForward bool
}

// PortForward starts port forwarding to the Prometheus instance.
Expand All @@ -165,7 +165,7 @@ func (ins *PrometheusInstance) getAPIClient() (v1.API, error) {
}

cfg := api.Config{
Address: fmt.Sprintf("%s", endpoint),
Address: endpoint,
}

c, err := api.NewClient(cfg)
Expand Down Expand Up @@ -227,7 +227,9 @@ func (ins *PrometheusInstance) QueryRange(query string, promRange v1.Range) (mod
}

// QueryRangeWithCtx sends a range query to Prometheus with the specified context.
func (ins *PrometheusInstance) QueryRangeWithCtx(ctx context.Context, query string, promRange v1.Range) (model.Value, error) {
func (ins *PrometheusInstance) QueryRangeWithCtx(ctx context.Context,
query string, promRange v1.Range,
) (model.Value, error) {
if err := ins.ensureAPIClient(); err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions tests/framework/resourcemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (rm *ResourceManager) WaitForAppsToBeReady(namespace string) error {
}

// WaitForAppsToBeReadyWithCtx waits for all apps in the specified namespace to be ready or
// until the provided context is cancelled.
// until the provided context is canceled.
func (rm *ResourceManager) WaitForAppsToBeReadyWithCtx(ctx context.Context, namespace string) error {
if err := rm.WaitForPodsToBeReady(ctx, namespace); err != nil {
return err
Expand All @@ -325,7 +325,7 @@ func (rm *ResourceManager) WaitForAppsToBeReadyWithCtx(ctx context.Context, name
}

// WaitForPodsToBeReady waits for all Pods in the specified namespace to be ready or
// until the provided context is cancelled.
// until the provided context is canceled.
func (rm *ResourceManager) WaitForPodsToBeReady(ctx context.Context, namespace string) error {
return wait.PollUntilContextCancel(
ctx,
Expand Down
7 changes: 4 additions & 3 deletions tests/suite/graceful_recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ func checkContainerRestart(ngfPodName, containerName string, currentRestartCount
}

if restartCount != currentRestartCount+1 {
return fmt.Errorf("expected current restart count: %d to match incremented restart count: %d", restartCount, currentRestartCount+1)
return fmt.Errorf("expected current restart count: %d to match incremented restart count: %d",
restartCount, currentRestartCount+1)
}

return nil
Expand Down Expand Up @@ -314,7 +315,7 @@ func getContainerRestartCount(ngfPodName, containerName string) (int, error) {

var ngfPod core.Pod
if err := k8sClient.Get(ctx, types.NamespacedName{Namespace: ngfNamespace, Name: ngfPodName}, &ngfPod); err != nil {
return 0, fmt.Errorf("error retriving NGF Pod: %w", err)
return 0, fmt.Errorf("error retrieving NGF Pod: %w", err)
}

var restartCount int
Expand All @@ -333,7 +334,7 @@ func runNodeDebuggerJob(ngfPodName, jobScript string) (*v1.Job, error) {

var ngfPod core.Pod
if err := k8sClient.Get(ctx, types.NamespacedName{Namespace: ngfNamespace, Name: ngfPodName}, &ngfPod); err != nil {
return nil, fmt.Errorf("error retriving NGF Pod: %w", err)
return nil, fmt.Errorf("error retrieving NGF Pod: %w", err)
}

b, err := resourceManager.GetFileContents("graceful-recovery/node-debugger-job.yaml")
Expand Down
26 changes: 11 additions & 15 deletions tests/suite/scale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ var _ = Describe("Scale test", Ordered, Label("nfr", "scale"), func() {
const testName = "TestScale_Listeners"

testResultsDir := filepath.Join(resultsDir, testName)
Expect(os.MkdirAll(testResultsDir, 0755)).To(Succeed())
Expect(os.MkdirAll(testResultsDir, 0o755)).To(Succeed())

objects, err := framework.GenerateScaleListenerObjects(httpListenerCount, false /*non-tls*/)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -734,22 +734,18 @@ type bucket struct {
}

type scaleTestResults struct {
Name string

ReloadCount int
ReloadErrsCount int
ReloadAvgTime int
ReloadBuckets []bucket

EventsCount int
EventsAvgTime int
EventsBuckets []bucket

NGFErrors int
NginxErrors int

Name string
EventsBuckets []bucket
ReloadBuckets []bucket
EventsAvgTime int
EventsCount int
NGFContainerRestarts int
NGFErrors int
NginxContainerRestarts int
NginxErrors int
ReloadAvgTime int
ReloadCount int
ReloadErrsCount int
}

const scaleResultTemplate = `
Expand Down
3 changes: 1 addition & 2 deletions tests/suite/system_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ var (
gatewayAPIPrevVersion = flag.String(
"gateway-api-prev-version", "", "Supported Gateway API version for previous NGF release",
)
k8sVersion = flag.String("k8s-version", "latest", "Version of k8s being tested on")
// Configurable NGF installation variables. Helm values will be used as defaults if not specified.
ngfImageRepository = flag.String("ngf-image-repo", "", "Image repo for NGF control plane")
nginxImageRepository = flag.String("nginx-image-repo", "", "Image repo for NGF data plane")
Expand Down Expand Up @@ -214,7 +213,7 @@ func teardown(relName string) {
output, err := framework.UninstallNGF(cfg, k8sClient)
Expect(err).ToNot(HaveOccurred(), string(output))

output, err = framework.UninstallGatewayAPI(*gatewayAPIVersion, *k8sVersion)
output, err = framework.UninstallGatewayAPI(*gatewayAPIVersion)
Expect(err).ToNot(HaveOccurred(), string(output))

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
Expand Down

0 comments on commit 212c0f6

Please sign in to comment.