Refactor image* commands

All the image commands was refactored for usage with new config
factory. Config object now is being initialized in pkg module on demand,
which provides more flexibility and cleaner code.

Change-Id: I3c51f201ec70b194ce1f127ff43a28292cf691f3
Signed-off-by: Ruslan Aliev <raliev@mirantis.com>
Relates-To: #327
This commit is contained in:
Ruslan Aliev 2020-08-27 18:48:12 -05:00
parent a2b8d45cb0
commit e81116b1a9
5 changed files with 51 additions and 69 deletions

View File

@ -18,16 +18,16 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/bootstrap/isogen" "opendev.org/airship/airshipctl/pkg/bootstrap/isogen"
"opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/config"
) )
// NewImageBuildCommand creates a new command with the capability to build an ISO image. // NewImageBuildCommand creates a new command with the capability to build an ISO image.
func NewImageBuildCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { func NewImageBuildCommand(cfgFactory config.Factory) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "build", Use: "build",
Short: "Build ISO image", Short: "Build ISO image",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return isogen.GenerateBootstrapIso(rootSettings) return isogen.GenerateBootstrapIso(cfgFactory)
}, },
} }

View File

@ -17,6 +17,7 @@ package image
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/environment"
) )
@ -25,18 +26,11 @@ func NewImageCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comman
imageRootCmd := &cobra.Command{ imageRootCmd := &cobra.Command{
Use: "image", Use: "image",
Short: "Manage ISO image creation", Short: "Manage ISO image creation",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if parentPreRun := cmd.Root().PersistentPreRun; parentPreRun != nil {
parentPreRun(cmd.Root(), args)
}
// Load or Initialize airship Config
rootSettings.InitConfig()
},
} }
imageBuildCmd := NewImageBuildCommand(rootSettings) cfgFactory := config.CreateFactory(&rootSettings.AirshipConfigPath, &rootSettings.KubeConfigPath)
imageRootCmd.AddCommand(imageBuildCmd)
imageRootCmd.AddCommand(NewImageBuildCommand(cfgFactory))
return imageRootCmd return imageRootCmd
} }

View File

@ -18,6 +18,7 @@ import (
"testing" "testing"
"opendev.org/airship/airshipctl/cmd/image" "opendev.org/airship/airshipctl/cmd/image"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/testutil" "opendev.org/airship/airshipctl/testutil"
) )
@ -26,7 +27,7 @@ func TestImage(t *testing.T) {
{ {
Name: "image-with-help", Name: "image-with-help",
CmdLine: "-h", CmdLine: "-h",
Cmd: image.NewImageCommand(nil), Cmd: image.NewImageCommand(&environment.AirshipCTLSettings{}),
}, },
} }

View File

