Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make kubectl config set-cluster easier to use #3768

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 32 additions & 26 deletions docs/kubectl.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,13 @@ Usage:
kubectl config [command]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 449 is the one that needs to be changed. Usage string should list server|insecure-skip-tls-verify|certificate-authority|api-version as flags.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 449 is the one that needs to be changed.

Sorry, two branches and I missed that part of the change.

How about the new flag types for .Provided() and merging the values?

Available Commands:
view displays the specified .kubeconfig file or a merged result
set-cluster name [server] [insecure-skip-tls-verify] [certificate-authority] [api-version] Sets a cluster entry in .kubeconfig
set-credentials name Sets a user entry in .kubeconfig
set-context name Sets a context entry in .kubeconfig
set property-name property-value Sets an individual value in a .kubeconfig file
unset property-name Unsets an individual value in a .kubeconfig file
use-context context-name Sets the current-context in a .kubeconfig file
view displays the specified .kubeconfig file or a merged result
set-cluster name [--server=server] [--certificate-authority=path/to/certficate/authority] [--api-version=apiversion] [--insecure-skip-tls-verify=true] Sets a cluster entry in .kubeconfig
set-credentials name [--auth-path=path/to/auth/file] [--client-certificate=path/to/certficate/file] [--client-key=path/to/key/file] [--token=bearer_token_string] Sets a user entry in .kubeconfig
set-context name [--cluster=cluster-nickname] [--user=user-nickname] [--namespace=namespace] Sets a context entry in .kubeconfig
set property-name property-value Sets an individual value in a .kubeconfig file
unset property-name Unsets an individual value in a .kubeconfig file
use-context context-name Sets the current-context in a .kubeconfig file

Available Flags:
--alsologtostderr=false: log to standard error as well as files
Expand Down Expand Up @@ -440,19 +440,21 @@ Usage:

#### config set-cluster
Sets a cluster entry in .kubeconfig

Specifying a name that already exists overwrites that cluster entry.
Specifying a name that already exists will merge new fields on top of existing values for those fields.
e.g.
kubectl config set-cluster e2e --certificate-authority=~/.kube/e2e/.kubernetes.ca.cert
only sets the certificate-authority field on the e2e cluster entry without touching other values.


Usage:
```
kubectl config set-cluster name [server] [insecure-skip-tls-verify] [certificate-authority] [api-version] [flags]
kubectl config set-cluster name [--server=server] [--certificate-authority=path/to/certficate/authority] [--api-version=apiversion] [--insecure-skip-tls-verify=true] [flags]

Available Flags:
--alsologtostderr=false: log to standard error as well as files
--api-version="": api-version for the cluster entry in .kubeconfig
--api-version=: api-version for the cluster entry in .kubeconfig
-a, --auth-path="": Path to the auth info file. If missing, prompt the user. Only used if using https.
--certificate-authority="": certificate-authority for the cluster entry in .kubeconfig
--certificate-authority=: certificate-authority for the cluster entry in .kubeconfig
--client-certificate="": Path to a client key file for TLS.
--client-key="": Path to a client key file for TLS.
--cluster="": The name of the kubeconfig cluster to use
Expand All @@ -469,7 +471,7 @@ Usage:
--match-server-version=false: Require server version to match client version
--namespace="": If present, the namespace scope for this CLI request.
--ns-path="": Path to the namespace info file that holds the namespace context to use for CLI requests.
--server="": server for the cluster entry in .kubeconfig
--server=: server for the cluster entry in .kubeconfig
--stderrthreshold=2: logs at or above this threshold go to stderr
--token="": Bearer token for authentication to the API server.
--user="": The name of the kubeconfig user to use
Expand All @@ -481,21 +483,23 @@ Usage:

#### config set-credentials
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.


Usage:
```
kubectl config set-credentials name [flags]
kubectl config set-credentials name [--auth-path=path/to/auth/file] [--client-certificate=path/to/certficate/file] [--client-key=path/to/key/file] [--token=bearer_token_string] [flags]

Available Flags:
--alsologtostderr=false: log to standard error as well as files
--api-version="": The API version to use when talking to the server
--auth-path="": auth-path for the user entry in .kubeconfig
--auth-path=: auth-path for the user entry in .kubeconfig
--certificate-authority="": Path to a cert. file for the certificate authority.
--client-certificate="": client-certificate for the user entry in .kubeconfig
--client-key="": client-key for the user entry in .kubeconfig
--client-certificate=: client-certificate for the user entry in .kubeconfig
--client-key=: client-key for the user entry in .kubeconfig
--cluster="": The name of the kubeconfig cluster to use
--context="": The name of the kubeconfig context to use
--global=false: use the .kubeconfig from /home/username
Expand All @@ -512,7 +516,7 @@ Usage:
--ns-path="": Path to the namespace info file that holds the namespace context to use for CLI requests.
-s, --server="": The address of the Kubernetes API server
--stderrthreshold=2: logs at or above this threshold go to stderr
--token="": token for the user entry in .kubeconfig
--token=: token for the user entry in .kubeconfig
--user="": The name of the kubeconfig user to use
--v=0: log level for V logs
--validate=false: If true, use a schema to validate the input before sending it
Expand All @@ -522,13 +526,15 @@ Usage:

