Move isogen phase executor to a separate package

Having an executor within isogen package creates
potential import cycling. This patch moves it to a separate
package which can be used to conveniently store all the executors
at one place.

Change-Id: I2154c0af6c28f47abb2448863332766260106759
Signed-off-by: Ruslan Aliev <raliev@mirantis.com>
Relates-To: #432
This commit is contained in:
Ruslan Aliev 2020-12-08 22:40:11 -06:00
parent 71dc1d4703
commit 36303006af
6 changed files with 45 additions and 48 deletions

View File

@ -105,13 +105,6 @@ func getContainerCfg(
return fls return fls
} }
func verifyArtifacts(cfg *v1alpha1.ImageConfiguration) error {
hostVol := strings.Split(cfg.Container.Volume, ":")[0]
metadataPath := filepath.Join(hostVol, cfg.Builder.OutputMetadataFileName)
_, err := os.Stat(metadataPath)
return err
}
// CreateBootstrapIso prepares and runs appropriate container to create a bootstrap ISO // CreateBootstrapIso prepares and runs appropriate container to create a bootstrap ISO
func (opts BootstrapIsoOptions) CreateBootstrapIso() error { func (opts BootstrapIsoOptions) CreateBootstrapIso() error {
cntVol := strings.Split(opts.Cfg.Container.Volume, ":")[1] cntVol := strings.Split(opts.Cfg.Container.Volume, ":")[1]

View File

@ -14,14 +14,6 @@
package isogen package isogen
// ErrIsoGenNilBundle is returned when isogen executor is not provided with bundle
type ErrIsoGenNilBundle struct {
}
func (e ErrIsoGenNilBundle) Error() string {
return "Cannot build iso with empty bundle, no data source is available"
}
// ErrNoParsedNumPkgs is returned when it's unable to find number of packages to install // ErrNoParsedNumPkgs is returned when it's unable to find number of packages to install
type ErrNoParsedNumPkgs struct { type ErrNoParsedNumPkgs struct {
} }

View File

@ -21,7 +21,6 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"opendev.org/airship/airshipctl/pkg/api/v1alpha1" "opendev.org/airship/airshipctl/pkg/api/v1alpha1"
"opendev.org/airship/airshipctl/pkg/bootstrap/isogen"
"opendev.org/airship/airshipctl/pkg/container" "opendev.org/airship/airshipctl/pkg/container"
"opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/events" "opendev.org/airship/airshipctl/pkg/events"
@ -46,7 +45,7 @@ func DefaultExecutorRegistry() map[schema.GroupVersionKind]ifc.ExecutorFactory {
if err := applier.RegisterExecutor(execMap); err != nil { if err := applier.RegisterExecutor(execMap); err != nil {
log.Fatal(ErrExecutorRegistration{ExecutorName: "kubernetes-apply", Err: err}) log.Fatal(ErrExecutorRegistration{ExecutorName: "kubernetes-apply", Err: err})
} }
if err := isogen.RegisterExecutor(execMap); err != nil { if err := executors.RegisterIsogenExecutor(execMap); err != nil {
log.Fatal(ErrExecutorRegistration{ExecutorName: "isogen", Err: err}) log.Fatal(ErrExecutorRegistration{ExecutorName: "isogen", Err: err})
} }
if err := container.RegisterExecutor(execMap); err != nil { if err := container.RegisterExecutor(execMap); err != nil {

View File

@ -27,3 +27,11 @@ type ErrUnknownExecutorAction struct {
func (e ErrUnknownExecutorAction) Error() string { func (e ErrUnknownExecutorAction) Error() string {
return fmt.Sprintf("unknown action type '%s'", e.Action) return fmt.Sprintf("unknown action type '%s'", e.Action)
} }
// ErrIsoGenNilBundle is returned when isogen executor is not provided with bundle
type ErrIsoGenNilBundle struct {
}
func (e ErrIsoGenNilBundle) Error() string {
return "Cannot build iso with empty bundle, no data source is available"
}

View File

@ -12,15 +12,19 @@
limitations under the License. limitations under the License.
*/ */
package isogen package executors
import ( import (
"context" "context"
"io" "io"
"os"
"path/filepath"
"strings"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"opendev.org/airship/airshipctl/pkg/api/v1alpha1" "opendev.org/airship/airshipctl/pkg/api/v1alpha1"
"opendev.org/airship/airshipctl/pkg/bootstrap/isogen"
"opendev.org/airship/airshipctl/pkg/container" "opendev.org/airship/airshipctl/pkg/container"
"opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/errors" "opendev.org/airship/airshipctl/pkg/errors"
@ -29,10 +33,10 @@ import (
"opendev.org/airship/airshipctl/pkg/phase/ifc" "opendev.org/airship/airshipctl/pkg/phase/ifc"
) )
var _ ifc.Executor = &Executor{} var _ ifc.Executor = &IsogenExecutor{}
// Executor contains resources for isogen executor // IsogenExecutor contains resources for isogen executor
type Executor struct { type IsogenExecutor struct {
ExecutorBundle document.Bundle ExecutorBundle document.Bundle
ExecutorDocument document.Document ExecutorDocument document.Document
@ -40,19 +44,19 @@ type Executor struct {
Builder container.Container Builder container.Container
} }
// RegisterExecutor adds executor to phase executor registry // RegisterIsogenExecutor adds executor to phase executor registry
func RegisterExecutor(registry map[schema.GroupVersionKind]ifc.ExecutorFactory) error { func RegisterIsogenExecutor(registry map[schema.GroupVersionKind]ifc.ExecutorFactory) error {
obj := v1alpha1.DefaultImageConfiguration() obj := v1alpha1.DefaultImageConfiguration()
gvks, _, err := v1alpha1.Scheme.ObjectKinds(obj) gvks, _, err := v1alpha1.Scheme.ObjectKinds(obj)
if err != nil { if err != nil {
return err return err
} }
registry[gvks[0]] = NewExecutor registry[gvks[0]] = NewIsogenExecutor
return nil return nil
} }
// NewExecutor creates instance of phase executor // NewIsogenExecutor creates instance of phase executor
func NewExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) { func NewIsogenExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) {
apiObj := &v1alpha1.ImageConfiguration{ apiObj := &v1alpha1.ImageConfiguration{
Container: &v1alpha1.Container{}, Container: &v1alpha1.Container{},
Builder: &v1alpha1.Builder{}, Builder: &v1alpha1.Builder{},
@ -67,7 +71,7 @@ func NewExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) {
return nil, err return nil, err
} }
return &Executor{ return &IsogenExecutor{
ExecutorBundle: bundle, ExecutorBundle: bundle,
ExecutorDocument: cfg.ExecutorDocument, ExecutorDocument: cfg.ExecutorDocument,
ImgConf: apiObj, ImgConf: apiObj,
@ -75,7 +79,7 @@ func NewExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) {
} }
// Run isogen as a phase runner // Run isogen as a phase runner
func (c *Executor) Run(evtCh chan events.Event, opts ifc.RunOptions) { func (c *IsogenExecutor) Run(evtCh chan events.Event, opts ifc.RunOptions) {
defer close(evtCh) defer close(evtCh)
if c.ExecutorBundle == nil { if c.ExecutorBundle == nil {
@ -109,7 +113,7 @@ func (c *Executor) Run(evtCh chan events.Event, opts ifc.RunOptions) {
} }
} }
bootstrapOpts := BootstrapIsoOptions{ bootstrapOpts := isogen.BootstrapIsoOptions{
DocBundle: c.ExecutorBundle, DocBundle: c.ExecutorBundle,
Builder: c.Builder, Builder: c.Builder,
Doc: c.ExecutorDocument, Doc: c.ExecutorDocument,
@ -141,13 +145,20 @@ func (c *Executor) Run(evtCh chan events.Event, opts ifc.RunOptions) {
}) })
} }
func verifyArtifacts(cfg *v1alpha1.ImageConfiguration) error {
hostVol := strings.Split(cfg.Container.Volume, ":")[0]
metadataPath := filepath.Join(hostVol, cfg.Builder.OutputMetadataFileName)
_, err := os.Stat(metadataPath)
return err
}
// Validate executor configuration and documents // Validate executor configuration and documents
func (c *Executor) Validate() error { func (c *IsogenExecutor) Validate() error {
return errors.ErrNotImplemented{} return errors.ErrNotImplemented{}
} }
// Render executor documents // Render executor documents
func (c *Executor) Render(w io.Writer, _ ifc.RenderOptions) error { func (c *IsogenExecutor) Render(w io.Writer, _ ifc.RenderOptions) error {
// will be implemented later // will be implemented later
_, err := w.Write([]byte{}) _, err := w.Write([]byte{})
return err return err

View File

@ -12,7 +12,7 @@
limitations under the License. limitations under the License.
*/ */
package isogen_test package executors_test
import ( import (
"testing" "testing"
@ -23,10 +23,10 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"opendev.org/airship/airshipctl/pkg/api/v1alpha1" "opendev.org/airship/airshipctl/pkg/api/v1alpha1"
"opendev.org/airship/airshipctl/pkg/bootstrap/isogen"
"opendev.org/airship/airshipctl/pkg/container" "opendev.org/airship/airshipctl/pkg/container"
"opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/events" "opendev.org/airship/airshipctl/pkg/events"
"opendev.org/airship/airshipctl/pkg/phase/executors"
"opendev.org/airship/airshipctl/pkg/phase/ifc" "opendev.org/airship/airshipctl/pkg/phase/ifc"
"opendev.org/airship/airshipctl/testutil" "opendev.org/airship/airshipctl/testutil"
testcontainer "opendev.org/airship/airshipctl/testutil/container" testcontainer "opendev.org/airship/airshipctl/testutil/container"
@ -34,7 +34,7 @@ import (
) )
var ( var (
executorDoc = ` isogenExecutorDoc = `
apiVersion: airshipit.org/v1alpha1 apiVersion: airshipit.org/v1alpha1
kind: ImageConfiguration kind: ImageConfiguration
metadata: metadata:
@ -49,33 +49,33 @@ container:
containerRuntime: docker containerRuntime: docker
image: quay.io/airshipit/isogen:latest-ubuntu_focal image: quay.io/airshipit/isogen:latest-ubuntu_focal
volume: /srv/iso:/config` volume: /srv/iso:/config`
executorBundlePath = "testdata/primary/site/test-site/ephemeral/bootstrap" executorBundlePath = "../../bootstrap/isogen/testdata/primary/site/test-site/ephemeral/bootstrap"
) )
func TestRegisterExecutor(t *testing.T) { func TestRegisterIsogenExecutor(t *testing.T) {
registry := make(map[schema.GroupVersionKind]ifc.ExecutorFactory) registry := make(map[schema.GroupVersionKind]ifc.ExecutorFactory)
expectedGVK := schema.GroupVersionKind{ expectedGVK := schema.GroupVersionKind{
Group: "airshipit.org", Group: "airshipit.org",
Version: "v1alpha1", Version: "v1alpha1",
Kind: "ImageConfiguration", Kind: "ImageConfiguration",
} }
err := isogen.RegisterExecutor(registry) err := executors.RegisterIsogenExecutor(registry)
require.NoError(t, err) require.NoError(t, err)
_, found := registry[expectedGVK] _, found := registry[expectedGVK]
assert.True(t, found) assert.True(t, found)
} }
func TestNewExecutor(t *testing.T) { func TestNewIsogenExecutor(t *testing.T) {
execDoc, err := document.NewDocumentFromBytes([]byte(executorDoc)) execDoc, err := document.NewDocumentFromBytes([]byte(isogenExecutorDoc))
require.NoError(t, err) require.NoError(t, err)
_, err = isogen.NewExecutor(ifc.ExecutorConfig{ _, err = executors.NewIsogenExecutor(ifc.ExecutorConfig{
ExecutorDocument: execDoc, ExecutorDocument: execDoc,
BundleFactory: testBundleFactory(executorBundlePath)}) BundleFactory: testBundleFactory(executorBundlePath)})
require.NoError(t, err) require.NoError(t, err)
} }
func TestExecutorRun(t *testing.T) { func TestIsogenExecutorRun(t *testing.T) {
bundle, err := document.NewBundleByPath(executorBundlePath) bundle, err := document.NewBundleByPath(executorBundlePath)
require.NoError(t, err) require.NoError(t, err)
require.NotNil(t, bundle) require.NotNil(t, bundle)
@ -144,7 +144,7 @@ func TestExecutorRun(t *testing.T) {
for _, test := range testCases { for _, test := range testCases {
tt := test tt := test
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
executor := &isogen.Executor{ executor := &executors.IsogenExecutor{
ExecutorDocument: testDoc, ExecutorDocument: testDoc,
ExecutorBundle: bundle, ExecutorBundle: bundle,
ImgConf: testCfg, ImgConf: testCfg,
@ -171,12 +171,6 @@ func TestExecutorRun(t *testing.T) {
} }
} }
func wrapError(err error) events.Event {
return events.NewEvent().WithErrorEvent(events.ErrorEvent{
Error: err,
})
}
func testBundleFactory(path string) document.BundleFactoryFunc { func testBundleFactory(path string) document.BundleFactoryFunc {
return func() (document.Bundle, error) { return func() (document.Bundle, error) {
return document.NewBundleByPath(path) return document.NewBundleByPath(path)