Merge pull request #6768 from deads2k/deads-allow-flag-elimination

allow selective removal of kubeconfig override flags
This commit is contained in:
Dawn Chen 2015-04-13 16:00:23 -07:00
commit fb25f9e12c
2 changed files with 73 additions and 48 deletions

View File

@ -17,6 +17,8 @@ limitations under the License.
package clientcmd package clientcmd
import ( import (
"strconv"
"github.com/spf13/pflag" "github.com/spf13/pflag"
clientcmdapi "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api" clientcmdapi "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api"
@ -37,36 +39,39 @@ type ConfigOverrideFlags struct {
AuthOverrideFlags AuthOverrideFlags AuthOverrideFlags AuthOverrideFlags
ClusterOverrideFlags ClusterOverrideFlags ClusterOverrideFlags ClusterOverrideFlags
ContextOverrideFlags ContextOverrideFlags ContextOverrideFlags ContextOverrideFlags
CurrentContext string CurrentContext FlagInfo
} }
// AuthOverrideFlags holds the flag names to be used for binding command line flags for AuthInfo objects // AuthOverrideFlags holds the flag names to be used for binding command line flags for AuthInfo objects
type AuthOverrideFlags struct { type AuthOverrideFlags struct {
AuthPath string AuthPath FlagInfo
AuthPathShort string ClientCertificate FlagInfo
ClientCertificate string ClientKey FlagInfo
ClientKey string Token FlagInfo
Token string Username FlagInfo
Username string Password FlagInfo
Password string
} }
// ContextOverrideFlags holds the flag names to be used for binding command line flags for Cluster objects // ContextOverrideFlags holds the flag names to be used for binding command line flags for Cluster objects
type ContextOverrideFlags struct { type ContextOverrideFlags struct {
ClusterName string ClusterName FlagInfo
AuthInfoName string AuthInfoName FlagInfo
Namespace string Namespace FlagInfo
// allow the potential for shorter namespace flags for commands that tend to work across namespaces
NamespaceShort string
} }
// ClusterOverride holds the flag names to be used for binding command line flags for Cluster objects // ClusterOverride holds the flag names to be used for binding command line flags for Cluster objects
type ClusterOverrideFlags struct { type ClusterOverrideFlags struct {
APIServer string APIServer FlagInfo
APIServerShort string APIVersion FlagInfo
APIVersion string CertificateAuthority FlagInfo
CertificateAuthority string InsecureSkipTLSVerify FlagInfo
InsecureSkipTLSVerify string }
type FlagInfo struct {
LongName string
ShortName string
Default string
Description string
} }
const ( const (
@ -90,22 +95,22 @@ const (
// RecommendedAuthOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing // RecommendedAuthOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing
func RecommendedAuthOverrideFlags(prefix string) AuthOverrideFlags { func RecommendedAuthOverrideFlags(prefix string) AuthOverrideFlags {
return AuthOverrideFlags{ return AuthOverrideFlags{
AuthPath: prefix + FlagAuthPath, AuthPath: FlagInfo{prefix + FlagAuthPath, "", "", "Path to the auth info file. If missing, prompt the user. Only used if using https."},
ClientCertificate: prefix + FlagCertFile, ClientCertificate: FlagInfo{prefix + FlagCertFile, "", "", "Path to a client key file for TLS."},
ClientKey: prefix + FlagKeyFile, ClientKey: FlagInfo{prefix + FlagKeyFile, "", "", "Path to a client key file for TLS."},
Token: prefix + FlagBearerToken, Token: FlagInfo{prefix + FlagBearerToken, "", "", "Bearer token for authentication to the API server."},
Username: prefix + FlagUsername, Username: FlagInfo{prefix + FlagUsername, "", "", "Username for basic authentication to the API server."},
Password: prefix + FlagPassword, Password: FlagInfo{prefix + FlagPassword, "", "", "Password for basic authentication to the API server."},
} }
} }
// RecommendedClusterOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing // RecommendedClusterOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing
func RecommendedClusterOverrideFlags(prefix string) ClusterOverrideFlags { func RecommendedClusterOverrideFlags(prefix string) ClusterOverrideFlags {
return ClusterOverrideFlags{ return ClusterOverrideFlags{
APIServer: prefix + FlagAPIServer, APIServer: FlagInfo{prefix + FlagAPIServer, "", "", "The address and port of the Kubernetes API server"},
APIVersion: prefix + FlagAPIVersion, APIVersion: FlagInfo{prefix + FlagAPIVersion, "", "", "The API version to use when talking to the server"},
CertificateAuthority: prefix + FlagCAFile, CertificateAuthority: FlagInfo{prefix + FlagCAFile, "", "", "Path to a cert. file for the certificate authority."},
InsecureSkipTLSVerify: prefix + FlagInsecure, InsecureSkipTLSVerify: FlagInfo{prefix + FlagInsecure, "", "false", "If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure."},
} }
} }
@ -115,35 +120,35 @@ func RecommendedConfigOverrideFlags(prefix string) ConfigOverrideFlags {
AuthOverrideFlags: RecommendedAuthOverrideFlags(prefix), AuthOverrideFlags: RecommendedAuthOverrideFlags(prefix),
ClusterOverrideFlags: RecommendedClusterOverrideFlags(prefix), ClusterOverrideFlags: RecommendedClusterOverrideFlags(prefix),
ContextOverrideFlags: RecommendedContextOverrideFlags(prefix), ContextOverrideFlags: RecommendedContextOverrideFlags(prefix),
CurrentContext: prefix + FlagContext, CurrentContext: FlagInfo{prefix + FlagContext, "", "", "The name of the kubeconfig context to use"},
} }
} }
// RecommendedContextOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing // RecommendedContextOverrideFlags is a convenience method to return recommended flag names prefixed with a string of your choosing
func RecommendedContextOverrideFlags(prefix string) ContextOverrideFlags { func RecommendedContextOverrideFlags(prefix string) ContextOverrideFlags {
return ContextOverrideFlags{ return ContextOverrideFlags{
ClusterName: prefix + FlagClusterName, ClusterName: FlagInfo{prefix + FlagClusterName, "", "", "The name of the kubeconfig cluster to use"},
AuthInfoName: prefix + FlagAuthInfoName, AuthInfoName: FlagInfo{prefix + FlagAuthInfoName, "", "", "The name of the kubeconfig user to use"},
Namespace: prefix + FlagNamespace, Namespace: FlagInfo{prefix + FlagNamespace, "", "", "If present, the namespace scope for this CLI request."},
} }
} }
// BindAuthInfoFlags is a convenience method to bind the specified flags to their associated variables // BindAuthInfoFlags is a convenience method to bind the specified flags to their associated variables
func BindAuthInfoFlags(authInfo *clientcmdapi.AuthInfo, flags *pflag.FlagSet, flagNames AuthOverrideFlags) { func BindAuthInfoFlags(authInfo *clientcmdapi.AuthInfo, flags *pflag.FlagSet, flagNames AuthOverrideFlags) {
flags.StringVarP(&authInfo.AuthPath, flagNames.AuthPath, flagNames.AuthPathShort, "", "Path to the auth info file. If missing, prompt the user. Only used if using https.") bindStringFlag(flags, &authInfo.AuthPath, flagNames.AuthPath)
flags.StringVar(&authInfo.ClientCertificate, flagNames.ClientCertificate, "", "Path to a client key file for TLS.") bindStringFlag(flags, &authInfo.ClientCertificate, flagNames.ClientCertificate)
flags.StringVar(&authInfo.ClientKey, flagNames.ClientKey, "", "Path to a client key file for TLS.") bindStringFlag(flags, &authInfo.ClientKey, flagNames.ClientKey)
flags.StringVar(&authInfo.Token, flagNames.Token, "", "Bearer token for authentication to the API server.") bindStringFlag(flags, &authInfo.Token, flagNames.Token)
flags.StringVar(&authInfo.Username, flagNames.Username, "", "Username for basic authentication to the API server.") bindStringFlag(flags, &authInfo.Username, flagNames.Username)
flags.StringVar(&authInfo.Password, flagNames.Password, "", "Password for basic authentication to the API server.") bindStringFlag(flags, &authInfo.Password, flagNames.Password)
} }
// BindClusterFlags is a convenience method to bind the specified flags to their associated variables // BindClusterFlags is a convenience method to bind the specified flags to their associated variables
func BindClusterFlags(clusterInfo *clientcmdapi.Cluster, flags *pflag.FlagSet, flagNames ClusterOverrideFlags) { func BindClusterFlags(clusterInfo *clientcmdapi.Cluster, flags *pflag.FlagSet, flagNames ClusterOverrideFlags) {
flags.StringVarP(&clusterInfo.Server, flagNames.APIServer, flagNames.APIServerShort, "", "The address and port of the Kubernetes API server") bindStringFlag(flags, &clusterInfo.Server, flagNames.APIServer)
flags.StringVar(&clusterInfo.APIVersion, flagNames.APIVersion, "", "The API version to use when talking to the server") bindStringFlag(flags, &clusterInfo.APIVersion, flagNames.APIVersion)
flags.StringVar(&clusterInfo.CertificateAuthority, flagNames.CertificateAuthority, "", "Path to a cert. file for the certificate authority.") bindStringFlag(flags, &clusterInfo.CertificateAuthority, flagNames.CertificateAuthority)
flags.BoolVar(&clusterInfo.InsecureSkipTLSVerify, flagNames.InsecureSkipTLSVerify, false, "If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure.") bindBoolFlag(flags, &clusterInfo.InsecureSkipTLSVerify, flagNames.InsecureSkipTLSVerify)
} }
// BindOverrideFlags is a convenience method to bind the specified flags to their associated variables // BindOverrideFlags is a convenience method to bind the specified flags to their associated variables
@ -151,12 +156,32 @@ func BindOverrideFlags(overrides *ConfigOverrides, flags *pflag.FlagSet, flagNam
BindAuthInfoFlags(&overrides.AuthInfo, flags, flagNames.AuthOverrideFlags) BindAuthInfoFlags(&overrides.AuthInfo, flags, flagNames.AuthOverrideFlags)
BindClusterFlags(&overrides.ClusterInfo, flags, flagNames.ClusterOverrideFlags) BindClusterFlags(&overrides.ClusterInfo, flags, flagNames.ClusterOverrideFlags)
BindContextFlags(&overrides.Context, flags, flagNames.ContextOverrideFlags) BindContextFlags(&overrides.Context, flags, flagNames.ContextOverrideFlags)
flags.StringVar(&overrides.CurrentContext, flagNames.CurrentContext, "", "The name of the kubeconfig context to use") bindStringFlag(flags, &overrides.CurrentContext, flagNames.CurrentContext)
} }
// BindFlags is a convenience method to bind the specified flags to their associated variables // BindFlags is a convenience method to bind the specified flags to their associated variables
func BindContextFlags(contextInfo *clientcmdapi.Context, flags *pflag.FlagSet, flagNames ContextOverrideFlags) { func BindContextFlags(contextInfo *clientcmdapi.Context, flags *pflag.FlagSet, flagNames ContextOverrideFlags) {
flags.StringVar(&contextInfo.Cluster, flagNames.ClusterName, "", "The name of the kubeconfig cluster to use") bindStringFlag(flags, &contextInfo.Cluster, flagNames.ClusterName)
flags.StringVar(&contextInfo.AuthInfo, flagNames.AuthInfoName, "", "The name of the kubeconfig user to use") bindStringFlag(flags, &contextInfo.AuthInfo, flagNames.AuthInfoName)
flags.StringVarP(&contextInfo.Namespace, flagNames.Namespace, flagNames.NamespaceShort, "", "If present, the namespace scope for this CLI request.") bindStringFlag(flags, &contextInfo.Namespace, flagNames.Namespace)
}
func bindStringFlag(flags *pflag.FlagSet, target *string, flagInfo FlagInfo) {
// you can't register a flag without a long name
if len(flagInfo.LongName) > 0 {
flags.StringVarP(target, flagInfo.LongName, flagInfo.ShortName, flagInfo.Default, flagInfo.Description)
}
}
func bindBoolFlag(flags *pflag.FlagSet, target *bool, flagInfo FlagInfo) {
// you can't register a flag without a long name
if len(flagInfo.LongName) > 0 {
// try to parse Default as a bool. If it fails, assume false
boolVal, err := strconv.ParseBool(flagInfo.Default)
if err != nil {
boolVal = false
}
flags.BoolVarP(target, flagInfo.LongName, flagInfo.ShortName, boolVal, flagInfo.Description)
}
} }

View File

@ -323,8 +323,8 @@ func DefaultClientConfig(flags *pflag.FlagSet) clientcmd.ClientConfig {
overrides := &clientcmd.ConfigOverrides{} overrides := &clientcmd.ConfigOverrides{}
flagNames := clientcmd.RecommendedConfigOverrideFlags("") flagNames := clientcmd.RecommendedConfigOverrideFlags("")
// short flagnames are disabled by default. These are here for compatibility with existing scripts // short flagnames are disabled by default. These are here for compatibility with existing scripts
flagNames.AuthOverrideFlags.AuthPathShort = "a" flagNames.AuthOverrideFlags.AuthPath.ShortName = "a"
flagNames.ClusterOverrideFlags.APIServerShort = "s" flagNames.ClusterOverrideFlags.APIServer.ShortName = "s"
clientcmd.BindOverrideFlags(overrides, flags, flagNames) clientcmd.BindOverrideFlags(overrides, flags, flagNames)
clientConfig := clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, os.Stdin) clientConfig := clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, os.Stdin)