#### config set-context
Sets a context entry in .kubeconfig

Specifying a name that already exists overwrites that context entry.
Specifying a name that already exists will merge new fields on top of existing values for those fields.
e.g.
kubectl config set-context gce --user=cluster-admin
only sets the user field on the gce context entry without touching other values.


Usage:
```
kubectl config set-context name [flags]
kubectl config set-context name [--cluster=cluster-nickname] [--user=user-nickname] [--namespace=namespace] [flags]

Available Flags:
--alsologtostderr=false: log to standard error as well as files
Expand All @@ -537,7 +543,7 @@ Usage:
--certificate-authority="": Path to a cert. file for the certificate authority.
--client-certificate="": Path to a client key file for TLS.
--client-key="": Path to a client key file for TLS.
--cluster="": cluster for the context entry in .kubeconfig
--cluster=: cluster for the context entry in .kubeconfig
--context="": The name of the kubeconfig context to use
--global=false: use the .kubeconfig from /home/username
-h, --help=false: help for set-context
Expand All @@ -549,12 +555,12 @@ Usage:
--log_flush_frequency=5s: Maximum number of seconds between log flushes
--logtostderr=true: log to standard error instead of files
--match-server-version=false: Require server version to match client version
--namespace="": namespace for the context entry in .kubeconfig
--namespace=: namespace for the context entry in .kubeconfig
--ns-path="": Path to the namespace info file that holds the namespace context to use for CLI requests.
-s, --server="": The address of the Kubernetes API server
--stderrthreshold=2: logs at or above this threshold go to stderr
--token="": Bearer token for authentication to the API server.
--user="": user for the context entry in .kubeconfig
--user=: user for the context entry in .kubeconfig
--v=0: log level for V logs
--validate=false: If true, use a schema to validate the input before sending it
--vmodule=: comma-separated list of pattern=N settings for file-filtered logging
Expand Down
16 changes: 8 additions & 8 deletions pkg/kubectl/cmd/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func newRedFederalCowHammerConfig() clientcmdapi.Config {
Clusters: map[string]clientcmdapi.Cluster{
"cow-cluster": {Server: "http://cow.org:8080"}},
Contexts: map[string]clientcmdapi.Context{
"federal-context": {AuthInfo: "red-user", Cluster: "cow-cluster", Namespace: "hammer-ns"}},
"federal-context": {AuthInfo: "red-user", Cluster: "cow-cluster"}},
}
}

Expand Down Expand Up @@ -166,11 +166,11 @@ func TestAdditionalAuth(t *testing.T) {
test.run(t)
}

func TestOverwriteExistingAuth(t *testing.T) {
func TestMergeExistingAuth(t *testing.T) {
expectedConfig := newRedFederalCowHammerConfig()
authInfo := clientcmdapi.NewAuthInfo()
authInfo := expectedConfig.AuthInfos["red-user"]
authInfo.AuthPath = "auth-path"
expectedConfig.AuthInfos["red-user"] = *authInfo
expectedConfig.AuthInfos["red-user"] = authInfo
test := configCommandTest{
args: []string{"set-credentials", "red-user", "--" + clientcmd.FlagAuthPath + "=auth-path"},
startingConfig: newRedFederalCowHammerConfig(),
Expand Down Expand Up @@ -252,14 +252,14 @@ func TestAdditionalContext(t *testing.T) {
test.run(t)
}

func TestOverwriteExistingContext(t *testing.T) {
func TestMergeExistingContext(t *testing.T) {
expectedConfig := newRedFederalCowHammerConfig()
context := *clientcmdapi.NewContext()
context.Cluster = "clustername"
context := expectedConfig.Contexts["federal-context"]
context.Namespace = "hammer"
expectedConfig.Contexts["federal-context"] = context

test := configCommandTest{
args: []string{"set-context", "federal-context", "--" + clientcmd.FlagClusterName + "=clustername"},
args: []string{"set-context", "federal-context", "--" + clientcmd.FlagNamespace + "=hammer"},
startingConfig: newRedFederalCowHammerConfig(),
expectedConfig: expectedConfig,
}
Expand Down
49 changes: 30 additions & 19 deletions pkg/kubectl/cmd/config/create_authinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
51 changes: 32 additions & 19 deletions pkg/kubectl/cmd/config/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 createClusterOptions struct {
pathOptions *pathOptions
name string
server string
apiVersion string
insecureSkipTLSVerify bool
certificateAuthority string
server util.StringFlag
apiVersion util.StringFlag
insecureSkipTLSVerify util.BoolFlag
certificateAuthority util.StringFlag
}

func NewCmdConfigSetCluster(out io.Writer, pathOptions *pathOptions) *cobra.Command {
options := &createClusterOptions{pathOptions: pathOptions}

cmd := &cobra.Command{
Use: "set-cluster name [server] [insecure-skip-tls-verify] [certificate-authority] [api-version]",
Use: fmt.Sprintf("set-cluster name [--%v=server] [--%v=path/to/certficate/authority] [--%v=apiversion] [--%v=true]", clientcmd.FlagAPIServer, clientcmd.FlagCAFile, clientcmd.FlagAPIVersion, clientcmd.FlagInsecure),
Short: "Sets a cluster entry in .kubeconfig",
Long: `Sets a cluster entry in .kubeconfig

