add namespace to kubeconfig file

This commit is contained in:
deads2k
2015-01-02 13:08:37 -05:00
parent 6ff26d924c
commit dd01137138
24 changed files with 300 additions and 177 deletions

View File

@@ -66,6 +66,8 @@ type Factory struct {
Resizer func(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.Resizer, error)
// Returns a schema that can validate objects stored on disk.
Validator func(*cobra.Command) (validation.Schema, error)
// Returns the default namespace to use in cases where no other namespace is specified
DefaultNamespace func(cmd *cobra.Command) (string, error)
}
// NewFactory creates a factory with the default Kubernetes resources defined
@@ -84,8 +86,11 @@ func NewFactory() *Factory {
flags: flags,
Object: func(cmd *cobra.Command) (meta.RESTMapper, runtime.ObjectTyper) {
version := GetFlagString(cmd, "api-version")
return kubectl.OutputVersionMapper{mapper, version}, api.Scheme
cfg, err := clientConfig.ClientConfig()
checkErr(err)
cmdApiVersion := cfg.Version
return kubectl.OutputVersionMapper{mapper, cmdApiVersion}, api.Scheme
},
Client: func(cmd *cobra.Command) (*client.Client, error) {
return clients.ClientForVersion("")
@@ -135,6 +140,9 @@ func NewFactory() *Factory {
}
return validation.NullSchema{}, nil
},
DefaultNamespace: func(cmd *cobra.Command) (string, error) {
return clientConfig.Namespace()
},
}
}
@@ -158,8 +166,6 @@ func (f *Factory) BindFlags(flags *pflag.FlagSet) {
// TODO Add a verbose flag that turns on glog logging. Probably need a way
// to do that automatically for every subcommand.
flags.BoolVar(&f.clients.matchVersion, FlagMatchBinaryVersion, false, "Require server version to match client version")
flags.String("ns-path", os.Getenv("HOME")+"/.kubernetes_ns", "Path to the namespace info file that holds the namespace context to use for CLI requests.")
flags.StringP("namespace", "n", "", "If present, the namespace scope for this CLI request.")
flags.Bool("validate", false, "If true, use a schema to validate the input before sending it")
}
@@ -264,38 +270,6 @@ func runHelp(cmd *cobra.Command, args []string) {
cmd.Help()
}
// GetKubeNamespace returns the value of the namespace a
// user provided on the command line or use the default
// namespace.
func GetKubeNamespace(cmd *cobra.Command) string {
result := api.NamespaceDefault
if ns := GetFlagString(cmd, "namespace"); len(ns) > 0 {
result = ns
glog.V(2).Infof("Using namespace from -ns flag")
} else {
nsPath := GetFlagString(cmd, "ns-path")
nsInfo, err := kubectl.LoadNamespaceInfo(nsPath)
if err != nil {
glog.Fatalf("Error loading current namespace: %v", err)
}
result = nsInfo.Namespace
}
glog.V(2).Infof("Using namespace %s", result)
return result
}
// GetExplicitKubeNamespace returns the value of the namespace a
// user explicitly provided on the command line, or false if no
// such namespace was specified.
func GetExplicitKubeNamespace(cmd *cobra.Command) (string, bool) {
if ns := GetFlagString(cmd, "namespace"); len(ns) > 0 {
return ns, true
}
// TODO: determine when --ns-path is set but equal to the default
// value and return its value and true.
return "", false
}
type clientSwaggerSchema struct {
c *client.Client
t runtime.ObjectTyper