Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compilation issues and add Makefile #25

Merged
merged 6 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
74 changes: 74 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
name: Lint & Test

# Run for all pushes to main and pull requests when Go or YAML files change
on:
push:
branches:
- main
pull_request:
workflow_dispatch:

jobs:
lint:
name: lint
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: '1.19'
JAORMX marked this conversation as resolved.
Show resolved Hide resolved

- name: Run golangci-lint
run: make lint

test:
name: test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: '1.19'
JAORMX marked this conversation as resolved.
Show resolved Hide resolved

- name: Run go tests
run: make test

# TODO(jaosorior): Add this back once we have a Dockerfile
# Related bug: # 26
# build-image:
# name: build-image
# runs-on: ubuntu-latest
# steps:
# - name: Checkout
# uses: actions/checkout@v3
#
# - name: docker-metadata
# id: docker-metadata
# uses: docker/metadata-action@v4
# with:
# images: ghcr.io/${{ github.repository }}/krakend-endpoints-tool
# tags: |
# type=sha

# - name: Build
# uses: docker/build-push-action@v4
# with:
# context: .
# file: ./Dockerfile
# push: false
# load: true
# tags: ${{ steps.docker-metadata.outputs.tags }}

# - name: Run Trivy vulnerability scanner
# uses: aquasecurity/trivy-action@master
# with:
# image-ref: ${{ steps.docker-metadata.outputs.tags }}
# security-checks: 'vuln,config,secret'
# ignore-unfixed: true
# severity: 'HIGH,CRITICAL'
# format: 'table'
96 changes: 92 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,92 @@
# go install github.com/volatiletech/sqlboiler/v4@latest
# go install github.com/glerchundi/sqlboiler-crdb/v4
# for now install this one though, once PR #44 is merged go back https://github.com/glerchundi/sqlboiler-crdb/pull/44
# go install github.com/adammohammed/[email protected]
BIN?=permissions-api

# Utility settings
TOOLS_DIR := .tools
GOLANGCI_LINT_VERSION = v1.50.1

# Container build settings
CONTAINER_BUILD_CMD?=docker build

# Container settings
CONTAINER_REPO?=ghcr.io/infratographer
PERMISSIONS_API_CONTAINER_IMAGE_NAME = $(CONTAINER_REPO)/permissions-api
CONTAINER_TAG?=latest

# go files to be checked
GO_FILES=$(shell git ls-files '*.go')

## Targets

.PHONY: help
help: Makefile ## Print help
@grep -h "##" $(MAKEFILE_LIST) | grep -v grep | sed -e 's/:.*##/#/' | column -c 2 -t -s#

.PHONY: build
build: ## Builds permissions-api binary.
go build -o $(BIN) ./main.go

.PHONY: test
test: ## Runs unit tests.
@echo Running unit tests...
@go test -timeout 30s -cover -short -tags testtools ./...

.PHONY: coverage
coverage: ## Generates a test coverage report.
@echo Generating coverage report...
@go test -timeout 30s -tags testtools ./... -coverprofile=coverage.out -covermode=atomic
@go tool cover -func=coverage.out
@go tool cover -html=coverage.out

lint: golint ## Runs all lint checks.

golint: | vendor $(TOOLS_DIR)/golangci-lint ## Runs Go lint checks.
@echo Linting Go files...
@$(TOOLS_DIR)/golangci-lint run

clean: ## Cleans generated files.
@echo Cleaning...
@rm -f coverage.out
@go clean -testcache
@rm -rf $(TOOLS_DIR)

vendor: ## Downloads and tidies go modules.
@go mod download
@go mod tidy

.PHONY: gci-diff gci-write gci
gci-diff: $(GO_FILES) | gci-tool ## Outputs improper go import ordering.
@gci diff -s 'standard,default,prefix(github.com/infratographer)' $^

gci-write: $(GO_FILES) | gci-tool ## Checks and updates all go files for proper import ordering.
@gci write -s 'standard,default,prefix(github.com/infratographer)' $^

gci: | gci-diff gci-write ## Outputs and corrects all improper go import ordering.

image: ## Builds all docker images.
$(CONTAINER_BUILD_CMD) -f Dockerfile . -t $(PERMISSIONS_API_CONTAINER_IMAGE_NAME):$(CONTAINER_TAG)

# Tools setup
$(TOOLS_DIR):
mkdir -p $(TOOLS_DIR)

$(TOOLS_DIR)/golangci-lint: $(TOOLS_DIR)
export \
VERSION=$(GOLANGCI_LINT_VERSION) \
URL=https://raw.githubusercontent.com/golangci/golangci-lint \
BINDIR=$(TOOLS_DIR) && \
curl -sfL $$URL/$$VERSION/install.sh | sh -s $$VERSION
$(TOOLS_DIR)/golangci-lint version
$(TOOLS_DIR)/golangci-lint linters

.PHONY: gci-tool
gci-tool:
@which gci &>/dev/null \
|| echo Installing gci tool \
&& go install github.com/daixiang0/gci@latest

.PHONY: nk-tool
nk-tool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless we're needing NATS related bits in this repo. Probably don't need this.

@which nk &>/dev/null || \
echo Installing "nk" tool && \
go install github.com/nats-io/nkeys/nk@latest && \
export PATH=$$PATH:$(shell go env GOPATH)
13 changes: 7 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ import (
)

