Skip to content

Commit

Permalink
Generic execution space provider for ETOS API
Browse files Browse the repository at this point in the history
  • Loading branch information
andmat900 committed Sep 19, 2024
1 parent efad081 commit ccf6f06
Show file tree
Hide file tree
Showing 34 changed files with 2,928 additions and 23 deletions.
2 changes: 1 addition & 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 iut
PROGRAMS = sse logarea iut executionspace
COMPILEDAEMON = $(GOBIN)/CompileDaemon
GIT = git
GOLANGCI_LINT = $(GOBIN)/golangci-lint
Expand Down
147 changes: 147 additions & 0 deletions cmd/executionspace/main.go
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, logger), 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)"
}
17 changes: 17 additions & 0 deletions deploy/etos-executionspace/Dockerfile
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
8 changes: 8 additions & 0 deletions deploy/etos-executionspace/Dockerfile.dev
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
16 changes: 16 additions & 0 deletions deploy/etos-executionspace/docker-compose.yml
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"]
48 changes: 26 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/eiffel-community/etos-api

go 1.21
go 1.22.0

toolchain go1.22.1

Expand All @@ -21,8 +21,8 @@ require (
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
k8s.io/apimachinery v0.31.1
k8s.io/client-go v0.31.1
)

require (
Expand All @@ -34,26 +34,28 @@ require (
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
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.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/swag v0.22.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.0.1 // indirect
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/gorilla/websocket v1.4.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -65,17 +67,19 @@ require (
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/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // 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/rabbitmq/amqp091-go v1.10.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/x448/float16 v0.8.4 // indirect
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
go.etcd.io/bbolt v1.3.10 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect
Expand All @@ -93,28 +97,28 @@ require (
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/crypto v0.21.0 // indirect
golang.org/x/net v0.23.0 // indirect
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.15.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.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.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
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.28.2 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
k8s.io/api v0.31.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading

0 comments on commit ccf6f06

Please sign in to comment.