Skip to content

Commit

Permalink
Merge pull request kubernetes#5955 from adamyy/export_kubecfg_add_kub…
Browse files Browse the repository at this point in the history
…econfig_option

Add --kubeconfig flag to `kops export kubecfg`
  • Loading branch information
k8s-ci-robot authored Mar 15, 2019
2 parents 04ccfb2 + 14862af commit 09e5cad
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
3 changes: 2 additions & 1 deletion cmd/kops/delete_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/kops/cmd/kops/util"
api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/kubeconfig"
Expand Down Expand Up @@ -202,7 +203,7 @@ func RunDeleteCluster(f *util.Factory, out io.Writer, options *DeleteClusterOpti
}
}

b := kubeconfig.NewKubeconfigBuilder()
b := kubeconfig.NewKubeconfigBuilder(clientcmd.NewDefaultPathOptions())
b.Context = clusterName
err = b.DeleteKubeConfig()
if err != nil {
Expand Down
22 changes: 19 additions & 3 deletions cmd/kops/export_kubecfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io"

"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/commands"
"k8s.io/kops/pkg/kubeconfig"
Expand All @@ -44,8 +45,9 @@ var (
)

type ExportKubecfgOptions struct {
tmpdir string
keyStore fi.CAStore
tmpdir string
keyStore fi.CAStore
KubeConfigPath string
}

func NewCmdExportKubecfg(f *util.Factory, out io.Writer) *cobra.Command {
Expand All @@ -64,6 +66,8 @@ func NewCmdExportKubecfg(f *util.Factory, out io.Writer) *cobra.Command {
},
}

cmd.Flags().StringVar(&options.KubeConfigPath, "kubeconfig", options.KubeConfigPath, "The location of the kubeconfig file to create.")

return cmd
}

Expand Down Expand Up @@ -93,10 +97,22 @@ func RunExportKubecfg(f *util.Factory, out io.Writer, options *ExportKubecfgOpti
return err
}

conf, err := kubeconfig.BuildKubecfg(cluster, keyStore, secretStore, &commands.CloudDiscoveryStatusStore{})
conf, err := kubeconfig.BuildKubecfg(cluster, keyStore, secretStore, &commands.CloudDiscoveryStatusStore{}, buildPathOptions(options))
if err != nil {
return err
}

return conf.WriteKubecfg()
}

func buildPathOptions(options *ExportKubecfgOptions) *clientcmd.PathOptions {
pathOptions := clientcmd.NewDefaultPathOptions()

if len(options.KubeConfigPath) > 0 {
pathOptions.GlobalFile = options.KubeConfigPath
pathOptions.EnvVar = ""
pathOptions.GlobalFileSubpath = ""
}

return pathOptions
}
3 changes: 2 additions & 1 deletion cmd/kops/update_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/golang/glog"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/commands"
Expand Down Expand Up @@ -288,7 +289,7 @@ func RunUpdateCluster(f *util.Factory, clusterName string, out io.Writer, c *Upd
}
if kubecfgCert != nil {
glog.Infof("Exporting kubecfg for cluster")
conf, err := kubeconfig.BuildKubecfg(cluster, keyStore, secretStore, &commands.CloudDiscoveryStatusStore{})
conf, err := kubeconfig.BuildKubecfg(cluster, keyStore, secretStore, &commands.CloudDiscoveryStatusStore{}, clientcmd.NewDefaultPathOptions())
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion docs/cli/kops_export_kubecfg.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ kops export kubecfg CLUSTERNAME [flags]
### Options

```
-h, --help help for kubecfg
-h, --help help for kubecfg
--kubeconfig string The location of the kubeconfig file to create.
```

### Options inherited from parent commands
Expand Down
5 changes: 3 additions & 2 deletions pkg/kubeconfig/create_kubecfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ import (
"sort"

"github.com/golang/glog"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/dns"
"k8s.io/kops/upup/pkg/fi"
)

func BuildKubecfg(cluster *kops.Cluster, keyStore fi.Keystore, secretStore fi.SecretStore, status kops.StatusStore) (*KubeconfigBuilder, error) {
func BuildKubecfg(cluster *kops.Cluster, keyStore fi.Keystore, secretStore fi.SecretStore, status kops.StatusStore, configAccess clientcmd.ConfigAccess) (*KubeconfigBuilder, error) {
clusterName := cluster.ObjectMeta.Name

master := cluster.Spec.MasterPublicName
Expand Down Expand Up @@ -81,7 +82,7 @@ func BuildKubecfg(cluster *kops.Cluster, keyStore fi.Keystore, secretStore fi.Se
}
}

b := NewKubeconfigBuilder()
b := NewKubeconfigBuilder(configAccess)

b.Context = clusterName

Expand Down
4 changes: 2 additions & 2 deletions pkg/kubeconfig/kubecfg_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ type KubeconfigBuilder struct {
}

// Create new KubeconfigBuilder
func NewKubeconfigBuilder() *KubeconfigBuilder {
func NewKubeconfigBuilder(configAccess clientcmd.ConfigAccess) *KubeconfigBuilder {
c := &KubeconfigBuilder{}
c.configAccess = clientcmd.NewDefaultPathOptions()
c.configAccess = configAccess
return c
}

Expand Down

0 comments on commit 09e5cad

Please sign in to comment.