Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Commit

Permalink
Merge pull request #17 from spotahome/slok/refactor-structure
Browse files Browse the repository at this point in the history
Update structure, dockerfiles, gen scripts and move to github actions
  • Loading branch information
slok authored Jul 13, 2020
2 parents a9fbe9e + 91d0c9e commit 71fced2
Show file tree
Hide file tree
Showing 31 changed files with 359 additions and 291 deletions.
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* @slok @ese

48 changes: 48 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: CI

on: [push]

jobs:
check:
name: Check
runs-on: ubuntu-latest
# Execute the checks inside the container instead the VM.
container: golangci/golangci-lint:v1.28.3-alpine
steps:
- uses: actions/checkout@v1
- run: ./scripts/check/check.sh

test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-go@v2-beta
with:
go-version: 1.14
- run: make ci-test

# release-image:
# if: startsWith(github.ref, 'refs/tags/')
# env:
# TAG_IMAGE_LATEST: "true"
# PROD_IMAGE_NAME: quay.com/${GITHUB_REPOSITORY}
# needs: [check, test]
# name: Release image
# runs-on: ubuntu-latest
# # Only run in master and when has a tag.
# steps:
# - name: Set tag on VERSION env var
# run: echo ::set-env name=VERSION::$(echo ${GITHUB_REF:10})
# - uses: actions/checkout@v1
# - name: Build image
# run: make build-image
# - name: Docker login
# run: docker login ${DOCKER_HOST} -u ${DOCKER_USER} -p ${DOCKER_TOKEN}
# env:
# DOCKER_HOST: ""
# DOCKER_USER: slok
# DOCKER_TOKEN: ${{secrets.DOCKER_HUB_TOKEN}}

# - name: Publish image
# run: make publish-image
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
# Binary
bin/

# vendor
vendor/
# Coverage.
.test_coverage.txt
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

run:
skip-dirs:
- pkg/k8sautogen
29 changes: 0 additions & 29 deletions .travis.yml

This file was deleted.

185 changes: 65 additions & 120 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,138 +1,83 @@
# Name of this service/application
SERVICE_NAME := service-level-operator

# Shell to use for running scripts
SHELL := $(shell which bash)

# Get docker path or an empty string
OSTYPE := $(shell uname)
DOCKER := $(shell command -v docker)
GID := $(shell id -g)
UID := $(shell id -u)
VERSION ?= $(shell git describe --tags --always)

# Get docker-compose path or an empty string
DOCKER_COMPOSE := $(shell command -v docker-compose)
UNIT_TEST_CMD := ./scripts/check/unit-test.sh
INTEGRATION_TEST_CMD := ./scripts/check/integration-test.sh
CHECK_CMD := ./scripts/check/check.sh

# Get the main unix group for the user running make (to be used by docker-compose later)
GID := $(shell id -g)
DEV_IMAGE_NAME := spotahome/service-level-operator-dev
PROD_IMAGE_NAME ?= spotahome/service-level-operator

# Get the unix user id for the user running make (to be used by docker-compose later)
UID := $(shell id -u)
DOCKER_RUN_CMD := docker run --env ostype=$(OSTYPE) -v ${PWD}:/src --rm -it ${DEV_IMAGE_NAME}
BUILD_BINARY_CMD := VERSION=${VERSION} ./scripts/build/build.sh
BUILD_DEV_IMAGE_CMD := IMAGE=${DEV_IMAGE_NAME} DOCKER_FILE_PATH=./docker/dev/Dockerfile VERSION=latest ./scripts/build/build-image.sh
BUILD_PROD_IMAGE_CMD := IMAGE=${PROD_IMAGE_NAME} DOCKER_FILE_PATH=./docker/prod/Dockerfile VERSION=${VERSION} ./scripts/build/build-image.sh
PUBLISH_PROD_IMAGE_CMD := IMAGE=${PROD_IMAGE_NAME} VERSION=${VERSION} ./scripts/build/publish-image.sh

# Bash history file for container shell
HISTORY_FILE := ~/.bash_history.$(SERVICE_NAME)

