Unverified Commit 40a626d3 authored by Bryan Liles's avatar Bryan Liles Committed by GitHub
Browse files

Merge pull request #337 from bryanl/jessicayuen-gen-lib

Attempt to generate lib directory when not found
parents 44700711 90fe67de
...@@ -50,7 +50,7 @@ var envPaths = []string{ ...@@ -50,7 +50,7 @@ var envPaths = []string{
func (m *manager) CreateEnvironment(name, server, namespace, k8sSpecFlag string) error { func (m *manager) CreateEnvironment(name, server, namespace, k8sSpecFlag string) error {
// generate the lib data for this kubernetes version // 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 { if err != nil {
return err return err
} }
...@@ -371,26 +371,36 @@ func (m *manager) SetEnvironmentParams(env, component string, params param.Param ...@@ -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) { func (m *manager) EnvPaths(env string) (libPath, mainPath, paramsPath string, err error) {
envSpec, err := m.GetEnvironment(env) mainPath, paramsPath = m.makeEnvPaths(env)
if err != nil { libPath, err = m.getLibPath(env)
return return
} }
libManager := lib.NewManager(envSpec.KubernetesVersion, m.appFS, m.libPath)
func (m *manager) makeEnvPaths(env string) (mainPath, paramsPath string) {
envPath := str.AppendToPath(m.environmentsPath, env) envPath := str.AppendToPath(m.environmentsPath, env)
// main.jsonnet file // main.jsonnet file
mainPath = str.AppendToPath(envPath, envFileName) mainPath = str.AppendToPath(envPath, envFileName)
// params.libsonnet file // params.libsonnet file
paramsPath = str.AppendToPath(envPath, componentParamsFile) paramsPath = str.AppendToPath(envPath, componentParamsFile)
// ksonnet-lib file directory
libPath, err = libManager.GetLibPath()
return return
} }
// errorOnSpec file is a temporary function to help migration from ks 0.8 to 0.9. func (m *manager) getLibPath(env string) (string, error) {
// It will return an error if a spec.json file is found in an environment directory. 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 { func (m *manager) errorOnSpecFile() error {
return afero.Walk(m.appFS, m.environmentsPath, func(p string, f os.FileInfo, err error) error { return afero.Walk(m.appFS, m.environmentsPath, func(p string, f os.FileInfo, err error) error {
if err != nil { if err != nil {
......
...@@ -48,13 +48,8 @@ type Manager struct { ...@@ -48,13 +48,8 @@ type Manager struct {
fs afero.Fs fs afero.Fs
} }
// NewManager creates a crew instance of lib.Manager // NewManager creates a new instance of lib.Manager
func NewManager(k8sVersion string, fs afero.Fs, libPath string) *Manager { func NewManager(k8sSpecFlag string, fs afero.Fs, libPath string) (*Manager, error) {
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) {
// //
// Generate the program text for ksonnet-lib. // Generate the program text for ksonnet-lib.
// //
...@@ -148,7 +143,14 @@ func (m *Manager) GetLibPath() (string, error) { ...@@ -148,7 +143,14 @@ func (m *Manager) GetLibPath() (string, error) {
return "", err return "", err
} }
if !ok { 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 return path, err
} }
...@@ -50,7 +50,7 @@ func TestGenerateLibData(t *testing.T) { ...@@ -50,7 +50,7 @@ func TestGenerateLibData(t *testing.T) {
specFlag := fmt.Sprintf("file:%s", blankSwagger) specFlag := fmt.Sprintf("file:%s", blankSwagger)
libPath := "lib" libPath := "lib"
libManager, err := NewManagerWithSpec(specFlag, testFS, libPath) libManager, err := NewManager(specFlag, testFS, libPath)
if err != nil { if err != nil {
t.Fatal("Failed to initialize lib.Manager") t.Fatal("Failed to initialize lib.Manager")
} }
......
...@@ -191,16 +191,13 @@ func TestLibPaths(t *testing.T) { ...@@ -191,16 +191,13 @@ func TestLibPaths(t *testing.T) {
} }
} }
func TestEnvPaths(t *testing.T) { func TestMakeEnvPaths(t *testing.T) {
appName := "test-env-paths" appName := "test-env-paths"
expectedMainPath := path.Join(appName, environmentsDir, mockEnvName, envFileName) expectedMainPath := path.Join(appName, environmentsDir, mockEnvName, envFileName)
expectedParamsPath := path.Join(appName, environmentsDir, mockEnvName, paramsFileName) expectedParamsPath := path.Join(appName, environmentsDir, mockEnvName, paramsFileName)
m := mockEnvironments(t, appName) m := mockEnvironments(t, appName)
_, mainPath, paramsPath, err := m.EnvPaths(mockEnvName) mainPath, paramsPath := m.makeEnvPaths(mockEnvName)
if err != nil {
t.Fatalf("Failure retrieving EnvPaths")
}
if mainPath != expectedMainPath { if mainPath != expectedMainPath {
t.Fatalf("Expected environment main path to be:\n '%s'\n, got:\n '%s'", expectedMainPath, mainPath) t.Fatalf("Expected environment main path to be:\n '%s'\n, got:\n '%s'", expectedMainPath, mainPath)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment