github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/runtime/options.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/runtime/options.go
    14  
    15  package runtime
    16  
    17  import (
    18  	"context"
    19  	"io"
    20  
    21  	"github.com/tickoalcantara12/micro/v3/service/client"
    22  )
    23  
    24  type Option func(o *Options)
    25  
    26  // Options configure runtime
    27  type Options struct {
    28  	// Service type to manage
    29  	Type string
    30  	// Client to use when making requests
    31  	Client client.Client
    32  	// Base image to use
    33  	Image string
    34  	// Source of the services repository
    35  	Source string
    36  	// Context to store additional options
    37  	Context context.Context
    38  }
    39  
    40  // WithSource sets the base image / repository
    41  func WithSource(src string) Option {
    42  	return func(o *Options) {
    43  		o.Source = src
    44  	}
    45  }
    46  
    47  // WithType sets the service type to manage
    48  func WithType(t string) Option {
    49  	return func(o *Options) {
    50  		o.Type = t
    51  	}
    52  }
    53  
    54  // WithImage sets the image to use
    55  func WithImage(t string) Option {
    56  	return func(o *Options) {
    57  		o.Image = t
    58  	}
    59  }
    60  
    61  // WithClient sets the client to use
    62  func WithClient(c client.Client) Option {
    63  	return func(o *Options) {
    64  		o.Client = c
    65  	}
    66  }
    67  
    68  type CreateOption func(o *CreateOptions)
    69  
    70  type ReadOption func(o *ReadOptions)
    71  
    72  // CreateOptions configure runtime services
    73  type CreateOptions struct {
    74  	// Command to execut
    75  	Command []string
    76  	// Args to pass into command
    77  	Args []string
    78  	// Environment to configure
    79  	Env []string
    80  	// Entrypoint within the folder (e.g. in the case of a mono-repo)
    81  	Entrypoint string
    82  	// Log output
    83  	Output io.Writer
    84  	// Type of service to create
    85  	Type string
    86  	// Retries before failing deploy
    87  	Retries int
    88  	// Specify the image to use
    89  	Image string
    90  	// Port to expose
    91  	Port string
    92  	// Namespace to create the service in
    93  	Namespace string
    94  	// Specify the context to use
    95  	Context context.Context
    96  	// Secrets to use
    97  	Secrets map[string]string
    98  	// Resources to allocate the service
    99  	Resources *Resources
   100  	// Volumes to mount
   101  	Volumes map[string]string
   102  	// ServiceAccount to start the container with
   103  	ServiceAccount string
   104  	// Number of instances to run
   105  	Instances int
   106  	// Force the service ignore the service status
   107  	Force bool
   108  }
   109  
   110  // ReadOptions queries runtime services
   111  type ReadOptions struct {
   112  	// Service name
   113  	Service string
   114  	// Version queries services with given version
   115  	Version string
   116  	// Type of service
   117  	Type string
   118  	// Namespace the service is running in
   119  	Namespace string
   120  	// Specify the context to use
   121  	Context context.Context
   122  }
   123  
   124  // CreateType sets the type of service to create
   125  func CreateType(t string) CreateOption {
   126  	return func(o *CreateOptions) {
   127  		o.Type = t
   128  	}
   129  }
   130  
   131  // CreateImage sets the image to use
   132  func CreateImage(img string) CreateOption {
   133  	return func(o *CreateOptions) {
   134  		o.Image = img
   135  	}
   136  }
   137  
   138  // CreateNamespace sets the namespace
   139  func CreateNamespace(ns string) CreateOption {
   140  	return func(o *CreateOptions) {
   141  		o.Namespace = ns
   142  	}
   143  }
   144  
   145  // CreateContext sets the context
   146  func CreateContext(ctx context.Context) CreateOption {
   147  	return func(o *CreateOptions) {
   148  		o.Context = ctx
   149  	}
   150  }
   151  
   152  // CreateEntrypoint sets the entrypoint
   153  func CreateEntrypoint(e string) CreateOption {
   154  	return func(o *CreateOptions) {
   155  		o.Entrypoint = e
   156  	}
   157  }
   158  
   159  // WithServiceAccount sets the ServiceAccount
   160  func WithServiceAccount(s string) CreateOption {
   161  	return func(o *CreateOptions) {
   162  		o.ServiceAccount = s
   163  	}
   164  }
   165  
   166  // WithSecret sets a secret to provide the service with
   167  func WithSecret(key, value string) CreateOption {
   168  	return func(o *CreateOptions) {
   169  		if o.Secrets == nil {
   170  			o.Secrets = map[string]string{key: value}
   171  		} else {
   172  			o.Secrets[key] = value
   173  		}
   174  	}
   175  }
   176  
   177  // WithCommand specifies the command to execute
   178  func WithCommand(cmd ...string) CreateOption {
   179  	return func(o *CreateOptions) {
   180  		// set command
   181  		o.Command = cmd
   182  	}
   183  }
   184  
   185  // WithArgs specifies the command to execute
   186  func WithArgs(args ...string) CreateOption {
   187  	return func(o *CreateOptions) {
   188  		// set command
   189  		o.Args = args
   190  	}
   191  }
   192  
   193  // WithRetries sets the max retries attemps
   194  func WithRetries(retries int) CreateOption {
   195  	return func(o *CreateOptions) {
   196  		o.Retries = retries
   197  	}
   198  }
   199  
   200  // WithEnv sets the created service environment
   201  func WithEnv(env []string) CreateOption {
   202  	return func(o *CreateOptions) {
   203  		o.Env = env
   204  	}
   205  }
   206  
   207  // WithOutput sets the arg output
   208  func WithOutput(out io.Writer) CreateOption {
   209  	return func(o *CreateOptions) {
   210  		o.Output = out
   211  	}
   212  }
   213  
   214  // WithVolume adds a volume to be mounted
   215  func WithVolume(name, path string) CreateOption {
   216  	return func(o *CreateOptions) {
   217  		if o.Volumes == nil {
   218  			o.Volumes = map[string]string{name: path}
   219  		} else {
   220  			o.Volumes[name] = path
   221  		}
   222  	}
   223  }
   224  
   225  // WithPort sets the port to expose
   226  func WithPort(p string) CreateOption {
   227  	return func(o *CreateOptions) {
   228  		o.Port = p
   229  	}
   230  }
   231  
   232  // CreateInstances sets the number of instances
   233  func CreateInstances(v int) CreateOption {
   234  	return func(o *CreateOptions) {
   235  		o.Instances = v
   236  	}
   237  }
   238  
   239  // ResourceLimits sets the resources for the service to use
   240  func ResourceLimits(r *Resources) CreateOption {
   241  	return func(o *CreateOptions) {
   242  		o.Resources = r
   243  	}
   244  }
   245  
   246  // WithForce sets the sign to force restart the service
   247  func WithForce(f bool) CreateOption {
   248  	return func(o *CreateOptions) {
   249  		o.Force = f
   250  	}
   251  }
   252  
   253  // ReadService returns services with the given name
   254  func ReadService(service string) ReadOption {
   255  	return func(o *ReadOptions) {
   256  		o.Service = service
   257  	}
   258  }
   259  
   260  // ReadVersion configures service version
   261  func ReadVersion(version string) ReadOption {
   262  	return func(o *ReadOptions) {
   263  		o.Version = version
   264  	}
   265  }
   266  
   267  // ReadType returns services of the given type
   268  func ReadType(t string) ReadOption {
   269  	return func(o *ReadOptions) {
   270  		o.Type = t
   271  	}
   272  }
   273  
   274  // ReadNamespace sets the namespace
   275  func ReadNamespace(ns string) ReadOption {
   276  	return func(o *ReadOptions) {
   277  		o.Namespace = ns
   278  	}
   279  }
   280  
   281  // ReadContext sets the context
   282  func ReadContext(ctx context.Context) ReadOption {
   283  	return func(o *ReadOptions) {
   284  		o.Context = ctx
   285  	}
   286  }
   287  
   288  type UpdateOption func(o *UpdateOptions)
   289  
   290  type UpdateOptions struct {
   291  	// Entrypoint within the folder (e.g. in the case of a mono-repo)
   292  	Entrypoint string
   293  	// Namespace the service is running in
   294  	Namespace string
   295  	// Specify the context to use
   296  	Context context.Context
   297  	// Secrets to use
   298  	Secrets map[string]string
   299  	// Number of instances
   300  	Instances int
   301  }
   302  
   303  // WithSecret sets a secret to provide the service with
   304  func UpdateSecret(key, value string) UpdateOption {
   305  	return func(o *UpdateOptions) {
   306  		if o.Secrets == nil {
   307  			o.Secrets = map[string]string{key: value}
   308  		} else {
   309  			o.Secrets[key] = value
   310  		}
   311  	}
   312  }
   313  
   314  // UpdateNamespace sets the namespace
   315  func UpdateNamespace(ns string) UpdateOption {
   316  	return func(o *UpdateOptions) {
   317  		o.Namespace = ns
   318  	}
   319  }
   320  
   321  // UpdateContext sets the context
   322  func UpdateContext(ctx context.Context) UpdateOption {
   323  	return func(o *UpdateOptions) {
   324  		o.Context = ctx
   325  	}
   326  }
   327  
   328  // UpdateEntrypoint sets the entrypoint
   329  func UpdateEntrypoint(e string) UpdateOption {
   330  	return func(o *UpdateOptions) {
   331  		o.Entrypoint = e
   332  	}
   333  }
   334  
   335  // UpdateInstances sets the number of instances
   336  func UpdateInstances(v int) UpdateOption {
   337  	return func(o *UpdateOptions) {
   338  		o.Instances = v
   339  	}
   340  }
   341  
   342  type DeleteOption func(o *DeleteOptions)
   343  
   344  type DeleteOptions struct {
   345  	// Namespace the service is running in
   346  	Namespace string
   347  	// Specify the context to use
   348  	Context context.Context
   349  }
   350  
   351  // DeleteNamespace sets the namespace
   352  func DeleteNamespace(ns string) DeleteOption {
   353  	return func(o *DeleteOptions) {
   354  		o.Namespace = ns
   355  	}
   356  }
   357  
   358  // DeleteContext sets the context
   359  func DeleteContext(ctx context.Context) DeleteOption {
   360  	return func(o *DeleteOptions) {
   361  		o.Context = ctx
   362  	}
   363  }
   364  
   365  // LogsOption configures runtime logging
   366  type LogsOption func(o *LogsOptions)
   367  
   368  // LogsOptions configure runtime logging
   369  type LogsOptions struct {
   370  	// How many existing lines to show
   371  	Count int64
   372  	// Stream new lines?
   373  	Stream bool
   374  	// Namespace the service is running in
   375  	Namespace string
   376  	// Specify the context to use
   377  	Context context.Context
   378  }
   379  
   380  // LogsExistingCount confiures how many existing lines to show
   381  func LogsCount(count int64) LogsOption {
   382  	return func(l *LogsOptions) {
   383  		l.Count = count
   384  	}
   385  }
   386  
   387  // LogsStream configures whether to stream new lines
   388  func LogsStream(stream bool) LogsOption {
   389  	return func(l *LogsOptions) {
   390  		l.Stream = stream
   391  	}
   392  }
   393  
   394  // LogsNamespace sets the namespace
   395  func LogsNamespace(ns string) LogsOption {
   396  	return func(o *LogsOptions) {
   397  		o.Namespace = ns
   398  	}
   399  }
   400  
   401  // LogsContext sets the context
   402  func LogsContext(ctx context.Context) LogsOption {
   403  	return func(o *LogsOptions) {
   404  		o.Context = ctx
   405  	}
   406  }