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
}
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
func (opts BootstrapIsoOptions) CreateBootstrapIso() error {
cntVol := strings.Split(opts.Cfg.Container.Volume, ":")[1]

View File

@ -14,14 +14,6 @@
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
type ErrNoParsedNumPkgs struct {
}

View File

@ -21,7 +21,6 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"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/document"
"opendev.org/airship/airshipctl/pkg/events"
@ -46,7 +45,7 @@ func DefaultExecutorRegistry() map[schema.GroupVersionKind]ifc.ExecutorFactory {
if err := applier.RegisterExecutor(execMap); err != nil {
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})
}
if err := container.RegisterExecutor(execMap); err != nil {

View File

@ -27,3 +27,11 @@ type ErrUnknownExecutorAction struct {
func (e ErrUnknownExecutorAction) Error() string {
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.
*/
package isogen
package executors
import (
"context"
"io"
"os"
"path/filepath"
"strings"
"k8s.io/apimachinery/pkg/runtime/schema"
"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/document"
"opendev.org/airship/airshipctl/pkg/errors"
@ -29,10 +33,10 @@ import (
"opendev.org/airship/airshipctl/pkg/phase/ifc"
)
var _ ifc.Executor = &Executor{}
var _ ifc.Executor = &IsogenExecutor{}
// Executor contains resources for isogen executor
type Executor struct {
// IsogenExecutor contains resources for isogen executor
type IsogenExecutor struct {
ExecutorBundle document.Bundle
ExecutorDocument document.Document
@ -40,19 +44,19 @@ type Executor struct {
Builder container.Container
}
// RegisterExecutor adds executor to phase executor registry
func RegisterExecutor(registry map[schema.GroupVersionKind]ifc.ExecutorFactory) error {
// RegisterIsogenExecutor adds executor to phase executor registry
func RegisterIsogenExecutor(registry map[schema.GroupVersionKind]ifc.ExecutorFactory) error {
obj := v1alpha1.DefaultImageConfiguration()
gvks, _, err := v1alpha1.Scheme.ObjectKinds(obj)
if err != nil {
return err
}
registry[gvks[0]] = NewExecutor
registry[gvks[0]] = NewIsogenExecutor
return nil
}
// NewExecutor creates instance of phase executor
func NewExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) {
// NewIsogenExecutor creates instance of phase executor
func NewIsogenExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) {
apiObj := &v1alpha1.ImageConfiguration{
Container: &v1alpha1.Container{},
Builder: &v1alpha1.Builder{},
@ -67,7 +71,7 @@ func NewExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) {
return nil, err
}
return &Executor{
return &IsogenExecutor{
ExecutorBundle: bundle,
ExecutorDocument: cfg.ExecutorDocument,
ImgConf: apiObj,
@ -75,7 +79,7 @@ func NewExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) {
}
// 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)
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,
Builder: c.Builder,
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
func (c *Executor) Validate() error {
func (c *IsogenExecutor) Validate() error {
return errors.ErrNotImplemented{}
}
// 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
_, err := w.Write([]byte{})
return err

View File

@ -12,7 +12,7 @@
limitations under the License.
*/
package isogen_test
package executors_test
import (
"testing"
@ -23,10 +23,10 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"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/document"
"opendev.org/airship/airshipctl/pkg/events"
"opendev.org/airship/airshipctl/pkg/phase/executors"
"opendev.org/airship/airshipctl/pkg/phase/ifc"
"opendev.org/airship/airshipctl/testutil"
testcontainer "opendev.org/airship/airshipctl/testutil/container"
@ -34,7 +34,7 @@ import (
)
var (
executorDoc = `
isogenExecutorDoc = `
apiVersion: airshipit.org/v1alpha1
kind: ImageConfiguration
metadata:
@ -49,33 +49,33 @@ container:
containerRuntime: docker
image: quay.io/airshipit/isogen:latest-ubuntu_focal
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)
expectedGVK := schema.GroupVersionKind{
Group: "airshipit.org",
Version: "v1alpha1",
Kind: "ImageConfiguration",
}
err := isogen.RegisterExecutor(registry)
err := executors.RegisterIsogenExecutor(registry)
require.NoError(t, err)
_, found := registry[expectedGVK]
assert.True(t, found)
}
func TestNewExecutor(t *testing.T) {
execDoc, err := document.NewDocumentFromBytes([]byte(executorDoc))
func TestNewIsogenExecutor(t *testing.T) {
execDoc, err := document.NewDocumentFromBytes([]byte(isogenExecutorDoc))
require.NoError(t, err)
_, err = isogen.NewExecutor(ifc.ExecutorConfig{
_, err = executors.NewIsogenExecutor(ifc.ExecutorConfig{
ExecutorDocument: execDoc,
BundleFactory: testBundleFactory(executorBundlePath)})
require.NoError(t, err)
}
func TestExecutorRun(t *testing.T) {
func TestIsogenExecutorRun(t *testing.T) {
bundle, err := document.NewBundleByPath(executorBundlePath)
require.NoError(t, err)
require.NotNil(t, bundle)
@ -144,7 +144,7 @@ func TestExecutorRun(t *testing.T) {
for _, test := range testCases {
tt := test
t.Run(tt.name, func(t *testing.T) {
executor := &isogen.Executor{
executor := &executors.IsogenExecutor{
ExecutorDocument: testDoc,
ExecutorBundle: bundle,
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 {
return func() (document.Bundle, error) {
return document.NewBundleByPath(path)