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 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) {
@@ -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")
return cmd
}
@@ -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)
@@ -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 {