micro/runtime/options.go

310 lines
6.1 KiB
Go
Raw Normal View History

2019-09-24 18:32:35 +01:00
package runtime
2019-09-24 19:00:11 +01:00
import (
"context"
2019-09-24 19:00:11 +01:00
"io"
2020-05-11 17:09:28 +01:00
"github.com/unistack-org/micro/v3/client"
"github.com/unistack-org/micro/v3/logger"
2019-09-24 19:00:11 +01:00
)
// Options configure runtime
type Options struct {
2020-01-16 13:34:04 +00:00
Scheduler Scheduler
Client client.Client
Logger logger.Logger
Type string
Source string
Image string
}
// Option func signature
type Option func(o *Options)
// WithLogger sets the logger
func WithLogger(l logger.Logger) Option {
return func(o *Options) {
o.Logger = l
}
}
// WithSource sets the base image / repository
func WithSource(src string) Option {
return func(o *Options) {
o.Source = src
}
}
2020-01-16 13:34:04 +00:00
// WithScheduler specifies a scheduler for updates
func WithScheduler(n Scheduler) Option {
return func(o *Options) {
2020-01-16 13:34:04 +00:00
o.Scheduler = n
}
}
2019-11-29 11:35:00 +00:00
// WithType sets the service type to manage
func WithType(t string) Option {
return func(o *Options) {
o.Type = t
}
}
// WithImage sets the image to use
func WithImage(t string) Option {
return func(o *Options) {
o.Image = t
}
}
2020-05-11 17:09:28 +01:00
// WithClient sets the client to use
func WithClient(c client.Client) Option {
return func(o *Options) {
o.Client = c
}
}
// CreateOption func signature
2019-09-24 18:32:35 +01:00
type CreateOption func(o *CreateOptions)
// ReadOption func signature
2019-11-29 11:35:00 +00:00
type ReadOption func(o *ReadOptions)
// CreateOptions configure runtime services
2019-09-24 18:32:35 +01:00
type CreateOptions struct {
Context context.Context
Output io.Writer
Resources *Resources
Secrets map[string]string
Image string
Namespace string
Type string
Command []string
Args []string
Env []string
Retries int
2019-11-29 11:35:00 +00:00
}
// ReadOptions queries runtime services
type ReadOptions struct {
Context context.Context
Service string
Version string
Type string
Namespace string
2019-09-24 18:32:35 +01:00
}
2020-01-17 14:14:47 +00:00
// CreateType sets the type of service to create
func CreateType(t string) CreateOption {
return func(o *CreateOptions) {
o.Type = t
}
}
// CreateImage sets the image to use
func CreateImage(img string) CreateOption {
return func(o *CreateOptions) {
o.Image = img
}
}
// 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 12:27:36 +01:00
}
}
// WithSecret sets a secret to provide the service with
func WithSecret(key, value string) CreateOption {
return func(o *CreateOptions) {
if o.Secrets == nil {
o.Secrets = map[string]string{key: value}
} else {
o.Secrets[key] = value
}
}
}
2019-09-24 19:00:11 +01:00
// WithCommand specifies the command to execute
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 18:32:35 +01:00
return func(o *CreateOptions) {
// set command
o.Args = args
2019-09-24 18:32:35 +01:00
}
}
// WithRetries sets the max retries attempts
func WithRetries(retries int) CreateOption {
return func(o *CreateOptions) {
o.Retries = retries
}
}
// WithEnv sets the created service environment
2019-09-24 18:32:35 +01:00
func WithEnv(env []string) CreateOption {
return func(o *CreateOptions) {
o.Env = env
}
}
2019-09-24 19:00:11 +01:00
// WithOutput sets the arg output
func WithOutput(out io.Writer) CreateOption {
return func(o *CreateOptions) {
o.Output = out
}
}
// ResourceLimits sets the resources for the service to use
func ResourceLimits(r *Resources) CreateOption {
return func(o *CreateOptions) {
o.Resources = r
}
}
2019-11-29 11:35:00 +00:00
// ReadService returns services with the given name
func ReadService(service string) ReadOption {
return func(o *ReadOptions) {
o.Service = service
}
}
// ReadVersion confifgures service version
2019-11-29 11:35:00 +00:00
func ReadVersion(version string) ReadOption {
return func(o *ReadOptions) {
o.Version = version
}
}
2019-11-29 11:35:00 +00:00
// ReadType returns services of the given type
func ReadType(t string) ReadOption {
return func(o *ReadOptions) {
o.Type = t
}
}
// 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
}
}
// UpdateOption func signature
type UpdateOption func(o *UpdateOptions)
// UpdateOptions struct
type UpdateOptions struct {
Context context.Context
Secrets map[string]string
Namespace string
}
// UpdateSecret sets a secret to provide the service with
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
}
}
}
// 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
}
}
// DeleteOption func signature
type DeleteOption func(o *DeleteOptions)
// DeleteOptions struct
type DeleteOptions struct {
Context context.Context
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
}
}
// LogsOption configures runtime logging
type LogsOption func(o *LogsOptions)
// LogsOptions configure runtime logging
type LogsOptions struct {
Context context.Context
Namespace string
Count int64
Stream bool
}
// LogsCount confiures how many existing lines to show
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
}
}
// 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
}
}