Runtime Namespace (#1547)

* Add context option to runtime; Add dynamic namespace to kubectl client

* Add namespace runtime arg

* Fixes & Debugging

* Pass options in k8s runtime

* Set namespace on k8s resources

* Additional Logging

* More debugging

* Remove Debugging

* Ensure namespace exists

* Add debugging

* Refactor namespaceExists check

* Fix

* Fix

* Fix

* Fix

* Change the way we check for namespace

* Fix

* Tidying Up

* Fix Test

* Fix merge bugs

* Serialize k8s namespaces

* Add namespace to watch

* Serialize namespace when creating k8s namespace

Co-authored-by: Ben Toogood <ben@micro.mu>
Co-authored-by: Asim Aslam <asim@aslam.me>
This commit is contained in:
ben-toogood
2020-04-23 13:53:42 +01:00
committed by GitHub
parent 7345ce9192
commit 692b27578c
14 changed files with 411 additions and 109 deletions

View File

@@ -60,11 +60,11 @@ message ReadOptions {
}
message ReadRequest {
ReadOptions options = 1;
ReadOptions options = 1;
}
message ReadResponse {
repeated Service services = 1;
repeated Service services = 1;
}
message DeleteRequest {
@@ -100,10 +100,10 @@ message LogsRequest{
message LogRecord {
// timestamp of log record
int64 timestamp = 1;
// record metadata
map<string,string> metadata = 2;
// message
string message = 3;
int64 timestamp = 1;
// record metadata
map<string,string> metadata = 2;
// message
string message = 3;
}

View File

@@ -30,11 +30,13 @@ func (s *svc) Init(opts ...runtime.Option) error {
// Create registers a service in the runtime
func (s *svc) Create(svc *runtime.Service, opts ...runtime.CreateOption) error {
options := runtime.CreateOptions{}
// apply requested options
var options runtime.CreateOptions
for _, o := range opts {
o(&options)
}
if options.Context == nil {
options.Context = context.Background()
}
// set the default source from MICRO_RUNTIME_SOURCE
if len(svc.Source) == 0 {
@@ -58,15 +60,23 @@ func (s *svc) Create(svc *runtime.Service, opts ...runtime.CreateOption) error {
},
}
if _, err := s.runtime.Create(context.Background(), req); err != nil {
if _, err := s.runtime.Create(options.Context, req); err != nil {
return err
}
return nil
}
func (s *svc) Logs(service *runtime.Service, options ...runtime.LogsOption) (runtime.LogStream, error) {
ls, err := s.runtime.Logs(context.Background(), &pb.LogsRequest{
func (s *svc) Logs(service *runtime.Service, opts ...runtime.LogsOption) (runtime.LogStream, error) {
var options runtime.LogsOptions
for _, o := range opts {
o(&options)
}
if options.Context == nil {
options.Context = context.Background()
}
ls, err := s.runtime.Logs(options.Context, &pb.LogsRequest{
Service: service.Name,
Stream: true,
Count: 10, // @todo pass in actual options
@@ -122,11 +132,13 @@ func (l *serviceLogStream) Stop() error {
// Read returns the service with the given name from the runtime
func (s *svc) Read(opts ...runtime.ReadOption) ([]*runtime.Service, error) {
options := runtime.ReadOptions{}
// apply requested options
var options runtime.ReadOptions
for _, o := range opts {
o(&options)
}
if options.Context == nil {
options.Context = context.Background()
}
// runtime service create request
req := &pb.ReadRequest{
@@ -137,7 +149,7 @@ func (s *svc) Read(opts ...runtime.ReadOption) ([]*runtime.Service, error) {
},
}
resp, err := s.runtime.Read(context.Background(), req)
resp, err := s.runtime.Read(options.Context, req)
if err != nil {
return nil, err
}
@@ -157,7 +169,15 @@ func (s *svc) Read(opts ...runtime.ReadOption) ([]*runtime.Service, error) {
}
// Update updates the running service
func (s *svc) Update(svc *runtime.Service) error {
func (s *svc) Update(svc *runtime.Service, opts ...runtime.UpdateOption) error {
var options runtime.UpdateOptions
for _, o := range opts {
o(&options)
}
if options.Context == nil {
options.Context = context.Background()
}
// runtime service create request
req := &pb.UpdateRequest{
Service: &pb.Service{
@@ -168,7 +188,7 @@ func (s *svc) Update(svc *runtime.Service) error {
},
}
if _, err := s.runtime.Update(context.Background(), req); err != nil {
if _, err := s.runtime.Update(options.Context, req); err != nil {
return err
}
@@ -176,7 +196,15 @@ func (s *svc) Update(svc *runtime.Service) error {
}
// Delete stops and removes the service from the runtime
func (s *svc) Delete(svc *runtime.Service) error {
func (s *svc) Delete(svc *runtime.Service, opts ...runtime.DeleteOption) error {
var options runtime.DeleteOptions
for _, o := range opts {
o(&options)
}
if options.Context == nil {
options.Context = context.Background()
}
// runtime service create request
req := &pb.DeleteRequest{
Service: &pb.Service{
@@ -187,7 +215,7 @@ func (s *svc) Delete(svc *runtime.Service) error {
},
}
if _, err := s.runtime.Delete(context.Background(), req); err != nil {
if _, err := s.runtime.Delete(options.Context, req); err != nil {
return err
}