Unverified Commit 9bb797ae authored by Jess's avatar Jess Committed by GitHub
Browse files

Merge pull request #315 from jessicayuen/metadata

Add LibManager for managing k8s API and ksonnet-lib metadata
parents 6bd97f75 dbdb6389
......@@ -21,7 +21,6 @@ import (
"github.com/spf13/cobra"
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/pkg/kubecfg"
)
......@@ -104,7 +103,6 @@ var applyCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
c.ClientPool, c.Discovery, err = restClientPool(cmd, &env)
if err != nil {
......@@ -120,14 +118,14 @@ var applyCmd = &cobra.Command{
cmd: cmd,
env: env,
components: componentNames,
cwd: wd,
cwd: cwd,
})
objs, err := te.Expand()
if err != nil {
return err
}
return c.Run(objs, wd)
return c.Run(objs, cwd)
},
Long: `
The ` + "`apply`" + `command uses local manifest(s) to update (and optionally create)
......
......@@ -21,7 +21,6 @@ import (
"github.com/spf13/cobra"
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/pkg/kubecfg"
)
......@@ -66,7 +65,6 @@ var deleteCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
c.ClientPool, c.Discovery, err = restClientPool(cmd, &env)
if err != nil {
......@@ -82,7 +80,7 @@ var deleteCmd = &cobra.Command{
cmd: cmd,
env: env,
components: componentNames,
cwd: wd,
cwd: cwd,
})
objs, err := te.Expand()
if err != nil {
......
......@@ -61,7 +61,6 @@ var diffCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
componentNames, err := flags.GetStringArray(flagComponent)
if err != nil {
......@@ -83,7 +82,7 @@ var diffCmd = &cobra.Command{
return err
}
c, err := initDiffCmd(appFs, cmd, wd, env1, env2, componentNames, diffStrategy)
c, err := initDiffCmd(appFs, cmd, cwd, env1, env2, componentNames, diffStrategy)
if err != nil {
return err
}
......@@ -141,7 +140,7 @@ ks diff dev -c redis
`,
}
func initDiffCmd(fs afero.Fs, cmd *cobra.Command, wd metadata.AbsPath, envFq1, envFq2 *string, files []string, diffStrategy string) (kubecfg.DiffCmd, error) {
func initDiffCmd(fs afero.Fs, cmd *cobra.Command, wd string, envFq1, envFq2 *string, files []string, diffStrategy string) (kubecfg.DiffCmd, error) {
const (
remote = "remote"
local = "local"
......@@ -186,7 +185,7 @@ func initDiffCmd(fs afero.Fs, cmd *cobra.Command, wd metadata.AbsPath, envFq1, e
}
// initDiffSingleEnv sets up configurations for diffing using one environment
func initDiffSingleEnv(fs afero.Fs, env, diffStrategy string, files []string, cmd *cobra.Command, wd metadata.AbsPath) (kubecfg.DiffCmd, error) {
func initDiffSingleEnv(fs afero.Fs, env, diffStrategy string, files []string, cmd *cobra.Command, wd string) (kubecfg.DiffCmd, error) {
c := kubecfg.DiffRemoteCmd{}
c.DiffStrategy = diffStrategy
c.Client = &kubecfg.Client{}
......@@ -322,8 +321,11 @@ func expandEnvObjs(fs afero.Fs, cmd *cobra.Command, env string, manager metadata
return nil, err
}
libPath, vendorPath := manager.LibPaths()
metadataPath, mainPath, paramsPath := manager.EnvPaths(env)
_, vendorPath := manager.LibPaths()
libPath, mainPath, paramsPath, err := manager.EnvPaths(env)
if err != nil {
return nil, err
}
componentPaths, err := manager.ComponentPaths()
if err != nil {
return nil, err
......@@ -339,7 +341,7 @@ func expandEnvObjs(fs afero.Fs, cmd *cobra.Command, env string, manager metadata
return nil, err
}
expander.FlagJpath = append([]string{string(libPath), string(vendorPath), string(metadataPath)}, expander.FlagJpath...)
expander.FlagJpath = append([]string{string(vendorPath), string(libPath)}, expander.FlagJpath...)
expander.ExtCodes = append([]string{baseObj, params, envSpec}, expander.ExtCodes...)
envFiles := []string{string(mainPath)}
......
......@@ -125,9 +125,8 @@ var envAddCmd = &cobra.Command{
if err != nil {
return err
}
appRoot := metadata.AbsPath(appDir)
manager, err := metadata.Find(appRoot)
manager, err := metadata.Find(appDir)
if err != nil {
return err
}
......@@ -209,9 +208,8 @@ var envRmCmd = &cobra.Command{
if err != nil {
return err
}
appRoot := metadata.AbsPath(appDir)
manager, err := metadata.Find(appRoot)
manager, err := metadata.Find(appDir)
if err != nil {
return err
}
......@@ -258,9 +256,8 @@ var envListCmd = &cobra.Command{
if err != nil {
return err
}
appRoot := metadata.AbsPath(appDir)
manager, err := metadata.Find(appRoot)
manager, err := metadata.Find(appDir)
if err != nil {
return err
}
......@@ -301,9 +298,8 @@ var envSetCmd = &cobra.Command{
if err != nil {
return err
}
appRoot := metadata.AbsPath(appDir)
manager, err := metadata.Find(appRoot)
manager, err := metadata.Find(appDir)
if err != nil {
return err
}
......
......@@ -21,7 +21,6 @@ import (
"os"
"path/filepath"
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/pkg/kubecfg"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
......@@ -62,13 +61,11 @@ var initCmd = &cobra.Command{
return err
}
path, err := genKsRoot(appName, wd, initDir)
appRoot, err := genKsRoot(appName, wd, initDir)
if err != nil {
return err
}
appRoot := metadata.AbsPath(path)
specFlag, err := flags.GetString(flagAPISpec)
if err != nil {
return err
......@@ -80,7 +77,7 @@ var initCmd = &cobra.Command{
}
log.Infof("Creating a new app '%s' at path '%s'", appName, appRoot)
c, err := kubecfg.NewInitCmd(appName, appRoot, specFlag, &server, &namespace)
c, err := kubecfg.NewInitCmd(appName, appRoot, &specFlag, &server, &namespace)
if err != nil {
return err
}
......
......@@ -22,7 +22,7 @@ import (
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/metadata/parts"
"github.com/ksonnet/ksonnet/utils"
str "github.com/ksonnet/ksonnet/strings"
"github.com/spf13/cobra"
)
......@@ -101,9 +101,8 @@ var pkgInstallCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
manager, err := metadata.Find(wd)
manager, err := metadata.Find(cwd)
if err != nil {
return err
}
......@@ -165,9 +164,8 @@ var pkgDescribeCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
manager, err := metadata.Find(wd)
manager, err := metadata.Find(cwd)
if err != nil {
return err
}
......@@ -238,9 +236,8 @@ var pkgListCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
manager, err := metadata.Find(wd)
manager, err := metadata.Find(cwd)
if err != nil {
return err
}
......@@ -273,7 +270,7 @@ var pkgListCmd = &cobra.Command{
}
}
formatted, err := utils.PadRows(rows)
formatted, err := str.PadRows(rows)
if err != nil {
return err
}
......
......@@ -26,7 +26,7 @@ import (
"github.com/ksonnet/ksonnet/prototype"
"github.com/ksonnet/ksonnet/prototype/snippet"
"github.com/ksonnet/ksonnet/prototype/snippet/jsonnet"
"github.com/ksonnet/ksonnet/utils"
str "github.com/ksonnet/ksonnet/strings"
"github.com/spf13/cobra"
)
......@@ -87,9 +87,8 @@ var prototypeListCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
manager, err := metadata.Find(wd)
manager, err := metadata.Find(cwd)
if err != nil {
return err
}
......@@ -143,10 +142,9 @@ var prototypeDescribeCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
extProtos := prototype.SpecificationSchemas{}
manager, err := metadata.Find(wd)
manager, err := metadata.Find(cwd)
if err == nil {
extProtos, err = manager.GetAllPrototypes()
if err != nil {
......@@ -255,10 +253,9 @@ var prototypePreviewCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
extProtos := prototype.SpecificationSchemas{}
manager, err := metadata.Find(wd)
manager, err := metadata.Find(cwd)
if err == nil {
extProtos, err = manager.GetAllPrototypes()
if err != nil {
......@@ -356,7 +353,7 @@ var prototypeUseCmd = &cobra.Command{
if err != nil {
return err
}
manager, err := metadata.Find(metadata.AbsPath(cwd))
manager, err := metadata.Find(cwd)
if err != nil {
return fmt.Errorf("Command can only be run in a ksonnet application directory:\n\n%v", err)
}
......@@ -500,7 +497,7 @@ func expandPrototype(proto *prototype.SpecificationSchema, templateType prototyp
}
if templateType == prototype.Jsonnet {
componentsText := "components." + componentName
if !utils.IsASCIIIdentifier(componentName) {
if !str.IsASCIIIdentifier(componentName) {
componentsText = fmt.Sprintf(`components["%s"]`, componentName)
}
template = append([]string{
......
......@@ -7,7 +7,7 @@ import (
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/pkg/kubecfg"
"github.com/ksonnet/ksonnet/utils"
str "github.com/ksonnet/ksonnet/strings"
"github.com/spf13/cobra"
)
......@@ -76,9 +76,8 @@ var registryListCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
manager, err := metadata.Find(wd)
manager, err := metadata.Find(cwd)
if err != nil {
return err
}
......@@ -100,7 +99,7 @@ var registryListCmd = &cobra.Command{
rows = append(rows, []string{name, regRef.Protocol, regRef.URI})
}
formatted, err := utils.PadRows(rows)
formatted, err := str.PadRows(rows)
if err != nil {
return err
}
......@@ -136,9 +135,8 @@ var registryDescribeCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
manager, err := metadata.Find(wd)
manager, err := metadata.Find(cwd)
if err != nil {
return err
}
......
......@@ -39,6 +39,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
"github.com/ksonnet/ksonnet/metadata"
str "github.com/ksonnet/ksonnet/strings"
"github.com/ksonnet/ksonnet/template"
"github.com/ksonnet/ksonnet/utils"
......@@ -332,9 +333,8 @@ func overrideCluster(envName string, clientConfig clientcmd.ClientConfig, overri
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
metadataManager, err := metadata.Find(wd)
metadataManager, err := metadata.Find(cwd)
if err != nil {
return err
}
......@@ -346,7 +346,7 @@ func overrideCluster(envName string, clientConfig clientcmd.ClientConfig, overri
var servers = make(map[string]string)
for name, cluster := range rawConfig.Clusters {
server, err := utils.NormalizeURL(cluster.Server)
server, err := str.NormalizeURL(cluster.Server)
if err != nil {
return err
}
......@@ -366,7 +366,7 @@ func overrideCluster(envName string, clientConfig clientcmd.ClientConfig, overri
}
// TODO support multi-cluster deployment.
server, err := utils.NormalizeURL(env.Destinations[0].Server)
server, err := str.NormalizeURL(env.Destinations[0].Server)
if err != nil {
return err
}
......@@ -392,7 +392,7 @@ type cmdObjExpanderConfig struct {
cmd *cobra.Command
env string
components []string
cwd metadata.AbsPath
cwd string
}
// cmdObjExpander finds and expands templates for the family of commands of
......@@ -432,10 +432,13 @@ func (te *cmdObjExpander) Expand() ([]*unstructured.Unstructured, error) {
return nil, errors.Wrap(err, "find metadata")
}
libPath, vendorPath := manager.LibPaths()
metadataPath, mainPath, paramsPath := manager.EnvPaths(te.config.env)
_, vendorPath := manager.LibPaths()
libPath, mainPath, paramsPath, err := manager.EnvPaths(te.config.env)
if err != nil {
return nil, err
}
expander.FlagJpath = append([]string{string(libPath), string(vendorPath), string(metadataPath)}, expander.FlagJpath...)
expander.FlagJpath = append([]string{string(vendorPath), string(libPath)}, expander.FlagJpath...)
componentPaths, err := manager.ComponentPaths()
if err != nil {
......@@ -524,7 +527,7 @@ func constructBaseObj(componentPaths, componentNames []string) (string, error) {
// Emit object field. Sanitize the name to guarantee we generate valid
// Jsonnet.
componentName = utils.QuoteNonASCII(componentName)
componentName = str.QuoteNonASCII(componentName)
fmt.Fprintf(&obj, " %s: %s,\n", componentName, importExpr)
}
......
......@@ -21,7 +21,6 @@ import (
"github.com/spf13/cobra"
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/pkg/kubecfg"
)
......@@ -97,13 +96,12 @@ ks show dev -c redis -c nginx-server
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
te := newCmdObjExpander(cmdObjExpanderConfig{
cmd: cmd,
env: env,
components: componentNames,
cwd: wd,
cwd: cwd,
})
objs, err := te.Expand()
if err != nil {
......
......@@ -21,7 +21,6 @@ import (
"github.com/spf13/cobra"
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/pkg/kubecfg"
)
......@@ -54,7 +53,6 @@ var validateCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
componentNames, err := flags.GetStringArray(flagComponent)
if err != nil {
......@@ -70,7 +68,7 @@ var validateCmd = &cobra.Command{
cmd: cmd,
env: env,
components: componentNames,
cwd: wd,
cwd: cwd,
})
objs, err := te.Expand()
if err != nil {
......
......@@ -100,6 +100,7 @@ func runKsonnetWith(flags []string, host, ns string) error {
Server: host,
},
},
KubernetesVersion: "v1.7.0",
},
},
}
......
......@@ -23,13 +23,14 @@ import (
param "github.com/ksonnet/ksonnet/metadata/params"
"github.com/ksonnet/ksonnet/prototype"
str "github.com/ksonnet/ksonnet/strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
)
func (m *manager) ComponentPaths() (AbsPaths, error) {
paths := AbsPaths{}
err := afero.Walk(m.appFS, string(m.componentsPath), func(p string, info os.FileInfo, err error) error {
func (m *manager) ComponentPaths() ([]string, error) {
paths := []string{}
err := afero.Walk(m.appFS, m.componentsPath, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
......@@ -67,7 +68,7 @@ func (m *manager) CreateComponent(name string, text string, params param.Params,
return fmt.Errorf("Component name '%s' is not valid; must not contain punctuation, spaces, or begin or end with a slash", name)
}
componentPath := string(appendToAbsPath(m.componentsPath, name))
componentPath := str.AppendToPath(m.componentsPath, name)
switch templateType {
case prototype.YAML:
componentPath = componentPath + ".yaml"
......@@ -105,7 +106,7 @@ func (m *manager) DeleteComponent(name string) error {
}
// Build the new component/params.libsonnet file.
componentParamsFile, err := afero.ReadFile(m.appFS, string(m.componentParamsPath))
componentParamsFile, err := afero.ReadFile(m.appFS, m.componentParamsPath)
if err != nil {
return err
}
......@@ -122,8 +123,8 @@ func (m *manager) DeleteComponent(name string) error {
return err
}
for _, env := range envs {
path := appendToAbsPath(m.environmentsPath, env.Name, paramsFileName)
envParamsFile, err := afero.ReadFile(m.appFS, string(path))
path := str.AppendToPath(m.environmentsPath, env.Name, paramsFileName)
envParamsFile, err := afero.ReadFile(m.appFS, path)
if err != nil {
return err
}
......@@ -141,16 +142,16 @@ 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, string(m.componentParamsPath), []byte(componentJsonnet), defaultFilePermissions)
err = afero.WriteFile(m.appFS, m.componentParamsPath, []byte(componentJsonnet), defaultFilePermissions)
if err != nil {
return err
}
// Remove the component references in each environment's
// environment/<env>/params.libsonnet.
for _, env := range envs {
path := appendToAbsPath(m.environmentsPath, env.Name, paramsFileName)
path := str.AppendToPath(m.environmentsPath, env.Name, paramsFileName)
log.Debugf("... deleting references in %s", path)
err = afero.WriteFile(m.appFS, string(path), []byte(envJsonnets[env.Name]), defaultFilePermissions)
err = afero.WriteFile(m.appFS, path, []byte(envJsonnets[env.Name]), defaultFilePermissions)
if err != nil {
return err
}
......@@ -172,7 +173,7 @@ func (m *manager) DeleteComponent(name string) error {
}
func (m *manager) GetComponentParams(component string) (param.Params, error) {
text, err := afero.ReadFile(m.appFS, string(m.componentParamsPath))
text, err := afero.ReadFile(m.appFS, m.componentParamsPath)
if err != nil {
return nil, err
}
......@@ -181,7 +182,7 @@ func (m *manager) GetComponentParams(component string) (param.Params, error) {
}
func (m *manager) GetAllComponentParams() (map[string]param.Params, error) {
text, err := afero.ReadFile(m.appFS, string(m.componentParamsPath))
text, err := afero.ReadFile(m.appFS, m.componentParamsPath)
if err != nil {
return nil, err
}
......@@ -190,7 +191,7 @@ func (m *manager) GetAllComponentParams() (map[string]param.Params, error) {
}
func (m *manager) SetComponentParams(component string, params param.Params) error {
text, err := afero.ReadFile(m.appFS, string(m.componentParamsPath))
text, err := afero.ReadFile(m.appFS, m.componentParamsPath)
if err != nil {
return err
}
......@@ -200,7 +201,7 @@ func (m *manager) SetComponentParams(component string, params param.Params) erro
return err
}
return afero.WriteFile(m.appFS, string(m.componentParamsPath), []byte(jsonnet), defaultFilePermissions)
return afero.WriteFile(m.appFS, m.componentParamsPath, []byte(jsonnet), defaultFilePermissions)
}
func (m *manager) findComponentPath(name string) (string, error) {
......@@ -232,7 +233,7 @@ func (m *manager) findComponentPath(name string) (string, error) {
}
func (m *manager) writeComponentParams(componentName string, params param.Params) error {
text, err := afero.ReadFile(m.appFS, string(m.componentParamsPath))
text, err := afero.ReadFile(m.appFS, m.componentParamsPath)
if err != nil {
return err
}
......@@ -242,7 +243,7 @@ func (m *manager) writeComponentParams(componentName string, params param.Params
return err
}
return afero.WriteFile(m.appFS, string(m.componentParamsPath), []byte(appended), defaultFilePermissions)
return afero.WriteFile(m.appFS, m.componentParamsPath, []byte(appended), defaultFilePermissions)
}
func genComponentParamsContent() []byte {
......
......@@ -21,6 +21,8 @@ import (
"sort"
"strings"
"testing"
str "github.com/ksonnet/ksonnet/strings"
)
const (
......@@ -31,42 +33,39 @@ const (
)
func populateComponentPaths(t *testing.T) *manager {
spec, err := parseClusterSpec(fmt.Sprintf("file:%s", blankSwagger), testFS)
if err != nil {
t.Fatalf("Failed to parse cluster spec: %v", err)
}
specFlag := fmt.Sprintf("file:%s", blankSwagger)
appPath := AbsPath(componentsPath)
appPath := componentsPath
reg := newMockRegistryManager("incubator")
m, err := initManager("componentPaths", appPath, spec, &mockAPIServer, &mockNamespace, reg, testFS)
m, err := initManager("componentPaths", appPath, &specFlag, &mockAPIServer, &mockNamespace, reg, testFS)
if err != nil {
t.Fatalf("Failed to init cluster spec: %v", err)
}
// Create empty app file.
components := appendToAbsPath(appPath, componentsDir)
appFile1 := appendToAbsPath(components, componentFile1)
f1, err := testFS.OpenFile(string(appFile1), os.O_RDONLY|os.O_CREATE, 0777)
components := str.AppendToPath(appPath, componentsDir)
appFile1 := str.AppendToPath(components, componentFile1)
f1, err := testFS.OpenFile(appFile1, os.O_RDONLY|os.O_CREATE, 0777)
if err != nil {
t.Fatalf("Failed to touch app file '%s'\n%v", appFile1, err)
}
f1.Close()