From dc686403898f6f27e6e2e0f022c99770df95147e Mon Sep 17 00:00:00 2001 From: Vladimir Kozhukalov Date: Wed, 7 Oct 2020 00:27:23 +0300 Subject: [PATCH] Phase docs are targetPath + phaseRepo + phase.DocumentEntrypoint Change-Id: I1d5ea75d1c19eb4ebaf37b28918ccde771bcef85 Relates-To: #356 --- manifests/metadata.yaml | 2 +- manifests/phases/phases.yaml | 12 ++++----- pkg/clusterctl/client/executor_test.go | 1 + pkg/config/config.go | 20 +++++++++++++- pkg/config/config_test.go | 33 ++++++++++++++++++++++-- pkg/config/testdata/config-string.yaml | 2 +- pkg/config/testdata/manifest-string.yaml | 2 +- pkg/config/utils.go | 1 + pkg/k8s/applier/executor_test.go | 10 +++++-- pkg/phase/client.go | 3 ++- pkg/phase/command_test.go | 20 +++++++++++--- pkg/phase/helper.go | 18 ++++++++++--- pkg/phase/helper_test.go | 7 +++-- pkg/phase/ifc/helper.go | 1 + pkg/phase/render_test.go | 6 +++++ pkg/remote/management_test.go | 5 +++- testutil/testconfig.go | 1 + tools/deployment/22_test_configs.sh | 2 +- 18 files changed, 119 insertions(+), 27 deletions(-) diff --git a/manifests/metadata.yaml b/manifests/metadata.yaml index b4fbe8880..be05d7ad1 100644 --- a/manifests/metadata.yaml +++ b/manifests/metadata.yaml @@ -1,2 +1,2 @@ phase: - path: airshipctl/manifests/phases \ No newline at end of file + path: manifests/phases diff --git a/manifests/phases/phases.yaml b/manifests/phases/phases.yaml index 2f41f38e0..a97c6e329 100644 --- a/manifests/phases/phases.yaml +++ b/manifests/phases/phases.yaml @@ -7,7 +7,7 @@ config: apiVersion: airshipit.org/v1alpha1 kind: ImageConfiguration name: isogen - documentEntryPoint: airshipctl/manifests/site/test-site/ephemeral/bootstrap + documentEntryPoint: manifests/site/test-site/ephemeral/bootstrap --- apiVersion: airshipit.org/v1alpha1 kind: Phase @@ -19,7 +19,7 @@ config: apiVersion: airshipit.org/v1alpha1 kind: KubernetesApply name: kubernetes-apply - documentEntryPoint: airshipctl/manifests/site/test-site/ephemeral/initinfra + documentEntryPoint: manifests/site/test-site/ephemeral/initinfra --- apiVersion: airshipit.org/v1alpha1 kind: Phase @@ -31,7 +31,7 @@ config: apiVersion: airshipit.org/v1alpha1 kind: KubernetesApply name: kubernetes-apply - documentEntryPoint: airshipctl/manifests/site/test-site/ephemeral/controlplane + documentEntryPoint: manifests/site/test-site/ephemeral/controlplane --- apiVersion: airshipit.org/v1alpha1 kind: Phase @@ -44,7 +44,7 @@ config: apiVersion: airshipit.org/v1alpha1 kind: KubernetesApply name: kubernetes-apply - documentEntryPoint: airshipctl/manifests/site/test-site/target/initinfra + documentEntryPoint: manifests/site/test-site/target/initinfra --- apiVersion: airshipit.org/v1alpha1 kind: Phase @@ -57,7 +57,7 @@ config: apiVersion: airshipit.org/v1alpha1 kind: KubernetesApply name: kubernetes-apply - documentEntryPoint: airshipctl/manifests/site/test-site/target/workers + documentEntryPoint: manifests/site/test-site/target/workers --- apiVersion: airshipit.org/v1alpha1 kind: Phase @@ -102,4 +102,4 @@ config: apiVersion: airshipit.org/v1alpha1 kind: KubernetesApply name: kubernetes-apply - documentEntryPoint: airshipctl/manifests/site/test-site/target/workload + documentEntryPoint: manifests/site/test-site/target/workload diff --git a/pkg/clusterctl/client/executor_test.go b/pkg/clusterctl/client/executor_test.go index bef6efe8a..c980cfdc2 100644 --- a/pkg/clusterctl/client/executor_test.go +++ b/pkg/clusterctl/client/executor_test.go @@ -222,6 +222,7 @@ func makeDefaultHelper(t *testing.T) ifc.Helper { cfg := config.NewConfig() cfg.Manifests[config.AirshipDefaultManifest].TargetPath = "./testdata" cfg.Manifests[config.AirshipDefaultManifest].MetadataPath = "metadata.yaml" + cfg.Manifests[config.AirshipDefaultManifest].Repositories[config.DefaultTestPhaseRepo].URLString = "" cfg.SetLoadedConfigPath(".") helper, err := phase.NewHelper(cfg) require.NoError(t, err) diff --git a/pkg/config/config.go b/pkg/config/config.go index b34888592..1e187c46a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -358,6 +358,20 @@ func (c *Config) CurrentContextTargetPath() (string, error) { return ccm.TargetPath, nil } +// CurrentContextPhaseRepositoryDir returns phase repository directory from current context's manifest +// E.g. let repository url be "http://dummy.org/phaserepo.git" then repo directory under targetPath is "phaserepo" +func (c *Config) CurrentContextPhaseRepositoryDir() (string, error) { + ccm, err := c.CurrentContextManifest() + if err != nil { + return "", err + } + repo, exist := ccm.Repositories[ccm.PhaseRepositoryName] + if !exist { + return "", ErrMissingRepositoryName{} + } + return util.GitDirNameFromURL(repo.URL()), nil +} + // CurrentContextClusterType returns cluster type of current context func (c *Config) CurrentContextClusterType() (string, error) { context, err := c.GetCurrentContext() @@ -568,12 +582,16 @@ func (c *Config) CurrentContextManifestMetadata() (*Metadata, error) { if err != nil { return nil, err } + phaseRepoDir, err := c.CurrentContextPhaseRepositoryDir() + if err != nil { + return nil, err + } meta := &Metadata{ // Populate with empty values to avoid nil pointers Inventory: &InventoryMeta{}, PhaseMeta: &PhaseMeta{}, } - err = util.ReadYAMLFile(filepath.Join(manifest.TargetPath, manifest.MetadataPath), meta) + err = util.ReadYAMLFile(filepath.Join(manifest.TargetPath, phaseRepoDir, manifest.MetadataPath), meta) if err != nil { return nil, err } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index e648b1c50..51a69d332 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -20,6 +20,8 @@ import ( "strings" "testing" + "opendev.org/airship/airshipctl/pkg/util" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -315,6 +317,26 @@ func TestCurrentTargetPath(t *testing.T) { assert.Equal(t, conf.Manifests[defaultString].TargetPath, targetPath) } +func TestCurrentPhaseRepositoryDir(t *testing.T) { + conf, cleanup := testutil.InitConfig(t) + defer cleanup(t) + + conf.CurrentContext = currentContextName + conf.Contexts[currentContextName].Manifest = defaultString + + phaseRepoDir, err := conf.CurrentContextPhaseRepositoryDir() + require.NoError(t, err) + assert.Equal(t, util.GitDirNameFromURL( + conf.Manifests[defaultString].Repositories[conf.Manifests[defaultString].PhaseRepositoryName].URL()), + phaseRepoDir) + + conf.Manifests[defaultString].PhaseRepositoryName = "nonexisting" + phaseRepoDir, err = conf.CurrentContextPhaseRepositoryDir() + require.Error(t, err) + assert.Equal(t, config.ErrMissingRepositoryName{}, err) + assert.Equal(t, "", phaseRepoDir) +} + func TestCurrentContextEntryPoint(t *testing.T) { conf, cleanup := testutil.InitConfig(t) defer cleanup(t) @@ -414,9 +436,16 @@ func TestCurrentContextManifestMetadata(t *testing.T) { context := &config.Context{ Manifest: "testManifest", } + repos := map[string]*config.Repository{ + config.DefaultTestPhaseRepo: { + URLString: "", + }, + } manifest := &config.Manifest{ - MetadataPath: tt.metaPath, - TargetPath: "testdata", + MetadataPath: tt.metaPath, + TargetPath: "testdata", + PhaseRepositoryName: config.DefaultTestPhaseRepo, + Repositories: repos, } conf.Manifests = map[string]*config.Manifest{ "testManifest": manifest, diff --git a/pkg/config/testdata/config-string.yaml b/pkg/config/testdata/config-string.yaml index 3881b1c8c..e783194b4 100644 --- a/pkg/config/testdata/config-string.yaml +++ b/pkg/config/testdata/config-string.yaml @@ -17,7 +17,7 @@ managementConfiguration: type: redfish manifests: dummy_manifest: - metadataPath: manifests/site/test-site/metadata.yaml + metadataPath: metadata.yaml phaseRepositoryName: primary repositories: primary: diff --git a/pkg/config/testdata/manifest-string.yaml b/pkg/config/testdata/manifest-string.yaml index 9e1239a5d..f4a41b00c 100644 --- a/pkg/config/testdata/manifest-string.yaml +++ b/pkg/config/testdata/manifest-string.yaml @@ -1,4 +1,4 @@ -metadataPath: manifests/site/test-site/metadata.yaml +metadataPath: metadata.yaml phaseRepositoryName: primary repositories: primary: diff --git a/pkg/config/utils.go b/pkg/config/utils.go index 80f71095e..50f2c7591 100644 --- a/pkg/config/utils.go +++ b/pkg/config/utils.go @@ -52,6 +52,7 @@ func NewConfig() *Config { TargetPath: "/tmp/" + AirshipDefaultManifest, PhaseRepositoryName: DefaultTestPhaseRepo, SubPath: AirshipDefaultManifestRepo + "/manifests/site", + MetadataPath: DefaultManifestMetadataFile, }, }, } diff --git a/pkg/k8s/applier/executor_test.go b/pkg/k8s/applier/executor_test.go index 875afa35f..42870287b 100644 --- a/pkg/k8s/applier/executor_test.go +++ b/pkg/k8s/applier/executor_test.go @@ -229,8 +229,14 @@ func makeDefaultHelper(t *testing.T) ifc.Helper { }, Manifests: map[string]*config.Manifest{ "default-manifest": { - MetadataPath: "metadata.yaml", - TargetPath: "testdata", + MetadataPath: "metadata.yaml", + TargetPath: "testdata", + PhaseRepositoryName: config.DefaultTestPhaseRepo, + Repositories: map[string]*config.Repository{ + config.DefaultTestPhaseRepo: { + URLString: "", + }, + }, }, }, } diff --git a/pkg/phase/client.go b/pkg/phase/client.go index a7a2cbf0c..6a2141c7a 100644 --- a/pkg/phase/client.go +++ b/pkg/phase/client.go @@ -154,7 +154,8 @@ func (p *phase) DocumentRoot() (string, error) { } targetPath := p.helper.TargetPath() - return filepath.Join(targetPath, relativePath), nil + phaseRepoDir := p.helper.PhaseRepoDir() + return filepath.Join(targetPath, phaseRepoDir, relativePath), nil } // Details returns description of the phase diff --git a/pkg/phase/command_test.go b/pkg/phase/command_test.go index 91fa7e4c4..f143e76d0 100644 --- a/pkg/phase/command_test.go +++ b/pkg/phase/command_test.go @@ -61,8 +61,14 @@ func TestRunCommand(t *testing.T) { conf := config.NewConfig() conf.Manifests = map[string]*config.Manifest{ "manifest": { - MetadataPath: "broken_metadata.yaml", - TargetPath: "testdata", + MetadataPath: "broken_metadata.yaml", + TargetPath: "testdata", + PhaseRepositoryName: config.DefaultTestPhaseRepo, + Repositories: map[string]*config.Repository{ + config.DefaultTestPhaseRepo: { + URLString: "", + }, + }, }, } conf.CurrentContext = "context" @@ -125,8 +131,14 @@ func TestPlanCommand(t *testing.T) { conf := config.NewConfig() conf.Manifests = map[string]*config.Manifest{ "manifest": { - MetadataPath: "broken_metadata.yaml", - TargetPath: "testdata", + MetadataPath: "broken_metadata.yaml", + TargetPath: "testdata", + PhaseRepositoryName: config.DefaultTestPhaseRepo, + Repositories: map[string]*config.Repository{ + config.DefaultTestPhaseRepo: { + URLString: "", + }, + }, }, } conf.CurrentContext = "context" diff --git a/pkg/phase/helper.go b/pkg/phase/helper.go index 58cd82148..da3868fc9 100644 --- a/pkg/phase/helper.go +++ b/pkg/phase/helper.go @@ -34,8 +34,8 @@ type Helper struct { phaseRoot string inventoryRoot string targetPath string - - metadata *config.Metadata + phaseRepoDir string + metadata *config.Metadata } // NewHelper constructs metadata interface based on config @@ -47,12 +47,16 @@ func NewHelper(cfg *config.Config) (ifc.Helper, error) { if err != nil { return nil, err } + helper.phaseRepoDir, err = cfg.CurrentContextPhaseRepositoryDir() + if err != nil { + return nil, err + } helper.metadata, err = cfg.CurrentContextManifestMetadata() if err != nil { return nil, err } - helper.phaseRoot = filepath.Join(helper.targetPath, helper.metadata.PhaseMeta.Path) - helper.inventoryRoot = filepath.Join(helper.targetPath, helper.metadata.Inventory.Path) + helper.phaseRoot = filepath.Join(helper.targetPath, helper.phaseRepoDir, helper.metadata.PhaseMeta.Path) + helper.inventoryRoot = filepath.Join(helper.targetPath, helper.phaseRepoDir, helper.metadata.Inventory.Path) return helper, nil } @@ -197,6 +201,12 @@ func (helper *Helper) TargetPath() string { return helper.targetPath } +// PhaseRepoDir returns the last part of the repo url +// E.g. http://dummy.org/reponame.git -> reponame +func (helper *Helper) PhaseRepoDir() string { + return helper.phaseRepoDir +} + // PhaseRoot returns path to document root with phase documents func (helper *Helper) PhaseRoot() string { return helper.phaseRoot diff --git a/pkg/phase/helper_test.go b/pkg/phase/helper_test.go index 08c2bf8a6..92f735522 100644 --- a/pkg/phase/helper_test.go +++ b/pkg/phase/helper_test.go @@ -449,8 +449,11 @@ manifests: phaseRepositoryName: primary targetPath: testdata metadataPath: valid_site/metadata.yaml - subPath: valid_site` - + subPath: valid_site + repositories: + primary: + url: "empty/filename/" +` conf := &config.Config{} err := yaml.Unmarshal([]byte(confString), conf) require.NoError(t, err) diff --git a/pkg/phase/ifc/helper.go b/pkg/phase/ifc/helper.go index 72d7e1fa1..136c5bbea 100644 --- a/pkg/phase/ifc/helper.go +++ b/pkg/phase/ifc/helper.go @@ -23,6 +23,7 @@ import ( // Helper is a phase helper that provides phases with additional config related information type Helper interface { TargetPath() string + PhaseRepoDir() string WorkDir() (string, error) Phase(phaseID ID) (*v1alpha1.Phase, error) Plan() (*v1alpha1.PhasePlan, error) diff --git a/pkg/phase/render_test.go b/pkg/phase/render_test.go index 2c4935fbd..c1f87884a 100644 --- a/pkg/phase/render_test.go +++ b/pkg/phase/render_test.go @@ -33,6 +33,12 @@ func TestRender(t *testing.T) { rs := testutil.DummyConfig() dummyManifest := rs.Manifests["dummy_manifest"] dummyManifest.TargetPath = "testdata" + dummyManifest.PhaseRepositoryName = config.DefaultTestPhaseRepo + dummyManifest.Repositories = map[string]*config.Repository{ + config.DefaultTestPhaseRepo: { + URLString: "", + }, + } dummyManifest.SubPath = "" dummyManifest.MetadataPath = "metadata.yaml" fixturePath := "phase" diff --git a/pkg/remote/management_test.go b/pkg/remote/management_test.go index ca2ad8ada..64ab99a66 100644 --- a/pkg/remote/management_test.go +++ b/pkg/remote/management_test.go @@ -56,8 +56,11 @@ func withTestDataPath(path string) Configuration { if err != nil { panic(fmt.Sprintf("Unable to initialize management tests. Current Context error %q", err)) } - manifest.TargetPath = fmt.Sprintf("testdata/%s", path) + manifest.TargetPath = "testdata" manifest.MetadataPath = "metadata.yaml" + dummyRepo := testutil.DummyRepository() + dummyRepo.URLString = fmt.Sprintf("http://dummy.url.com/%s.git", path) + manifest.Repositories = map[string]*config.Repository{manifest.PhaseRepositoryName: dummyRepo} } } diff --git a/testutil/testconfig.go b/testutil/testconfig.go index bbfb98863..647dcaf18 100644 --- a/testutil/testconfig.go +++ b/testutil/testconfig.go @@ -69,6 +69,7 @@ func DummyManifest() *config.Manifest { // Repositories is the map of repository addressable by a name m.Repositories = map[string]*config.Repository{"primary": DummyRepository()} m.PhaseRepositoryName = "primary" + m.MetadataPath = "metadata.yaml" m.TargetPath = "/var/tmp/" m.SubPath = "manifests/site/test-site" return m diff --git a/tools/deployment/22_test_configs.sh b/tools/deployment/22_test_configs.sh index 5fb3e16a5..7f6495932 100755 --- a/tools/deployment/22_test_configs.sh +++ b/tools/deployment/22_test_configs.sh @@ -30,7 +30,7 @@ export REMOTE_PROXY=false export AIRSHIP_CONFIG_ISO_SERVE_HOST=${HOST:-"localhost"} export AIRSHIP_CONFIG_ISO_PORT=${SERVE_PORT} export AIRSHIP_CONFIG_ISO_NAME=${ISO_NAME:-"ubuntu-focal.iso"} -export AIRSHIP_CONFIG_METADATA_PATH=${AIRSHIP_CONFIG_METADATA_PATH:-"airshipctl/manifests/metadata.yaml"} +export AIRSHIP_CONFIG_METADATA_PATH=${AIRSHIP_CONFIG_METADATA_PATH:-"manifests/metadata.yaml"} export SYSTEM_ACTION_RETRIES=30 export SYSTEM_REBOOT_DELAY=30 export AIRSHIP_CONFIG_PHASE_REPO_BRANCH=${BRANCH:-"master"}