Specifying a name that already exists overwrites that cluster entry.
Specifying a name that already exists will merge new fields on top of existing values for those fields.
e.g.
kubectl config set-cluster e2e --certificate-authority=~/.kube/e2e/.kubernetes.ca.cert
only sets the certificate-authority field on the e2e cluster entry without touching other values.
`,
Run: func(cmd *cobra.Command, args []string) {
if !options.complete(cmd) {
Expand All @@ -58,10 +61,12 @@ func NewCmdConfigSetCluster(out io.Writer, pathOptions *pathOptions) *cobra.Comm
},
}

cmd.Flags().StringVar(&options.server, clientcmd.FlagAPIServer, "", clientcmd.FlagAPIServer+" for the cluster entry in .kubeconfig")
cmd.Flags().StringVar(&options.apiVersion, clientcmd.FlagAPIVersion, "", clientcmd.FlagAPIVersion+" for the cluster entry in .kubeconfig")
cmd.Flags().BoolVar(&options.insecureSkipTLSVerify, clientcmd.FlagInsecure, false, clientcmd.FlagInsecure+" for the cluster entry in .kubeconfig")
cmd.Flags().StringVar(&options.certificateAuthority, clientcmd.FlagCAFile, "", clientcmd.FlagCAFile+" for the cluster entry in .kubeconfig")
options.insecureSkipTLSVerify.Default(false)

cmd.Flags().Var(&options.server, clientcmd.FlagAPIServer, clientcmd.FlagAPIServer+" for the cluster entry in .kubeconfig")
cmd.Flags().Var(&options.apiVersion, clientcmd.FlagAPIVersion, clientcmd.FlagAPIVersion+" for the cluster entry in .kubeconfig")
cmd.Flags().Var(&options.insecureSkipTLSVerify, clientcmd.FlagInsecure, clientcmd.FlagInsecure+" for the cluster entry in .kubeconfig")
cmd.Flags().Var(&options.certificateAuthority, clientcmd.FlagCAFile, clientcmd.FlagCAFile+" for the cluster entry in .kubeconfig")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this does what you want it to? I believe Cobra will call Set() on the variable with the default value if none is provided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok then, this approach with .Provided() lgtm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this approach with .Provided() lgtm

Great. I'll get on updating the other sets.


return cmd
}
Expand All @@ -81,7 +86,7 @@ func (o createClusterOptions) run() error {
config.Clusters = make(map[string]clientcmdapi.Cluster)
}

cluster := o.cluster()
cluster := o.modifyCluster(config.Clusters[o.name])
config.Clusters[o.name] = cluster

err = clientcmd.WriteToFile(*config, filename)
Expand All @@ -93,15 +98,23 @@ func (o createClusterOptions) run() error {
}

// cluster builds a Cluster object from the options
func (o *createClusterOptions) cluster() clientcmdapi.Cluster {
cluster := clientcmdapi.Cluster{
Server: o.server,
APIVersion: o.apiVersion,
InsecureSkipTLSVerify: o.insecureSkipTLSVerify,
CertificateAuthority: o.certificateAuthority,
func (o *createClusterOptions) modifyCluster(existingCluster clientcmdapi.Cluster) clientcmdapi.Cluster {
modifiedCluster := existingCluster

if o.server.Provided() {
modifiedCluster.Server = o.server.Value()
}
if o.apiVersion.Provided() {
modifiedCluster.APIVersion = o.apiVersion.Value()
}
if o.insecureSkipTLSVerify.Provided() {
modifiedCluster.InsecureSkipTLSVerify = o.insecureSkipTLSVerify.Value()
}
if o.certificateAuthority.Provided() {
modifiedCluster.CertificateAuthority = o.certificateAuthority.Value()
}

return cluster
return modifiedCluster
}

func (o *createClusterOptions) complete(cmd *cobra.Command) bool {
Expand Down