Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #634 from bryanl/move-ks-lib
Browse files Browse the repository at this point in the history
move/upgrade ks lib location
  • Loading branch information
bryanl committed Jun 20, 2018
2 parents b6c47c8 + c2bef13 commit 6daf293
Show file tree
Hide file tree
Showing 11 changed files with 325 additions and 77 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
VERSION?=dev-$(shell date +%FT%T%z)
KS_BIN?=ks

APIMACHINERY_VER := $(shell grep -B1 k8s.io/apimachinery Gopkg.lock | head -n1 | cut -d'"' -f2)
APIMACHINERY_VER := $(shell grep -A100 k8s.io/apimachinery Gopkg.lock | grep -m1 "version =" | cut -d'"' -f2)
REVISION=$(shell git rev-parse HEAD)
GIT_TAG=$(shell git describe --tags)

Expand Down
2 changes: 1 addition & 1 deletion pkg/actions/env_udpate.go → pkg/actions/env_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ func genLib(a app.App, k8sSpecFlag, libPath string) error {
return err
}

return libManager.GenerateLibData(false)
return libManager.GenerateLibData()
}
6 changes: 3 additions & 3 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ func Load(fs afero.Fs, cwd string, skipFindRoot bool) (App, error) {
}
}

func updateLibData(fs afero.Fs, k8sSpecFlag, libPath string, useVersionPath bool) (string, error) {
func updateLibData(fs afero.Fs, k8sSpecFlag, libPath string) (string, error) {
lm, err := lib.NewManager(k8sSpecFlag, fs, libPath)
if err != nil {
return "", err
}

if err := lm.GenerateLibData(useVersionPath); err != nil {
if err := lm.GenerateLibData(); err != nil {
return "", err
}

Expand All @@ -142,7 +142,7 @@ func app010LibPath(root string) string {
}

// StubUpdateLibData always returns no error.
func StubUpdateLibData(fs afero.Fs, k8sSpecFlag, libPath string, useVersionPath bool) (string, error) {
func StubUpdateLibData(fs afero.Fs, k8sSpecFlag, libPath string) (string, error) {
return "v1.8.7", nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/app/app001.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (a *App001) AddEnvironment(name, k8sSpecFlag string, spec *EnvironmentSpec,
return err
}

_, err = LibUpdater(a.fs, k8sSpecFlag, a.appLibPath(name), false)
_, err = LibUpdater(a.fs, k8sSpecFlag, a.appLibPath(name))
return err
}

Expand Down Expand Up @@ -287,7 +287,7 @@ func (a *App001) convertEnvironment(envName string, dryRun bool) error {
}

k8sSpecFlag := fmt.Sprintf("version:%s", env.KubernetesVersion)
_, err = LibUpdater(a.fs, k8sSpecFlag, app010LibPath(a.root), true)
_, err = LibUpdater(a.fs, k8sSpecFlag, app010LibPath(a.root))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/app001_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func TestApp001_UpdateTargets(t *testing.T) {

func withApp001Fs(t *testing.T, appName string, fn func(app *App001)) {
ogLibUpdater := LibUpdater
LibUpdater = func(fs afero.Fs, k8sSpecFlag string, libPath string, useVersionPath bool) (string, error) {
LibUpdater = func(fs afero.Fs, k8sSpecFlag string, libPath string) (string, error) {
path := filepath.Join(libPath, "swagger.json")
stageFile(t, fs, "swagger.json", path)
return "v1.8.7", nil
Expand Down
82 changes: 80 additions & 2 deletions pkg/app/app010.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package app

import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/ksonnet/ksonnet/pkg/lib"
Expand All @@ -31,6 +33,9 @@ import (
// App010 is a ksonnet 0.1.0 application.
type App010 struct {
*baseApp

out io.Writer
libPaths map[string]string
}

var _ App = (*App010)(nil)
Expand All @@ -41,6 +46,9 @@ func NewApp010(fs afero.Fs, root string) *App010 {

a := &App010{
baseApp: ba,
out: os.Stdout,

libPaths: make(map[string]string),
}

return a
Expand All @@ -59,7 +67,7 @@ func (a *App010) AddEnvironment(name, k8sSpecFlag string, newEnv *EnvironmentSpe
}

if k8sSpecFlag != "" {
ver, err := LibUpdater(a.fs, k8sSpecFlag, app010LibPath(a.root), true)
ver, err := LibUpdater(a.fs, k8sSpecFlag, app010LibPath(a.root))
if err != nil {
return err
}
Expand Down Expand Up @@ -139,6 +147,10 @@ func (a *App010) Init() error {

// LibPath returns the lib path for an env environment.
func (a *App010) LibPath(envName string) (string, error) {
if lp, ok := a.libPaths[envName]; ok {
return lp, nil
}

env, err := a.Environment(envName)
if err != nil {
return "", err
Expand All @@ -150,7 +162,24 @@ func (a *App010) LibPath(envName string) (string, error) {
return "", err
}

return lm.GetLibPath(true)
lp, err := lm.GetLibPath()
if err != nil {
return "", err
}

a.checkKsonnetLib(lp)

a.libPaths[envName] = lp
return lp, nil
}

func (a *App010) checkKsonnetLib(lp string) {
libRoot := filepath.Join(a.Root(), LibDirName, "ksonnet-lib")
if !strings.HasPrefix(lp, libRoot) {
logrus.Warnf("ksonnet has moved ksonnet-lib paths to %q. The current location of "+
"of your existing ksonnet-libs can be automatically moved by ksonnet with `ks upgrade`",
libRoot)
}
}

// Libraries returns application libraries.
Expand Down Expand Up @@ -239,6 +268,55 @@ func (a *App010) UpdateTargets(envName string, targets []string) error {

// Upgrade upgrades the app to the latest apiVersion.
func (a *App010) Upgrade(dryRun bool) error {
if err := a.checkForOldKSLibLocation(dryRun); err != nil {
return err
}

return nil
}

var (
// reKSLibName matches a ksonnet library directory e.g. v1.10.3.
reKSLibName = regexp.MustCompile(`^v\d+\.\d+\.\d+$`)
)

func (a *App010) checkForOldKSLibLocation(dryRun bool) error {
libRoot := filepath.Join(a.Root(), LibDirName)
fis, err := afero.ReadDir(a.Fs(), libRoot)
if err != nil {
return err
}

if dryRun {
fmt.Fprintf(a.out, "[dry run] Updating ksonnet-lib paths\n")
}

if err = a.fs.MkdirAll(filepath.Join(libRoot, lib.KsonnetLibHome), DefaultFolderPermissions); err != nil {
return err
}

for _, fi := range fis {
if !fi.IsDir() {
continue
}

if reKSLibName.MatchString(fi.Name()) {
p := filepath.Join(libRoot, fi.Name())
new := filepath.Join(libRoot, lib.KsonnetLibHome, fi.Name())

if dryRun {
fmt.Fprintf(a.out, "[dry run] Moving %q from %s to %s\n", fi.Name(), p, new)
continue
}

fmt.Fprintf(a.out, "Moving %q from %s to %s\n", fi.Name(), p, new)
err = a.fs.Rename(p, new)
if err != nil {
return errors.Wrapf(err, "renaming %s to %s", p, new)
}
}
}

return nil
}

Expand Down
83 changes: 77 additions & 6 deletions pkg/app/app010_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func TestApp0101_LibPath(t *testing.T) {
path, err := app.LibPath("default")
require.NoError(t, err)

expected := filepath.Join("/", "lib", "v1.7.0")
expected := filepath.Join("/", "lib", "ksonnet-lib", "v1.7.0")
require.Equal(t, expected, path)
})
}
Expand Down Expand Up @@ -263,10 +263,81 @@ func TestApp010_RenameEnvironment(t *testing.T) {
}

func TestApp010_Upgrade(t *testing.T) {
withApp010Fs(t, "app010_app.yaml", func(app *App010) {
err := app.Upgrade(false)
require.NoError(t, err)
})
cases := []struct {
name string
init func(t *testing.T, app *App010)
checkUpgrade func(t *testing.T, app *App010)
dryRun bool
isErr bool
}{
{
name: "ksonnet lib doesn't need to be upgraded",
init: func(t *testing.T, app *App010) {
err := app.Fs().MkdirAll("/lib", DefaultFolderPermissions)
require.NoError(t, err)

p := filepath.Join(app.Root(), "lib", "ksonnet-lib", "v1.10.3")
err = app.Fs().MkdirAll(p, DefaultFolderPermissions)
require.NoError(t, err)
},
dryRun: false,
},
{
name: "ksonnet lib needs to be upgraded",
init: func(t *testing.T, app *App010) {
err := app.Fs().MkdirAll("/lib", DefaultFolderPermissions)
require.NoError(t, err)

p := filepath.Join(app.Root(), "lib", "v1.10.3")
err = app.Fs().MkdirAll(p, DefaultFolderPermissions)
require.NoError(t, err)
},
dryRun: false,
},
{
name: "ksonnet lib needs to be upgraded - dry run",
init: func(t *testing.T, app *App010) {
err := app.Fs().MkdirAll("/lib", DefaultFolderPermissions)
require.NoError(t, err)

p := filepath.Join(app.Root(), "lib", "v1.10.3")
err = app.Fs().MkdirAll(p, DefaultFolderPermissions)
require.NoError(t, err)
},
checkUpgrade: func(t *testing.T, app *App010) {
isDir, err := afero.IsDir(app.Fs(), filepath.Join("/lib", "v1.10.3"))
require.NoError(t, err)
require.True(t, isDir)
},
dryRun: true,
},
{
name: "lib doesn't exist",
isErr: true,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
withApp010Fs(t, "app010_app.yaml", func(app *App010) {
if tc.init != nil {
tc.init(t, app)
}

err := app.Upgrade(tc.dryRun)
if tc.isErr {
require.Error(t, err)
return
}

require.NoError(t, err)

if tc.checkUpgrade != nil {
tc.checkUpgrade(t, app)
}
})
})
}
}

func TestApp0101_UpdateTargets(t *testing.T) {
Expand All @@ -284,7 +355,7 @@ func TestApp0101_UpdateTargets(t *testing.T) {

func withApp010Fs(t *testing.T, appName string, fn func(app *App010)) {
ogLibUpdater := LibUpdater
LibUpdater = func(fs afero.Fs, k8sSpecFlag string, libPath string, useVersionPath bool) (string, error) {
LibUpdater = func(fs afero.Fs, k8sSpecFlag string, libPath string) (string, error) {
return "v1.8.7", nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/appinit/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func assertExists(t *testing.T, fs afero.Fs, path string) {
func assertLib(t *testing.T, fs afero.Fs, root, version string) {
files := []string{"swagger.json", "k.libsonnet", "k8s.libsonnet"}
for _, f := range files {
path := filepath.Join(root, "lib", version, f)
path := filepath.Join(root, "lib", "ksonnet-lib", version, f)
assertExists(t, fs, path)
}
}
4 changes: 2 additions & 2 deletions pkg/lib/clusterspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type parseSuccess struct {

func TestClusterSpecParsingSuccess(t *testing.T) {
testFS := afero.NewMemMapFs()
afero.WriteFile(testFS, blankSwagger, []byte(blankSwaggerData), os.ModePerm)
afero.WriteFile(testFS, swaggerLocation, []byte(blankSwaggerData), os.ModePerm)

var successTests = []parseSuccess{
{"version:v1.7.1", &clusterSpecVersion{"v1.7.1"}},
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestClusterSpecParsingFailure(t *testing.T) {

for _, test := range failureTests {
testFS := afero.NewMemMapFs()
afero.WriteFile(testFS, blankSwagger, []byte(blankSwaggerData), os.ModePerm)
afero.WriteFile(testFS, swaggerLocation, []byte(blankSwaggerData), os.ModePerm)
_, err := ParseClusterSpec(test.input, testFS)
if err == nil {
t.Errorf("Cluster spec parse for '%s' should have failed, but succeeded", test.input)
Expand Down

0 comments on commit 6daf293

Please sign in to comment.