Fixes for runtime builder (#2029)

* util/tar: add archive util funcs

* runtime: add store, builder options

* runtime/local: update RuntimeSource func

* runtime/builder/golang: use tar util

* store/s3: make keys safe

* runtime: add entrypoint options

* runtime/builder: remove debugging

* wip: integrate builder into k8s runtime

* runtime/builder/golang: build for a linux architecture

* runtime/kubernetes: write builds to the users namespace

* runtime/local/source: fixes for mono-repo builds

* runtime/local: stop checking out source (the responsibility is on the client)

* runtime/builder: fix golang tests

* runtime/local: fix out of bounds panic

* Update

* revert changes

* runtime/local/source: refactor git package (wip)

* runtime/kubernetes: map err not found

* fix TestRunGenericRemote test

* runtime/local: fix update not reassining source

* Tidy go mod

* runtime.Pending => runtime.Starting

* store/s3: only use credentials option when set

* store/s3: add tls config option
This commit is contained in:
ben-toogood
2020-10-02 09:54:26 +01:00
committed by GitHub
parent 231cfc48f0
commit 0a6e451539
6 changed files with 111 additions and 106 deletions

View File

@@ -14,7 +14,6 @@ import (
"github.com/hpcloud/tail"
"github.com/micro/go-micro/v3/logger"
"github.com/micro/go-micro/v3/runtime"
"github.com/micro/go-micro/v3/runtime/local/source/git"
)
// defaultNamespace to use if not provided as an option
@@ -63,68 +62,6 @@ func NewRuntime(opts ...runtime.Option) runtime.Runtime {
}
}
func (r *localRuntime) checkoutSourceIfNeeded(s *runtime.Service, secrets map[string]string) error {
// Runtime service like config have no source.
// Skip checkout in that case
if len(s.Source) == 0 {
return nil
}
// Incoming uploaded files have format lastfolder.tar.gz or
// lastfolder.tar.gz/relative/path
sourceParts := strings.Split(s.Source, "/")
compressedFilepath := filepath.Join(SourceDir, sourceParts[0])
uncompressPath := strings.ReplaceAll(compressedFilepath, ".tar.gz", "")
tarName := strings.ReplaceAll(sourceParts[0], ".tar.gz", "")
if len(sourceParts) > 1 {
uncompressPath = filepath.Join(SourceDir, tarName)
}
// check if the directory already exists
if ex, _ := exists(compressedFilepath); ex {
err := os.RemoveAll(uncompressPath)
if err != nil {
return err
}
err = os.MkdirAll(uncompressPath, 0777)
if err != nil {
return err
}
err = git.Uncompress(compressedFilepath, uncompressPath)
if err != nil {
return err
}
if len(sourceParts) > 1 {
lastFolderPart := tarName
fullp := append([]string{uncompressPath}, sourceParts[1:]...)
s.Source = filepath.Join(append(fullp, lastFolderPart)...)
} else {
// The tar name is 'helloworld' for both
// the case when the code is uploaded from `$REPO/helloworld`
// and when it's uploaded from outside a repo ie `~/helloworld`.
if _, err := Entrypoint(filepath.Join(uncompressPath, tarName)); err == nil {
s.Source = filepath.Join(uncompressPath, tarName)
} else {
s.Source = uncompressPath
}
}
return nil
}
source, err := git.ParseSourceLocal("", s.Source)
if err != nil {
return err
}
source.Ref = s.Version
err = git.CheckoutSource(os.TempDir(), source, secrets)
if err != nil {
return err
}
s.Source = source.FullPath
return nil
}
// Init initializes runtime options
func (r *localRuntime) Init(opts ...runtime.Option) error {
r.Lock()
@@ -284,16 +221,15 @@ func (r *localRuntime) Create(s *runtime.Service, opts ...runtime.CreateOption)
o(&options)
}
err := r.checkoutSourceIfNeeded(s, options.Secrets)
if err != nil {
return err
}
r.Lock()
defer r.Unlock()
if len(options.Namespace) == 0 {
options.Namespace = defaultNamespace
}
if len(options.Entrypoint) > 0 {
s.Source = filepath.Join(s.Source, options.Entrypoint)
}
if len(options.Command) == 0 {
ep, err := Entrypoint(s.Source)
if err != nil {
@@ -501,14 +437,12 @@ func (r *localRuntime) Update(s *runtime.Service, opts ...runtime.UpdateOption)
for _, o := range opts {
o(&options)
}
err := r.checkoutSourceIfNeeded(s, options.Secrets)
if err != nil {
return err
}
if len(options.Namespace) == 0 {
options.Namespace = defaultNamespace
}
if len(options.Entrypoint) > 0 {
s.Source = filepath.Join(s.Source, options.Entrypoint)
}
r.Lock()
srvs, ok := r.namespaces[options.Namespace]
@@ -529,6 +463,9 @@ func (r *localRuntime) Update(s *runtime.Service, opts ...runtime.UpdateOption)
return err
}
// update the source to the new location and restart the service
service.Source = s.Source
service.Exec.Dir = s.Source
return service.Start()
}