2014-12-17 13:03:03 +00:00
/ *
2015-05-01 16:19:44 +00:00
Copyright 2014 The Kubernetes Authors All rights reserved .
2014-12-17 13:03:03 +00:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
package config
import (
"errors"
"fmt"
"io"
2015-03-02 19:49:31 +00:00
"io/ioutil"
2015-06-29 20:27:31 +00:00
"path/filepath"
2014-12-17 13:03:03 +00:00
2016-05-20 17:49:56 +00:00
"github.com/renstrom/dedent"
2014-12-17 13:03:03 +00:00
"github.com/spf13/cobra"
2015-08-13 19:01:50 +00:00
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/util"
2016-03-09 07:25:34 +00:00
"k8s.io/kubernetes/pkg/util/flag"
2014-12-17 13:03:03 +00:00
)
type createClusterOptions struct {
2016-04-14 18:47:19 +00:00
configAccess clientcmd . ConfigAccess
2014-12-17 13:03:03 +00:00
name string
2015-01-23 19:18:25 +00:00
server util . StringFlag
apiVersion util . StringFlag
2016-03-09 07:25:34 +00:00
insecureSkipTLSVerify flag . Tristate
2015-01-23 19:18:25 +00:00
certificateAuthority util . StringFlag
2016-03-09 07:25:34 +00:00
embedCAData flag . Tristate
2014-12-17 13:03:03 +00:00
}
2016-05-20 17:49:56 +00:00
var (
create_cluster_long = dedent . Dedent ( `
Sets a cluster entry in kubeconfig .
Specifying a name that already exists will merge new fields on top of existing values for those fields . ` )
create_cluster_example = dedent . Dedent ( `
# Set only the server field on the e2e cluster entry without touching other values .
kubectl config set - cluster e2e -- server = https : //1.2.3.4
2015-03-11 17:22:08 +00:00
2016-05-20 17:49:56 +00:00
# Embed certificate authority data for the e2e cluster entry
kubectl config set - cluster e2e -- certificate - authority = ~ / . kube / e2e / kubernetes . ca . crt
2015-03-11 17:22:08 +00:00
2016-05-20 17:49:56 +00:00
# Disable cert checking for the dev cluster entry
kubectl config set - cluster e2e -- insecure - skip - tls - verify = true ` )
2015-03-11 17:22:08 +00:00
)
2016-04-14 18:47:19 +00:00
func NewCmdConfigSetCluster ( out io . Writer , configAccess clientcmd . ConfigAccess ) * cobra . Command {
2015-04-10 12:54:22 +00:00
options := & createClusterOptions { configAccess : configAccess }
2014-12-17 13:03:03 +00:00
cmd := & cobra . Command {
2016-03-03 01:35:55 +00:00
Use : fmt . Sprintf ( "set-cluster NAME [--%v=server] [--%v=path/to/certficate/authority] [--%v=true]" , clientcmd . FlagAPIServer , clientcmd . FlagCAFile , clientcmd . FlagInsecure ) ,
2015-04-08 14:32:32 +00:00
Short : "Sets a cluster entry in kubeconfig" ,
2015-03-11 17:22:08 +00:00
Long : create_cluster_long ,
Example : create_cluster_example ,
2014-12-17 13:03:03 +00:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
if ! options . complete ( cmd ) {
return
}
err := options . run ( )
if err != nil {
2015-02-18 02:37:43 +00:00
fmt . Fprintf ( out , "%v\n" , err )
2015-09-23 08:08:42 +00:00
} else {
fmt . Fprintf ( out , "cluster %q set.\n" , options . name )
2014-12-17 13:03:03 +00:00
}
} ,
}
2015-01-23 19:18:25 +00:00
options . insecureSkipTLSVerify . Default ( false )
2015-04-08 14:32:32 +00:00
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" )
2016-01-08 16:42:06 +00:00
f := cmd . Flags ( ) . VarPF ( & options . insecureSkipTLSVerify , clientcmd . FlagInsecure , "" , clientcmd . FlagInsecure + " for the cluster entry in kubeconfig" )
f . NoOptDefVal = "true"
2016-03-11 22:06:42 +00:00
cmd . Flags ( ) . Var ( & options . certificateAuthority , clientcmd . FlagCAFile , "path to " + clientcmd . FlagCAFile + " file for the cluster entry in kubeconfig" )
cmd . MarkFlagFilename ( clientcmd . FlagCAFile )
2016-01-08 16:42:06 +00:00
f = cmd . Flags ( ) . VarPF ( & options . embedCAData , clientcmd . FlagEmbedCerts , "" , clientcmd . FlagEmbedCerts + " for the cluster entry in kubeconfig" )
f . NoOptDefVal = "true"
2014-12-17 13:03:03 +00:00
return cmd
}
func ( o createClusterOptions ) run ( ) error {
err := o . validate ( )
if err != nil {
return err
}
2015-04-10 12:54:22 +00:00
config , err := o . configAccess . GetStartingConfig ( )
2014-12-17 13:03:03 +00:00
if err != nil {
return err
}
2015-06-29 18:39:48 +00:00
startingStanza , exists := config . Clusters [ o . name ]
if ! exists {
startingStanza = clientcmdapi . NewCluster ( )
}
cluster := o . modifyCluster ( * startingStanza )
config . Clusters [ o . name ] = & cluster
2014-12-17 13:03:03 +00:00
2016-04-14 18:47:19 +00:00
if err := clientcmd . ModifyConfig ( o . configAccess , * config , true ) ; err != nil {
2014-12-17 13:03:03 +00:00
return err
}
return nil
}
// cluster builds a Cluster object from the options
2015-01-23 19:18:25 +00:00
func ( o * createClusterOptions ) modifyCluster ( existingCluster clientcmdapi . Cluster ) clientcmdapi . Cluster {
modifiedCluster := existingCluster
if o . server . Provided ( ) {
modifiedCluster . Server = o . server . Value ( )
}
if o . insecureSkipTLSVerify . Provided ( ) {
modifiedCluster . InsecureSkipTLSVerify = o . insecureSkipTLSVerify . Value ( )
2015-02-18 02:37:43 +00:00
// Specifying insecure mode clears any certificate authority
if modifiedCluster . InsecureSkipTLSVerify {
modifiedCluster . CertificateAuthority = ""
modifiedCluster . CertificateAuthorityData = nil
}
2015-01-23 19:18:25 +00:00
}
if o . certificateAuthority . Provided ( ) {
2015-03-02 19:49:31 +00:00
caPath := o . certificateAuthority . Value ( )
if o . embedCAData . Value ( ) {
modifiedCluster . CertificateAuthorityData , _ = ioutil . ReadFile ( caPath )
2015-02-18 02:37:43 +00:00
modifiedCluster . InsecureSkipTLSVerify = false
2015-03-02 19:49:31 +00:00
modifiedCluster . CertificateAuthority = ""
} else {
2015-06-29 20:27:31 +00:00
caPath , _ = filepath . Abs ( caPath )
2015-03-02 19:49:31 +00:00
modifiedCluster . CertificateAuthority = caPath
// Specifying a certificate authority file clears certificate authority data and insecure mode
if caPath != "" {
modifiedCluster . InsecureSkipTLSVerify = false
modifiedCluster . CertificateAuthorityData = nil
}
2015-02-18 02:37:43 +00:00
}
2014-12-17 13:03:03 +00:00
}
2015-01-23 19:18:25 +00:00
return modifiedCluster
2014-12-17 13:03:03 +00:00
}
func ( o * createClusterOptions ) complete ( cmd * cobra . Command ) bool {
args := cmd . Flags ( ) . Args ( )
if len ( args ) != 1 {
cmd . Help ( )
return false
}
o . name = args [ 0 ]
return true
}
func ( o createClusterOptions ) validate ( ) error {
if len ( o . name ) == 0 {
2015-09-23 08:08:42 +00:00
return errors . New ( "you must specify a non-empty cluster name" )
2014-12-17 13:03:03 +00:00
}
2015-02-18 02:37:43 +00:00
if o . insecureSkipTLSVerify . Value ( ) && o . certificateAuthority . Value ( ) != "" {
2015-09-23 08:08:42 +00:00
return errors . New ( "you cannot specify a certificate authority and insecure mode at the same time" )
2015-02-18 02:37:43 +00:00
}
2015-03-02 19:49:31 +00:00
if o . embedCAData . Value ( ) {
caPath := o . certificateAuthority . Value ( )
if caPath == "" {
2015-09-23 08:08:42 +00:00
return fmt . Errorf ( "you must specify a --%s to embed" , clientcmd . FlagCAFile )
2015-03-02 19:49:31 +00:00
}
if _ , err := ioutil . ReadFile ( caPath ) ; err != nil {
2015-09-23 08:08:42 +00:00
return fmt . Errorf ( "could not read %s data from %s: %v" , clientcmd . FlagCAFile , caPath , err )
2015-03-02 19:49:31 +00:00
}
}
2014-12-17 13:03:03 +00:00
2015-04-10 12:54:22 +00:00
return nil
2014-12-17 13:03:03 +00:00
}