forked from eiffel-community/etos-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generic execution space provider for ETOS API
- Loading branch information
Showing
32 changed files
with
2,812 additions
and
23 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
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,147 @@ | ||
// 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" | ||
|
||
config "github.com/eiffel-community/etos-api/internal/configs/executionspace" | ||
"github.com/eiffel-community/etos-api/internal/executionspace/etcd" | ||
"github.com/eiffel-community/etos-api/internal/executionspace/provider" | ||
"github.com/eiffel-community/etos-api/internal/logging" | ||
"github.com/eiffel-community/etos-api/internal/server" | ||
"github.com/eiffel-community/etos-api/pkg/application" | ||
"github.com/sirupsen/logrus" | ||
"github.com/snowzach/rotatefilehook" | ||
"github.com/eiffel-community/etos-api/internal/executionspace/logging/rabbitmqhook" | ||
"github.com/eiffel-community/etos-api/internal/executionspace/rabbitmq" | ||
providerservice "github.com/eiffel-community/etos-api/pkg/executionspace/v1alpha" | ||
"go.elastic.co/ecslogrus" | ||
) | ||
|
||
// main sets up logging and starts up the webservice. | ||
func main() { | ||
cfg := config.Get() | ||
ctx := context.Background() | ||
|
||
var hooks []logrus.Hook | ||
if publisher := remoteLogging(cfg); publisher != nil { | ||
defer publisher.Close() | ||
hooks = append(hooks, rabbitmqhook.NewRabbitMQHook(publisher)) | ||
} | ||
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 Execution Space Provider Kubernetes", | ||
"version": vcsRevision(), | ||
"name": "ETOS Execution Space Provider", | ||
"user_log": false, | ||
}) | ||
|
||
log.Info("Loading v1alpha routes") | ||
provider := provider.Kubernetes{}.New(etcd.New(cfg), cfg) | ||
providerServiceApp := providerservice.New(cfg, log, provider, ctx) | ||
defer providerServiceApp.Close() | ||
handler := application.New(providerServiceApp) | ||
|
||
srv := server.NewWebService(cfg, log, handler) | ||
|
||
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("WebService shutdown: %+v", err) | ||
} | ||
}() | ||
|
||
sig := <-done | ||
log.Infof("%s received", sig.String()) | ||
|
||
ctx, cancel := context.WithTimeout(ctx, cfg.Timeout()) | ||
defer cancel() | ||
|
||
if err := srv.Close(ctx); err != nil { | ||
log.Errorf("WebService shutdown failed: %+v", err) | ||
} | ||
log.Info("Wait for checkout, flash and checkin jobs 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 | ||
} | ||
|
||
// remoteLogging starts a new rabbitmq publisher if the rabbitmq parameters are set | ||
// Warning: Must call publisher.Close() on the publisher returned from this function | ||
func remoteLogging(cfg config.Config) *rabbitmq.Publisher { | ||
if cfg.RabbitMQHookUrl() != "" { | ||
if cfg.RabbitMQHookExchangeName() == "" { | ||
panic("-rabbitmq_hook_exchange (env:ETOS_RABBITMQ_EXCHANGE) must be set when using -rabbitmq_hook_url (env:ETOS_RABBITMQ_URL)") | ||
} | ||
publisher := rabbitmq.NewPublisher(rabbitmq.PublisherConfig{ | ||
URL: cfg.RabbitMQHookUrl(), | ||
ExchangeName: cfg.RabbitMQHookExchangeName(), | ||
}) | ||
return publisher | ||
} | ||
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)" | ||
} |
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.22-alpine AS build | ||
WORKDIR /tmp/executionspace | ||
COPY . . | ||
RUN apk add --no-cache make=4.4.1-r2 git=2.45.2-r0 && make executionspace | ||
|
||
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/executionspace"] | ||
|
||
COPY --from=build /tmp/executionspace/bin/executionspace /app/executionspace |
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.22 | ||
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-executionspace: | ||
build: | ||
context: . | ||
dockerfile: ./deploy/etos-executionspace/Dockerfile.dev | ||
args: | ||
http_proxy: "${http_proxy}" | ||
https_proxy: "${https_proxy}" | ||
volumes: | ||
- ./:/app | ||
ports: | ||
- 8080:8080 | ||
env_file: | ||
- ./configs/development.env | ||
entrypoint: ["/app/bin/executionspace"] |
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.