* Add String to Runtime interface

* Setup Dynamic Runtime Configuration
This commit is contained in:
ben-toogood 2020-02-05 13:59:35 +00:00 committed by GitHub
parent 4333f00a43
commit bf747a86f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 50 additions and 1 deletions

View File

@ -40,6 +40,11 @@ import (
rmem "github.com/micro/go-micro/v2/registry/memory" rmem "github.com/micro/go-micro/v2/registry/memory"
regSrv "github.com/micro/go-micro/v2/registry/service" regSrv "github.com/micro/go-micro/v2/registry/service"
// runtimes
kRuntime "github.com/micro/go-micro/v2/runtime/kubernetes"
lRuntime "github.com/micro/go-micro/v2/runtime/local"
srvRuntime "github.com/micro/go-micro/v2/runtime/service"
// selectors // selectors
"github.com/micro/go-micro/v2/client/selector/dns" "github.com/micro/go-micro/v2/client/selector/dns"
"github.com/micro/go-micro/v2/client/selector/router" "github.com/micro/go-micro/v2/client/selector/router"
@ -188,6 +193,12 @@ var (
EnvVars: []string{"MICRO_RUNTIME"}, EnvVars: []string{"MICRO_RUNTIME"},
Value: "local", Value: "local",
}, },
&cli.StringFlag{
Name: "runtime_source",
Usage: "Runtime source for building and running services e.g github.com/micro/service",
EnvVars: []string{"MICRO_RUNTIME_SOURCE"},
Value: "github.com/micro/services",
},
&cli.StringFlag{ &cli.StringFlag{
Name: "selector", Name: "selector",
EnvVars: []string{"MICRO_SELECTOR"}, EnvVars: []string{"MICRO_SELECTOR"},
@ -281,7 +292,9 @@ var (
} }
DefaultRuntimes = map[string]func(...runtime.Option) runtime.Runtime{ DefaultRuntimes = map[string]func(...runtime.Option) runtime.Runtime{
"local": runtime.NewRuntime, "local": lRuntime.NewRuntime,
"service": srvRuntime.NewRuntime,
"kubernetes": kRuntime.NewRuntime,
} }
DefaultStores = map[string]func(...store.Option) store.Store{ DefaultStores = map[string]func(...store.Option) store.Store{
@ -580,6 +593,12 @@ func (c *cmd) Before(ctx *cli.Context) error {
} }
} }
if len(ctx.String("runtime_source")) > 0 {
if err := (*c.opts.Runtime).Init(runtime.WithSource(ctx.String("runtime_source"))); err != nil {
log.Fatalf("Error configuring runtime: %v", err)
}
}
// client opts // client opts
if r := ctx.Int("client_retries"); r >= 0 { if r := ctx.Int("client_retries"); r >= 0 {
clientOpts = append(clientOpts, client.Retries(r)) clientOpts = append(clientOpts, client.Retries(r))

View File

@ -2,7 +2,9 @@
package kubernetes package kubernetes
import ( import (
"errors"
"fmt" "fmt"
"strings"
"sync" "sync"
"time" "time"
@ -245,6 +247,10 @@ func (k *kubernetes) Init(opts ...runtime.Option) error {
o(&k.options) o(&k.options)
} }
if strings.HasPrefix(k.options.Source, "github.com") {
return errors.New("invalid source provided to kubernetes runtime, expected docker image")
}
return nil return nil
} }

11
runtime/local/local.go Normal file
View File

@ -0,0 +1,11 @@
// Package local provides a local runtime
package local
import (
"github.com/micro/go-micro/v2/runtime"
)
// NewRuntime returns a new local runtime
func NewRuntime(opts ...runtime.Option) runtime.Runtime {
return runtime.NewRuntime(opts...)
}

View File

@ -12,6 +12,15 @@ type Options struct {
Scheduler Scheduler Scheduler Scheduler
// Service type to manage // Service type to manage
Type string Type string
// Source of the services repository
Source string
}
// WithSource sets the host addresses to be used by the broker
func WithSource(src string) Option {
return func(o *Options) {
o.Source = src
}
} }
// WithScheduler specifies a scheduler for updates // WithScheduler specifies a scheduler for updates

View File

@ -17,6 +17,8 @@ var (
// Runtime is a service runtime manager // Runtime is a service runtime manager
type Runtime interface { type Runtime interface {
// String describes runtime
String() string
// Init initializes runtime // Init initializes runtime
Init(...Option) error Init(...Option) error
// Create registers a service // Create registers a service

View File

@ -170,6 +170,8 @@ func (s *service) Wait() {
s.Metadata["status"] = "error" s.Metadata["status"] = "error"
s.Metadata["error"] = err.Error() s.Metadata["error"] = err.Error()
s.err = err s.err = err
} else {
s.Metadata["status"] = "done"
} }
// no longer running // no longer running