Skip to content
Snippets Groups Projects
Commit 361a62c0 authored by Alex Clemmer's avatar Alex Clemmer
Browse files

Cause `init` to generate env using current-context

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