Skip to content

Commit

Permalink
Add a new API for listing logs and artifacts from a log area
Browse files Browse the repository at this point in the history
  • Loading branch information
t-persson committed Jun 4, 2024
1 parent d332bdd commit 21f6e21
Show file tree
Hide file tree
Showing 9 changed files with 569 additions and 13 deletions.
122 changes: 122 additions & 0 deletions cmd/logarea/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright 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"

"github.com/eiffel-community/etos-api/internal/config"
"github.com/eiffel-community/etos-api/internal/logging"
"github.com/eiffel-community/etos-api/internal/server"
"github.com/eiffel-community/etos-api/pkg/application"
v1alpha "github.com/eiffel-community/etos-api/pkg/logarea/v1alpha"
"github.com/sirupsen/logrus"
"github.com/snowzach/rotatefilehook"
"go.elastic.co/ecslogrus"
)

// main sets up logging and starts up the logarea webservice.
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 API LogArea Server",
"version": vcsRevision(),
"name": "ETOS API",
})

log.Info("Loading logarea routes")
v1AlphaLogArea := v1alpha.New(cfg, log, ctx)
defer v1AlphaLogArea.Close()

app := application.New(v1AlphaLogArea)
srv := server.NewWebService(cfg, log, app)

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

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

sig := <-done
log.Infof("%s received", sig.String())

ctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
defer cancel()

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

// 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
}

// vcsRevision returns vcs revision from build info, if any. Otherwise '(unknown)'.
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)"
}
17 changes: 17 additions & 0 deletions deploy/etos-logarea/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.21-alpine AS build
WORKDIR /tmp/logarea
COPY . .
RUN apk add --no-cache make=4.4.1-r2 git=2.43.0-r0 && make build

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/etos-logarea"]

COPY --from=build /tmp/logarea/bin/etos-logarea /app/etos-logarea
8 changes: 8 additions & 0 deletions deploy/etos-logarea/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM golang:1.20
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-logarea/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: "3.7"
services:
etos-logarea:
build:
context: .
dockerfile: ./deploy/etos-logarea/Dockerfile.dev
args:
http_proxy: "${http_proxy}"
https_proxy: "${https_proxy}"
volumes:
- ./:/app
ports:
- 8080:8080
env_file:
- ./configs/development.env
entrypoint: ["/bin/bash", "./scripts/entrypoint.sh"]
2 changes: 1 addition & 1 deletion deploy/etos-sse/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20-alpine AS build
FROM golang:1.21-alpine AS build
WORKDIR /tmp/sse
COPY . .
RUN apk add --no-cache make=4.4.1-r2 git=2.43.4-r0 && make build
Expand Down
31 changes: 27 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
module github.com/eiffel-community/etos-api

go 1.20
go 1.21

toolchain go1.22.1

require (
github.com/fernet/fernet-go v0.0.0-20240119011108-303da6aec611
github.com/jmespath/go-jmespath v0.4.0
github.com/julienschmidt/httprouter v1.3.0
github.com/sirupsen/logrus v1.9.3
github.com/snowzach/rotatefilehook v0.0.0-20220211133110-53752135082d
github.com/stretchr/testify v1.8.2
github.com/stretchr/testify v1.8.4
go.elastic.co/ecslogrus v1.0.0
go.etcd.io/etcd/client/v3 v3.5.14
k8s.io/apimachinery v0.28.2
k8s.io/client-go v0.28.2
)

require (
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
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/go-logr/logr v1.2.4 // 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/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magefile/mage v1.9.0 // indirect
Expand All @@ -40,6 +47,22 @@ require (
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
go.etcd.io/etcd/api/v3 v3.5.14 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.14 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.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
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
Expand Down
Loading

0 comments on commit 21f6e21

Please sign in to comment.