Skip to content
Snippets Groups Projects
Commit e21a8e17 authored by Alex Clemmer's avatar Alex Clemmer Committed by GitHub
Browse files

Merge pull request #149 from hausdorff/fix-init-env

Cause `init` to generate env using current-context
parents 4dbc82b9 361a62c0
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,8 @@ import (
"os"
"path"
"k8s.io/client-go/tools/clientcmd/api"
"github.com/ksonnet/kubecfg/metadata"
"github.com/ksonnet/kubecfg/pkg/kubecfg"
"github.com/spf13/cobra"
......@@ -53,7 +55,32 @@ var initCmd = &cobra.Command{
return err
}
c, err := kubecfg.NewInitCmd(appRoot, specFlag)
//
// Find the URI of the current cluster, if it exists.
//
rawConfig, err := clientConfig.RawConfig()
if err != nil {
return err
}
var currCtx *api.Context
for name, ctx := range rawConfig.Contexts {
if name == rawConfig.CurrentContext {
currCtx = ctx
}
}
var currClusterURI *string
if rawConfig.CurrentContext != "" && currCtx != nil {
for name, cluster := range rawConfig.Clusters {
if currCtx.Cluster == name {
currClusterURI = &cluster.Server
}
}
}
c, err := kubecfg.NewInitCmd(appRoot, specFlag, currClusterURI)
if err != nil {
return err
}
......@@ -73,12 +100,12 @@ consists of two steps:
app-name/
.gitignore Default .gitignore; can customize VCS
.ksonnet/ Metadata for ksonnet
envs/ Env specs (defaults: dev, test, prod)
params.yaml Specifies the schema of the environments
dev.yaml
test.yaml
prod.yaml
us-east.yaml [Example of user-generated env]
envs/
default/ Default generated environment]
k.libsonnet
k8s.libsonnet
swagger.json
spec.json
components/ Top-level Kubernetes objects defining application
lib/ user-written .libsonnet files
vendor/ mixin libraries, prototypes
......
......@@ -34,6 +34,8 @@ const (
mockEnvName3 = "us-east/test"
)
var mockAPIServerURI = "http://google.com"
func mockEnvironments(t *testing.T, appName string) *manager {
spec, err := parseClusterSpec(fmt.Sprintf("file:%s", blankSwagger), testFS)
if err != nil {
......@@ -41,7 +43,7 @@ func mockEnvironments(t *testing.T, appName string) *manager {
}
appPath := AbsPath(appName)
m, err := initManager(appPath, spec, testFS)
m, err := initManager(appPath, spec, &mockAPIServerURI, testFS)
if err != nil {
t.Fatalf("Failed to init cluster spec: %v", err)
}
......
......@@ -64,8 +64,8 @@ func Find(path AbsPath) (Manager, error) {
// Init will retrieve a cluster API specification, generate a
// capabilities-compliant version of ksonnet-lib, and then generate the
// directory tree for an application.
func Init(rootPath AbsPath, spec ClusterSpec) (Manager, error) {
return initManager(rootPath, spec, appFS)
func Init(rootPath AbsPath, spec ClusterSpec, serverURI *string) (Manager, error) {
return initManager(rootPath, spec, serverURI, appFS)
}
// ClusterSpec represents the API supported by some cluster. There are several
......
......@@ -70,7 +70,7 @@ func findManager(abs AbsPath, appFS afero.Fs) (*manager, error) {
}
}
func initManager(rootPath AbsPath, spec ClusterSpec, appFS afero.Fs) (*manager, error) {
func initManager(rootPath AbsPath, spec ClusterSpec, serverURI *string, appFS afero.Fs) (*manager, error) {
m := newManager(rootPath, appFS)
// Generate the program text for ksonnet-lib.
......@@ -92,8 +92,11 @@ func initManager(rootPath AbsPath, spec ClusterSpec, appFS afero.Fs) (*manager,
// Initialize environment, and cache specification data.
// TODO the URI for the default environment needs to be generated from KUBECONFIG
if err := m.createEnvironment(defaultEnvName, "", extensionsLibData, k8sLibData, specData); err != nil {
return nil, err
if serverURI != nil {
err := m.createEnvironment(defaultEnvName, *serverURI, extensionsLibData, k8sLibData, specData)
if err != nil {
return nil, err
}
}
return m, nil
......
......@@ -59,7 +59,7 @@ func TestInitSuccess(t *testing.T) {
}
appPath := AbsPath("/fromEmptySwagger")
_, err = initManager(appPath, spec, testFS)
_, err = initManager(appPath, spec, &mockAPIServerURI, testFS)
if err != nil {
t.Fatalf("Failed to init cluster spec: %v", err)
}
......@@ -126,7 +126,7 @@ func TestFindSuccess(t *testing.T) {
}
appPath := AbsPath("/findSuccess")
_, err = initManager(appPath, spec, testFS)
_, err = initManager(appPath, spec, &mockAPIServerURI, testFS)
if err != nil {
t.Fatalf("Failed to init cluster spec: %v", err)
}
......@@ -154,7 +154,7 @@ func TestComponentPaths(t *testing.T) {
}
appPath := AbsPath("/componentPaths")
m, err := initManager(appPath, spec, testFS)
m, err := initManager(appPath, spec, &mockAPIServerURI, testFS)
if err != nil {
t.Fatalf("Failed to init cluster spec: %v", err)
}
......@@ -221,13 +221,13 @@ func TestDoubleNewFailure(t *testing.T) {
appPath := AbsPath("/doubleNew")
_, err = initManager(appPath, spec, testFS)
_, err = initManager(appPath, spec, &mockAPIServerURI, testFS)
if err != nil {
t.Fatalf("Failed to init cluster spec: %v", err)
}
targetErr := fmt.Sprintf("Could not create app; directory '%s' already exists", appPath)
_, err = initManager(appPath, spec, testFS)
_, err = initManager(appPath, spec, &mockAPIServerURI, testFS)
if err == nil || err.Error() != targetErr {
t.Fatalf("Expected to fail to create app with message '%s', got '%s'", targetErr, err.Error())
}
......
......@@ -3,11 +3,12 @@ package kubecfg
import "github.com/ksonnet/kubecfg/metadata"
type InitCmd struct {
rootPath metadata.AbsPath
spec metadata.ClusterSpec
rootPath metadata.AbsPath
spec metadata.ClusterSpec
serverURI *string
}
func NewInitCmd(rootPath metadata.AbsPath, specFlag string) (*InitCmd, error) {
func NewInitCmd(rootPath metadata.AbsPath, specFlag string, serverURI *string) (*InitCmd, error) {
// NOTE: We're taking `rootPath` here as an absolute path (rather than a partial path we expand to an absolute path)
// to make it more testable.
......@@ -16,10 +17,10 @@ func NewInitCmd(rootPath metadata.AbsPath, specFlag string) (*InitCmd, error) {
return nil, err
}
return &InitCmd{rootPath: rootPath, spec: spec}, nil
return &InitCmd{rootPath: rootPath, spec: spec, serverURI: serverURI}, nil
}
func (c *InitCmd) Run() error {
_, err := metadata.Init(c.rootPath, c.spec)
_, err := metadata.Init(c.rootPath, c.spec, c.serverURI)
return err
}
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