Merge "Refactor generic container executor"

This commit is contained in:
Zuul 2021-01-06 19:27:11 +00:00 committed by Gerrit Code Review
commit 957abd5432
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 return
} }
c.SetInput(evtCh) if err := c.SetInput(); err != nil {
c.PrepareFunctions(evtCh) handleError(evtCh, err)
return
}
if err := c.PrepareFunctions(); err != nil {
handleError(evtCh, err)
return
}
c.SetMounts() c.SetMounts()
if c.ContConf.PrintOutput { if c.ContConf.PrintOutput {
c.RunFns.Output = os.Stdout c.RunFns.Output = os.Stdout
} }
err := c.RunFns.Execute() if err := c.RunFns.Execute(); err != nil {
if err != nil {
handleError(evtCh, err) handleError(evtCh, err)
return return
} }
@ -124,39 +131,38 @@ func (c *ContainerExecutor) Run(evtCh chan events.Event, opts ifc.RunOptions) {
} }
// SetInput sets input for function // SetInput sets input for function
func (c *ContainerExecutor) SetInput(evtCh chan events.Event) { func (c *ContainerExecutor) SetInput() error {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err := c.ExecutorBundle.Write(buf) err := c.ExecutorBundle.Write(buf)
if err != nil { if err != nil {
handleError(evtCh, err) return err
return
} }
c.RunFns.Input = buf c.RunFns.Input = buf
return nil
} }
// PrepareFunctions prepares data for function // 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) rnode, err := kyaml.Parse(c.ContConf.Config)
if err != nil { if err != nil {
handleError(evtCh, err) return err
return
} }
// Transform GenericContainer.Spec to annotation, // Transform GenericContainer.Spec to annotation,
// because we need to specify runFns config in annotation // because we need to specify runFns config in annotation
spec, err := yaml.Marshal(c.ContConf.Spec) spec, err := yaml.Marshal(c.ContConf.Spec)
if err != nil { if err != nil {
handleError(evtCh, err) return err
return
} }
annotation := kyaml.SetAnnotation(runtimeutil.FunctionAnnotationKey, string(spec)) annotation := kyaml.SetAnnotation(runtimeutil.FunctionAnnotationKey, string(spec))
_, err = annotation.Filter(rnode) _, err = annotation.Filter(rnode)
if err != nil { if err != nil {
handleError(evtCh, err) return err
return
} }
c.RunFns.Functions = append(c.RunFns.Functions, rnode) c.RunFns.Functions = append(c.RunFns.Functions, rnode)
return nil
} }
// SetMounts allows to set relative path for storage mounts to prevent security issues // 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/api/v1alpha1"
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/events"
"opendev.org/airship/airshipctl/pkg/phase" "opendev.org/airship/airshipctl/pkg/phase"
"opendev.org/airship/airshipctl/pkg/phase/executors" "opendev.org/airship/airshipctl/pkg/phase/executors"
"opendev.org/airship/airshipctl/pkg/phase/ifc" "opendev.org/airship/airshipctl/pkg/phase/ifc"
@ -151,9 +150,8 @@ func TestSetInputSingleDocument(t *testing.T) {
Functions: []*kyaml.RNode{}, Functions: []*kyaml.RNode{},
}, },
} }
ch := make(chan events.Event) err = e.SetInput()
e.SetInput(ch) require.NoError(t, err)
assert.Empty(t, ch)
// need to use kustomize here, because // need to use kustomize here, because
// it changes order of lines in document // it changes order of lines in document
@ -185,9 +183,8 @@ func TestSetInputManyDocuments(t *testing.T) {
Functions: []*kyaml.RNode{}, Functions: []*kyaml.RNode{},
}, },
} }
ch := make(chan events.Event) err = e.SetInput()
e.SetInput(ch) require.NoError(t, err)
assert.Empty(t, ch)
// need to use kustomize here, because // need to use kustomize here, because
// it changes order of lines in document // it changes order of lines in document
@ -232,9 +229,8 @@ func TestPrepareFunctions(t *testing.T) {
}, },
} }
ch := make(chan events.Event) err = e.PrepareFunctions()
e.PrepareFunctions(ch) require.NoError(t, err)
assert.Empty(t, ch)
strFuncs, err := e.RunFns.Functions[0].String() strFuncs, err := e.RunFns.Functions[0].String()
require.NoError(t, err) require.NoError(t, err)