-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new API for listing logs and artifacts from a log area
- Loading branch information
Showing
9 changed files
with
569 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.