diff --git a/metadata/environment.go b/metadata/environment.go index f31d36472962754fcc325db91db94e977eb980d6..d00fe04c324a6c1ae57ca0bb04fa2383f2c0ed2f 100644 --- a/metadata/environment.go +++ b/metadata/environment.go @@ -50,7 +50,7 @@ var envPaths = []string{ func (m *manager) CreateEnvironment(name, server, namespace, k8sSpecFlag string) error { // generate the lib data for this kubernetes version - libManager, err := lib.NewManagerWithSpec(k8sSpecFlag, m.appFS, m.libPath) + libManager, err := lib.NewManager(k8sSpecFlag, m.appFS, m.libPath) if err != nil { return err } @@ -371,26 +371,36 @@ func (m *manager) SetEnvironmentParams(env, component string, params param.Param } func (m *manager) EnvPaths(env string) (libPath, mainPath, paramsPath string, err error) { - envSpec, err := m.GetEnvironment(env) - if err != nil { - return - } - - libManager := lib.NewManager(envSpec.KubernetesVersion, m.appFS, m.libPath) + mainPath, paramsPath = m.makeEnvPaths(env) + libPath, err = m.getLibPath(env) + return +} +func (m *manager) makeEnvPaths(env string) (mainPath, paramsPath string) { envPath := str.AppendToPath(m.environmentsPath, env) // main.jsonnet file mainPath = str.AppendToPath(envPath, envFileName) // params.libsonnet file paramsPath = str.AppendToPath(envPath, componentParamsFile) - // ksonnet-lib file directory - libPath, err = libManager.GetLibPath() + return } -// errorOnSpec file is a temporary function to help migration from ks 0.8 to 0.9. -// It will return an error if a spec.json file is found in an environment directory. +func (m *manager) getLibPath(env string) (string, error) { + envSpec, err := m.GetEnvironment(env) + if err != nil { + return "", err + } + + libManager, err := lib.NewManager(fmt.Sprintf("version:%s", envSpec.KubernetesVersion), m.appFS, m.libPath) + if err != nil { + return "", err + } + + return libManager.GetLibPath() +} + func (m *manager) errorOnSpecFile() error { return afero.Walk(m.appFS, m.environmentsPath, func(p string, f os.FileInfo, err error) error { if err != nil { diff --git a/metadata/lib/lib.go b/metadata/lib/lib.go index df5fd18600e4a236c582b18060a04475566e18f1..8be7d908ecda2e7d1c9f710d5bfd840217829776 100644 --- a/metadata/lib/lib.go +++ b/metadata/lib/lib.go @@ -48,13 +48,8 @@ type Manager struct { fs afero.Fs } -// NewManager creates a crew instance of lib.Manager -func NewManager(k8sVersion string, fs afero.Fs, libPath string) *Manager { - return &Manager{K8sVersion: k8sVersion, fs: fs, libPath: libPath} -} - -// NewManagerWithSpec creates a new instance of lib.Manager with the cluster spec initialized. -func NewManagerWithSpec(k8sSpecFlag string, fs afero.Fs, libPath string) (*Manager, error) { +// NewManager creates a new instance of lib.Manager +func NewManager(k8sSpecFlag string, fs afero.Fs, libPath string) (*Manager, error) { // // Generate the program text for ksonnet-lib. // @@ -148,7 +143,14 @@ func (m *Manager) GetLibPath() (string, error) { return "", err } if !ok { - return "", fmt.Errorf("Expected lib directory '%s' but was not found", m.K8sVersion) + log.Debugf("Expected lib directory '%s' but was not found", m.K8sVersion) + + // create the directory + if err := m.GenerateLibData(); err != nil { + return "", err + } + + return path, nil } return path, err } diff --git a/metadata/lib/lib_test.go b/metadata/lib/lib_test.go index ac5777c72ca92a9c54c90fe4729be3cb60664c13..8e070e975f97cbe61591ebf76d2034416aaf3887 100644 --- a/metadata/lib/lib_test.go +++ b/metadata/lib/lib_test.go @@ -50,7 +50,7 @@ func TestGenerateLibData(t *testing.T) { specFlag := fmt.Sprintf("file:%s", blankSwagger) libPath := "lib" - libManager, err := NewManagerWithSpec(specFlag, testFS, libPath) + libManager, err := NewManager(specFlag, testFS, libPath) if err != nil { t.Fatal("Failed to initialize lib.Manager") } diff --git a/metadata/manager_test.go b/metadata/manager_test.go index 9ecb855c77a86f3f5768f09882cb99a8389f7846..aeb1e65068d473956a752b1df40c9ab27970c779 100644 --- a/metadata/manager_test.go +++ b/metadata/manager_test.go @@ -191,16 +191,13 @@ func TestLibPaths(t *testing.T) { } } -func TestEnvPaths(t *testing.T) { +func TestMakeEnvPaths(t *testing.T) { appName := "test-env-paths" expectedMainPath := path.Join(appName, environmentsDir, mockEnvName, envFileName) expectedParamsPath := path.Join(appName, environmentsDir, mockEnvName, paramsFileName) m := mockEnvironments(t, appName) - _, mainPath, paramsPath, err := m.EnvPaths(mockEnvName) - if err != nil { - t.Fatalf("Failure retrieving EnvPaths") - } + mainPath, paramsPath := m.makeEnvPaths(mockEnvName) if mainPath != expectedMainPath { t.Fatalf("Expected environment main path to be:\n '%s'\n, got:\n '%s'", expectedMainPath, mainPath)