make nats-js-kv the default registry

Signed-off-by: jkoberg <jkoberg@owncloud.com>
This commit is contained in:
jkoberg
2023-12-19 12:13:28 +01:00
parent 39b6fe1750
commit e1fad5c6e9
9 changed files with 133 additions and 55 deletions
+5
View File
@@ -0,0 +1,5 @@
Enhancement: Make nats-js-kv the default registry
The previously used default `mdns` is faulty. Deprecated it and `consul`, `nats` and `etcd` implementations
https://github.com/owncloud/ocis/pull/8011
+42
View File
@@ -0,0 +1,42 @@
---
title: Registry
date: 2023-12-19T00:00:00+00:00
weight: 20
geekdocRepo: https://github.com/owncloud/ocis
geekdocEditPath: edit/master/docs/services/general-info
geekdocFilePath: registry.md
geekdocCollapseSection: true
---
To be able to communicate with each other, services need to register in a common registry.
## Configuration
The type of registry to use can be configured with the `MICRO_REGISTRY` environment variable. Supported values are:
### `memory`
Setting the environment variable to `memory` starts an inmemory registry. This only works with the single binary deployment.
### `nats-js-kv`
Set the environment variable to `nats-js-kv` (or leave it empty) to use a nats-js key value store as registry.
- Note: If not running build-in nats, `MICRO_REGISTRY_ADDRESS` needs to be set to the address of the nats-js cluster. (Same as `OCIS_EVENTS_ENDPOINT`)
- Optional: Use `MICRO_REGISTRY_AUTH_USERNAME` and `MICRO_REGISTRY_AUTH_PASSWORD` to authenticate with the nats cluster.
This is the default.
### `kubernetes`
When deploying in a kubernetes cluster, the kubernetes registry can be used. Additionally the `MICRO_REGISTRY_ADDRESS` environment
variable needs to be set to the url of the kubernetes registry.
### Deprecated registries
These registries are currently working but will be removed in a later version. It is recommended to switch to a supported one.
- `nats`. Uses a registry based on nats streams. Requires `MICRO_REGISTRY_ADDRESS` to bet set.
- `etcd`. Uses an etcd cluster as registry. Requires `MICRO_REGISTRY_ADDRESS` to bet set.
- `consul`. Uses `HashiCorp Consul` as registry. Requires `MICRO_REGISTRY_ADDRESS` to bet set.
- `mdns`. Uses multicast dns for registration. This type can have unwanted side effects when other devices in the local network use mdns too.
+81 -43
View File
@@ -1,6 +1,7 @@
package registry package registry
import ( import (
"fmt"
"os" "os"
"strings" "strings"
"sync" "sync"
@@ -21,73 +22,110 @@ import (
const ( const (
registryEnv = "MICRO_REGISTRY" registryEnv = "MICRO_REGISTRY"
registryAddressEnv = "MICRO_REGISTRY_ADDRESS" registryAddressEnv = "MICRO_REGISTRY_ADDRESS"
regisryUsernameEnv = "MICRO_REGISTRY_AUTH_USERNAME" registryUsernameEnv = "MICRO_REGISTRY_AUTH_USERNAME"
registryPasswordEnv = "MICRO_REGISTRY_AUTH_PASSWORD" registryPasswordEnv = "MICRO_REGISTRY_AUTH_PASSWORD"
) )
var ( var (
once sync.Once _once sync.Once
regPlugin string _reg mRegistry.Registry
reg mRegistry.Registry
) )
// Configure configures the registry // Config is the config for a registry
func Configure(plugin string) { type Config struct {
if reg == nil { Type string `mapstructure:"type"`
regPlugin = plugin Addresses []string `mapstructure:"addresses"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
DisableCache bool `mapstructure:"disable_cache"`
}
// Option allows configuring the registry
type Option func(*Config)
// Inmemory overrides env values to use an in-memory registry
func Inmemory() Option {
return func(c *Config) {
c.Type = "memory"
} }
} }
// GetRegistry returns a configured micro registry based on Micro env vars. // GetRegistry returns a configured micro registry based on Micro env vars.
// It defaults to mDNS, so mind that systems with mDNS disabled by default (i.e SUSE) will have a hard time // It defaults to mDNS, so mind that systems with mDNS disabled by default (i.e SUSE) will have a hard time
// and it needs to explicitly use etcd. Os awareness for providing a working registry out of the box should be done. // and it needs to explicitly use etcd. Os awareness for providing a working registry out of the box should be done.
func GetRegistry() mRegistry.Registry { func GetRegistry(opts ...Option) mRegistry.Registry {
once.Do(func() { _once.Do(func() {
addresses := strings.Split(os.Getenv(registryAddressEnv), ",") cfg := getEnvs(opts...)
// prefer env of setting from Configure()
plugin := os.Getenv(registryEnv)
if plugin == "" {
plugin = regPlugin
}
switch plugin { switch cfg.Type {
case "nats": default:
reg = natsr.NewRegistry( fmt.Println("Attention: unknown registry type, using default nats-js-kv")
mRegistry.Addrs(addresses...), fallthrough
natsr.RegisterAction("put"), case "natsjs", "nats-js", "nats-js-kv": // for backwards compatibility - we will stick with one of those
_reg = natsjsregistry.NewRegistry(
mRegistry.Addrs(cfg.Addresses...),
natsjsregistry.Authenticate(cfg.Username, cfg.Password),
) )
case "kubernetes": case "kubernetes":
reg = kubernetesr.NewRegistry( _reg = kubernetesr.NewRegistry(
mRegistry.Addrs(addresses...), mRegistry.Addrs(cfg.Addresses...),
)
case "etcd":
reg = etcdr.NewRegistry(
mRegistry.Addrs(addresses...),
)
case "consul":
reg = consulr.NewRegistry(
mRegistry.Addrs(addresses...),
) )
case "memory": case "memory":
reg = memr.NewRegistry() _reg = memr.NewRegistry()
case "natsjs", "nats-js", "nats-js-kv": // for backwards compatibility - we will stick with one of those cfg.DisableCache = true // no cache needed for in-memory registry
reg = natsjsregistry.NewRegistry( case "nats":
mRegistry.Addrs(addresses...), fmt.Println("Attention: nats registry is deprecated, use nats-js-kv instead")
natsjsregistry.Authenticate(os.Getenv(regisryUsernameEnv), os.Getenv(registryPasswordEnv)), _reg = natsr.NewRegistry(
mRegistry.Addrs(cfg.Addresses...),
natsr.RegisterAction("put"),
) )
default: case "etcd":
reg = mdnsr.NewRegistry() fmt.Println("Attention: etcd registry is deprecated, use nats-js-kv instead")
_reg = etcdr.NewRegistry(
mRegistry.Addrs(cfg.Addresses...),
)
case "consul":
fmt.Println("Attention: consul registry is deprecated, use nats-js-kv instead")
_reg = consulr.NewRegistry(
mRegistry.Addrs(cfg.Addresses...),
)
case "mdns":
fmt.Println("Attention: mdns registry is deprecated, use nats-js-kv instead")
_reg = mdnsr.NewRegistry()
} }
// No cache needed for in-memory registry // Disable cache if wanted
if plugin != "memory" { if !cfg.DisableCache {
reg = cache.New(reg, cache.WithTTL(30*time.Second)) _reg = cache.New(_reg, cache.WithTTL(30*time.Second))
} }
// fixme: lazy initialization of reva registry, needs refactor to a explicit call per service // fixme: lazy initialization of reva registry, needs refactor to a explicit call per service
_ = rRegistry.Init(reg) _ = rRegistry.Init(_reg)
}) })
// always use cached registry to prevent registry // always use cached registry to prevent registry
// lookup for every request // lookup for every request
return reg return _reg
}
func getEnvs(opts ...Option) *Config {
cfg := &Config{
Type: "nats-js-kv",
Addresses: []string{"127.0.0.1:9233"},
Username: os.Getenv(registryUsernameEnv),
Password: os.Getenv(registryPasswordEnv),
}
if s := os.Getenv(registryEnv); s != "" {
cfg.Type = s
}
if s := strings.Split(os.Getenv(registryAddressEnv), ","); len(s) > 0 {
cfg.Addresses = s
}
for _, o := range opts {
o(cfg)
}
return cfg
} }
-2
View File
@@ -4,7 +4,6 @@ import (
"github.com/owncloud/ocis/v2/ocis-pkg/config" "github.com/owncloud/ocis/v2/ocis-pkg/config"
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
"github.com/owncloud/ocis/v2/ocis-pkg/config/parser" "github.com/owncloud/ocis/v2/ocis-pkg/config/parser"
"github.com/owncloud/ocis/v2/ocis-pkg/registry"
"github.com/owncloud/ocis/v2/ocis/pkg/register" "github.com/owncloud/ocis/v2/ocis/pkg/register"
"github.com/owncloud/ocis/v2/ocis/pkg/runtime" "github.com/owncloud/ocis/v2/ocis/pkg/runtime"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@@ -21,7 +20,6 @@ func Server(cfg *config.Config) *cli.Command {
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
// Prefer the in-memory registry as the default when running in single-binary mode // Prefer the in-memory registry as the default when running in single-binary mode
registry.Configure("memory")
r := runtime.New(cfg) r := runtime.New(cfg)
return r.Start() return r.Start()
}, },
@@ -11,8 +11,7 @@ import (
) )
func init() { func init() {
registry.Configure("memory") r := registry.GetRegistry(registry.Inmemory())
r := registry.GetRegistry()
service := registry.BuildGRPCService("com.owncloud.api.gateway", "", "", "") service := registry.BuildGRPCService("com.owncloud.api.gateway", "", "", "")
service.Nodes = []*mRegistry.Node{{ service.Nodes = []*mRegistry.Node{{
Address: "any", Address: "any",
@@ -11,8 +11,7 @@ import (
) )
func init() { func init() {
registry.Configure("memory") r := registry.GetRegistry(registry.Inmemory())
r := registry.GetRegistry()
service := registry.BuildGRPCService("com.owncloud.api.gateway", "", "", "") service := registry.BuildGRPCService("com.owncloud.api.gateway", "", "", "")
service.Nodes = []*mRegistry.Node{{ service.Nodes = []*mRegistry.Node{{
Address: "any", Address: "any",
@@ -11,8 +11,7 @@ import (
) )
func init() { func init() {
registry.Configure("memory") r := registry.GetRegistry(registry.Inmemory())
r := registry.GetRegistry()
service := registry.BuildGRPCService("com.owncloud.api.gateway", "", "", "") service := registry.BuildGRPCService("com.owncloud.api.gateway", "", "", "")
service.Nodes = []*mRegistry.Node{{ service.Nodes = []*mRegistry.Node{{
Address: "any", Address: "any",
@@ -11,8 +11,7 @@ import (
) )
func init() { func init() {
registry.Configure("memory") r := registry.GetRegistry(registry.Inmemory())
r := registry.GetRegistry()
service := registry.BuildGRPCService("com.owncloud.api.gateway", "", "", "") service := registry.BuildGRPCService("com.owncloud.api.gateway", "", "", "")
service.Nodes = []*mRegistry.Node{{ service.Nodes = []*mRegistry.Node{{
Address: "any", Address: "any",
@@ -11,8 +11,7 @@ import (
) )
func init() { func init() {
registry.Configure("memory") r := registry.GetRegistry(registry.Inmemory())
r := registry.GetRegistry()
service := registry.BuildGRPCService("com.owncloud.api.gateway", "", "", "") service := registry.BuildGRPCService("com.owncloud.api.gateway", "", "", "")
service.Nodes = []*mRegistry.Node{{ service.Nodes = []*mRegistry.Node{{
Address: "any", Address: "any",