Skip to content

Commit

Permalink
Generic IUT provider for ETOS API (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
andmat900 authored Sep 13, 2024
1 parent 308e19b commit efad081
Show file tree
Hide file tree
Showing 32 changed files with 1,177 additions and 28 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export DOCKER_REGISTRY ?= registry.nordix.org
export DOCKER_NAMESPACE ?= eiffel
export DEPLOY ?= etos-sse

PROGRAMS = sse logarea
PROGRAMS = sse logarea iut
COMPILEDAEMON = $(GOBIN)/CompileDaemon
GIT = git
GOLANGCI_LINT = $(GOBIN)/golangci-lint
Expand Down Expand Up @@ -87,6 +87,10 @@ tidy:
check-dirty:
$(GIT) diff --exit-code HEAD

.PHONY: gen
gen:
go generate ./...


# Setup the dynamic commands
#
Expand Down
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ Installation
pip install .


Running dockers in development mode
===================================

make DEPLOY=etos-iut start
make DEPLOY=etos-logarea start
make DEPLOY=etos-sse start


Contribute
==========

Expand Down
133 changes: 133 additions & 0 deletions cmd/iut/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Copyright 2022 Axis Communications AB.
//
// For a full list of individual contributors, please see the commit history.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main

import (
"context"
"net/http"
"os"
"os/signal"
"runtime/debug"
"syscall"
"time"

config "github.com/eiffel-community/etos-api/internal/configs/iut"
"github.com/eiffel-community/etos-api/internal/logging"
server "github.com/eiffel-community/etos-api/internal/server"
"github.com/eiffel-community/etos-api/pkg/application"
"github.com/eiffel-community/etos-api/pkg/iut/v1alpha1"
"github.com/sirupsen/logrus"
"github.com/snowzach/rotatefilehook"
"go.elastic.co/ecslogrus"
clientv3 "go.etcd.io/etcd/client/v3"
)

// main sets up logging and starts up the webserver.
func main() {
cfg := config.Get()
ctx := context.Background()

var hooks []logrus.Hook
if fileHook := fileLogging(cfg); fileHook != nil {
hooks = append(hooks, fileHook)
}

logger, err := logging.Setup(cfg.LogLevel(), hooks)
if err != nil {
logrus.Fatal(err.Error())
}

hostname, err := os.Hostname()
if err != nil {
logrus.Fatal(err.Error())
}
log := logger.WithFields(logrus.Fields{
"hostname": hostname,
"application": "ETOS IUT Provider Service Mini",
"version": vcsRevision(),
"name": "ETOS IUT Provider Mini",
"user_log": false,
})

// Database connection test
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{cfg.DatabaseURI()},
DialTimeout: 5 * time.Second,
})
if err != nil {
log.WithError(err).Fatal("failed to create etcd connection")
}

log.Info("Loading v1alpha1 routes")
v1alpha1App := v1alpha1.New(cfg, log, ctx, cli)
defer v1alpha1App.Close()
router := application.New(v1alpha1App)

srv := server.NewWebService(cfg, log, router)

done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

go func() {
if err := srv.Start(); err != nil && err != http.ErrServerClosed {
log.Errorf("Webserver shutdown: %+v", err)
}
}()

<-done
log.Info("SIGTERM received")

ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
v1alpha1App.Close()

if err := srv.Close(ctx); err != nil {
log.Errorf("Webserver shutdown failed: %+v", err)
}
}

// fileLogging adds a hook into a slice of hooks, if the filepath configuration is set
func fileLogging(cfg config.Config) logrus.Hook {
if filePath := cfg.LogFilePath(); filePath != "" {
// TODO: Make these parameters configurable.
// NewRotateFileHook cannot return an error which is why it's set to '_'.
rotateFileHook, _ := rotatefilehook.NewRotateFileHook(rotatefilehook.RotateFileConfig{
Filename: filePath,
MaxSize: 10, // megabytes
MaxBackups: 3,
MaxAge: 0, // days
Level: logrus.DebugLevel,
Formatter: &ecslogrus.Formatter{
DataKey: "labels",
},
})
return rotateFileHook
}
return nil
}

func vcsRevision() string {
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return "(unknown)"
}
for _, val := range buildInfo.Settings {
if val.Key == "vcs.revision" {
return val.Value
}
}
return "(unknown)"
}
2 changes: 1 addition & 1 deletion cmd/logarea/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"syscall"
"time"

