Skip to content

Commit

Permalink
Add Golangci lint, replace statik with embed and test swagger (#566)
Browse files Browse the repository at this point in the history
* use Github workflow testing/linting instead of Docker

* replace statik with embed

* fail build if swagger is changed

* install swagger

* Add comment

* add warning on breaking change to api

* create a bootstrap target to ensure toolchain

* create a bootstrap target to ensure toolchain

* remove statik from gitignore

---------

Co-authored-by: Richard Hagen <richard.hagen@bouvet.no>
  • Loading branch information
Richard87 and Richard87 authored Dec 15, 2023
1 parent 9680de1 commit e246b82
Show file tree
Hide file tree
Showing 24 changed files with 159 additions and 100 deletions.
80 changes: 80 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: Pull-Request Testing
on:
pull_request:
workflow_dispatch:

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: Install dependencies
run: go mod download
- name: Install GolangCI Lint
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2
- name: Install StaticCheck
run: go install honnef.co/go/tools/cmd/staticcheck@2023.1.6

- name: golangci-lint
run: golangci-lint run --timeout=30m --max-same-issues=0 --out-format=github-actions --new

# Add legacy checks since golangci-lint only tests changed files for now
- name: StaticCheck
run: staticcheck -f text `go list ./... | grep -v "pkg/client"`
- name: Go Vet
run: go vet `go list ./... | grep -v "pkg/client"`
test:
name: Unit Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: Install dependencies
run: go mod download
- name: Run Tests
run: go test -cover `go list ./... | grep -v 'pkg/client'`

test-swagger:
name: Test Swagger
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: Install dependencies
run: go mod download
- name: Install Swagger
run: go install github.com/go-swagger/go-swagger/cmd/swagger@v0.30.5
- name: Generate Swagger
run: swagger generate spec -o ./swagger.json --scan-models --exclude-deps
- name: Validate no changes
run: diff swagger.json ./swaggerui/html/swagger.json
- name: Check breaking changes
if: always()
id: breaking
continue-on-error: true
run: swagger diff --break <(git show master:swaggerui/html/swagger.json) swagger.json > /tmp/swagger_breaking_changes.txt
- name: Add comment
if: failure() && steps.breaking.outcome == 'failure'
uses: mshick/add-pr-comment@v2
with:
message-id: breaking-comment
preformatted: true
message-path: /tmp/swagger_breaking_changes.txt

build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build docker image
run: docker build .
15 changes: 0 additions & 15 deletions .github/workflows/radix-api-pr.yml

This file was deleted.

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ bin/*
vendor

debug
swaggerui/statik.go

__debug_bin
.dccache
Expand Down
14 changes: 0 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ RUN apk update && \
apk add bash jq alpine-sdk sed gawk git ca-certificates curl && \
apk add --no-cache gcc musl-dev

RUN go install honnef.co/go/tools/cmd/staticcheck@2023.1.3 && \
go install github.com/rakyll/statik@v0.1.7 && \
go install github.com/go-swagger/go-swagger/cmd/swagger@v0.30.5

WORKDIR /go/src/github.com/equinor/radix-api/

# get dependencies
Expand All @@ -18,16 +14,6 @@ RUN go mod download
# copy api code
COPY . .

# Generate swagger
RUN swagger generate spec -o ./swaggerui_src/swagger.json --scan-models --exclude-deps && \
swagger validate ./swaggerui_src/swagger.json && \
statik -src=./swaggerui_src/ -p swaggerui

# lint and unit tests
RUN staticcheck ./... && \
go vet ./... && \
CGO_ENABLED=0 GOOS=linux go test ./...

# Build radix api go project
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-s -w" -a -installsuffix cgo -o /usr/local/bin/radix-api

Expand Down
64 changes: 28 additions & 36 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ CX_ARCHS = amd64
.PHONY: build
build: $(BINS)

.PHONY: test
test:
go test -cover `go list ./...`

mocks:
.PHONY: mocks
mocks: bootstrap
mockgen -source ./api/buildstatus/models/buildstatus.go -destination ./api/test/mock/buildstatus_mock.go -package mock
mockgen -source ./api/deployments/deployment_handler.go -destination ./api/deployments/mock/deployment_handler_mock.go -package mock
mockgen -source ./api/environments/job_handler.go -destination ./api/environments/mock/job_handler_mock.go -package mock
Expand All @@ -31,21 +28,29 @@ mocks:
mockgen -source ./api/utils/jobscheduler/interface.go -destination ./api/utils/jobscheduler/mock/job_scheduler_factory_mock.go -package mock
mockgen -source ./api/events/event_handler.go -destination ./api/events/mock/event_handler_mock.go -package mock

.PHONY: test
test:
go test -cover `go list ./...`

.PHONY: lint
lint: bootstrap
golangci-lint run --max-same-issues 0 --new
staticcheck ./...

build-kaniko:
docker run --rm -it -v $(CURRENT_FOLDER):/workspace gcr.io/kaniko-project/executor:latest --destination=$(DOCKER_REGISTRY)/radix-api-server:3hv6o --snapshotMode=time --cache=true

# This make command is only needed for local testing now
# we also do make swagger inside Dockerfile
.PHONY: swagger
swagger:
rm -f ./swaggerui_src/swagger.json ./swaggerui/statik.go
swagger: SHELL:=/bin/bash
swagger: bootstrap
swagger generate spec -o ./swagger.json --scan-models --exclude-deps
swagger validate ./swagger.json
mv swagger.json ./swaggerui_src/swagger.json
statik -src=./swaggerui_src/ -p swaggerui
mv swagger.json ./swaggerui/html/swagger.json

.PHONY: $(BINS)
$(BINS): vendor
$(BINS): bootstrap
go build -ldflags '$(LDFLAGS)' -o bin/$@ .

.PHONY: docker-build
Expand All @@ -61,34 +66,21 @@ docker-push: $(addsuffix -push,$(IMAGES))
az acr login --name $(CONTAINER_REPO)
docker push $(DOCKER_REGISTRY)/$*-server:$(IMAGE_TAG)

HAS_GOMETALINTER := $(shell command -v gometalinter;)
HAS_DEP := $(shell command -v dep;)
HAS_GIT := $(shell command -v git;)
HAS_SWAGGER := $(shell command -v swagger;)
HAS_STATIK := $(shell command -v statik;)
HAS_SWAGGER := $(shell command -v swagger;)
HAS_STATICCHECK := $(shell command -v staticcheck;)
HAS_GOLANGCI_LINT := $(shell command -v golangci-lint;)
HAS_MOCKGEN := $(shell command -v golangci-lint;)

vendor:
ifndef HAS_GIT
$(error You must install git)
bootstrap:
ifndef HAS_SWAGGER
go install github.com/go-swagger/go-swagger/cmd/swagger@v0.30.5
endif
ifndef HAS_DEP
go get -u github.com/golang/dep/cmd/dep
ifndef HAS_GOLANGCI_LINT
go install github.com/golangci/golangci-lint/cmd/golangci-lint@1.55.2
endif
ifndef HAS_GOMETALINTER
go get -u github.com/alecthomas/gometalinter
gometalinter --install
ifndef HAS_STATICCHECK
go install honnef.co/go/tools/cmd/staticcheck@v0.4.6
endif
dep ensure
ifndef HAS_SWAGGER
go get -u github.com/go-swagger/go-swagger/cmd/swagger
ifndef HAS_MOCKGEN
go install github.com/golang/mock/mockgen@v1.6.0
endif

ifndef HAS_STATIK
go get github.com/rakyll/statik
endif

.PHONY: bootstrap
bootstrap: vendor

staticcheck:
staticcheck ./...
20 changes: 9 additions & 11 deletions api/router/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (

"github.com/equinor/radix-api/api/utils"
"github.com/equinor/radix-api/models"
_ "github.com/equinor/radix-api/swaggerui" // statik files
"github.com/equinor/radix-api/swaggerui"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rakyll/statik/fs"
"github.com/rs/cors"
"github.com/urfave/negroni/v3"
)
Expand All @@ -21,22 +20,15 @@ const (
buildstatusControllerRootPath = "/buildstatus"
healthControllerPath = "/health/"
radixDNSZoneEnvironmentVariable = "RADIX_DNS_ZONE"
swaggerUIPath = "/swaggerui"
)

// NewServer Constructor function
func NewServer(clusterName string, kubeUtil utils.KubeUtil, controllers ...models.Controller) http.Handler {
router := mux.NewRouter().StrictSlash(true)
statikFS, err := fs.New()
if err != nil {
panic(err)
}

staticServer := http.FileServer(statikFS)
sh := http.StripPrefix("/swaggerui/", staticServer)
router.PathPrefix("/swaggerui/").Handler(sh)

initializeSwaggerUI(router)
initializeAPIServer(kubeUtil, router, controllers)

initializeHealthEndpoint(router)

serveMux := http.NewServeMux()
Expand Down Expand Up @@ -128,6 +120,12 @@ func initializeAPIServer(kubeUtil utils.KubeUtil, router *mux.Router, controller
}
}

func initializeSwaggerUI(router *mux.Router) {
swaggerFsHandler := http.FileServer(http.FS(swaggerui.FS()))
swaggerui := http.StripPrefix(swaggerUIPath, swaggerFsHandler)
router.PathPrefix(swaggerUIPath).Handler(swaggerui)
}

func initializeHealthEndpoint(router *mux.Router) {
router.HandleFunc(healthControllerPath, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
Expand Down
30 changes: 15 additions & 15 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
//
// This is the API Server for the Radix platform.
//
// Schemes: http, https
// BasePath: /api/v1
// Version: 1.23.0
// Contact: https://equinor.slack.com/messages/CBKM6N2JY
// Schemes: https, http
// BasePath: /api/v1
// Version: 1.23.0
// Contact: https://equinor.slack.com/messages/CBKM6N2JY
//
// Consumes:
// - application/json
// Consumes:
// - application/json
//
// Produces:
// - application/json
// Produces:
// - application/json
//
// Security:
// - bearer:
// Security:
// - bearer:
//
// SecurityDefinitions:
// bearer:
// type: apiKey
// name: Authorization
// in: header
// SecurityDefinitions:
// bearer:
// type: apiKey
// name: Authorization
// in: header
//
// swagger:meta
package docs
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/equinor/radix-job-scheduler v1.8.3
github.com/equinor/radix-operator v1.43.10
github.com/evanphx/json-patch/v5 v5.6.0
github.com/go-swagger/go-swagger v0.30.5
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/golang/mock v1.6.0
github.com/gorilla/handlers v1.5.1
Expand All @@ -15,7 +16,6 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/prometheus-operator/prometheus-operator/pkg/client v0.64.1
github.com/prometheus/client_golang v1.16.0
github.com/rakyll/statik v0.1.7
github.com/rs/cors v1.8.3
github.com/sirupsen/logrus v1.9.3
github.com/spf13/pflag v1.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+
github.com/go-openapi/swag v0.22.4 h1:QLMzNJnMGPRNDCbySlcj1x01tzU8/9LTTL9hZZZogBU=
github.com/go-openapi/swag v0.22.4/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-swagger/go-swagger v0.30.5 h1:SQ2+xSonWjjoEMOV5tcOnZJVlfyUfCBhGQGArS1b9+U=
github.com/go-swagger/go-swagger v0.30.5/go.mod h1:cWUhSyCNqV7J1wkkxfr5QmbcnCewetCdvEXqgPvbc/Q=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down
3 changes: 0 additions & 3 deletions swaggerui/dummy.go

This file was deleted.

File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Large diffs are not rendered by default.

File renamed without changes.

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions swaggerui_src/swagger.json → swaggerui/html/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"application/json"
],
"schemes": [
"http",
"https"
"https",
"http"
],
"swagger": "2.0",
"info": {
Expand Down
20 changes: 20 additions & 0 deletions swaggerui/swagger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package swaggerui

import (
"embed"
"io/fs"

_ "github.com/go-swagger/go-swagger"
)

//go:embed html
var swaggerFS embed.FS

// FS returns a FS with SwaggerUI files in root
func FS() fs.FS {
rootFS, err := fs.Sub(swaggerFS, "html")
if err != nil {
panic(err)
}
return rootFS
}

0 comments on commit e246b82

Please sign in to comment.