From bf747a86f4e00d3022e67f786a243d6247297429 Mon Sep 17 00:00:00 2001 From: ben-toogood Date: Wed, 5 Feb 2020 13:59:35 +0000 Subject: [PATCH] Runtime (#1160) * Add String to Runtime interface * Setup Dynamic Runtime Configuration --- config/cmd/cmd.go | 21 ++++++++++++++++++++- runtime/kubernetes/kubernetes.go | 6 ++++++ runtime/local/local.go | 11 +++++++++++ runtime/{default.go => local_runtime.go} | 0 runtime/options.go | 9 +++++++++ runtime/runtime.go | 2 ++ runtime/service.go | 2 ++ 7 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 runtime/local/local.go rename runtime/{default.go => local_runtime.go} (100%) diff --git a/config/cmd/cmd.go b/config/cmd/cmd.go index 04fa0d23..f2b8b4a0 100644 --- a/config/cmd/cmd.go +++ b/config/cmd/cmd.go @@ -40,6 +40,11 @@ import ( rmem "github.com/micro/go-micro/v2/registry/memory" 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 "github.com/micro/go-micro/v2/client/selector/dns" "github.com/micro/go-micro/v2/client/selector/router" @@ -188,6 +193,12 @@ var ( EnvVars: []string{"MICRO_RUNTIME"}, 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{ Name: "selector", EnvVars: []string{"MICRO_SELECTOR"}, @@ -281,7 +292,9 @@ var ( } 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{ @@ -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 if r := ctx.Int("client_retries"); r >= 0 { clientOpts = append(clientOpts, client.Retries(r)) diff --git a/runtime/kubernetes/kubernetes.go b/runtime/kubernetes/kubernetes.go index 995ea9d9..520ab78b 100644 --- a/runtime/kubernetes/kubernetes.go +++ b/runtime/kubernetes/kubernetes.go @@ -2,7 +2,9 @@ package kubernetes import ( + "errors" "fmt" + "strings" "sync" "time" @@ -245,6 +247,10 @@ func (k *kubernetes) Init(opts ...runtime.Option) error { 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 } diff --git a/runtime/local/local.go b/runtime/local/local.go new file mode 100644 index 00000000..e8ed179e --- /dev/null +++ b/runtime/local/local.go @@ -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...) +} diff --git a/runtime/default.go b/runtime/local_runtime.go similarity index 100% rename from runtime/default.go rename to runtime/local_runtime.go diff --git a/runtime/options.go b/runtime/options.go index 5c2ce688..1034e7f2 100644 --- a/runtime/options.go +++ b/runtime/options.go @@ -12,6 +12,15 @@ type Options struct { Scheduler Scheduler // Service type to manage 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 diff --git a/runtime/runtime.go b/runtime/runtime.go index 4dfe5d27..ce1b03f3 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -17,6 +17,8 @@ var ( // Runtime is a service runtime manager type Runtime interface { + // String describes runtime + String() string // Init initializes runtime Init(...Option) error // Create registers a service diff --git a/runtime/service.go b/runtime/service.go index ea42dc9e..45f70470 100644 --- a/runtime/service.go +++ b/runtime/service.go @@ -170,6 +170,8 @@ func (s *service) Wait() { s.Metadata["status"] = "error" s.Metadata["error"] = err.Error() s.err = err + } else { + s.Metadata["status"] = "done" } // no longer running