Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more addon types #2984

Merged
merged 6 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
woodpeckerGrpcServer "go.woodpecker-ci.org/woodpecker/v2/server/grpc"
"go.woodpecker-ci.org/woodpecker/v2/server/logging"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/config"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/permissions"
"go.woodpecker-ci.org/woodpecker/v2/server/pubsub"
"go.woodpecker-ci.org/woodpecker/v2/server/router"
Expand Down Expand Up @@ -90,15 +89,18 @@ func run(c *cli.Context) error {

_store, err := setupStore(c)
if err != nil {
log.Fatal().Err(err).Msg("cant't setup database store")
log.Fatal().Err(err).Msg("can't setup database store")
}
defer func() {
if err := _store.Close(); err != nil {
log.Error().Err(err).Msg("could not close store")
}
}()

setupEvilGlobals(c, _store, _forge)
err = setupEvilGlobals(c, _store, _forge)
if err != nil {
log.Fatal().Err(err).Msg("can't setup globals")
}

var g errgroup.Group

Expand Down Expand Up @@ -266,7 +268,7 @@ func run(c *cli.Context) error {
return g.Wait()
}

func setupEvilGlobals(c *cli.Context, v store.Store, f forge.Forge) {
func setupEvilGlobals(c *cli.Context, v store.Store, f forge.Forge) error {
// forge
server.Config.Services.Forge = f
server.Config.Services.Timeout = c.Duration("forge-timeout")
Expand All @@ -275,7 +277,11 @@ func setupEvilGlobals(c *cli.Context, v store.Store, f forge.Forge) {
server.Config.Services.Queue = setupQueue(c, v)
server.Config.Services.Logs = logging.New()
server.Config.Services.Pubsub = pubsub.New()
server.Config.Services.Registries = setupRegistryService(c, v)
var err error
server.Config.Services.Registries, err = setupRegistryService(c, v)
if err != nil {
return err
}

// TODO(1544): fix encrypted store
// // encryption
Expand All @@ -285,15 +291,21 @@ func setupEvilGlobals(c *cli.Context, v store.Store, f forge.Forge) {
// log.Fatal().Err(err).Msg("could not create encryption service")
// }
// server.Config.Services.Secrets = setupSecretService(c, encryptedSecretStore)
server.Config.Services.Secrets = setupSecretService(c, v)

server.Config.Services.Environ = setupEnvironService(c, v)
server.Config.Services.Secrets, err = setupSecretService(c, v)
if err != nil {
return err
}
server.Config.Services.Environ, err = setupEnvironService(c, v)
if err != nil {
return err
}
server.Config.Services.Membership = setupMembershipService(c, f)

server.Config.Services.SignaturePrivateKey, server.Config.Services.SignaturePublicKey = setupSignatureKeys(v)

if endpoint := c.String("config-service-endpoint"); endpoint != "" {
server.Config.Services.ConfigService = config.NewHTTP(endpoint, server.Config.Services.SignaturePrivateKey)
server.Config.Services.ConfigService, err = setupConfigService(c)
if err != nil {
return err
}

// authentication
Expand Down Expand Up @@ -368,4 +380,5 @@ func setupEvilGlobals(c *cli.Context, v store.Store, f forge.Forge) {
server.Config.Permissions.Admins = permissions.NewAdmins(c.StringSlice("admin"))
server.Config.Permissions.Orgs = permissions.NewOrgs(c.StringSlice("orgs"))
server.Config.Permissions.OwnersAllowlist = permissions.NewOwnersAllowlist(c.StringSlice("repo-owners"))
return nil
}
55 changes: 48 additions & 7 deletions cmd/server/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"go.woodpecker-ci.org/woodpecker/v2/server/forge/github"
"go.woodpecker-ci.org/woodpecker/v2/server/forge/gitlab"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/config"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/environments"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/registry"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/secrets"
Expand Down Expand Up @@ -110,22 +111,46 @@ func setupQueue(c *cli.Context, s store.Store) queue.Queue {
return queue.WithTaskStore(queue.New(c.Context), s)
}

func setupSecretService(c *cli.Context, s model.SecretStore) model.SecretService {
return secrets.New(c.Context, s)
func setupSecretService(c *cli.Context, s model.SecretStore) (model.SecretService, error) {
addonService, err := addon.Load[model.SecretService](c.StringSlice("addons"), addonTypes.TypeSecretService)
if err != nil {
return nil, err
}
if addonService != nil {
return addonService.Value, nil
}

return secrets.New(c.Context, s), nil
}

func setupRegistryService(c *cli.Context, s store.Store) model.RegistryService {
func setupRegistryService(c *cli.Context, s store.Store) (model.RegistryService, error) {
addonService, err := addon.Load[model.RegistryService](c.StringSlice("addons"), addonTypes.TypeRegistryService)
if err != nil {
return nil, err
}
if addonService != nil {
return addonService.Value, nil
}

if c.String("docker-config") != "" {
return registry.Combined(
registry.New(s),
registry.Filesystem(c.String("docker-config")),
)
), nil
}
return registry.New(s)
return registry.New(s), nil
}

func setupEnvironService(c *cli.Context, _ store.Store) model.EnvironService {
return environments.Parse(c.StringSlice("environment"))
func setupEnvironService(c *cli.Context, _ store.Store) (model.EnvironService, error) {
addonService, err := addon.Load[model.EnvironService](c.StringSlice("addons"), addonTypes.TypeEnvironmentService)
if err != nil {
return nil, err
}
if addonService != nil {
return addonService.Value, nil
}

return environments.Parse(c.StringSlice("environment")), nil
}

func setupMembershipService(_ *cli.Context, r forge.Forge) cache.MembershipService {
Expand Down Expand Up @@ -298,3 +323,19 @@ func setupSignatureKeys(_store store.Store) (crypto.PrivateKey, crypto.PublicKey
privateKey := ed25519.PrivateKey(privKeyStr)
return privateKey, privateKey.Public()
}

func setupConfigService(c *cli.Context) (config.Extension, error) {
addonExt, err := addon.Load[config.Extension](c.StringSlice("addons"), addonTypes.TypeConfigService)
if err != nil {
return nil, err
}
if addonExt != nil {
return addonExt.Value, nil
}

if endpoint := c.String("config-service-endpoint"); endpoint != "" {
return config.NewHTTP(endpoint, server.Config.Services.SignaturePrivateKey), nil
}

return nil, nil
}
4 changes: 4 additions & 0 deletions docs/docs/30-administration/75-addons/00-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Addons can be used for:

- Forges
- Agent backends
- Config services
- Secret services
- Environment services
- Registry services

## Restrictions

Expand Down
12 changes: 8 additions & 4 deletions docs/docs/30-administration/75-addons/20-creating-addons.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ Directly import Woodpecker's Go package (`go.woodpecker-ci.org/woodpecker/woodpe

### Return types

| Addon type | Return type |
| ---------- | -------------------------------------------------------------------------------- |
| `Forge` | `"go.woodpecker-ci.org/woodpecker/woodpecker/v2/server/forge".Forge` |
| `Backend` | `"go.woodpecker-ci.org/woodpecker/woodpecker/v2/pipeline/backend/types".Backend` |
| Addon type | Return type |
| -------------------- | -------------------------------------------------------------------------------- |
| `Forge` | `"go.woodpecker-ci.org/woodpecker/woodpecker/v2/server/forge".Forge` |
| `Backend` | `"go.woodpecker-ci.org/woodpecker/woodpecker/v2/pipeline/backend/types".Backend` |
| `ConfigService` | `"go.woodpecker-ci.org/woodpecker/v2/server/plugins/config".ConfigService` |
| `SecretService` | `"go.woodpecker-ci.org/woodpecker/v2/server/model".SecretService` |
| `EnvironmentService` | `"go.woodpecker-ci.org/woodpecker/v2/server/model".EnvironmentService` |
| `RegistryService` | `"go.woodpecker-ci.org/woodpecker/v2/server/model".RegistryService` |

## Compiling

Expand Down
4 changes: 2 additions & 2 deletions server/model/environ.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
)

var (
errEnvironNameInvalid = errors.New("Invalid Environment Variable Name")
errEnvironValueInvalid = errors.New("Invalid Environment Variable Value")
errEnvironNameInvalid = errors.New("invalid Environment Variable Name")
errEnvironValueInvalid = errors.New("invalid Environment Variable Value")
)

// EnvironService defines a service for managing environment variables.
Expand Down
8 changes: 6 additions & 2 deletions shared/addon/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package types
type Type string

const (
TypeForge Type = "forge"
TypeBackend Type = "backend"
TypeForge Type = "forge"
TypeBackend Type = "backend"
TypeConfigService Type = "config_service"
TypeSecretService Type = "secret_service"
TypeEnvironmentService Type = "environment_service"
TypeRegistryService Type = "registry_service"
)