Skip to content
Snippets Groups Projects
Unverified Commit 0c1422e4 authored by bryanl's avatar bryanl
Browse files

moved env to pkg/env


Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent 3043f720
No related branches found
No related tags found
No related merge requests found
Showing
with 80 additions and 385 deletions
local params = import "../../components/params.libsonnet";
params {
components +: {
"guestbook-ui" +: {
name: "guestbook-dev",
},
},
}
......@@ -55,7 +55,7 @@ var _ = Describe("ks component", func() {
Describe("rm", func() {
It("removes a component", func() {
o := a.runKs("component", "rm", "guestbook-ui")
o := a.runKs("component", "rm", "guestbook-ui", "-v")
assertExitStatus(o, 0)
o = a.componentList()
......
......@@ -20,6 +20,7 @@ import (
"path"
"github.com/ksonnet/ksonnet/component"
"github.com/ksonnet/ksonnet/metadata/app"
param "github.com/ksonnet/ksonnet/metadata/params"
"github.com/ksonnet/ksonnet/prototype"
str "github.com/ksonnet/ksonnet/strings"
......@@ -115,7 +116,7 @@ func (m *manager) DeleteComponent(name string) error {
// Build the new environment/<env>/params.libsonnet files.
// environment name -> jsonnet
envJsonnets := make(map[string]string)
envs, err := m.GetEnvironments()
envs, err := ksApp.Environments()
if err != nil {
return err
}
......@@ -139,7 +140,7 @@ func (m *manager) DeleteComponent(name string) error {
// Remove the references in component/params.libsonnet.
log.Debugf("... deleting references in %s", m.componentParamsPath)
err = afero.WriteFile(m.appFS, ns.ParamsPath(), []byte(componentJsonnet), defaultFilePermissions)
err = afero.WriteFile(m.appFS, ns.ParamsPath(), []byte(componentJsonnet), app.DefaultFilePermissions)
if err != nil {
return err
}
......@@ -148,7 +149,7 @@ func (m *manager) DeleteComponent(name string) error {
for _, env := range envs {
path := str.AppendToPath(m.environmentsPath, env.Name, paramsFileName)
log.Debugf("... deleting references in %s", path)
err = afero.WriteFile(m.appFS, path, []byte(envJsonnets[env.Name]), defaultFilePermissions)
err = afero.WriteFile(m.appFS, path, []byte(envJsonnets[env.Name]), app.DefaultFilePermissions)
if err != nil {
return err
}
......@@ -234,5 +235,5 @@ func (m *manager) SetComponentParams(path string, params param.Params) error {
return err
}
return afero.WriteFile(m.appFS, paramsPath, []byte(jsonnet), defaultFilePermissions)
return afero.WriteFile(m.appFS, paramsPath, []byte(jsonnet), app.DefaultFilePermissions)
}
......@@ -16,23 +16,13 @@
package metadata
import (
"bytes"
"fmt"
"runtime/debug"
"github.com/ksonnet/ksonnet/env"
"github.com/ksonnet/ksonnet/metadata/lib"
"github.com/ksonnet/ksonnet/pkg/env"
str "github.com/ksonnet/ksonnet/strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
param "github.com/ksonnet/ksonnet/metadata/params"
)
const (
defaultEnvName = "default"
// primary environment files
envFileName = "main.jsonnet"
paramsFileName = "params.libsonnet"
......@@ -43,66 +33,15 @@ var (
envCreate = env.Create
)
func (m *manager) CreateEnvironment(name, server, namespace, k8sSpecFlag string) error {
debug.PrintStack()
return errors.Errorf("deprecated")
// a, err := m.App()
// if err != nil {
// return err
// }
// config := env.CreateConfig{
// App: a,
// Destination: env.NewDestination(server, namespace),
// K8sSpecFlag: k8sSpecFlag,
// Name: name,
// OverrideData: m.generateOverrideData(),
// ParamsData: m.generateParamsData(),
// }
// return envCreate(config)
}
func (m *manager) DeleteEnvironment(name string) error {
a, err := m.App()
if err != nil {
return err
}
// TODO: move this to actions
return env.Delete(a, name, false)
}
func (m *manager) GetEnvironments() (map[string]env.Env, error) {
a, err := m.App()
if err != nil {
return nil, err
}
log.Debug("Retrieving all environments")
return env.List(a)
}
func (m *manager) GetEnvironment(name string) (*env.Env, error) {
a, err := m.App()
if err != nil {
return nil, err
}
return env.Retrieve(a, name)
}
func (m *manager) SetEnvironment(from, to string) error {
a, err := m.App()
if err != nil {
return err
}
// func (m *manager) DeleteEnvironment(name string) error {
// a, err := m.App()
// if err != nil {
// return err
// }
// TODO: move this to an action
return env.Rename(a, from, to, false)
}
// // TODO: move this to actions
// return env.Delete(a, name, false)
// }
func (m *manager) GetEnvironmentParams(name, nsName string) (map[string]param.Params, error) {
a, err := m.App()
......@@ -155,32 +94,3 @@ func (m *manager) getLibPath(envName string) (string, error) {
return a.LibPath(envName)
}
func (m *manager) generateOverrideData() []byte {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("local base = import \"%s\";\n", baseLibsonnetFile))
buf.WriteString(fmt.Sprintf("local k = import \"%s\";\n\n", lib.ExtensionsLibFilename))
buf.WriteString("base + {\n")
buf.WriteString(" // Insert user-specified overrides here. For example if a component is named \"nginx-deployment\", you might have something like:\n")
buf.WriteString(" // \"nginx-deployment\"+: k.deployment.mixin.metadata.labels({foo: \"bar\"})\n")
buf.WriteString("}\n")
return buf.Bytes()
}
func (m *manager) generateParamsData() []byte {
const (
relComponentParamsPath = "../../" + componentsDir + "/" + paramsFileName
)
return []byte(`local params = import "` + relComponentParamsPath + `";
params + {
components +: {
// Insert component parameter overrides here. Ex:
// guestbook +: {
// name: "guestbook-dev",
// replicas: params.global.replicas,
// },
},
}
`)
}
......@@ -16,20 +16,14 @@
package metadata
import (
"os"
"github.com/ksonnet/ksonnet/component"
"github.com/ksonnet/ksonnet/env"
"github.com/ksonnet/ksonnet/metadata/app"
param "github.com/ksonnet/ksonnet/metadata/params"
"github.com/ksonnet/ksonnet/pkg/registry"
"github.com/ksonnet/ksonnet/prototype"
"github.com/spf13/afero"
)
var appFS afero.Fs
var defaultFolderPermissions = os.FileMode(0755)
var defaultFilePermissions = os.FileMode(0644)
// Manager abstracts over a ksonnet application's metadata, allowing users to do
// things like: create and delete environments; search for prototypes; vendor
......@@ -59,12 +53,7 @@ type Manager interface {
SetEnvironmentParams(env, component string, params param.Params) error
// Environment API.
// CreateEnvironment(name, uri, namespace, spec string) error
DeleteEnvironment(name string) error
GetEnvironments() (map[string]env.Env, error)
GetEnvironment(name string) (*env.Env, error)
SetEnvironment(name, desiredName string) error
GetDestination(envName string) (env.Destination, error)
// DeleteEnvironment(name string) error
}
// Find will recursively search the current directory and its parents for a
......@@ -74,27 +63,6 @@ func Find(path string) (Manager, error) {
return findManager(path, afero.NewOsFs())
}
// Init will generate the directory tree for a ksonnet project.
func Init(name, rootPath string, k8sSpecFlag, serverURI, namespace *string) (Manager, error) {
// Generate `incubator` registry. We do this before before creating
// directory tree, in case the network call fails.
const (
defaultIncubatorRegName = "incubator"
defaultIncubatorURI = "github.com/ksonnet/parts/tree/master/" + defaultIncubatorRegName
)
gh, err := registry.NewGitHub(&app.RegistryRefSpec{
Name: "incubator",
Protocol: registry.ProtocolGitHub,
URI: defaultIncubatorURI,
})
if err != nil {
return nil, err
}
return initManager(name, rootPath, k8sSpecFlag, serverURI, namespace, gh, appFS)
}
func init() {
appFS = afero.NewOsFs()
}
......@@ -21,13 +21,9 @@ import (
"path"
"path/filepath"
"github.com/ksonnet/ksonnet/component"
"github.com/ksonnet/ksonnet/env"
"github.com/ksonnet/ksonnet/metadata/app"
"github.com/ksonnet/ksonnet/pkg/registry"
str "github.com/ksonnet/ksonnet/strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
)
......@@ -115,55 +111,6 @@ func findManager(p string, appFS afero.Fs) (*manager, error) {
}
}
func initManager(name, rootPath string, k8sSpecFlag, serverURI, namespace *string, incubatorReg registry.Registry, appFS afero.Fs) (*manager, error) {
m, err := newManager(rootPath, appFS)
if err != nil {
return nil, errors.Wrap(err, "create manager")
}
// Retrieve `registry.yaml`.
registryYAMLData, err := generateRegistryYAMLData(incubatorReg)
if err != nil {
return nil, err
}
// Generate data for `app.yaml`.
appYAMLData, err := generateAppYAMLData(name, incubatorReg.MakeRegistryRefSpec())
if err != nil {
return nil, err
}
// Generate data for `base.libsonnet`.
baseLibData := genBaseLibsonnetContent()
// Initialize directory structure.
if err := m.createAppDirTree(name, appYAMLData, baseLibData, incubatorReg); err != nil {
return nil, err
}
// Initialize user dir structure.
if err := m.createUserDirTree(); err != nil {
return nil, errorOnCreateFailure(name, err)
}
// Initialize environment, and cache specification data.
if serverURI != nil {
err := m.CreateEnvironment(defaultEnvName, *serverURI, *namespace, *k8sSpecFlag)
if err != nil {
return nil, errorOnCreateFailure(name, err)
}
}
// Write out `incubator` registry spec.
registryPath := m.registryPath(incubatorReg)
err = afero.WriteFile(m.appFS, registryPath, registryYAMLData, defaultFilePermissions)
if err != nil {
return nil, errorOnCreateFailure(name, err)
}
return m, nil
}
func newManager(rootPath string, appFS afero.Fs) (*manager, error) {
usr, err := user.Current()
if err != nil {
......@@ -206,126 +153,3 @@ func (m *manager) App() (app.App, error) {
func (m *manager) LibPaths() (envPath, vendorPath string) {
return m.environmentsPath, m.vendorPath
}
func (m *manager) GetDestination(envName string) (env.Destination, error) {
appEnv, err := m.GetEnvironment(envName)
if err != nil {
return env.Destination{}, err
}
return appEnv.Destination, nil
}
func (m *manager) createUserDirTree() error {
dirPaths := []string{
m.userKsonnetRootPath,
m.pkgSrcCachePath,
}
for _, p := range dirPaths {
if err := m.appFS.MkdirAll(p, defaultFolderPermissions); err != nil {
return err
}
}
return nil
}
func (m *manager) createAppDirTree(name string, appYAMLData, baseLibData []byte, gh registry.Registry) error {
exists, err := afero.DirExists(m.appFS, m.rootPath)
if err != nil {
return fmt.Errorf("Could not check existence of directory '%s':\n%v", m.rootPath, err)
} else if exists {
return fmt.Errorf("Could not create app; directory '%s' already exists", m.rootPath)
}
dirPaths := []string{
m.rootPath,
m.ksonnetPath,
m.registriesPath,
m.libPath,
m.componentsPath,
m.environmentsPath,
m.vendorPath,
m.registryDir(gh),
}
for _, p := range dirPaths {
log.Debugf("Creating directory '%s'", p)
if err := m.appFS.MkdirAll(p, defaultFolderPermissions); err != nil {
return errorOnCreateFailure(name, err)
}
}
filePaths := []struct {
path string
content []byte
}{
{
m.componentParamsPath,
component.GenParamsContent(),
},
{
m.baseLibsonnetPath,
genBaseLibsonnetContent(),
},
{
m.appYAMLPath,
appYAMLData,
},
{
m.baseLibsonnetPath,
baseLibData,
},
}
for _, f := range filePaths {
log.Debugf("Creating file '%s'", f.path)
if err := afero.WriteFile(m.appFS, f.path, f.content, defaultFilePermissions); err != nil {
return err
}
}
return nil
}
func generateRegistryYAMLData(incubatorReg registry.Registry) ([]byte, error) {
regSpec, err := incubatorReg.FetchRegistrySpec()
if err != nil {
return nil, err
}
return regSpec.Marshal()
}
func generateAppYAMLData(name string, refs ...*app.RegistryRefSpec) ([]byte, error) {
content := app.Spec{
APIVersion: app.DefaultAPIVersion,
Kind: app.Kind,
Name: name,
Version: app.DefaultVersion,
Registries: app.RegistryRefSpecs{},
Environments: app.EnvironmentSpecs{},
}
for _, ref := range refs {
err := content.AddRegistryRef(ref)
if err != nil {
return nil, err
}
}
return content.Marshal()
}
func genBaseLibsonnetContent() []byte {
return []byte(`local components = std.extVar("` + ComponentsExtCodeKey + `");
components + {
// Insert user-specified overrides here.
}
`)
}
func errorOnCreateFailure(appName string, err error) error {
return fmt.Errorf("%s\nTo undo this simply delete directory '%s' and re-run `ks init`.\nIf the error persists, try using flag '--context' to set a different context or run `ks init --help` for more options", err, appName)
}
......@@ -19,8 +19,8 @@ import (
"path/filepath"
"github.com/ksonnet/ksonnet/component"
"github.com/ksonnet/ksonnet/env"
"github.com/ksonnet/ksonnet/metadata/app"
"github.com/ksonnet/ksonnet/pkg/env"
"github.com/ksonnet/ksonnet/pkg/registry"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
......@@ -78,8 +78,8 @@ func (i *initApp) Run() error {
d,
env.DefaultEnvName,
i.k8sSpecFlag,
env.DefaultOverrideData(),
env.DefaultParamsData(),
env.DefaultOverrideData,
env.DefaultParamsData,
false,
)
......@@ -193,7 +193,7 @@ func (i *initApp) createAppDirTree() error {
},
{
filepath.Join(i.rootPath, "environments", "base.libsonnet"),
env.DefaultBaseData(),
env.DefaultBaseData,
},
{
filepath.Join(i.rootPath, "app.yaml"),
......
......@@ -33,17 +33,6 @@ const (
DefaultEnvName = "default"
)
// CreateConfig is configuration for creating an environment.
type CreateConfig struct {
App app.App
Destination Destination
K8sSpecFlag string
Name string
OverrideData []byte
ParamsData []byte
}
// Create creates a new environment for the project.
func Create(a app.App, d Destination, name, k8sSpecFlag string, overrideData, paramsData []byte, isOverride bool) error {
c, err := newCreator(a, d, name, k8sSpecFlag, overrideData, paramsData, isOverride)
......
File moved
......@@ -15,35 +15,25 @@
package env
import (
"bytes"
"fmt"
)
const (
// ComponentsExtCodeKey is the ExtCode key for component imports
ComponentsExtCodeKey = "__ksonnet/components"
relComponentParamsPath = "../../components/params.libsonnet"
)
// DefaultOverrideData generates the contents for an environment's `main.jsonnet`.
func DefaultOverrideData() []byte {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("local base = import \"%s\";\n", "base.libsonnet"))
buf.WriteString(fmt.Sprintf("local k = import \"%s\";\n\n", "k.libsonnet"))
buf.WriteString("base + {\n")
buf.WriteString(" // Insert user-specified overrides here. For example if a component is named \"nginx-deployment\", you might have something like:\n")
buf.WriteString(" // \"nginx-deployment\"+: k.deployment.mixin.metadata.labels({foo: \"bar\"})\n")
buf.WriteString("}\n")
return buf.Bytes()
var DefaultOverrideData = []byte(`local base = import "base.libsonnet";
local k = import "k.libsonnet"
base + {
// Insert user-specified overrides here. For example if a component is named \"nginx-deployment\", you might have something like:\n")
// "nginx-deployment"+: k.deployment.mixin.metadata.labels({foo: "bar"})
}
`)
// DefaultParamsData generates the contents for an environment's `params.libsonnet`
func DefaultParamsData() []byte {
const (
relComponentParamsPath = "../../components/params.libsonnet"
)
return []byte(`local params = import "` + relComponentParamsPath + `";
var DefaultParamsData = []byte(`local params = import "` + relComponentParamsPath + `";
params + {
components +: {
// Insert component parameter overrides here. Ex:
......@@ -54,13 +44,10 @@ params + {
},
}
`)
}
// DefaultBaseData generates environment `base.libsonnet`.
func DefaultBaseData() []byte {
return []byte(`local components = std.extVar("` + ComponentsExtCodeKey + `");
var DefaultBaseData = []byte(`local components = std.extVar("` + ComponentsExtCodeKey + `");
components + {
// Insert user-specified overrides here.
}
`)
}
File moved
File moved
File moved
// Copyright 2017 The kubecfg authors
// Copyright 2018 The kubecfg authors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
......@@ -13,42 +13,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package kubecfg
package env
import (
"testing"
const (
// primary environment files.
envFileName = "main.jsonnet"
paramsFileName = "params.libsonnet"
"github.com/stretchr/testify/require"
// envRoot is the name for the environment root.
envRoot = "environments"
)
func TestSanitizeParamValue(t *testing.T) {
tests := []struct {
value string
expected string
}{
// numbers
{
value: "64.5",
expected: "64.5",
},
{
value: "64",
expected: "64",
},
// boolean
{
value: "false",
expected: `"false"`,
},
// string
{
value: "my string",
expected: `"my string"`,
},
}
for _, te := range tests {
got := sanitizeParamValue(te.value)
require.Equal(t, got, te.expected, "Unexpected sanitized param value")
}
}
......@@ -13,29 +13,4 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package kubecfg
import (
"github.com/ksonnet/ksonnet/metadata"
log "github.com/sirupsen/logrus"
)
type InitCmd struct {
name string
rootPath string
k8sSpecFlag *string
serverURI *string
namespace *string
}
func NewInitCmd(name, rootPath string, k8sSpecFlag, serverURI, namespace *string) (*InitCmd, error) {
return &InitCmd{name: name, rootPath: rootPath, k8sSpecFlag: k8sSpecFlag, serverURI: serverURI, namespace: namespace}, nil
}
func (c *InitCmd) Run() error {
_, err := metadata.Init(c.name, c.rootPath, c.k8sSpecFlag, c.serverURI, c.namespace)
if err == nil {
log.Info("ksonnet app successfully created! Next, try creating a component with `ks generate`.")
}
return err
}
package env
File moved
File moved
......@@ -98,39 +98,6 @@ func envExists(ksApp app.App, name string) (bool, error) {
return afero.Exists(ksApp.Fs(), path)
}
func moveDir(fs afero.Fs, src, dest string) error {
exists, err := afero.DirExists(fs, dest)
if err != nil {
return err
}
if !exists {
if err = fs.MkdirAll(dest, app.DefaultFolderPermissions); err != nil {
return errors.Wrapf(err, "unable to create destination %q", dest)
}
}
fis, err := afero.ReadDir(fs, src)
if err != nil {
return err
}
for _, fi := range fis {
if fi.IsDir() && fi.Name() != ".metadata" {
continue
}
srcPath := filepath.Join(src, fi.Name())
destPath := filepath.Join(dest, fi.Name())
if err = fs.Rename(srcPath, destPath); err != nil {
return err
}
}
return nil
}
func envPath(ksApp app.App, name string, subPath ...string) string {
return filepath.Join(append([]string{ksApp.Root(), envRoot, name}, subPath...)...)
}
......
File moved
File moved
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