Skip to content

Commit

Permalink
add clientlog service
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <jkoberg@owncloud.com>
  • Loading branch information
kobergj committed Sep 5, 2023
1 parent 8836c86 commit 1ba5acb
Show file tree
Hide file tree
Showing 31 changed files with 818 additions and 0 deletions.
1 change: 1 addition & 0 deletions .drone.star
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ config = {
"services/auth-bearer",
"services/auth-machine",
"services/auth-service",
"services/clientlog",
"services/eventhistory",
"services/frontend",
"services/gateway",
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ OCIS_MODULES = \
services/auth-bearer \
services/auth-machine \
services/auth-service \
services/clientlog \
services/eventhistory \
services/frontend \
services/gateway \
Expand Down
5 changes: 5 additions & 0 deletions changelog/unreleased/clientlog-service.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Introduce clientlog service

Add the clientlog service which will send machine readable notifications to clients

https://github.com/owncloud/ocis/pull/7217
2 changes: 2 additions & 0 deletions docs/services/general-info/new-service-checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Use this checklist with copy/paste in your PR - right from the beginning. It ren
- [ ] Make the service startable for binary and individual startup:
- For single binary add service to `ocis/pkg/runtime`
- For individual startup add service to `ocis/pkg/commands`
- Add the service config to `ocis-pkg/config/defaultconfig.go`
- [ ] If the service is using service accounts, add it to `ocis/pkg/init/init.go`
- [ ] Add the service to `.drone.star` to enable CI.
- [ ] Inform doc team in an _early stage_ to review the readme AND the environment variables created.
- The description must reflect the behaviour AND usually has a positive code quality impact.
Expand Down
2 changes: 2 additions & 0 deletions ocis-pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
authbearer "github.com/owncloud/ocis/v2/services/auth-bearer/pkg/config"
authmachine "github.com/owncloud/ocis/v2/services/auth-machine/pkg/config"
authservice "github.com/owncloud/ocis/v2/services/auth-service/pkg/config"
clientlog "github.com/owncloud/ocis/v2/services/clientlog/pkg/config"
eventhistory "github.com/owncloud/ocis/v2/services/eventhistory/pkg/config"
frontend "github.com/owncloud/ocis/v2/services/frontend/pkg/config"
gateway "github.com/owncloud/ocis/v2/services/gateway/pkg/config"
Expand Down Expand Up @@ -85,6 +86,7 @@ type Config struct {
AuthBearer *authbearer.Config `yaml:"auth_bearer"`
AuthMachine *authmachine.Config `yaml:"auth_machine"`
AuthService *authservice.Config `yaml:"auth_service"`
Clientlog *clientlog.Config `yaml:"clientlog"`
EventHistory *eventhistory.Config `yaml:"eventhistory"`
Frontend *frontend.Config `yaml:"frontend"`
Gateway *gateway.Config `yaml:"gateway"`
Expand Down
2 changes: 2 additions & 0 deletions ocis-pkg/config/defaultconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
authbearer "github.com/owncloud/ocis/v2/services/auth-bearer/pkg/config/defaults"
authmachine "github.com/owncloud/ocis/v2/services/auth-machine/pkg/config/defaults"
authservice "github.com/owncloud/ocis/v2/services/auth-service/pkg/config/defaults"
clientlog "github.com/owncloud/ocis/v2/services/clientlog/pkg/config/defaults"
eventhistory "github.com/owncloud/ocis/v2/services/eventhistory/pkg/config/defaults"
frontend "github.com/owncloud/ocis/v2/services/frontend/pkg/config/defaults"
gateway "github.com/owncloud/ocis/v2/services/gateway/pkg/config/defaults"
Expand Down Expand Up @@ -57,6 +58,7 @@ func DefaultConfig() *Config {
AuthBearer: authbearer.DefaultConfig(),
AuthMachine: authmachine.DefaultConfig(),
AuthService: authservice.DefaultConfig(),
Clientlog: clientlog.DefaultConfig(),
EventHistory: eventhistory.DefaultConfig(),
Frontend: frontend.DefaultConfig(),
Gateway: gateway.DefaultConfig(),
Expand Down
6 changes: 6 additions & 0 deletions ocis/pkg/command/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
authbearer "github.com/owncloud/ocis/v2/services/auth-bearer/pkg/command"
authmachine "github.com/owncloud/ocis/v2/services/auth-machine/pkg/command"
authservice "github.com/owncloud/ocis/v2/services/auth-service/pkg/command"
clientlog "github.com/owncloud/ocis/v2/services/clientlog/pkg/command"
eventhistory "github.com/owncloud/ocis/v2/services/eventhistory/pkg/command"
frontend "github.com/owncloud/ocis/v2/services/frontend/pkg/command"
gateway "github.com/owncloud/ocis/v2/services/gateway/pkg/command"
Expand Down Expand Up @@ -89,6 +90,11 @@ var svccmds = []register.Command{
cfg.AuthService.Commons = cfg.Commons
})
},
func(cfg *config.Config) *cli.Command {
return ServiceCommand(cfg, cfg.Clientlog.Service.Name, clientlog.GetCommands(cfg.Clientlog), func(c *config.Config) {
cfg.Clientlog.Commons = cfg.Commons
})
},
func(cfg *config.Config) *cli.Command {
return ServiceCommand(cfg, cfg.EventHistory.Service.Name, eventhistory.GetCommands(cfg.EventHistory), func(c *config.Config) {
cfg.EventHistory.Commons = cfg.Commons
Expand Down
8 changes: 8 additions & 0 deletions ocis/pkg/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ type AuthService struct {
ServiceAccount ServiceAccount `yaml:"service_account"`
}

type Clientlog struct {
ServiceAccount ServiceAccount `yaml:"service_account"`
}

type Nats struct {
// The nats config has a field called nats
Nats struct {
Expand Down Expand Up @@ -193,6 +197,7 @@ type OcisConfig struct {
Gateway Gateway
Userlog Userlog
AuthService AuthService `yaml:"auth_service"`
Clientlog Clientlog
}

func checkConfigPath(configPath string) error {
Expand Down Expand Up @@ -377,6 +382,9 @@ func CreateConfig(insecure, forceOverwrite bool, configPath, adminPassword strin
Notifications: Notifications{
ServiceAccount: serviceAccount,
},
Clientlog: Clientlog{
ServiceAccount: serviceAccount,
},
}

if insecure {
Expand Down
6 changes: 6 additions & 0 deletions ocis/pkg/runtime/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
authbasic "github.com/owncloud/ocis/v2/services/auth-basic/pkg/command"
authmachine "github.com/owncloud/ocis/v2/services/auth-machine/pkg/command"
authservice "github.com/owncloud/ocis/v2/services/auth-service/pkg/command"
clientlog "github.com/owncloud/ocis/v2/services/clientlog/pkg/command"
eventhistory "github.com/owncloud/ocis/v2/services/eventhistory/pkg/command"
frontend "github.com/owncloud/ocis/v2/services/frontend/pkg/command"
gateway "github.com/owncloud/ocis/v2/services/gateway/pkg/command"
Expand Down Expand Up @@ -141,6 +142,11 @@ func NewService(options ...Option) (*Service, error) {
cfg.AuthService.Commons = cfg.Commons
return authservice.Execute(cfg.AuthService)
})
reg(opts.Config.Clientlog.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
cfg.Clientlog.Context = ctx
cfg.Clientlog.Commons = cfg.Commons
return clientlog.Execute(cfg.Clientlog)
})
reg(opts.Config.EventHistory.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
cfg.EventHistory.Context = ctx
cfg.EventHistory.Commons = cfg.Commons
Expand Down
38 changes: 38 additions & 0 deletions services/clientlog/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
SHELL := bash
NAME := clientlog

include ../../.make/recursion.mk

############ tooling ############
ifneq (, $(shell command -v go 2> /dev/null)) # suppress `command not found warnings` for non go targets in CI
include ../../.bingo/Variables.mk
endif

############ go tooling ############
include ../../.make/go.mk

############ release ############
include ../../.make/release.mk

############ docs generate ############
include ../../.make/docs.mk

.PHONY: docs-generate
docs-generate: config-docs-generate

############ generate ############
include ../../.make/generate.mk

.PHONY: ci-go-generate
ci-go-generate: $(MOCKERY) # CI runs ci-node-generate automatically before this target
$(MOCKERY) --dir ../../protogen/gen/ocis/services/eventhistory/v0 --case underscore --name EventHistoryService

.PHONY: ci-node-generate
ci-node-generate:

############ licenses ############
.PHONY: ci-node-check-licenses
ci-node-check-licenses:

.PHONY: ci-node-save-licenses
ci-node-save-licenses:
14 changes: 14 additions & 0 deletions services/clientlog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Clientlog service

The `clientlog` service is responsible for composing machine readable notifications for clients

## The `...log` service ecosystem

`...log` services (`userlog`, `clientlog`) are responsible for composing notifications for a certain audience.
- `userlog` service translates and adjust messages to be human readable
- `clientlog` service composes machine readable messages so clients can act without needing to query the server
- `sse` service is only responsible for sending these messages. It does not care about their form or language

## `clientlog` events

The messages the `clientlog` service sends are meant to be used by clients, not by users. The client might for example be informed that a file is finished postprocessing, so it can make the file available to the user without needing to make another call to the server.
14 changes: 14 additions & 0 deletions services/clientlog/cmd/clientlog/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"os"

"github.com/owncloud/ocis/v2/services/clientlog/pkg/command"
"github.com/owncloud/ocis/v2/services/clientlog/pkg/config/defaults"
)

func main() {
if err := command.Execute(defaults.DefaultConfig()); err != nil {
os.Exit(1)
}
}
18 changes: 18 additions & 0 deletions services/clientlog/pkg/command/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package command

import (
"github.com/owncloud/ocis/v2/services/clientlog/pkg/config"
"github.com/urfave/cli/v2"
)

// Health is the entrypoint for the health command.
func Health(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "health",
Usage: "Check health status",
Action: func(c *cli.Context) error {
// Not implemented
return nil
},
}
}
34 changes: 34 additions & 0 deletions services/clientlog/pkg/command/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package command

import (
"os"

"github.com/owncloud/ocis/v2/ocis-pkg/clihelper"
"github.com/owncloud/ocis/v2/services/clientlog/pkg/config"
"github.com/urfave/cli/v2"
)

// GetCommands provides all commands for this service
func GetCommands(cfg *config.Config) cli.Commands {
return []*cli.Command{
// start this service
Server(cfg),

// interaction with this service

// infos about this service
Health(cfg),
Version(cfg),
}
}

// Execute is the entry point for the clientlog command.
func Execute(cfg *config.Config) error {
app := clihelper.DefaultApp(&cli.App{
Name: "clientlog",
Usage: "starts clientlog service",
Commands: GetCommands(cfg),
})

return app.Run(os.Args)
}
137 changes: 137 additions & 0 deletions services/clientlog/pkg/command/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package command

import (
"context"
"fmt"

"github.com/cs3org/reva/v2/pkg/events"
"github.com/cs3org/reva/v2/pkg/events/stream"
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
"github.com/oklog/run"
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
"github.com/owncloud/ocis/v2/ocis-pkg/registry"
"github.com/owncloud/ocis/v2/ocis-pkg/service/debug"
"github.com/owncloud/ocis/v2/ocis-pkg/tracing"
"github.com/owncloud/ocis/v2/ocis-pkg/version"
"github.com/owncloud/ocis/v2/services/clientlog/pkg/config"
"github.com/owncloud/ocis/v2/services/clientlog/pkg/config/parser"
"github.com/owncloud/ocis/v2/services/clientlog/pkg/logging"
"github.com/owncloud/ocis/v2/services/clientlog/pkg/metrics"
"github.com/owncloud/ocis/v2/services/clientlog/pkg/service"
"github.com/urfave/cli/v2"
)

// all events we care about
var _registeredEvents = []events.Unmarshaller{
events.UploadReady{},
}

// Server is the entrypoint for the server command.
func Server(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "server",
Usage: fmt.Sprintf("start the %s service without runtime (unsupervised mode)", cfg.Service.Name),
Category: "server",
Before: func(c *cli.Context) error {
return configlog.ReturnFatal(parser.ParseConfig(cfg))
},
Action: func(c *cli.Context) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
tracerProvider, err := tracing.GetServiceTraceProvider(cfg.Tracing, cfg.Service.Name)
if err != nil {
return err
}

/*
grpcClient, err := ogrpc.NewClient(
append(ogrpc.GetClientOptions(cfg.GRPCClientTLS), ogrpc.WithTraceProvider(tracerProvider))...,
)
if err != nil {
return err
}
*/ // TODO: remove

gr := run.Group{}
ctx, cancel := func() (context.Context, context.CancelFunc) {
if cfg.Context == nil {
return context.WithCancel(context.Background())
}
return context.WithCancel(cfg.Context)
}()

mtrcs := metrics.New()
mtrcs.BuildInfo.WithLabelValues(version.GetString()).Set(1)

defer cancel()

stream, err := stream.NatsFromConfig(cfg.Service.Name, stream.NatsConfig(cfg.Events))
if err != nil {
return err
}

tm, err := pool.StringToTLSMode(cfg.GRPCClientTLS.Mode)
if err != nil {
return err
}
gatewaySelector, err := pool.GatewaySelector(
cfg.RevaGateway,
pool.WithTLSCACert(cfg.GRPCClientTLS.CACert),
pool.WithTLSMode(tm),
pool.WithRegistry(registry.GetRegistry()),
pool.WithTracerProvider(tracerProvider),
)
if err != nil {
return fmt.Errorf("could not get reva client selector: %s", err)
}

{
svc, err := service.NewClientlogService(
service.Logger(logger),
service.Config(cfg),
service.Stream(stream),
service.GatewaySelector(gatewaySelector),
service.RegisteredEvents(_registeredEvents),
service.TraceProvider(tracerProvider),
)

if err != nil {
logger.Info().Err(err).Str("transport", "http").Msg("Failed to initialize server")
return err
}

gr.Add(func() error {
return svc.Run()
}, func(err error) {
logger.Error().
Str("transport", "http").
Err(err).
Msg("Shutting down server")

cancel()
})
}

{
server := debug.NewService(
debug.Logger(logger),
debug.Name(cfg.Service.Name),
debug.Version(version.GetString()),
debug.Address(cfg.Debug.Addr),
debug.Token(cfg.Debug.Token),
debug.Pprof(cfg.Debug.Pprof),
debug.Zpages(cfg.Debug.Zpages),
debug.Health(handlers.Health),
debug.Ready(handlers.Ready),
)

gr.Add(server.ListenAndServe, func(_ error) {
_ = server.Shutdown(ctx)
cancel()
})
}

return gr.Run()
},
}
}
Loading

0 comments on commit 1ba5acb

Please sign in to comment.