var (
appName = "permissionapi"
cfgFile string
logger *zap.SugaredLogger
appName = "permissionapi"
cfgFile string
logger *zap.SugaredLogger
globalCfg *config.AppConfig
)

// rootCmd represents the base command when called without any subcommands
Expand All @@ -55,7 +56,7 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is /etc/infratographer/permission-api.yaml)")
loggingx.MustViperFlags(rootCmd.PersistentFlags())
loggingx.MustViperFlags(viper.GetViper(), rootCmd.PersistentFlags())

// Add version command
versionx.RegisterCobraCommand(rootCmd, func() { versionx.PrintVersion(logger) })
Expand Down Expand Up @@ -90,7 +91,7 @@ func initConfig() {
// If a config file is found, read it in.
err := viper.ReadInConfig()

var settings config.Settings
var settings config.AppConfig

if err := viper.Unmarshal(&settings); err != nil {
log.Fatalf("unable to process app config, error: %s", err.Error())
Expand All @@ -105,5 +106,5 @@ func initConfig() {
)
}

config.AppConfig = settings
globalCfg = &settings
}
10 changes: 5 additions & 5 deletions cmd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@ var schemaCmd = &cobra.Command{
Use: "schema",
Short: "write the schema into SpiceDB",
Run: func(cmd *cobra.Command, args []string) {
writeSchema(cmd.Context())
writeSchema(cmd.Context(), globalCfg)
},
}

func init() {
rootCmd.AddCommand(schemaCmd)
}

func writeSchema(ctx context.Context) {
err := otelx.InitTracer(config.AppConfig.Tracing, appName, logger)
func writeSchema(ctx context.Context, cfg *config.AppConfig) {
err := otelx.InitTracer(cfg.Tracing, appName, logger)
if err != nil {
logger.Fatalw("unable to initialize tracing system", "error", err)
}

client, err := spicedbx.NewClient(config.AppConfig.SpiceDB, config.AppConfig.Tracing.Enabled)
client, err := spicedbx.NewClient(cfg.SpiceDB, cfg.Tracing.Enabled)
if err != nil {
logger.Fatalw("unable to initialize spicedb client", "error", err)
}

schema := spicedbx.GeneratedSchema(config.AppConfig.SpiceDB.Prefix)
schema := spicedbx.GeneratedSchema(cfg.SpiceDB.Prefix)

logger.Debugw("Writing schema to DB", "schema", schema)

Expand Down
10 changes: 5 additions & 5 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var serverCmd = &cobra.Command{
Use: "server",
Short: "starts the permission api server",
Run: func(cmd *cobra.Command, args []string) {
serve(cmd.Context())
serve(cmd.Context(), globalCfg)
},
}

Expand All @@ -49,18 +49,18 @@ func init() {
otelx.MustViperFlags(viper.GetViper(), serverCmd.Flags())
}

func serve(ctx context.Context) {
err := otelx.InitTracer(config.AppConfig.Tracing, appName, logger)
func serve(ctx context.Context, cfg *config.AppConfig) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 💯

err := otelx.InitTracer(cfg.Tracing, appName, logger)
if err != nil {
logger.Fatalw("unable to initialize tracing system", "error", err)
}

spiceClient, err := spicedbx.NewClient(config.AppConfig.SpiceDB, config.AppConfig.Tracing.Enabled)
spiceClient, err := spicedbx.NewClient(cfg.SpiceDB, cfg.Tracing.Enabled)
if err != nil {
logger.Fatalw("unable to initialize spicedb client", "error", err)
}

s := ginx.NewServer(logger.Desugar(), config.AppConfig.Server, versionx.BuildDetails())
s := ginx.NewServer(logger.Desugar(), cfg.Server, versionx.BuildDetails())
r := api.NewRouter(spiceClient, logger)

s = s.AddHandler(r).
Expand Down
8 changes: 4 additions & 4 deletions cmd/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var workerCmd = &cobra.Command{
Use: "worker",
Short: "starts a permission api queue working",
Run: func(cmd *cobra.Command, args []string) {
worker(cmd.Context())
worker(cmd.Context(), globalCfg)
},
}

Expand All @@ -43,13 +43,13 @@ func init() {
otelx.MustViperFlags(viper.GetViper(), workerCmd.Flags())
}

func worker(ctx context.Context) {
err := otelx.InitTracer(config.AppConfig.Tracing, appName, logger)
func worker(ctx context.Context, cfg *config.AppConfig) {
err := otelx.InitTracer(cfg.Tracing, appName, logger)
if err != nil {
logger.Fatalw("unable to initialize tracing system", "error", err)
}

spiceClient, err := spicedbx.NewClient(config.AppConfig.SpiceDB, config.AppConfig.Tracing.Enabled)
spiceClient, err := spicedbx.NewClient(cfg.SpiceDB, cfg.Tracing.Enabled)
if err != nil {
logger.Fatalw("unable to initialize spicedb client", "error", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"go.infratographer.com/permissions-api/internal/spicedbx"
)

var AppConfig struct {
type AppConfig struct {
Logging loggingx.Config
Server ginx.Config
SpiceDB spicedbx.Config
Expand Down