@ -26,7 +26,6 @@ import (
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"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/environment"
"opendev.org/airship/airshipctl/pkg/log" "opendev.org/airship/airshipctl/pkg/log"
"opendev.org/airship/airshipctl/pkg/util" "opendev.org/airship/airshipctl/pkg/util"
) )
@ -36,11 +35,11 @@ const (
) )
// GenerateBootstrapIso will generate data for cloud init and start ISO builder container // GenerateBootstrapIso will generate data for cloud init and start ISO builder container
func GenerateBootstrapIso(settings *environment.AirshipCTLSettings) error { func GenerateBootstrapIso(cfgFactory config.Factory) error {
ctx := context.Background() ctx := context.Background()
globalConf := settings.Config globalConf, err := cfgFactory()
if err := globalConf.EnsureComplete(); err != nil { if err != nil {
return err return err
} }

View File

@ -21,8 +21,6 @@ import (
"strings" "strings"
"testing" "testing"
"opendev.org/airship/airshipctl/pkg/environment"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -232,82 +230,72 @@ func TestVerifyInputs(t *testing.T) {
} }
func TestGenerateBootstrapIso(t *testing.T) { func TestGenerateBootstrapIso(t *testing.T) {
airshipConfigPath := "testdata/config/config"
kubeConfigPath := "testdata/config/kubeconfig"
t.Run("EnsureCompleteError", func(t *testing.T) { t.Run("EnsureCompleteError", func(t *testing.T) {
settings := &environment.AirshipCTLSettings{ settings, err := config.CreateFactory(&airshipConfigPath, &kubeConfigPath)()
AirshipConfigPath: "testdata/config/config", require.NoError(t, err)
KubeConfigPath: "testdata/config/kubeconfig", expectedErr := config.ErrMissingConfig{What: "Context with name ''"}
Config: &config.Config{}, settings.CurrentContext = ""
} actualErr := GenerateBootstrapIso(func() (*config.Config, error) {
expectedErr := config.ErrMissingConfig{What: "Current Context is not defined"} return settings, nil
settings.InitConfig() })
settings.Config.CurrentContext = ""
actualErr := GenerateBootstrapIso(settings)
assert.Equal(t, expectedErr, actualErr) assert.Equal(t, expectedErr, actualErr)
}) })
t.Run("ContextEntryPointError", func(t *testing.T) { t.Run("ContextEntryPointError", func(t *testing.T) {
settings := &environment.AirshipCTLSettings{ settings, err := config.CreateFactory(&airshipConfigPath, &kubeConfigPath)()
AirshipConfigPath: "testdata/config/config", require.NoError(t, err)
KubeConfigPath: "testdata/config/kubeconfig",
Config: &config.Config{},
}
expectedErr := config.ErrMissingPrimaryRepo{} expectedErr := config.ErrMissingPrimaryRepo{}
settings.InitConfig() settings.Manifests["default"].Repositories = make(map[string]*config.Repository)
settings.Config.Manifests["default"].Repositories = make(map[string]*config.Repository) actualErr := GenerateBootstrapIso(func() (*config.Config, error) {
actualErr := GenerateBootstrapIso(settings) return settings, nil
})
assert.Equal(t, expectedErr, actualErr) assert.Equal(t, expectedErr, actualErr)
}) })
t.Run("NewBundleByPathError", func(t *testing.T) { t.Run("NewBundleByPathError", func(t *testing.T) {
settings := &environment.AirshipCTLSettings{ settings, err := config.CreateFactory(&airshipConfigPath, &kubeConfigPath)()
AirshipConfigPath: "testdata/config/config", require.NoError(t, err)
KubeConfigPath: "testdata/config/kubeconfig",
Config: &config.Config{},
}
expectedErr := config.ErrMissingPhaseDocument{PhaseName: "bootstrap"} expectedErr := config.ErrMissingPhaseDocument{PhaseName: "bootstrap"}
settings.InitConfig() settings.Manifests["default"].TargetPath = "/nonexistent"
settings.Config.Manifests["default"].TargetPath = "/nonexistent" actualErr := GenerateBootstrapIso(func() (*config.Config, error) {
actualErr := GenerateBootstrapIso(settings) return settings, nil
})
assert.Equal(t, expectedErr, actualErr) assert.Equal(t, expectedErr, actualErr)
}) })
t.Run("SelectOneError", func(t *testing.T) { t.Run("SelectOneError", func(t *testing.T) {
settings := &environment.AirshipCTLSettings{ settings, err := config.CreateFactory(&airshipConfigPath, &kubeConfigPath)()
AirshipConfigPath: "testdata/config/config", require.NoError(t, err)
KubeConfigPath: "testdata/config/kubeconfig",
Config: &config.Config{},
}
expectedErr := document.ErrDocNotFound{ expectedErr := document.ErrDocNotFound{
Selector: document.NewSelector().ByGvk("airshipit.org", "v1alpha1", "ImageConfiguration")} Selector: document.NewSelector().ByGvk("airshipit.org", "v1alpha1", "ImageConfiguration")}
settings.InitConfig() settings.Manifests["default"].SubPath = "missingkinddoc/site/test-site"
settings.Config.Manifests["default"].SubPath = "missingkinddoc/site/test-site" actualErr := GenerateBootstrapIso(func() (*config.Config, error) {
actualErr := GenerateBootstrapIso(settings) return settings, nil
})
assert.Equal(t, expectedErr, actualErr) assert.Equal(t, expectedErr, actualErr)
}) })
t.Run("ToObjectError", func(t *testing.T) { t.Run("ToObjectError", func(t *testing.T) {
settings := &environment.AirshipCTLSettings{ settings, err := config.CreateFactory(&airshipConfigPath, &kubeConfigPath)()
AirshipConfigPath: "testdata/config/config", require.NoError(t, err)
KubeConfigPath: "testdata/config/kubeconfig",
Config: &config.Config{},
}
expectedErrMessage := "missing metadata.name in object" expectedErrMessage := "missing metadata.name in object"
settings.InitConfig() settings.Manifests["default"].SubPath = "missingmetadoc/site/test-site"
settings.Config.Manifests["default"].SubPath = "missingmetadoc/site/test-site" actualErr := GenerateBootstrapIso(func() (*config.Config, error) {
actualErr := GenerateBootstrapIso(settings) return settings, nil
})
assert.Contains(t, actualErr.Error(), expectedErrMessage) assert.Contains(t, actualErr.Error(), expectedErrMessage)
}) })
t.Run("verifyInputsError", func(t *testing.T) { t.Run("verifyInputsError", func(t *testing.T) {
settings := &environment.AirshipCTLSettings{ settings, err := config.CreateFactory(&airshipConfigPath, &kubeConfigPath)()
AirshipConfigPath: "testdata/config/config", require.NoError(t, err)
KubeConfigPath: "testdata/config/kubeconfig",
Config: &config.Config{},
}
expectedErr := config.ErrMissingConfig{What: "Must specify volume bind for ISO builder container"} expectedErr := config.ErrMissingConfig{What: "Must specify volume bind for ISO builder container"}
settings.InitConfig() settings.Manifests["default"].SubPath = "missingvoldoc/site/test-site"
settings.Config.Manifests["default"].SubPath = "missingvoldoc/site/test-site" actualErr := GenerateBootstrapIso(func() (*config.Config, error) {
actualErr := GenerateBootstrapIso(settings) return settings, nil
})
assert.Equal(t, expectedErr, actualErr) assert.Equal(t, expectedErr, actualErr)
}) })
} }