Skip to content

Commit

Permalink
feat: stamus deamon setup
Browse files Browse the repository at this point in the history
  • Loading branch information
lanathlor authored and LeVraiBaptiste committed Jul 4, 2024
1 parent 71f4974 commit 37c0378
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 24 deletions.
12 changes: 12 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
dist/
docker/
scripts/
tmp/

.air.toml
.dockerignore
.envrc
.gitignore
.gitlab-*
default.nix
README.md
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
dist/

.configs/
tmp/
tmp/
docs/
63 changes: 62 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,44 @@ kaniko cli main:
--cache-copy-layers=true \
--cache-ttl=72h
kaniko daemon:
stage: build
before_script: []
image:
name: gcr.io/kaniko-project/executor:v1.14.0-debug
entrypoint: ['']
script:
- |
echo "{\"auths\":{\"${CI_REGISTRY}\":{\"auth\":\"$(printf "%s:%s" "${CI_REGISTRY_USER}" "${CI_REGISTRY_PASSWORD}" | base64 | tr -d '\n')\"},\"$(echo -n $CI_DEPENDENCY_PROXY_SERVER | awk -F[:] '{print $1}')\":{\"auth\":\"$(printf "%s:%s" ${CI_DEPENDENCY_PROXY_USER} "${CI_DEPENDENCY_PROXY_PASSWORD}" | base64 | tr -d '\n')\"}}}" > /kaniko/.docker/config.json
/kaniko/executor \
--context "${CI_PROJECT_DIR}" \
--dockerfile "${CI_PROJECT_DIR}/docker/Dockerfile.daemon" \
--destination "${CI_REGISTRY_IMAGE}/daemon:${CI_COMMIT_BRANCH}-${CI_COMMIT_SHORT_SHA}" \
--destination "${CI_REGISTRY_IMAGE}/daemon:${CI_COMMIT_BRANCH}" \
--cache=true \
--cache-copy-layers=true \
--cache-ttl=72h
kaniko daemon main:
stage: build
before_script: []
only:
- main
image:
name: gcr.io/kaniko-project/executor:v1.14.0-debug
entrypoint: ['']
script:
- |
echo "{\"auths\":{\"${CI_REGISTRY}\":{\"auth\":\"$(printf "%s:%s" "${CI_REGISTRY_USER}" "${CI_REGISTRY_PASSWORD}" | base64 | tr -d '\n')\"},\"$(echo -n $CI_DEPENDENCY_PROXY_SERVER | awk -F[:] '{print $1}')\":{\"auth\":\"$(printf "%s:%s" ${CI_DEPENDENCY_PROXY_USER} "${CI_DEPENDENCY_PROXY_PASSWORD}" | base64 | tr -d '\n')\"}}}" > /kaniko/.docker/config.json
/kaniko/executor \
--context "${CI_PROJECT_DIR}" \
--dockerfile "${CI_PROJECT_DIR}/docker/Dockerfile.daemon" \
--destination "${CI_REGISTRY_IMAGE}/daemon:${CI_COMMIT_SHORT_SHA}" \
--destination "${CI_REGISTRY_IMAGE}/daemon:latest" \
--cache=true \
--cache-copy-layers=true \
--cache-ttl=72h
build cli:
image: golang:alpine
stage: build
Expand All @@ -58,6 +96,21 @@ build cli:
make cli
mv dist/stamusctl .
build daemon:
image: golang:alpine
stage: build
artifacts:
expire_in: 1 day
paths:
- stamusd
script:
- |
apk add --no-cache gcc musl-dev make
go install github.com/swaggo/swag/cmd/swag@latest
swag init -g root.go -o cmd/docs -d cmd/daemon/run/
make daemon
mv dist/stamusd .
build test:
image: golang:alpine
stage: build
Expand All @@ -84,6 +137,15 @@ test cli:
- |
./stamusctl
test daemon:
image: busybox
stage: test
dependencies:
- build daemon
script:
- |
./stamusd
unit tests:
image: golang:alpine
stage: test
Expand Down Expand Up @@ -160,4 +222,3 @@ compose config reload:
./cli compose config set --reload
chmod +x $(pwd)/scripts/compare.sh
$(pwd)/scripts/compare.sh ./tmp ./outputs/compose-init
24 changes: 19 additions & 5 deletions cmd/daemon/run/compose/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@ package compose

import "github.com/gin-gonic/gin"

func NewCompose(router *gin.RouterGroup) {
router.GET("/pingcompose", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pongcompose",
})
// Ping godoc
// @Summary ping example
// @Schemes
// @Description do ping
// @Tags example
// @Accept json
// @Produce json
// @Success 200 {string} Helloworld
// @Router /compose/pingcompose [get]
func ping(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pongcompose",
})
}

