[WIP] Micro Runtime (#947)
* Add Get() and GetOptions. * Removed watcher. Outline of client. YAML templates * Added default service and deployment templates and types * Added API tests and cleaned up errors. * Small refactoring. Template package is no more. * Ripped out existing code in preparation to small rework * Reshuffled the source code to make it organized better * Create service and deployment in kubernetes runtime * Major cleanup and refactoring of Kubernetes runtime * Service now handles low level K8s API calls across both K8s deployment an service API objects * Runtime has a task queue that serves for queueing runtime action requests * General refactoring * No need for Lock in k8s service * Added kubernetes runtime env var to default deployment * Enable running different versions of the same service * Can't delete services through labels * Proto cruft. Added runtime.CreateOptions implementation in proto * Removed proxy service from default env variables * Make service name mandatory param to Get method * Get Delete changes from https://github.com/micro/go-micro/pull/945 * Replaced template files with global variables * Validate service names before sending K8s API request * Refactored Kubernetes API client. Fixed typos. * Added client.Resource to make API resources more explicit in code
This commit is contained in:
@@ -32,14 +32,48 @@ func toService(s *pb.Service) *runtime.Service {
|
||||
}
|
||||
}
|
||||
|
||||
func toCreateOptions(opts *pb.CreateOptions) []runtime.CreateOption {
|
||||
options := []runtime.CreateOption{}
|
||||
// command options
|
||||
l := len(opts.Command)
|
||||
if l == 1 {
|
||||
options = append(options, runtime.WithCommand(opts.Command[0]))
|
||||
}
|
||||
if l > 1 {
|
||||
options = append(options, runtime.WithCommand(opts.Command[0], opts.Command[1:]...))
|
||||
}
|
||||
// env options
|
||||
if len(opts.Env) > 0 {
|
||||
options = append(options, runtime.WithEnv(opts.Env))
|
||||
}
|
||||
|
||||
// TODO: output options
|
||||
|
||||
return options
|
||||
}
|
||||
|
||||
func toGetOptions(opts *pb.GetOptions) []runtime.GetOption {
|
||||
options := []runtime.GetOption{}
|
||||
// version options
|
||||
if len(opts.Version) > 0 {
|
||||
options = append(options, runtime.WithVersion(opts.Version))
|
||||
}
|
||||
|
||||
return options
|
||||
}
|
||||
|
||||
func (r *Runtime) Create(ctx context.Context, req *pb.CreateRequest, rsp *pb.CreateResponse) error {
|
||||
if req.Service == nil {
|
||||
return errors.BadRequest("go.micro.runtime", "blank service")
|
||||
}
|
||||
|
||||
// TODO: add opts
|
||||
var options []runtime.CreateOption
|
||||
if req.Options != nil {
|
||||
options = toCreateOptions(req.Options)
|
||||
}
|
||||
|
||||
service := toService(req.Service)
|
||||
err := r.Runtime.Create(service)
|
||||
err := r.Runtime.Create(service, options...)
|
||||
if err != nil {
|
||||
return errors.InternalServerError("go.micro.runtime", err.Error())
|
||||
}
|
||||
@@ -47,6 +81,28 @@ func (r *Runtime) Create(ctx context.Context, req *pb.CreateRequest, rsp *pb.Cre
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Runtime) Get(ctx context.Context, req *pb.GetRequest, rsp *pb.GetResponse) error {
|
||||
if len(req.Name) == 0 {
|
||||
return errors.BadRequest("go.micro.runtime", "blank service")
|
||||
}
|
||||
|
||||
var options []runtime.GetOption
|
||||
if req.Options != nil {
|
||||
options = toGetOptions(req.Options)
|
||||
}
|
||||
|
||||
services, err := r.Runtime.Get(req.Name, options...)
|
||||
if err != nil {
|
||||
return errors.InternalServerError("go.micro.runtime", err.Error())
|
||||
}
|
||||
|
||||
for _, service := range services {
|
||||
rsp.Services = append(rsp.Services, toProto(service))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Runtime) Update(ctx context.Context, req *pb.UpdateRequest, rsp *pb.UpdateResponse) error {
|
||||
if req.Service == nil {
|
||||
return errors.BadRequest("go.micro.runtime", "blank service")
|
||||
|
Reference in New Issue
Block a user