mirror of https://github.com/harness/drone.git
[MISC] Move configurations to their respective packages (#256)
parent
001d706998
commit
bfb0466b11
|
@ -16,135 +16,122 @@ import (
|
|||
"github.com/harness/gitness/internal/services/webhook"
|
||||
"github.com/harness/gitness/types"
|
||||
|
||||
"github.com/google/wire"
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
)
|
||||
|
||||
// load returns the system configuration from the
|
||||
// LoadConfig returns the system configuration from the
|
||||
// host environment.
|
||||
func load() (*types.Config, error) {
|
||||
func LoadConfig() (*types.Config, error) {
|
||||
config := new(types.Config)
|
||||
// read the configuration from the environment and
|
||||
// populate the configuration structure.
|
||||
err := envconfig.Process("", config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = ensureInstanceIDIsSet(config)
|
||||
config.InstanceID, err = getSanitizedMachineName()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to ensure that instance ID is set in config: %w", err)
|
||||
}
|
||||
|
||||
err = ensureGitRootIsSet(config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to ensure that git root is set in config: %w", err)
|
||||
}
|
||||
|
||||
err = ensureGitServerHookIsSet(config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to ensure that server hook is set in config: %w", err)
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func ensureInstanceIDIsSet(config *types.Config) error {
|
||||
if config.InstanceID == "" {
|
||||
// use the hostname as default id of the instance
|
||||
hostName, err := os.Hostname()
|
||||
// getSanitizedMachineName gets the name of the machine and returns it in sanitized format.
|
||||
func getSanitizedMachineName() (string, error) {
|
||||
// use the hostname as default id of the instance
|
||||
hostName, err := os.Hostname()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Always cast to lower and remove all unwanted chars
|
||||
// NOTE: this could theoretically lead to overlaps, then it should be passed explicitly
|
||||
// NOTE: for k8s names/ids below modifications are all noops
|
||||
// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/
|
||||
hostName = strings.ToLower(hostName)
|
||||
hostName = strings.Map(func(r rune) rune {
|
||||
switch {
|
||||
case 'a' <= r && r <= 'z':
|
||||
return r
|
||||
case '0' <= r && r <= '9':
|
||||
return r
|
||||
case r == '-', r == '.':
|
||||
return r
|
||||
default:
|
||||
return '_'
|
||||
}
|
||||
}, hostName)
|
||||
|
||||
return hostName, nil
|
||||
}
|
||||
|
||||
// ProvideGitRPCServerConfig loads the gitrpc server config from the environment.
|
||||
// It backfills certain config elements to work with cmdone.
|
||||
func ProvideGitRPCServerConfig() (server.Config, error) {
|
||||
config := server.Config{}
|
||||
err := envconfig.Process("", &config)
|
||||
if err != nil {
|
||||
return server.Config{}, fmt.Errorf("failed to load gitrpc server config: %w", err)
|
||||
}
|
||||
if config.GitHookPath == "" {
|
||||
var executablePath string
|
||||
executablePath, err = os.Executable()
|
||||
if err != nil {
|
||||
return err
|
||||
return server.Config{}, fmt.Errorf("failed to get path of current executable: %w", err)
|
||||
}
|
||||
|
||||
// Always cast to lower and remove all unwanted chars
|
||||
// NOTE: this could theoretically lead to overlaps, then it should be passed explicitly
|
||||
// NOTE: for k8s names/ids below modifications are all noops
|
||||
// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/
|
||||
hostName = strings.ToLower(hostName)
|
||||
hostName = strings.Map(func(r rune) rune {
|
||||
switch {
|
||||
case 'a' <= r && r <= 'z':
|
||||
return r
|
||||
case '0' <= r && r <= '9':
|
||||
return r
|
||||
case r == '-', r == '.':
|
||||
return r
|
||||
default:
|
||||
return '_'
|
||||
}
|
||||
}, hostName)
|
||||
|
||||
config.InstanceID = hostName
|
||||
config.GitHookPath = executablePath
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureGitRootIsSet(config *types.Config) error {
|
||||
if config.Git.Root == "" {
|
||||
homedir, err := os.UserHomeDir()
|
||||
if config.GitRoot == "" {
|
||||
var homedir string
|
||||
homedir, err = os.UserHomeDir()
|
||||
if err != nil {
|
||||
return err
|
||||
return server.Config{}, err
|
||||
}
|
||||
|
||||
config.Git.Root = filepath.Join(homedir, ".gitness")
|
||||
config.GitRoot = filepath.Join(homedir, ".gitness")
|
||||
}
|
||||
|
||||
return nil
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func ensureGitServerHookIsSet(config *types.Config) error {
|
||||
if config.Git.ServerHookPath == "" {
|
||||
executablePath, err := os.Executable()
|
||||
// ProvideGitRPCClientConfig loads the gitrpc client config from the environment.
|
||||
func ProvideGitRPCClientConfig() (gitrpc.Config, error) {
|
||||
config := gitrpc.Config{}
|
||||
err := envconfig.Process("", &config)
|
||||
if err != nil {
|
||||
return gitrpc.Config{}, fmt.Errorf("failed to load gitrpc client config: %w", err)
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// ProvideEventsConfig loads the events config from the environment.
|
||||
func ProvideEventsConfig() (events.Config, error) {
|
||||
config := events.Config{}
|
||||
err := envconfig.Process("", &config)
|
||||
if err != nil {
|
||||
return events.Config{}, fmt.Errorf("failed to load events config: %w", err)
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// ProvideWebhookConfig loads the webhook config from the environment.
|
||||
// It backfills certain config elements if required.
|
||||
func ProvideWebhookConfig() (webhook.Config, error) {
|
||||
config := webhook.Config{}
|
||||
err := envconfig.Process("", &config)
|
||||
if err != nil {
|
||||
return webhook.Config{}, fmt.Errorf("failed to load events config: %w", err)
|
||||
}
|
||||
|
||||
if config.EventReaderName == "" {
|
||||
config.EventReaderName, err = getSanitizedMachineName()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get path of current executable: %w", err)
|
||||
return webhook.Config{}, fmt.Errorf("failed to get sanitized machine name: %w", err)
|
||||
}
|
||||
|
||||
config.Git.ServerHookPath = executablePath
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PackageConfigsWireSet contains providers that generate configs required for sub packages.
|
||||
var PackageConfigsWireSet = wire.NewSet(
|
||||
ProvideGitRPCServerConfig,
|
||||
ProvideGitRPCClientConfig,
|
||||
ProvideEventsConfig,
|
||||
ProvideWebhookConfig,
|
||||
)
|
||||
|
||||
func ProvideGitRPCServerConfig(config *types.Config) server.Config {
|
||||
return server.Config{
|
||||
Bind: config.Server.GRPC.Bind,
|
||||
GitRoot: config.Git.Root,
|
||||
TmpDir: config.Git.TmpDir,
|
||||
ServerHookPath: config.Git.ServerHookPath,
|
||||
}
|
||||
}
|
||||
|
||||
func ProvideGitRPCClientConfig(config *types.Config) gitrpc.Config {
|
||||
return gitrpc.Config{
|
||||
Bind: config.Server.GRPC.Bind,
|
||||
}
|
||||
}
|
||||
|
||||
func ProvideEventsConfig(config *types.Config) events.Config {
|
||||
return events.Config{
|
||||
Mode: events.Mode(config.Events.Mode),
|
||||
Namespace: config.Events.Namespace,
|
||||
MaxStreamLength: config.Events.MaxStreamLength,
|
||||
}
|
||||
}
|
||||
|
||||
func ProvideWebhookConfig(config *types.Config) webhook.Config {
|
||||
return webhook.Config{
|
||||
// Use instanceID as readerName as every instance should be one reader
|
||||
EventReaderName: config.InstanceID,
|
||||
Concurrency: config.Webhook.Concurrency,
|
||||
MaxRetryCount: config.Webhook.MaxRetryCount,
|
||||
AllowLoopback: config.Webhook.AllowLoopback,
|
||||
AllowPrivateNetwork: config.Webhook.AllowPrivateNetwork,
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
// Copyright 2022 Harness Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Polyform Free Trial License
|
||||
// that can be found in the LICENSE.md file for this repository.
|
||||
|
||||
// go:build harness
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/harness/gitness/harness/types"
|
||||
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
)
|
||||
|
||||
func ProvideHarnessConfig() (*types.Config, error) {
|
||||
config := new(types.Config)
|
||||
err := envconfig.Process("", config)
|
||||
return config, err
|
||||
}
|
|
@ -9,6 +9,7 @@ package server
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
pullreqevents "github.com/harness/gitness/internal/events/pullreq"
|
||||
|
||||
"github.com/harness/gitness/events"
|
||||
|
@ -20,7 +21,6 @@ import (
|
|||
"github.com/harness/gitness/harness/client"
|
||||
"github.com/harness/gitness/harness/router"
|
||||
"github.com/harness/gitness/harness/store"
|
||||
"github.com/harness/gitness/harness/types"
|
||||
"github.com/harness/gitness/harness/types/check"
|
||||
"github.com/harness/gitness/internal/api/controller/githook"
|
||||
"github.com/harness/gitness/internal/api/controller/pullreq"
|
||||
|
@ -48,7 +48,7 @@ import (
|
|||
func initSystem(ctx context.Context, config *gitnesstypes.Config) (*system, error) {
|
||||
wire.Build(
|
||||
newSystem,
|
||||
PackageConfigsWireSet,
|
||||
ProvideHarnessConfig,
|
||||
ProvideRedis,
|
||||
bootstrap.WireSet,
|
||||
database.WireSet,
|
||||
|
@ -67,16 +67,19 @@ func initSystem(ctx context.Context, config *gitnesstypes.Config) (*system, erro
|
|||
serviceaccount.WireSet,
|
||||
gitevents.WireSet,
|
||||
pullreqevents.WireSet,
|
||||
ProvideGitRPCServerConfig,
|
||||
gitrpcserver.WireSet,
|
||||
ProvideGitRPCClientConfig,
|
||||
gitrpc.WireSet,
|
||||
types.LoadConfig,
|
||||
router.WireSet,
|
||||
authn.WireSet,
|
||||
authz.WireSet,
|
||||
client.WireSet,
|
||||
store.WireSet,
|
||||
check.WireSet,
|
||||
ProvideEventsConfig,
|
||||
events.WireSet,
|
||||
ProvideWebhookConfig,
|
||||
webhook.WireSet,
|
||||
githook.WireSet,
|
||||
lock.WireSet,
|
||||
|
|
|
@ -7,6 +7,7 @@ package server
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/harness/gitness/events"
|
||||
"github.com/harness/gitness/gitrpc"
|
||||
server2 "github.com/harness/gitness/gitrpc/server"
|
||||
|
@ -16,7 +17,6 @@ import (
|
|||
"github.com/harness/gitness/harness/client"
|
||||
"github.com/harness/gitness/harness/router"
|
||||
"github.com/harness/gitness/harness/store"
|
||||
types2 "github.com/harness/gitness/harness/types"
|
||||
"github.com/harness/gitness/harness/types/check"
|
||||
"github.com/harness/gitness/internal/api/controller/githook"
|
||||
"github.com/harness/gitness/internal/api/controller/pullreq"
|
||||
|
@ -45,7 +45,7 @@ import (
|
|||
|
||||
func initSystem(ctx context.Context, config *types.Config) (*system, error) {
|
||||
checkUser := check.ProvideUserCheck()
|
||||
typesConfig, err := types2.LoadConfig()
|
||||
typesConfig, err := ProvideHarnessConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -102,7 +102,10 @@ func initSystem(ctx context.Context, config *types.Config) (*system, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gitrpcConfig := ProvideGitRPCClientConfig(config)
|
||||
gitrpcConfig, err := ProvideGitRPCClientConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gitrpcInterface, err := gitrpc.ProvideClient(gitrpcConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -114,7 +117,10 @@ func initSystem(ctx context.Context, config *types.Config) (*system, error) {
|
|||
pullReqActivityStore := database.ProvidePullReqActivityStore(db, principalInfoCache)
|
||||
pullReqReviewStore := database.ProvidePullReqReviewStore(db)
|
||||
pullReqReviewerStore := database.ProvidePullReqReviewerStore(db, principalInfoCache)
|
||||
eventsConfig := ProvideEventsConfig(config)
|
||||
eventsConfig, err := ProvideEventsConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
universalClient, err := ProvideRedis(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -130,9 +136,12 @@ func initSystem(ctx context.Context, config *types.Config) (*system, error) {
|
|||
lockConfig := lock.ProvideConfig(config)
|
||||
mutexManager := lock.ProvideMutexManager(lockConfig, universalClient)
|
||||
pullreqController := pullreq.ProvideController(db, provider, authorizer, pullReqStore, pullReqActivityStore, pullReqReviewStore, pullReqReviewerStore, repoStore, principalStore, gitrpcInterface, reporter, mutexManager)
|
||||
webhookConfig, err := ProvideWebhookConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
webhookStore := database.ProvideWebhookStore(db)
|
||||
webhookExecutionStore := database.ProvideWebhookExecutionStore(db)
|
||||
webhookConfig := ProvideWebhookConfig(config)
|
||||
readerFactory, err := events3.ProvideReaderFactory(eventsSystem)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -141,7 +150,7 @@ func initSystem(ctx context.Context, config *types.Config) (*system, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
webhookController := webhook2.ProvideController(config, db, authorizer, webhookStore, webhookExecutionStore, repoStore, webhookService)
|
||||
webhookController := webhook2.ProvideController(webhookConfig, db, authorizer, webhookStore, webhookExecutionStore, repoStore, webhookService)
|
||||
eventsReporter, err := events3.ProvideReporter(eventsSystem)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -152,7 +161,10 @@ func initSystem(ctx context.Context, config *types.Config) (*system, error) {
|
|||
webHandler := router2.ProvideWebHandler(config)
|
||||
routerRouter := router2.ProvideRouter(config, apiHandler, gitHandler, webHandler)
|
||||
serverServer := server.ProvideServer(config, routerRouter)
|
||||
serverConfig := ProvideGitRPCServerConfig(config)
|
||||
serverConfig, err := ProvideGitRPCServerConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
server3, err := server2.ProvideServer(serverConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -44,7 +44,7 @@ func (c *command) run(*kingpin.ParseContext) error {
|
|||
|
||||
// create the system configuration store by loading
|
||||
// data from the environment.
|
||||
config, err := load()
|
||||
config, err := LoadConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("encountered an error while loading configuration: %w", err)
|
||||
}
|
||||
|
|
|
@ -45,7 +45,6 @@ import (
|
|||
func initSystem(ctx context.Context, config *types.Config) (*system, error) {
|
||||
wire.Build(
|
||||
newSystem,
|
||||
PackageConfigsWireSet,
|
||||
ProvideRedis,
|
||||
bootstrap.WireSet,
|
||||
database.WireSet,
|
||||
|
@ -66,11 +65,15 @@ func initSystem(ctx context.Context, config *types.Config) (*system, error) {
|
|||
authz.WireSet,
|
||||
gitevents.WireSet,
|
||||
pullreqevents.WireSet,
|
||||
ProvideGitRPCServerConfig,
|
||||
gitrpcserver.WireSet,
|
||||
ProvideGitRPCClientConfig,
|
||||
gitrpc.WireSet,
|
||||
store.WireSet,
|
||||
check.WireSet,
|
||||
ProvideEventsConfig,
|
||||
events.WireSet,
|
||||
ProvideWebhookConfig,
|
||||
webhook.WireSet,
|
||||
githook.WireSet,
|
||||
lock.WireSet,
|
||||
|
|
|
@ -7,6 +7,7 @@ package server
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/harness/gitness/events"
|
||||
"github.com/harness/gitness/gitrpc"
|
||||
server2 "github.com/harness/gitness/gitrpc/server"
|
||||
|
@ -62,7 +63,10 @@ func initSystem(ctx context.Context, config *types.Config) (*system, error) {
|
|||
pathCache := cache.ProvidePathCache(pathStore, pathTransformation)
|
||||
repoStore := database.ProvideRepoStore(db, pathCache)
|
||||
spaceStore := database.ProvideSpaceStore(db, pathCache)
|
||||
gitrpcConfig := ProvideGitRPCClientConfig(config)
|
||||
gitrpcConfig, err := ProvideGitRPCClientConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gitrpcInterface, err := gitrpc.ProvideClient(gitrpcConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -75,7 +79,10 @@ func initSystem(ctx context.Context, config *types.Config) (*system, error) {
|
|||
pullReqActivityStore := database.ProvidePullReqActivityStore(db, principalInfoCache)
|
||||
pullReqReviewStore := database.ProvidePullReqReviewStore(db)
|
||||
pullReqReviewerStore := database.ProvidePullReqReviewerStore(db, principalInfoCache)
|
||||
eventsConfig := ProvideEventsConfig(config)
|
||||
eventsConfig, err := ProvideEventsConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
universalClient, err := ProvideRedis(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -91,9 +98,12 @@ func initSystem(ctx context.Context, config *types.Config) (*system, error) {
|
|||
lockConfig := lock.ProvideConfig(config)
|
||||
mutexManager := lock.ProvideMutexManager(lockConfig, universalClient)
|
||||
pullreqController := pullreq.ProvideController(db, provider, authorizer, pullReqStore, pullReqActivityStore, pullReqReviewStore, pullReqReviewerStore, repoStore, principalStore, gitrpcInterface, reporter, mutexManager)
|
||||
webhookConfig, err := ProvideWebhookConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
webhookStore := database.ProvideWebhookStore(db)
|
||||
webhookExecutionStore := database.ProvideWebhookExecutionStore(db)
|
||||
webhookConfig := ProvideWebhookConfig(config)
|
||||
readerFactory, err := events3.ProvideReaderFactory(eventsSystem)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -102,7 +112,7 @@ func initSystem(ctx context.Context, config *types.Config) (*system, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
webhookController := webhook2.ProvideController(config, db, authorizer, webhookStore, webhookExecutionStore, repoStore, service)
|
||||
webhookController := webhook2.ProvideController(webhookConfig, db, authorizer, webhookStore, webhookExecutionStore, repoStore, service)
|
||||
eventsReporter, err := events3.ProvideReporter(eventsSystem)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -115,7 +125,10 @@ func initSystem(ctx context.Context, config *types.Config) (*system, error) {
|
|||
webHandler := router.ProvideWebHandler(config)
|
||||
routerRouter := router.ProvideRouter(config, apiHandler, gitHandler, webHandler)
|
||||
serverServer := server.ProvideServer(config, routerRouter)
|
||||
serverConfig := ProvideGitRPCServerConfig(config)
|
||||
serverConfig, err := ProvideGitRPCServerConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
server3, err := server2.ProvideServer(serverConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
package events
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
@ -38,8 +39,22 @@ const (
|
|||
|
||||
// Config defines the config of the events system.
|
||||
type Config struct {
|
||||
Mode Mode `json:"mode"`
|
||||
Namespace string `json:"namespace"`
|
||||
MaxStreamLength int64 `json:"max_stream_length"`
|
||||
ApproxMaxStreamLength bool `json:"approx_max_stream_length"`
|
||||
Mode Mode `envconfig:"GITNESS_EVENTS_MODE" default:"inmemory"`
|
||||
Namespace string `envconfig:"GITNESS_EVENTS_NAMESPACE" default:"gitness"`
|
||||
MaxStreamLength int64 `envconfig:"GITNESS_EVENTS_MAX_STREAM_LENGTH" default:"10000"`
|
||||
ApproxMaxStreamLength bool `envconfig:"GITNESS_EVENTS_APPROX_MAX_STREAM_LENGTH" default:"true"`
|
||||
}
|
||||
|
||||
func (c *Config) Validate() error {
|
||||
if c == nil {
|
||||
return errors.New("config is required")
|
||||
}
|
||||
if c.Mode != ModeRedis && c.Mode != ModeInMemory {
|
||||
return fmt.Errorf("config.Mode '%s' is not supported", c.Mode)
|
||||
}
|
||||
if c.MaxStreamLength < 1 {
|
||||
return errors.New("config.MaxStreamLength has to be a positive number")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -20,6 +20,10 @@ var WireSet = wire.NewSet(
|
|||
)
|
||||
|
||||
func ProvideSystem(config Config, redisClient redis.UniversalClient) (*System, error) {
|
||||
if err := config.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("provided config is invalid: %w", err)
|
||||
}
|
||||
|
||||
var system *System
|
||||
var err error
|
||||
switch config.Mode {
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
package gitrpc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/harness/gitness/gitrpc/rpc"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
@ -21,7 +23,11 @@ type Client struct {
|
|||
mergeService rpc.MergeServiceClient
|
||||
}
|
||||
|
||||
func New(remoteAddr string) (*Client, error) {
|
||||
func New(config Config) (*Client, error) {
|
||||
if err := config.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("provided config is invalid: %w", err)
|
||||
}
|
||||
|
||||
// create interceptors
|
||||
logIntc := NewClientLogInterceptor()
|
||||
|
||||
|
@ -36,7 +42,7 @@ func New(remoteAddr string) (*Client, error) {
|
|||
),
|
||||
}
|
||||
|
||||
conn, err := grpc.Dial(remoteAddr, grpcOpts...)
|
||||
conn, err := grpc.Dial(config.Addr, grpcOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -4,7 +4,22 @@
|
|||
|
||||
package gitrpc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Config represents the config for the gitrpc client.
|
||||
type Config struct {
|
||||
Bind string
|
||||
Addr string `envconfig:"GITRPC_CLIENT_ADDR" default:"127.0.0.1:3001"`
|
||||
}
|
||||
|
||||
func (c *Config) Validate() error {
|
||||
if c == nil {
|
||||
return errors.New("config is required")
|
||||
}
|
||||
if c.Addr == "" {
|
||||
return errors.New("config.Addr is required")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -57,19 +57,19 @@ type Storage interface {
|
|||
|
||||
type RepositoryService struct {
|
||||
rpc.UnimplementedRepositoryServiceServer
|
||||
adapter GitAdapter
|
||||
store Storage
|
||||
reposRoot string
|
||||
serverHookPath string
|
||||
adapter GitAdapter
|
||||
store Storage
|
||||
reposRoot string
|
||||
gitHookPath string
|
||||
}
|
||||
|
||||
func NewRepositoryService(adapter GitAdapter, store Storage, reposRoot string,
|
||||
serverHookPath string) (*RepositoryService, error) {
|
||||
gitHookPath string) (*RepositoryService, error) {
|
||||
return &RepositoryService{
|
||||
adapter: adapter,
|
||||
store: store,
|
||||
reposRoot: reposRoot,
|
||||
serverHookPath: serverHookPath,
|
||||
adapter: adapter,
|
||||
store: store,
|
||||
reposRoot: reposRoot,
|
||||
gitHookPath: gitHookPath,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -170,10 +170,10 @@ func (s RepositoryService) CreateRepository(stream rpc.RepositoryService_CreateR
|
|||
// setup server hook symlinks pointing to configured server hook binary
|
||||
for _, hook := range gitServerHookNames {
|
||||
hookPath := path.Join(repoPath, gitHooksDir, hook)
|
||||
err = os.Symlink(s.serverHookPath, hookPath)
|
||||
err = os.Symlink(s.gitHookPath, hookPath)
|
||||
if err != nil {
|
||||
return status.Errorf(codes.Internal,
|
||||
"failed to setup symlink for hook '%s' ('%s' -> '%s'): %s", hook, hookPath, s.serverHookPath, err)
|
||||
"failed to setup symlink for hook '%s' ('%s' -> '%s'): %s", hook, hookPath, s.gitHookPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,10 +4,35 @@
|
|||
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Config represents the configuration for the gitrpc server.
|
||||
type Config struct {
|
||||
GitRoot string
|
||||
TmpDir string
|
||||
Bind string
|
||||
ServerHookPath string
|
||||
// Bind specifies the addr used to bind the grpc server.
|
||||
Bind string `envconfig:"GITRPC_SERVER_BIND" default:":3001"`
|
||||
// GitRoot specifies the directory containing git related data (e.g. repos, ...)
|
||||
GitRoot string `envconfig:"GITRPC_SERVER_GIT_ROOT"`
|
||||
// TmpDir (optional) specifies the directory for temporary data (e.g. repo clones, ...)
|
||||
TmpDir string `envconfig:"GITRPC_SERVER_TMP_DIR"`
|
||||
// GitHookPath points to the binary used as git server hook.
|
||||
GitHookPath string `envconfig:"GITRPC_SERVER_GIT_HOOK_PATH"`
|
||||
}
|
||||
|
||||
func (c *Config) Validate() error {
|
||||
if c == nil {
|
||||
return errors.New("config is required")
|
||||
}
|
||||
if c.Bind == "" {
|
||||
return errors.New("config.Bind is required")
|
||||
}
|
||||
if c.GitRoot == "" {
|
||||
return errors.New("config.GitRoot is required")
|
||||
}
|
||||
if c.GitHookPath == "" {
|
||||
return errors.New("config.GitHookPath is required")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ package server
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -32,6 +33,9 @@ type Server struct {
|
|||
}
|
||||
|
||||
func NewServer(config Config) (*Server, error) {
|
||||
if err := config.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("configuration is invalid: %w", err)
|
||||
}
|
||||
// Create repos folder
|
||||
reposRoot := filepath.Join(config.GitRoot, repoSubdirName)
|
||||
if _, err := os.Stat(reposRoot); errors.Is(err, os.ErrNotExist) {
|
||||
|
@ -66,7 +70,7 @@ func NewServer(config Config) (*Server, error) {
|
|||
store := storage.NewLocalStore()
|
||||
|
||||
// initialize services
|
||||
repoService, err := service.NewRepositoryService(adapter, store, reposRoot, config.ServerHookPath)
|
||||
repoService, err := service.NewRepositoryService(adapter, store, reposRoot, config.GitHookPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -12,5 +12,5 @@ var WireSet = wire.NewSet(
|
|||
)
|
||||
|
||||
func ProvideClient(config Config) (Interface, error) {
|
||||
return New(config.Bind)
|
||||
return New(config)
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@ import (
|
|||
"github.com/harness/gitness/internal/auth/authz"
|
||||
"github.com/harness/gitness/internal/services/webhook"
|
||||
"github.com/harness/gitness/internal/store"
|
||||
"github.com/harness/gitness/types"
|
||||
|
||||
"github.com/google/wire"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
@ -19,9 +18,9 @@ var WireSet = wire.NewSet(
|
|||
ProvideController,
|
||||
)
|
||||
|
||||
func ProvideController(config *types.Config, db *sqlx.DB, authorizer authz.Authorizer,
|
||||
func ProvideController(config webhook.Config, db *sqlx.DB, authorizer authz.Authorizer,
|
||||
webhookStore store.WebhookStore, webhookExecutionStore store.WebhookExecutionStore,
|
||||
repoStore store.RepoStore, webhookService *webhook.Service) *Controller {
|
||||
return NewController(config.Webhook.AllowLoopback, config.Webhook.AllowPrivateNetwork,
|
||||
return NewController(config.AllowLoopback, config.AllowPrivateNetwork,
|
||||
db, authorizer, webhookStore, webhookExecutionStore, repoStore, webhookService)
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ package webhook
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
@ -23,11 +24,28 @@ const (
|
|||
)
|
||||
|
||||
type Config struct {
|
||||
EventReaderName string `json:"event_reader_name"`
|
||||
Concurrency int `json:"concurrency"`
|
||||
MaxRetryCount int64 `json:"max_retry_count"`
|
||||
AllowPrivateNetwork bool `json:"allow_private_network"`
|
||||
AllowLoopback bool `json:"allow_loopback"`
|
||||
EventReaderName string `envconfig:"GITNESS_WEBHOOK_EVENT_READER_NAME"`
|
||||
Concurrency int `envconfig:"GITNESS_WEBHOOK_CONCURRENCY" default:"4"`
|
||||
MaxRetryCount int64 `envconfig:"GITNESS_WEBHOOK_MAX_RETRY_COUNT" default:"3"`
|
||||
AllowPrivateNetwork bool `envconfig:"GITNESS_WEBHOOK_ALLOW_PRIVATE_NETWORK" default:"false"`
|
||||
AllowLoopback bool `envconfig:"GITNESS_WEBHOOK_ALLOW_LOOPBACK" default:"false"`
|
||||
}
|
||||
|
||||
func (c *Config) Validate() error {
|
||||
if c == nil {
|
||||
return errors.New("config is required")
|
||||
}
|
||||
if c.EventReaderName == "" {
|
||||
return errors.New("config.EventReaderName is required")
|
||||
}
|
||||
if c.Concurrency < 1 {
|
||||
return errors.New("config.Concurrency has to be a positive number")
|
||||
}
|
||||
if c.MaxRetryCount < 0 {
|
||||
return errors.New("config.MaxRetryCount can't be negative")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Service is responsible for processing webhook events.
|
||||
|
@ -49,6 +67,9 @@ func NewService(ctx context.Context, config Config,
|
|||
webhookStore store.WebhookStore, webhookExecutionStore store.WebhookExecutionStore,
|
||||
repoStore store.RepoStore, urlProvider *url.Provider,
|
||||
principalStore store.PrincipalStore, gitRPCClient gitrpc.Interface) (*Service, error) {
|
||||
if err := config.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("provided config is invalid: %w", err)
|
||||
}
|
||||
service := &Service{
|
||||
webhookStore: webhookStore,
|
||||
webhookExecutionStore: webhookExecutionStore,
|
||||
|
|
|
@ -39,10 +39,7 @@ type Config struct {
|
|||
|
||||
// Git defines the git configuration parameters
|
||||
Git struct {
|
||||
Root string `envconfig:"GITNESS_GIT_ROOT"`
|
||||
TmpDir string `envconfig:"GITNESS_GIT_TMP_DIR"` // directory for temporary data (repo clone)
|
||||
ServerHookPath string `envconfig:"GITNESS_GIT_SERVER_HOOK_PATH"` // path to binary used as git server hook
|
||||
DefaultBranch string `envconfig:"GITNESS_GIT_DEFAULTBRANCH" default:"main"`
|
||||
DefaultBranch string `envconfig:"GITNESS_GIT_DEFAULTBRANCH" default:"main"`
|
||||
}
|
||||
|
||||
// Server defines the server configuration parameters.
|
||||
|
@ -56,11 +53,6 @@ type Config struct {
|
|||
GitHost string `envconfig:"GITNESS_HTTP_GIT_HOST" default:"git.localhost"`
|
||||
}
|
||||
|
||||
// GRPC defines the grpc configuration parameters
|
||||
GRPC struct {
|
||||
Bind string `envconfig:"GITNESS_GRPC_BIND" default:":3001"`
|
||||
}
|
||||
|
||||
// Acme defines Acme configuration parameters.
|
||||
Acme struct {
|
||||
Enabled bool `envconfig:"GITNESS_ACME_ENABLED"`
|
||||
|
@ -123,13 +115,6 @@ type Config struct {
|
|||
Password string `envconfig:"GITNESS_REDIS_PASSWORD"`
|
||||
}
|
||||
|
||||
Events struct {
|
||||
Mode string `envconfig:"GITNESS_EVENTS_MODE" default:"inmemory"`
|
||||
Namespace string `envconfig:"GITNESS_EVENTS_NAMESPACE" default:"gitness"`
|
||||
MaxStreamLength int64 `envconfig:"GITNESS_EVENTS_MAX_STREAM_LENGTH" default:"1000"`
|
||||
ApproxMaxStreamLength bool `envconfig:"GITNESS_EVENTS_APPROX_MAX_STREAM_LENGTH" default:"true"`
|
||||
}
|
||||
|
||||
Lock struct {
|
||||
// Provider is a name of distributed lock service like redis, memory, file etc...
|
||||
Provider string `envconfig:"GITNESS_LOCK_PROVIDER" default:"inmemory"`
|
||||
|
@ -143,11 +128,4 @@ type Config struct {
|
|||
// DefaultNamespace is when mutex doesn't specify custom namespace for their keys
|
||||
DefaultNamespace string `envconfig:"GITNESS_LOCK_DEFAULT_NAMESPACE" default:"default"`
|
||||
}
|
||||
|
||||
Webhook struct {
|
||||
MaxRetryCount int64 `envconfig:"GITNESS_WEBHOOK_MAX_RETRY_COUNT" default:"3"`
|
||||
Concurrency int `envconfig:"GITNESS_WEBHOOK_CONCURRENCY" default:"4"`
|
||||
AllowLoopback bool `envconfig:"GITNESS_WEBHOOK_ALLOW_LOOPBACK" default:"false"`
|
||||
AllowPrivateNetwork bool `envconfig:"GITNESS_WEBHOOK_ALLOW_PRIVATE_NETWORK" default:"false"`
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue