Skip to content

Commit

Permalink
Merge pull request #1 from vouch-opensource/init
Browse files Browse the repository at this point in the history
initial code
  • Loading branch information
raphapr authored Aug 30, 2021
2 parents 4dfdd11 + 9d214be commit b3bfeda
Show file tree
Hide file tree
Showing 18 changed files with 1,723 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
bin
.bin
31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
ARG GO_VERSION=1.17

# FIRST STAGE: build
FROM golang:${GO_VERSION}-alpine AS builder

# force the go compiler to use modules
ENV GO111MODULE=on

# install dependencies rewuire to build
RUN apk add --update make git gcc libc-dev

WORKDIR /app

# download dependencies
COPY ./go.mod ./go.sum ./
RUN go mod download

COPY ./ ./

# compile binary
RUN make build

# FINAL STAGE: run application
FROM alpine:latest

# dev env always default
ENV ENV=development

COPY --from=builder /app /app

ENTRYPOINT ["/app/bin/containerd-healthcheck"]
62 changes: 62 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
BIN_NAME = containerd-healthcheck
IMAGE_NAME = vouchio/containerd-healthcheck
ROOT_DIR = $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
BIN_DIR = $(ROOT_DIR)/bin
BIN_PATH = $(ROOT_DIR)/bin/$(BIN_NAME)
CMD_PATH = $(ROOT_DIR)/cmd/$(BIN_NAME)
VERSION = $(shell cat $(ROOT_DIR)/VERSION)
GIT_BRANCH = $(shell git rev-parse --abbrev-ref HEAD)
GIT_COMMIT = $(shell git rev-parse HEAD)
BUILD_DATE = $(shell date +'%Y-%m-%dT%H:%M:%SZ')
LDFLAGS = "-X main.commit=$(GIT_COMMIT) -X main.version=$(VERSION) -X main.date=$(BUILD_DATE)"
PORT = "9891"
ENV = development

export PATH := $(PATH):$(ROOT_DIR)/.bin

.PHONY: tidy
tidy:
@(go mod tidy)

.PHONY: setup
setup: tidy
@(./scripts/install-go-release.sh "goreleaser/goreleaser")

.PHONY: build
build:
@(echo "-> Compiling packages")
GOOS=linux go build -ldflags $(LDFLAGS) -o $(BIN_PATH) $(CMD_PATH)/main.go
@(echo "-> Binary created at '$(BIN_PATH)'")

.PHONY: run
run:
@($(BIN_PATH) --addr ":$(PORT)" --env $(ENV))

.PHONY: docker-build
docker-build:
@(docker build -t $(IMAGE_NAME):$(VERSION) .)

.PHONY: docker-push
docker-push:
@(docker push $(IMAGE_NAME):$(VERSION))

.PHONY: docker-run
docker-run:
@(docker run -p 8080:$(PORT) -e "ENV=$(ENV)" -it $(BIN_NAME):latest)

.PHONY: release
release:
ifeq ($(GIT_BRANCH),master)
@(echo "-> Creating tag '$(VERSION)'")
@(git tag $(VERSION))
@(echo "-> Pushing tag '$(VERSION)'")
@(git push origin $(VERSION))
@(echo "-> Releasing to remote repository")
@(goreleaser --rm-dist)
else
@echo "You need to be in branch master"
endif

.PHONY: release-snapshot
release-snapshot:
@(goreleaser release --skip-publish --snapshot --rm-dist)
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.2
68 changes: 68 additions & 0 deletions cmd/containerd-healthcheck/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"containerdhealthcheck/internal/models"
"containerdhealthcheck/internal/server"
"fmt"
"os"

flag "github.com/spf13/pflag"

cleanenv "github.com/ilyakaznacheev/cleanenv"
"github.com/sirupsen/logrus"
)

var version, commit, date string

func main() {

var yamlConfig models.YAMLConfig

// Config
env := flag.StringP("env", "e", "development", "Application environment")
addr := flag.StringP("addr", "a", ":9434", "HTTP address for prometheus endpoint")
configPath := flag.StringP("config", "c", "config.yml", "Path to configuration file")
// Version
versionOpt := flag.BoolP("version", "v", false, "Print app version")

flag.Parse()

if *versionOpt {
fmt.Println(version)
os.Exit(0)
}

logger := logrus.New()
logger.SetFormatter(&logrus.JSONFormatter{})

appBuildInfo := models.BuildInfo{
Version: version,
Commit: commit,
Date: date,
}

serverConfig := models.ServerConfig{
Env: *env,
Addr: *addr,
}

// read configuration from the file and environment variables
if err := cleanenv.ReadConfig(*configPath, &yamlConfig); err != nil {
logger.Fatal(err)
}

err := cleanenv.ReadConfig(*configPath, &yamlConfig)

if err != nil {
logger.Fatal(err)
}

server, err := server.NewApp(serverConfig, yamlConfig, appBuildInfo, logger)

if err != nil {
logger.Fatal("unexpected error: ", err)
}

server.Run()

}
21 changes: 21 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
containerd:
socket: /run/containerd/containerd.sock
namespace: services.linuxkit
checks:
- container_task: hello-world-api
http:
url: http://127.0.0.1:3000
expected_body: Hello
timeout: 1
execution_period: 4
initial_delay: 10
restart_delay: 10
threshold: 3
- container_task: google
http:
url: https://www.google.com
timeout: 1
execution_period: 2
initial_delay: 2
threshold: 3
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module containerdhealthcheck

go 1.16

require (
contrib.go.opencensus.io/exporter/prometheus v0.4.0
github.com/AppsFlyer/go-sundheit v0.5.0
github.com/AppsFlyer/go-sundheit/opencensus v0.0.0-20210815135105-b87b5e51e25b
github.com/containerd/containerd v1.5.5
github.com/ilyakaznacheev/cleanenv v1.2.5
github.com/prometheus/client_golang v1.11.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/pflag v1.0.5
go.opencensus.io v0.23.0
)
Loading

0 comments on commit b3bfeda

Please sign in to comment.