# Version from Git.
VERSION=$(shell git describe --tags --always)

# Dev direcotry has all the required dev files.
DEV_DIR := ./docker/dev

# cmds
UNIT_TEST_CMD := ./hack/scripts/unit-test.sh
INTEGRATION_TEST_CMD := ./hack/scripts/integration-test.sh
TEST_ALERTS_CMD := ./hack/scripts/test-alerts.sh
MOCKS_CMD := ./hack/scripts/mockgen.sh
DOCKER_RUN_CMD := docker run \
-v ${PWD}:/src \
--rm -it $(SERVICE_NAME)
DOCKER_ALERTS_TEST_RUN_CMD := docker run \
-v ${PWD}:/prometheus \
--entrypoint=${TEST_ALERTS_CMD} \
--rm -it prom/prometheus
BUILD_BINARY_CMD := VERSION=${VERSION} ./hack/scripts/build-binary.sh
BUILD_IMAGE_CMD := VERSION=${VERSION} ./hack/scripts/build-image.sh
DEBUG_CMD := go run ./cmd/service-level-operator/* --debug
DEV_CMD := $(DEBUG_CMD) --development
FAKE_CMD := $(DEV_CMD) --fake
K8S_CODE_GEN_CMD := ./hack/scripts/k8scodegen.sh
OPENAPI_CODE_GEN_CMD := ./hack/scripts/openapicodegen.sh
DEPS_CMD := GO111MODULE=on go mod tidy && GO111MODULE=on go mod vendor
K8S_VERSION := 1.13.12
SET_K8S_DEPS_CMD := GO111MODULE=on go mod edit \
-require=k8s.io/apiextensions-apiserver@kubernetes-${K8S_VERSION} \
-require=k8s.io/client-go@kubernetes-${K8S_VERSION} \
-require=k8s.io/apimachinery@kubernetes-${K8S_VERSION} \
-require=k8s.io/api@kubernetes-${K8S_VERSION} \
-require=k8s.io/kubernetes@v${K8S_VERSION} && \
$(DEPS_CMD)


# The default action of this Makefile is to build the development docker image
default: build

# Test if the dependencies we need to run this Makefile are installed
.PHONY: deps-development
deps-development:
ifndef DOCKER
@echo "Docker is not available. Please install docker"
@exit 1
endif
ifndef DOCKER_COMPOSE
@echo "docker-compose is not available. Please install docker-compose"
@exit 1
endif

# Build the development docker images
.PHONY: build
build:
docker build -t $(SERVICE_NAME) --build-arg uid=$(UID) --build-arg gid=$(GID) -f $(DEV_DIR)/Dockerfile .

# run the development stack.
.PHONY: stack
stack: deps-development
cd $(DEV_DIR) && \
( docker-compose -p $(SERVICE_NAME) up --build; \
docker-compose -p $(SERVICE_NAME) stop; \
docker-compose -p $(SERVICE_NAME) rm -f; )
help: ## Show this help
@echo "Help"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-20s\033[93m %s\n", $$1, $$2}'

# Build production stuff.
build-binary: build
$(DOCKER_RUN_CMD) /bin/sh -c '$(BUILD_BINARY_CMD)'
.PHONY: default
default: help

.PHONY: build-image
build-image:
$(BUILD_IMAGE_CMD)
build-image: ## Builds the production docker image.
@$(BUILD_PROD_IMAGE_CMD)

# Dependencies stuff.
.PHONY: set-k8s-deps
set-k8s-deps:
$(SET_K8S_DEPS_CMD)
.PHONY: publish-image
publish-image: ## Publishes the production docker image.
@$(PUBLISH_PROD_IMAGE_CMD)

.PHONY: build-dev-image
build-dev-image: ## Builds the development docker image.
@$(BUILD_DEV_IMAGE_CMD)

.PHONY: build
build: build-dev-image ## Builds the production binary.
@$(DOCKER_RUN_CMD) /bin/sh -c '$(BUILD_BINARY_CMD)'

.PHONY: deps
deps:
$(DEPS_CMD)

k8s-code-gen:
$(K8S_CODE_GEN_CMD)

openapi-code-gen:
$(OPENAPI_CODE_GEN_CMD)

# Test stuff in dev
.PHONY: test-alerts
test-alerts:
$(DOCKER_ALERTS_TEST_RUN_CMD)
.PHONY: unit-test
unit-test: build
$(DOCKER_RUN_CMD) /bin/sh -c '$(UNIT_TEST_CMD)'
.PHONY: integration-test
integration-test: build
$(DOCKER_RUN_CMD) /bin/sh -c '$(INTEGRATION_TEST_CMD)'
.PHONY: test
test: integration-test
.PHONY: test
ci: test test-alerts
test: build-dev-image ## Runs unit test.
@$(DOCKER_RUN_CMD) /bin/sh -c '$(UNIT_TEST_CMD)'

.PHONY: check
check: build-dev-image ## Runs checks.
@$(DOCKER_RUN_CMD) /bin/sh -c '$(CHECK_CMD)'

.PHONY: integration
integration: build-dev-image ## Runs integration test.
@$(DOCKER_RUN_CMD) /bin/sh -c '$(INTEGRATION_TEST_CMD)'

# Mocks stuff in dev
.PHONY: mocks
mocks: build
# FIX: Problem using go mod with vektra/mockery.
#$(DOCKER_RUN_CMD) /bin/sh -c '$(MOCKS_CMD)'
$(MOCKS_CMD)
.PHONY: go-gen
go-gen: build-dev-image ## Generates go based code.
@$(DOCKER_RUN_CMD) /bin/sh -c './scripts/gogen.sh'

.PHONY: kube-gen
kube-gen: ## Generates Kubernetes based code.
/bin/sh -c './scripts/kubegen.sh'

.PHONY: gen
gen: go-gen kube-gen ## Generates all.

.PHONY: deps
deps: ## Fixes the dependencies
@$(DOCKER_RUN_CMD) /bin/sh -c './scripts/deps.sh'

.PHONY: dev
dev:
$(DEV_CMD)
.PHONY: ci-unit-test
ci-test: ## Runs unit test in CI environment (without docker).
@$(UNIT_TEST_CMD)

.PHONY: ci-check
ci-check: ## Runs checks in CI environment (without docker).
@$(CHECK_CMD)

.PHONY: push
push: export PUSH_IMAGE=true
push: build-image
.PHONY: ci-integration-test
ci-integration: ## Runs integraton test in CI environment (without docker).
@$(INTEGRATION_TEST_CMD)
35 changes: 19 additions & 16 deletions cmd/service-level-operator/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,33 @@ const (
type cmdFlags struct {
fs *flag.FlagSet

kubeConfig string
resyncSeconds int
workers int
metricsPath string
listenAddress string
labelSelector string
namespace string
kubeConfig string
resyncSeconds int
workers int
metricsPath string
listenAddress string
labelSelector string
namespace string
defSLISourcePath string
debug bool
development bool
fake bool
debug bool
development bool
fake bool
}

func newCmdFlags() *cmdFlags {
func newCmdFlags() (*cmdFlags, error) {
c := &cmdFlags{
fs: flag.NewFlagSet(os.Args[0], flag.ExitOnError),
}
c.init()

return c
err := c.init()
if err != nil {
return nil, err
}

return c, nil
}

func (c *cmdFlags) init() {
func (c *cmdFlags) init() error {

kubehome := filepath.Join(homedir.HomeDir(), ".kube", "config")
// register flags
Expand All @@ -60,8 +64,7 @@ func (c *cmdFlags) init() {
c.fs.BoolVar(&c.debug, "debug", false, "enable debug mode")
c.fs.BoolVar(&c.fake, "fake", false, "enable faked mode, in faked node external services/dependencies are not needed")

// Parse flags
c.fs.Parse(os.Args[1:])
return c.fs.Parse(os.Args[1:])
}

func (c *cmdFlags) toOperatorConfig() operator.Config {
Expand Down
Loading

0 comments on commit 71fced2

Please sign in to comment.