make all kubectl config set-* easier to use

This commit is contained in:
deads2k
2015-01-23 14:18:25 -05:00
parent df84f70781
commit 05bc508a13
7 changed files with 246 additions and 88 deletions

View File

@@ -25,26 +25,29 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
clientcmdapi "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
type createAuthInfoOptions struct {
pathOptions *pathOptions
name string
authPath string
clientCertificate string
clientKey string
token string
authPath util.StringFlag
clientCertificate util.StringFlag
clientKey util.StringFlag
token util.StringFlag
}
func NewCmdConfigSetAuthInfo(out io.Writer, pathOptions *pathOptions) *cobra.Command {
options := &createAuthInfoOptions{pathOptions: pathOptions}
cmd := &cobra.Command{
Use: "set-credentials name",
Use: fmt.Sprintf("set-credentials name [--%v=path/to/auth/file] [--%v=path/to/certficate/file] [--%v=path/to/key/file] [--%v=bearer_token_string]", clientcmd.FlagAuthPath, clientcmd.FlagCertFile, clientcmd.FlagKeyFile, clientcmd.FlagBearerToken),
Short: "Sets a user entry in .kubeconfig",
Long: `Sets a user entry in .kubeconfig
Specifying a name that already exists overwrites that user entry.
Specifying a name that already exists will merge new fields on top of existing values for those fields.
e.g.
kubectl config set-credentials cluster-admin --client-key=~/.kube/cluster-admin/.kubecfg.key
only sets the client-key field on the cluster-admin user entry without touching other values.
`,
Run: func(cmd *cobra.Command, args []string) {
if !options.complete(cmd) {
@@ -58,10 +61,10 @@ func NewCmdConfigSetAuthInfo(out io.Writer, pathOptions *pathOptions) *cobra.Com
},
}
cmd.Flags().StringVar(&options.authPath, clientcmd.FlagAuthPath, "", clientcmd.FlagAuthPath+" for the user entry in .kubeconfig")
cmd.Flags().StringVar(&options.clientCertificate, clientcmd.FlagCertFile, "", clientcmd.FlagCertFile+" for the user entry in .kubeconfig")
cmd.Flags().StringVar(&options.clientKey, clientcmd.FlagKeyFile, "", clientcmd.FlagKeyFile+" for the user entry in .kubeconfig")
cmd.Flags().StringVar(&options.token, clientcmd.FlagBearerToken, "", clientcmd.FlagBearerToken+" for the user entry in .kubeconfig")
cmd.Flags().Var(&options.authPath, clientcmd.FlagAuthPath, clientcmd.FlagAuthPath+" for the user entry in .kubeconfig")
cmd.Flags().Var(&options.clientCertificate, clientcmd.FlagCertFile, clientcmd.FlagCertFile+" for the user entry in .kubeconfig")
cmd.Flags().Var(&options.clientKey, clientcmd.FlagKeyFile, clientcmd.FlagKeyFile+" for the user entry in .kubeconfig")
cmd.Flags().Var(&options.token, clientcmd.FlagBearerToken, clientcmd.FlagBearerToken+" for the user entry in .kubeconfig")
return cmd
}
@@ -77,7 +80,7 @@ func (o createAuthInfoOptions) run() error {
return err
}
authInfo := o.authInfo()
authInfo := o.modifyAuthInfo(config.AuthInfos[o.name])
config.AuthInfos[o.name] = authInfo
err = clientcmd.WriteToFile(*config, filename)
@@ -89,15 +92,23 @@ func (o createAuthInfoOptions) run() error {
}
// authInfo builds an AuthInfo object from the options
func (o *createAuthInfoOptions) authInfo() clientcmdapi.AuthInfo {
authInfo := clientcmdapi.AuthInfo{
AuthPath: o.authPath,
ClientCertificate: o.clientCertificate,
ClientKey: o.clientKey,
Token: o.token,
func (o *createAuthInfoOptions) modifyAuthInfo(existingAuthInfo clientcmdapi.AuthInfo) clientcmdapi.AuthInfo {
modifiedAuthInfo := existingAuthInfo
if o.authPath.Provided() {
modifiedAuthInfo.AuthPath = o.authPath.Value()
}
if o.clientCertificate.Provided() {
modifiedAuthInfo.ClientCertificate = o.clientCertificate.Value()
}
if o.clientKey.Provided() {
modifiedAuthInfo.ClientKey = o.clientKey.Value()
}
if o.token.Provided() {
modifiedAuthInfo.Token = o.token.Value()
}
return authInfo
return modifiedAuthInfo
}
func (o *createAuthInfoOptions) complete(cmd *cobra.Command) bool {