2019-09-24 20:32:35 +03:00
|
|
|
package runtime
|
|
|
|
|
2019-09-24 21:00:11 +03:00
|
|
|
import (
|
2020-04-23 15:53:42 +03:00
|
|
|
"context"
|
2019-09-24 21:00:11 +03:00
|
|
|
"io"
|
2020-05-11 19:09:28 +03:00
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
2019-09-24 21:00:11 +03:00
|
|
|
)
|
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
// Options configure runtime
|
|
|
|
type Options struct {
|
2020-01-16 16:34:04 +03:00
|
|
|
Scheduler Scheduler
|
2021-03-06 19:45:13 +03:00
|
|
|
Client client.Client
|
|
|
|
Logger logger.Logger
|
|
|
|
Type string
|
|
|
|
Source string
|
|
|
|
Image string
|
2020-08-29 17:44:49 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// Option func signature
|
|
|
|
type Option func(o *Options)
|
|
|
|
|
2020-08-29 17:44:49 +03:00
|
|
|
// WithLogger sets the logger
|
|
|
|
func WithLogger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
2020-02-05 16:59:35 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 11:52:25 +03:00
|
|
|
// WithSource sets the base image / repository
|
2020-02-05 16:59:35 +03:00
|
|
|
func WithSource(src string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Source = src
|
|
|
|
}
|
2019-11-02 16:25:10 +03:00
|
|
|
}
|
|
|
|
|
2020-01-16 16:34:04 +03:00
|
|
|
// WithScheduler specifies a scheduler for updates
|
|
|
|
func WithScheduler(n Scheduler) Option {
|
2019-11-02 16:25:10 +03:00
|
|
|
return func(o *Options) {
|
2020-01-16 16:34:04 +03:00
|
|
|
o.Scheduler = n
|
2019-11-02 16:25:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 14:35:00 +03:00
|
|
|
// WithType sets the service type to manage
|
|
|
|
func WithType(t string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Type = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 21:39:59 +03:00
|
|
|
// WithImage sets the image to use
|
|
|
|
func WithImage(t string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Image = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 19:09:28 +03:00
|
|
|
// WithClient sets the client to use
|
|
|
|
func WithClient(c client.Client) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Client = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// CreateOption func signature
|
2019-09-24 20:32:35 +03:00
|
|
|
type CreateOption func(o *CreateOptions)
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// ReadOption func signature
|
2019-11-29 14:35:00 +03:00
|
|
|
type ReadOption func(o *ReadOptions)
|
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
// CreateOptions configure runtime services
|
2019-09-24 20:32:35 +03:00
|
|
|
type CreateOptions struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
Context context.Context
|
|
|
|
Output io.Writer
|
2020-08-14 13:47:28 +03:00
|
|
|
Resources *Resources
|
2021-03-06 19:45:13 +03:00
|
|
|
Secrets map[string]string
|
|
|
|
Image string
|
|
|
|
Namespace string
|
|
|
|
Type string
|
|
|
|
Command []string
|
|
|
|
Args []string
|
|
|
|
Env []string
|
|
|
|
Retries int
|
2019-11-29 14:35:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadOptions queries runtime services
|
|
|
|
type ReadOptions struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
Context context.Context
|
|
|
|
Service string
|
|
|
|
Version string
|
|
|
|
Type string
|
2020-04-23 15:53:42 +03:00
|
|
|
Namespace string
|
2019-09-24 20:32:35 +03:00
|
|
|
}
|
|
|
|
|
2020-01-17 17:14:47 +03:00
|
|
|
// CreateType sets the type of service to create
|
|
|
|
func CreateType(t string) CreateOption {
|
|
|
|
return func(o *CreateOptions) {
|
|
|
|
o.Type = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 21:39:59 +03:00
|
|
|
// CreateImage sets the image to use
|
|
|
|
func CreateImage(img string) CreateOption {
|
2020-02-24 20:47:47 +03:00
|
|
|
return func(o *CreateOptions) {
|
2020-03-13 21:39:59 +03:00
|
|
|
o.Image = img
|
2020-02-24 20:47:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 15:53:42 +03:00
|
|
|
// CreateNamespace sets the namespace
|
|
|
|
func CreateNamespace(ns string) CreateOption {
|
|
|
|
return func(o *CreateOptions) {
|
|
|
|
o.Namespace = ns
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateContext sets the context
|
|
|
|
func CreateContext(ctx context.Context) CreateOption {
|
|
|
|
return func(o *CreateOptions) {
|
|
|
|
o.Context = ctx
|
2020-04-23 14:27:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:41:50 +03:00
|
|
|
// WithSecret sets a secret to provide the service with
|
|
|
|
func WithSecret(key, value string) CreateOption {
|
2020-07-10 18:25:46 +03:00
|
|
|
return func(o *CreateOptions) {
|
2020-07-29 15:41:50 +03:00
|
|
|
if o.Secrets == nil {
|
|
|
|
o.Secrets = map[string]string{key: value}
|
|
|
|
} else {
|
|
|
|
o.Secrets[key] = value
|
|
|
|
}
|
2020-07-10 18:25:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 21:00:11 +03:00
|
|
|
// WithCommand specifies the command to execute
|
2020-03-13 21:39:59 +03:00
|
|
|
func WithCommand(cmd ...string) CreateOption {
|
|
|
|
return func(o *CreateOptions) {
|
|
|
|
// set command
|
|
|
|
o.Command = cmd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithArgs specifies the command to execute
|
|
|
|
func WithArgs(args ...string) CreateOption {
|
2019-09-24 20:32:35 +03:00
|
|
|
return func(o *CreateOptions) {
|
|
|
|
// set command
|
2020-03-13 21:39:59 +03:00
|
|
|
o.Args = args
|
2019-09-24 20:32:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 14:33:36 +03:00
|
|
|
// WithRetries sets the max retries attempts
|
2020-02-07 15:02:41 +03:00
|
|
|
func WithRetries(retries int) CreateOption {
|
|
|
|
return func(o *CreateOptions) {
|
|
|
|
o.Retries = retries
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
// WithEnv sets the created service environment
|
2019-09-24 20:32:35 +03:00
|
|
|
func WithEnv(env []string) CreateOption {
|
|
|
|
return func(o *CreateOptions) {
|
|
|
|
o.Env = env
|
|
|
|
}
|
|
|
|
}
|
2019-09-24 21:00:11 +03:00
|
|
|
|
|
|
|
// WithOutput sets the arg output
|
|
|
|
func WithOutput(out io.Writer) CreateOption {
|
|
|
|
return func(o *CreateOptions) {
|
|
|
|
o.Output = out
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 16:41:40 +03:00
|
|
|
|
2020-08-14 13:47:28 +03:00
|
|
|
// ResourceLimits sets the resources for the service to use
|
|
|
|
func ResourceLimits(r *Resources) CreateOption {
|
|
|
|
return func(o *CreateOptions) {
|
|
|
|
o.Resources = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 14:35:00 +03:00
|
|
|
// ReadService returns services with the given name
|
|
|
|
func ReadService(service string) ReadOption {
|
|
|
|
return func(o *ReadOptions) {
|
|
|
|
o.Service = service
|
|
|
|
}
|
2019-11-15 16:41:40 +03:00
|
|
|
}
|
|
|
|
|
2020-02-07 15:02:41 +03:00
|
|
|
// ReadVersion confifgures service version
|
2019-11-29 14:35:00 +03:00
|
|
|
func ReadVersion(version string) ReadOption {
|
2019-11-25 19:31:14 +03:00
|
|
|
return func(o *ReadOptions) {
|
2019-11-15 16:41:40 +03:00
|
|
|
o.Version = version
|
|
|
|
}
|
|
|
|
}
|
2019-11-29 14:35:00 +03:00
|
|
|
|
|
|
|
// ReadType returns services of the given type
|
|
|
|
func ReadType(t string) ReadOption {
|
|
|
|
return func(o *ReadOptions) {
|
|
|
|
o.Type = t
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 16:40:15 +03:00
|
|
|
|
2020-04-23 15:53:42 +03:00
|
|
|
// ReadNamespace sets the namespace
|
|
|
|
func ReadNamespace(ns string) ReadOption {
|
|
|
|
return func(o *ReadOptions) {
|
|
|
|
o.Namespace = ns
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadContext sets the context
|
|
|
|
func ReadContext(ctx context.Context) ReadOption {
|
|
|
|
return func(o *ReadOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// UpdateOption func signature
|
2020-04-23 15:53:42 +03:00
|
|
|
type UpdateOption func(o *UpdateOptions)
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// UpdateOptions struct
|
2020-04-23 15:53:42 +03:00
|
|
|
type UpdateOptions struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
Context context.Context
|
|
|
|
Secrets map[string]string
|
2020-04-23 15:53:42 +03:00
|
|
|
Namespace string
|
2020-08-18 19:26:14 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// UpdateSecret sets a secret to provide the service with
|
2020-08-18 19:26:14 +03:00
|
|
|
func UpdateSecret(key, value string) UpdateOption {
|
|
|
|
return func(o *UpdateOptions) {
|
|
|
|
if o.Secrets == nil {
|
|
|
|
o.Secrets = map[string]string{key: value}
|
|
|
|
} else {
|
|
|
|
o.Secrets[key] = value
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 15:53:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateNamespace sets the namespace
|
|
|
|
func UpdateNamespace(ns string) UpdateOption {
|
|
|
|
return func(o *UpdateOptions) {
|
|
|
|
o.Namespace = ns
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateContext sets the context
|
|
|
|
func UpdateContext(ctx context.Context) UpdateOption {
|
|
|
|
return func(o *UpdateOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// DeleteOption func signature
|
2020-04-23 15:53:42 +03:00
|
|
|
type DeleteOption func(o *DeleteOptions)
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// DeleteOptions struct
|
2020-04-23 15:53:42 +03:00
|
|
|
type DeleteOptions struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
Context context.Context
|
2020-04-23 15:53:42 +03:00
|
|
|
Namespace string
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteNamespace sets the namespace
|
|
|
|
func DeleteNamespace(ns string) DeleteOption {
|
|
|
|
return func(o *DeleteOptions) {
|
|
|
|
o.Namespace = ns
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteContext sets the context
|
|
|
|
func DeleteContext(ctx context.Context) DeleteOption {
|
|
|
|
return func(o *DeleteOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 16:40:15 +03:00
|
|
|
// LogsOption configures runtime logging
|
|
|
|
type LogsOption func(o *LogsOptions)
|
|
|
|
|
|
|
|
// LogsOptions configure runtime logging
|
|
|
|
type LogsOptions struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
Context context.Context
|
2020-04-23 15:53:42 +03:00
|
|
|
Namespace string
|
2021-03-06 19:45:13 +03:00
|
|
|
Count int64
|
|
|
|
Stream bool
|
2020-04-01 16:40:15 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// LogsCount confiures how many existing lines to show
|
2020-04-01 16:40:15 +03:00
|
|
|
func LogsCount(count int64) LogsOption {
|
|
|
|
return func(l *LogsOptions) {
|
|
|
|
l.Count = count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogsStream configures whether to stream new lines
|
|
|
|
func LogsStream(stream bool) LogsOption {
|
|
|
|
return func(l *LogsOptions) {
|
|
|
|
l.Stream = stream
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 15:53:42 +03:00
|
|
|
|
|
|
|
// LogsNamespace sets the namespace
|
|
|
|
func LogsNamespace(ns string) LogsOption {
|
|
|
|
return func(o *LogsOptions) {
|
|
|
|
o.Namespace = ns
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogsContext sets the context
|
|
|
|
func LogsContext(ctx context.Context) LogsOption {
|
|
|
|
return func(o *LogsOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|