"github.com/eiffel-community/etos-api/internal/config"
config "github.com/eiffel-community/etos-api/internal/configs/logarea"
"github.com/eiffel-community/etos-api/internal/logging"
"github.com/eiffel-community/etos-api/internal/server"
"github.com/eiffel-community/etos-api/pkg/application"
Expand Down
2 changes: 1 addition & 1 deletion cmd/sse/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"syscall"
"time"

"github.com/eiffel-community/etos-api/internal/config"
config "github.com/eiffel-community/etos-api/internal/configs/sse"
"github.com/eiffel-community/etos-api/internal/logging"
"github.com/eiffel-community/etos-api/internal/server"
"github.com/eiffel-community/etos-api/pkg/application"
Expand Down
17 changes: 17 additions & 0 deletions deploy/etos-iut/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.21-alpine AS build
WORKDIR /tmp/iut
COPY . .
RUN apk add --no-cache make=4.4.1-r2 git=2.45.2-r0 && make iut

FROM alpine:3.17.3
ARG TZ
ENV TZ=$TZ

LABEL org.opencontainers.image.source=https://github.com/eiffel-community/etos-api
LABEL org.opencontainers.image.authors=etos-maintainers@googlegroups.com
LABEL org.opencontainers.image.licenses=Apache-2.0

RUN apk add --no-cache tzdata=2024a-r0
ENTRYPOINT ["/app/iut"]

COPY --from=build /tmp/iut/bin/iut /app/iut
8 changes: 8 additions & 0 deletions deploy/etos-iut/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM golang:1.21
WORKDIR /app

COPY ./go.mod ./go.sum ./
RUN go mod tidy
COPY . .
RUN git config --global --add safe.directory /app
EXPOSE 8080
16 changes: 16 additions & 0 deletions deploy/etos-iut/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: "3.7"
services:
etos-iut:
build:
context: .
dockerfile: ./deploy/etos-iut/Dockerfile.dev
args:
http_proxy: "${http_proxy}"
https_proxy: "${https_proxy}"
volumes:
- ./:/app
ports:
- 8080:8080
env_file:
- ./configs/development.env
entrypoint: ["/app/bin/iut"]
2 changes: 1 addition & 1 deletion deploy/etos-logarea/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ services:
- 8080:8080
env_file:
- ./configs/development.env
entrypoint: ["/bin/bash", "./scripts/entrypoint.sh"]
entrypoint: ["/app/bin/logarea"]
24 changes: 18 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@ go 1.21
toolchain go1.22.1

require (
github.com/eiffel-community/eiffelevents-sdk-go v0.0.0-20240807115026-5ca5c194b7dc
github.com/fernet/fernet-go v0.0.0-20240119011108-303da6aec611
github.com/google/uuid v1.6.0
github.com/jmespath/go-jmespath v0.4.0
github.com/julienschmidt/httprouter v1.3.0
github.com/machinebox/graphql v0.2.2
github.com/maxcnunes/httpfake v1.2.4
github.com/package-url/packageurl-go v0.1.3
github.com/sethvargo/go-retry v0.3.0
github.com/sirupsen/logrus v1.9.3
github.com/snowzach/rotatefilehook v0.0.0-20220211133110-53752135082d
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
go.elastic.co/ecslogrus v1.0.0
go.etcd.io/etcd/client/v3 v3.5.14
go.etcd.io/etcd/api/v3 v3.5.15
go.etcd.io/etcd/client/v3 v3.5.15
go.etcd.io/etcd/server/v3 v3.5.14
k8s.io/apimachinery v0.28.2
k8s.io/client-go v0.28.2
)

require (
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Showmax/go-fqdn v1.0.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/clarketm/json v1.17.1 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -40,7 +49,6 @@ require (
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
Expand All @@ -51,22 +59,26 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/magefile/mage v1.9.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matryer/is v1.4.1 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/soheilhy/cmux v0.1.5 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 // indirect
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
go.etcd.io/bbolt v1.3.10 // indirect
go.etcd.io/etcd/api/v3 v3.5.14 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.14 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect
go.etcd.io/etcd/client/v2 v2.305.14 // indirect
go.etcd.io/etcd/pkg/v3 v3.5.14 // indirect
go.etcd.io/etcd/raft/v3 v3.5.14 // indirect
Expand All @@ -86,7 +98,7 @@ require (
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect
Expand Down
Loading

0 comments on commit efad081

Please sign in to comment.