Refactor generic container executor

Refactor executor functions in go lang style

Change-Id: I81f5ff32e0ac354790c7afbc0bf4db91f58397b4
Relates-To: #202
Relates-To: #369
This commit is contained in:
Vladislav Kuzmin 2020-12-04 18:09:37 +04:00 committed by Alexey Odinokov
parent 1016a29870
commit 53ef504bd6
2 changed files with 26 additions and 24 deletions

View File

@ -103,16 +103,23 @@ func (c *ContainerExecutor) Run(evtCh chan events.Event, opts ifc.RunOptions) {
return
}
c.SetInput(evtCh)
c.PrepareFunctions(evtCh)
if err := c.SetInput(); err != nil {
handleError(evtCh, err)
return
}
if err := c.PrepareFunctions(); err != nil {
handleError(evtCh, err)
return
}
c.SetMounts()
if c.ContConf.PrintOutput {
c.RunFns.Output = os.Stdout
}
err := c.RunFns.Execute()
if err != nil {
if err := c.RunFns.Execute(); err != nil {
handleError(evtCh, err)
return
}
@ -124,39 +131,38 @@ func (c *ContainerExecutor) Run(evtCh chan events.Event, opts ifc.RunOptions) {
}
// SetInput sets input for function
func (c *ContainerExecutor) SetInput(evtCh chan events.Event) {
func (c *ContainerExecutor) SetInput() error {
buf := &bytes.Buffer{}
err := c.ExecutorBundle.Write(buf)
if err != nil {
handleError(evtCh, err)
return
return err
}
c.RunFns.Input = buf
return nil
}
// PrepareFunctions prepares data for function
func (c *ContainerExecutor) PrepareFunctions(evtCh chan events.Event) {
func (c *ContainerExecutor) PrepareFunctions() error {
rnode, err := kyaml.Parse(c.ContConf.Config)
if err != nil {
handleError(evtCh, err)
return
return err
}
// Transform GenericContainer.Spec to annotation,
// because we need to specify runFns config in annotation
spec, err := yaml.Marshal(c.ContConf.Spec)
if err != nil {
handleError(evtCh, err)
return
return err
}
annotation := kyaml.SetAnnotation(runtimeutil.FunctionAnnotationKey, string(spec))
_, err = annotation.Filter(rnode)
if err != nil {
handleError(evtCh, err)
return
return err
}
c.RunFns.Functions = append(c.RunFns.Functions, rnode)
return nil
}
// SetMounts allows to set relative path for storage mounts to prevent security issues

View File

@ -28,7 +28,6 @@ import (
"opendev.org/airship/airshipctl/pkg/api/v1alpha1"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/events"
"opendev.org/airship/airshipctl/pkg/phase"
"opendev.org/airship/airshipctl/pkg/phase/executors"
"opendev.org/airship/airshipctl/pkg/phase/ifc"
@ -151,9 +150,8 @@ func TestSetInputSingleDocument(t *testing.T) {
Functions: []*kyaml.RNode{},
},
}
ch := make(chan events.Event)
e.SetInput(ch)
assert.Empty(t, ch)
err = e.SetInput()
require.NoError(t, err)
// need to use kustomize here, because
// it changes order of lines in document
@ -185,9 +183,8 @@ func TestSetInputManyDocuments(t *testing.T) {
Functions: []*kyaml.RNode{},
},
}
ch := make(chan events.Event)
e.SetInput(ch)
assert.Empty(t, ch)
err = e.SetInput()
require.NoError(t, err)
// need to use kustomize here, because
// it changes order of lines in document
@ -232,9 +229,8 @@ func TestPrepareFunctions(t *testing.T) {
},
}
ch := make(chan events.Event)
e.PrepareFunctions(ch)
assert.Empty(t, ch)
err = e.PrepareFunctions()
require.NoError(t, err)
strFuncs, err := e.RunFns.Functions[0].String()
require.NoError(t, err)