Unverified Commit 75fac5f5 authored by bryanl's avatar bryanl
Browse files

Allow user to skip inclusion of default registries



Introduces a new init flag `--skip-default-registries` that skips adding
incubator to the intial list of available registries. This will allow
`ks` to work without access to GitHub.

Users can add additional registries at a later time with `ks registry
add`.

Fixes #204
Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent bb346e6f
......@@ -74,6 +74,8 @@ const (
OptionServer = "server"
// OptionServerURI is serverURI option.
OptionServerURI = "server-uri"
// OptionSkipDefaultRegistries is skipDefaultRegistries option. Used by init.
OptionSkipDefaultRegistries = "skip-default-registries"
// OptionSkipGc is skipGc option.
OptionSkipGc = "skip-gc"
// OptionSpecFlag is specFlag option. Used for setting k8s spec.
......
......@@ -48,6 +48,7 @@ type Init struct {
k8sSpecFlag string
serverURI string
namespace string
skipDefaultRegistries bool
appInitFn appInitFn
initIncubatorFn initIncubatorFn
......@@ -64,6 +65,7 @@ func NewInit(m map[string]interface{}) (*Init, error) {
k8sSpecFlag: ol.loadString(OptionSpecFlag),
serverURI: ol.loadOptionalString(OptionServer),
namespace: ol.loadString(OptionNamespaceName),
skipDefaultRegistries: ol.loadBool(OptionSkipDefaultRegistries),
appInitFn: appinit.Init,
initIncubatorFn: initIncubator,
......@@ -78,12 +80,16 @@ func NewInit(m map[string]interface{}) (*Init, error) {
// Run runs that ns create action.
func (i *Init) Run() error {
var registries []registry.Registry
if !i.skipDefaultRegistries {
gh, err := i.initIncubatorFn()
if err != nil {
return err
}
registries := []registry.Registry{gh}
registries = append(registries, gh)
}
return i.appInitFn(
i.fs,
......
......@@ -35,6 +35,21 @@ func TestInit(t *testing.T) {
aServerURI := "http://example.com"
aNamespace := "my-namespace"
cases := []struct {
name string
skipRegistries bool
}{
{
name: "with registries",
},
{
name: "without registries",
skipRegistries: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
in := map[string]interface{}{
OptionFs: aFs,
OptionName: aName,
......@@ -42,6 +57,7 @@ func TestInit(t *testing.T) {
OptionSpecFlag: aK8sSpecFlag,
OptionServer: aServerURI,
OptionNamespaceName: aNamespace,
OptionSkipDefaultRegistries: tc.skipRegistries,
}
a, err := NewInit(in)
......@@ -55,12 +71,16 @@ func TestInit(t *testing.T) {
assert.Equal(t, aServerURI, serverURI)
assert.Equal(t, aNamespace, namespace)
if !tc.skipRegistries {
assert.Len(t, registries, 1)
r := registries[0]
assert.Equal(t, "github", r.Protocol())
assert.Equal(t, "github.com/ksonnet/parts/tree/master/incubator", r.URI())
assert.Equal(t, "incubator", r.Name())
} else {
assert.Empty(t, registries)
}
return nil
}
......@@ -76,4 +96,7 @@ func TestInit(t *testing.T) {
err = a.Run()
require.NoError(t, err)
})
}
})
}
......@@ -18,17 +18,28 @@ package cmd
const (
// For use in the commands (e.g., diff, apply, delete) that require either an
// environment or the -f flag.
flagAPISpec = "api-spec"
flagComponent = "component"
flagCreate = "create"
flagDir = "dir"
flagDryRun = "dry-run"
flagEnv = "env"
flagExtVar = "ext-str"
flagExtVarFile = "ext-str-file"
flagFilename = "filename"
flagGcTag = "gc-tag"
flagIndex = "index"
flagJpath = "jpath"
flagNamespace = "namespace"
flagResolver = "resolve-images"
flagResolvFail = "resolve-images-error"
flagSkipDefaultRegistries = "skip-default-registries"
flagSkipGc = "skip-gc"
flagTlaVar = "tla-str"
flagTlaVarFile = "tla-str-file"
flagOutput = "output"
flagOverride = "override"
flagVerbose = "verbose"
flagVersion = "version"
shortComponent = "c"
......
......@@ -21,14 +21,19 @@ import (
"os"
"path/filepath"
"github.com/spf13/viper"
"github.com/ksonnet/ksonnet/actions"
"github.com/ksonnet/ksonnet/client"
"github.com/spf13/cobra"
)
const (
flagInitDir = "dir"
initShortDesc = "Initialize a ksonnet application"
vInitAPISpec = "init-api-spec"
vInitDir = "init-dir"
vInitSkipDefaultRegistries = "init-skip-default-registries"
)
var (
......@@ -37,13 +42,19 @@ var (
func init() {
RootCmd.AddCommand(initCmd)
initClientConfig = client.NewDefaultClientConfig()
initClientConfig.BindClientGoFlags(initCmd)
initCmd.Flags().String(flagDir, "", "Ksonnet application directory")
viper.BindPFlag(vInitDir, initCmd.Flag(flagDir))
// TODO: We need to make this default to checking the `kubeconfig` file.
initCmd.PersistentFlags().String(flagAPISpec, "",
initCmd.Flags().String(flagAPISpec, "",
"Manually specified Kubernetes API version. The corresponding OpenAPI spec is used to generate ksonnet's Kubernetes libraries")
viper.BindPFlag(vInitAPISpec, initCmd.Flag(flagAPISpec))
initClientConfig = client.NewDefaultClientConfig()
initClientConfig.BindClientGoFlags(initCmd)
initCmd.Flags().String(flagInitDir, "", "Ksonnet application directory")
initCmd.Flags().Bool(flagSkipDefaultRegistries, false, "Skip configuration of default registries")
viper.BindPFlag(vInitSkipDefaultRegistries, initCmd.Flag(flagSkipDefaultRegistries))
}
var initCmd = &cobra.Command{
......@@ -61,10 +72,7 @@ var initCmd = &cobra.Command{
return err
}
initDir, err := flags.GetString(flagInitDir)
if err != nil {
return err
}
initDir := viper.GetString(vInitDir)
appRoot, err := genKsRoot(appName, wd, initDir)
if err != nil {
......@@ -76,10 +84,7 @@ var initCmd = &cobra.Command{
return err
}
specFlag, err := flags.GetString(flagAPISpec)
if err != nil {
return err
}
specFlag := viper.GetString(vInitAPISpec)
if specFlag == "" {
specFlag = initClientConfig.GetAPISpec(server)
}
......@@ -91,6 +96,7 @@ var initCmd = &cobra.Command{
actions.OptionSpecFlag: specFlag,
actions.OptionServer: server,
actions.OptionNamespaceName: namespace,
actions.OptionSkipDefaultRegistries: viper.GetBool(vInitSkipDefaultRegistries),
}
return runAction(actionInit, m)
......
......@@ -42,6 +42,7 @@ func Test_initCmd(t *testing.T) {
actions.OptionServer: "http://127.0.0.1",
actions.OptionSpecFlag: "version:v1.7.0",
actions.OptionNamespaceName: "new-namespace",
actions.OptionSkipDefaultRegistries: false,
},
},
}
......
......@@ -45,18 +45,6 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
const (
flagVerbose = "verbose"
flagJpath = "jpath"
flagExtVar = "ext-str"
flagExtVarFile = "ext-str-file"
flagTlaVar = "tla-str"
flagTlaVarFile = "tla-str-file"
flagResolver = "resolve-images"
flagResolvFail = "resolve-images-error"
flagAPISpec = "api-spec"
)
var (
appFs = afero.NewOsFs()
ka app.App
......
......@@ -97,6 +97,7 @@ ks init app-name --dir=custom-location
--password string Password for basic authentication to the API server
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
--server string The address and port of the Kubernetes API server
--skip-default-registries Skip configuration of default registries
--token string Bearer token for authentication to the API server
--user string The name of the kubeconfig user to use
--username string Username for basic authentication to the API server
......
......@@ -165,6 +165,7 @@ type initOptions struct {
server string
context string
namespace string
skipRegistries bool
}
func (o *initOptions) toSlice() ([]string, error) {
......@@ -187,5 +188,9 @@ func (o *initOptions) toSlice() ([]string, error) {
options = append(options, "--namespace", o.namespace)
}
if o.skipRegistries {
options = append(options, "--skip-default-registries")
}
return options, nil
}
// +build e2e
package e2e
import (
"path/filepath"
. "github.com/onsi/ginkgo"
)
var _ = Describe("ks init", func() {
var a app
var opts = &initOptions{}
JustBeforeEach(func() {
a = e.initApp(opts)
})
Context("in general", func() {
It("doesn't generate default registries", func() {
o := a.registryList()
assertOutput(filepath.Join("init", "registry-output.txt"), o.stdout)
})
})
Context("without default registries", func() {
BeforeEach(func() {
opts.skipRegistries = true
})
It("doesn't generate default registries", func() {
o := a.registryList()
assertOutput(filepath.Join("init", "skip-registry-output.txt"), o.stdout)
})
})
})
NAME OVERRIDE PROTOCOL URI
==== ======== ======== ===
incubator github github.com/ksonnet/parts/tree/master/incubator
NAME OVERRIDE PROTOCOL URI
==== ======== ======== ===
Markdown is supported
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