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

Tweak go-micro behavior #840

Merged
merged 13 commits into from
Nov 12, 2020
5 changes: 3 additions & 2 deletions accounts/pkg/command/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"os"

"github.com/owncloud/ocis/ocis-pkg/registry"

"github.com/micro/cli/v2"
"github.com/micro/go-micro/v2/registry/mdns"
tw "github.com/olekukonko/tablewriter"
"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/accounts/pkg/flagset"
Expand All @@ -18,7 +19,7 @@ func PrintVersion(cfg *config.Config) *cli.Command {
Usage: "Print the versions of the running instances",
Flags: flagset.ListAccountsWithConfig(cfg),
Action: func(c *cli.Context) error {
reg := mdns.NewRegistry()
reg := *registry.GetRegistry()
services, err := reg.GetService(cfg.GRPC.Namespace + "." + cfg.Server.Name)
if err != nil {
fmt.Println(fmt.Errorf("could not get accounts services from the registry: %v", err))
Expand Down
7 changes: 3 additions & 4 deletions accounts/pkg/service/v0/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package service
import (
"context"

mclient "github.com/micro/go-micro/v2/client"
"github.com/owncloud/ocis/ocis-pkg/service/grpc"

olog "github.com/owncloud/ocis/ocis-pkg/log"
settings "github.com/owncloud/ocis/settings/pkg/proto/v0"
ssvc "github.com/owncloud/ocis/settings/pkg/service/v0"
Expand All @@ -26,9 +27,7 @@ const (

// RegisterPermissions registers permissions for account management and group management with the settings service.
func RegisterPermissions(l *olog.Logger) {
// TODO this won't work with a registry other than mdns. Look into Micro's client initialization.
// https://github.com/owncloud/ocis-proxy/issues/38
service := settings.NewBundleService("com.owncloud.api.settings", mclient.DefaultClient)
service := settings.NewBundleService("com.owncloud.api.settings", grpc.DefaultClient)

permissionRequests := generateAccountManagementPermissionsRequests()
for i := range permissionRequests {
Expand Down
7 changes: 3 additions & 4 deletions accounts/pkg/service/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
"strings"
"time"

"github.com/owncloud/ocis/ocis-pkg/service/grpc"

"github.com/owncloud/ocis/accounts/pkg/storage"
"github.com/owncloud/ocis/ocis-pkg/indexer"
idxcfg "github.com/owncloud/ocis/ocis-pkg/indexer/config"
idxerrs "github.com/owncloud/ocis/ocis-pkg/indexer/errors"

mclient "github.com/micro/go-micro/v2/client"
"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/accounts/pkg/proto/v0"
"github.com/owncloud/ocis/ocis-pkg/log"
Expand All @@ -34,9 +35,7 @@ func New(opts ...Option) (s *Service, err error) {

roleService := options.RoleService
if roleService == nil {
// https://github.com/owncloud/ocis-proxy/issues/38
// TODO this won't work with a registry other than mdns. Look into Micro's client initialization.
roleService = settings.NewRoleService("com.owncloud.api.settings", mclient.DefaultClient)
roleService = settings.NewRoleService("com.owncloud.api.settings", grpc.DefaultClient)
}
roleManager := options.RoleManager
if roleManager == nil {
Expand Down
6 changes: 2 additions & 4 deletions accounts/pkg/service/v0/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package service
import (
"context"

mclient "github.com/micro/go-micro/v2/client"
olog "github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/ocis-pkg/service/grpc"
settings "github.com/owncloud/ocis/settings/pkg/proto/v0"
ssvc "github.com/owncloud/ocis/settings/pkg/service/v0"
)
Expand All @@ -15,9 +15,7 @@ const (

// RegisterSettingsBundles pushes the settings bundle definitions for this extension to the ocis-settings service.
func RegisterSettingsBundles(l *olog.Logger) {
// TODO this won't work with a registry other than mdns. Look into Micro's client initialization.
// https://github.com/owncloud/ocis-proxy/issues/38
service := settings.NewBundleService("com.owncloud.api.settings", mclient.DefaultClient)
service := settings.NewBundleService("com.owncloud.api.settings", grpc.DefaultClient)

bundleRequests := []settings.SaveBundleRequest{
generateBundleProfileRequest(),
Expand Down
14 changes: 14 additions & 0 deletions changelog/unreleased/better-go-micro-adoption.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Enhancement: Better adopt Go-Micro

Tags: ocis

There are a few building blocks that we were relying on default behavior, such as `micro.Registry` and the go-micro client. In order for oCIS to work in any environment and not relying in black magic configuration or running daemons we need to be able to:

- Provide with a configurable go-micro registry.
- Use our own go-micro client adjusted to our own needs (i.e: custom timeout, custom dial timeout, custom transport...)

This PR is relying on 2 env variables from Micro: `MICRO_REGISTRY` and `MICRO_REGISTRY_ADDRESS`. The latter does not make sense to provide if the registry is not `etcd`.

The current implementation only accounts for `mdns` and `etcd` registries, defaulting to `mdns` when not explicitly defined to use `etcd`.

https://github.com/owncloud/ocis/pull/840
7 changes: 4 additions & 3 deletions glauth/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"time"

"github.com/owncloud/ocis/ocis-pkg/service/grpc"

"github.com/owncloud/ocis/glauth/pkg/metrics"

"github.com/owncloud/ocis/glauth/pkg/crypto"
Expand All @@ -17,7 +19,6 @@ import (
glauthcfg "github.com/glauth/glauth/pkg/config"

"github.com/micro/cli/v2"
"github.com/micro/go-micro/v2/client"
"github.com/oklog/run"
openzipkin "github.com/openzipkin/zipkin-go"
zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http"
Expand Down Expand Up @@ -309,6 +310,6 @@ func Server(cfg *config.Config) *cli.Command {

// getAccountsServices returns an ocis-accounts service
func getAccountsServices() (accounts.AccountsService, accounts.GroupsService) {
return accounts.NewAccountsService("com.owncloud.api.accounts", client.DefaultClient),
accounts.NewGroupsService("com.owncloud.api.accounts", client.DefaultClient)
return accounts.NewAccountsService("com.owncloud.api.accounts", grpc.DefaultClient),
accounts.NewGroupsService("com.owncloud.api.accounts", grpc.DefaultClient)
}
5 changes: 3 additions & 2 deletions konnectd/pkg/command/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"os"

"github.com/owncloud/ocis/ocis-pkg/registry"

"github.com/micro/cli/v2"
"github.com/micro/go-micro/v2/registry/mdns"
tw "github.com/olekukonko/tablewriter"
"github.com/owncloud/ocis/konnectd/pkg/config"
"github.com/owncloud/ocis/konnectd/pkg/flagset"
Expand All @@ -18,7 +19,7 @@ func PrintVersion(cfg *config.Config) *cli.Command {
Usage: "Print the versions of the running instances",
Flags: flagset.ListKonnectdWithConfig(cfg),
Action: func(c *cli.Context) error {
reg := mdns.NewRegistry()
reg := *registry.GetRegistry()
services, err := reg.GetService(cfg.Service.Namespace + "." + cfg.Service.Name)
if err != nil {
fmt.Println(fmt.Errorf("could not get konnectd services from the registry: %v", err))
Expand Down
2 changes: 2 additions & 0 deletions ocis-pkg/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,8 @@ github.com/micro/cli/v2 v2.1.2 h1:43J1lChg/rZCC1rvdqZNFSQDrGT7qfMrtp6/ztpIkEM=
github.com/micro/cli/v2 v2.1.2/go.mod h1:EguNh6DAoWKm9nmk+k/Rg0H3lQnDxqzu5x5srOtGtYg=
github.com/micro/go-micro/v2 v2.9.1 h1:+S9koIrNWARjpP6k2TZ7kt0uC9zUJtNXzIdZTZRms7Q=
github.com/micro/go-micro/v2 v2.9.1/go.mod h1:x55ZM3Puy0FyvvkR3e0ha0xsE9DFwfPSUMWAIbFY0SY=
github.com/micro/go-plugins v1.5.1 h1:swcFD7ynCTUo98APqIEIbPu2XMd6yVGTnI8PqdnCwOQ=
github.com/micro/go-plugins/v2 v2.0.0 h1:QbzQBaOVNtAIhjTgtLoOHtlhcwNFbcJenEsT66IwpkQ=
github.com/micro/go-plugins/wrapper/trace/opencensus/v2 v2.9.1 h1:IaZUsLp0Omb/ozDnRKEvVY56C0UocBdPxxg2S2Pk2j0=
github.com/micro/go-plugins/wrapper/trace/opencensus/v2 v2.9.1/go.mod h1:26UmOLM/I487NqTg3n6zJiBrYmIb684M2Zp4WH98XzU=
github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00vh2OMYv+xgHpAMF4=
Expand Down
35 changes: 35 additions & 0 deletions ocis-pkg/registry/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package registry

import (
"os"
"strings"

etcdr "github.com/micro/go-micro/v2/registry/etcd"
mdnsr "github.com/micro/go-micro/v2/registry/mdns"

"github.com/micro/go-micro/v2/registry"
)

var (
registryEnv = "MICRO_REGISTRY"
registryAddressEnv = "MICRO_REGISTRY_ADDRESS"
)

// 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
// and it needs to explicitly use etcd. Os awareness for providing a working registry out of the box should be done.
func GetRegistry() *registry.Registry {
addresses := strings.Split(os.Getenv(registryAddressEnv), ",")

var r registry.Registry
switch os.Getenv(registryEnv) {
case "etcd":
r = etcdr.NewRegistry(
registry.Addrs(addresses...),
)
default:
r = mdnsr.NewRegistry()
}

return &r
}
29 changes: 29 additions & 0 deletions ocis-pkg/service/grpc/service.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
package grpc

import (
"os"
"strings"
"time"

"github.com/micro/go-micro/v2"
mclient "github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/grpc"

etcdr "github.com/micro/go-micro/v2/registry/etcd"
mdnsr "github.com/micro/go-micro/v2/registry/mdns"

"github.com/micro/go-micro/v2/registry"

"github.com/micro/go-plugins/wrapper/trace/opencensus/v2"
"github.com/owncloud/ocis/ocis-pkg/wrapper/prometheus"
)

// DefaultClient is a custom ocis grpc configured client.
var DefaultClient = newGrpcClient()

func newGrpcClient() mclient.Client {
var r registry.Registry
switch os.Getenv("MICRO_REGISTRY") {
case "etcd":
r = etcdr.NewRegistry()
default:
r = mdnsr.NewRegistry()
}

c := grpc.NewClient(
mclient.RequestTimeout(10*time.Second),
mclient.Registry(r),
)
return c
}

// Service simply wraps the go-micro grpc service.
type Service struct {
micro.Service
Expand All @@ -33,6 +61,7 @@ func NewService(opts ...Option) Service {
".",
),
),
micro.Client(DefaultClient),
micro.Version(sopts.Version),
micro.Address(sopts.Address),
micro.WrapHandler(prometheus.NewHandlerWrapper()),
Expand Down
1 change: 1 addition & 0 deletions ocis/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/go-test/deep v1.0.6 // indirect
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
github.com/micro/cli/v2 v2.1.2
github.com/micro/go-micro/v2 v2.9.1
github.com/micro/micro/v2 v2.8.0
github.com/openzipkin/zipkin-go v0.2.5
github.com/owncloud/flaex v0.2.0
Expand Down
Loading