func NewCompose(router *gin.RouterGroup) {
compose := router.Group("/compose")
{
compose.GET("/pingcompose", ping)
}
}
61 changes: 48 additions & 13 deletions cmd/daemon/run/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,85 @@ import (
"os/signal"

"stamus-ctl/cmd/daemon/run/compose"
docs "stamus-ctl/cmd/docs"
"stamus-ctl/internal/logging"

"github.com/docker/distribution/context"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
ginprometheus "github.com/zsais/go-gin-prometheus"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"

swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)

// Commands
func NewPrometheusServer(ctx context.Context) {
engineProm := gin.New()
p := ginprometheus.NewPrometheus("gin")
p.Use(engineProm)

logging.LoggerWithContextToSpanContext(ctx).Info("Starting prometheus endpoint")
engineProm.Run(":9001")
}

// Ping godoc
// @Summary ping example
// @Schemes
// @Description do ping
// @Tags example
// @Accept json
// @Produce json
// @Success 200 {string} Helloworld
// @Router /ping [get]
func ping(c *gin.Context) {
logging.LoggerWithRequest(c.Request).Info("Ping")

c.JSON(200, gin.H{
"message": "pong",
})
}

// @title Swagger Stamusd API
// @version 1.0
// @description Stamus daemon server.

// @BasePath /api/v1
func RunCmd() *cobra.Command {
_, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

logging.NewTraceProvider()

// Create command
cmd := &cobra.Command{
Use: "run",
Short: "Run daemon",
RunE: func(cmd *cobra.Command, args []string) error {

c := context.Background()
_, span := logging.Tracer.Start(c, "main")
ctx, span := logging.Tracer.Start(c, "main")
defer span.End()

gin.SetMode(gin.ReleaseMode)
r := gin.New()

go NewPrometheusServer(ctx)

logging.LoggerWithSpanContext(span.SpanContext()).Info("Setup middleware")
r.Use(gin.Recovery())
r.Use(otelgin.Middleware("service-name", otelgin.WithTracerProvider(logging.TracerProvider)))

logging.LoggerWithSpanContext(span.SpanContext()).Info("Setup routes")
r.GET("/ping", func(c *gin.Context) {
logging.LoggerWithRequest(c.Request).Info("Ping")
v1 := r.Group("/api/v1")
{
v1.GET("/ping", ping)
compose.NewCompose(v1)
}

c.JSON(200, gin.H{
"message": "pong",
})
})
compose.NewCompose(r.Group("/compose"))
logging.LoggerWithSpanContext(span.SpanContext()).Info("Setup swagger")
docs.SwaggerInfo.BasePath = "/api/v1"
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))

// r.RunUnix("./daemon.sock")
logging.LoggerWithSpanContext(span.SpanContext()).Info("Starting daemon")
// r.RunUnix("./daemon.sock")
r.Run(":9000")
return nil
},
Expand Down
23 changes: 23 additions & 0 deletions docker/Dockerfile.daemon
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM golang:alpine AS Builder

ARG var_version=devel
ENV VERSION=$var_version

RUN mkdir -p /src
RUN apk update && apk add --no-cache gcc musl-dev make
RUN go install github.com/swaggo/swag/cmd/swag@latest

COPY ./go.mod /src/stamus-ctl/go.mod
COPY ./go.sum /src/stamus-ctl/go.sum
WORKDIR /src/stamus-ctl
RUN go mod download

COPY . /src/stamus-ctl
RUN swag init -g root.go -o cmd/docs -d cmd/daemon/run/
RUN CGO_ENABLED=1 make daemon

FROM scratch
COPY --from=Builder /src/stamus-ctl/dist /bin/

ENTRYPOINT [ "stamusd" ]
CMD [ "run" ]
15 changes: 11 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect
github.com/AlecAivazis/survey/v2 v2.3.7 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
Expand Down Expand Up @@ -77,9 +78,10 @@ require (
github.com/gin-gonic/gin v1.10.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/spec v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.0 // indirect
Expand Down Expand Up @@ -171,6 +173,9 @@ require (
github.com/spf13/viper v1.18.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/swaggo/files v1.0.1 // indirect
github.com/swaggo/gin-swagger v1.6.0 // indirect
github.com/swaggo/swag v1.16.3 // indirect
github.com/theupdateframework/notary v0.7.0 // indirect
github.com/tilt-dev/fsnotify v1.4.8-0.20220602155310-fff9c274a375 // indirect
github.com/tonistiigi/fsutil v0.0.0-20240424095704-91a3fc46842c // indirect
Expand All @@ -183,6 +188,7 @@ require (
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/zsais/go-gin-prometheus v0.1.0 // indirect
go.opentelemetry.io/contrib/bridges/otelslog v0.2.0 // indirect
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.52.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect
Expand Down Expand Up @@ -212,14 +218,15 @@ require (
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.22.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 // indirect
Expand Down
Loading

0 comments on commit 37c0378

Please sign in to comment.