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

Extend kubeconfig writer to support global path with merging #80

Merged
merged 7 commits into from
Jul 5, 2018

Conversation

errordeveloper
Copy link
Contributor

Incorporates @richardcase's changes from #78.

@errordeveloper errordeveloper force-pushed the issue-29-merge-kubeconfig branch 2 times, most recently from 04425e9 to 40eafbd Compare June 22, 2018 17:04
@errordeveloper
Copy link
Contributor Author

errordeveloper commented Jun 22, 2018

@richardcase so I couldn't stop wondering if k8s.io/client-go/tools/clientcmd should be able to do the merging for us, but all references to "merge" in the doc seem to refer to merging of configs on load...

So I looked at github.com/kubicorn/kubicorn/pkg/kubeconfig and that seems to be very specific to kubicorn and mostly implements whatever is needed to fetch the config via SSH.

I also looked at github.com/kubernetes/kops/pkg/kubeconfig and discovered that it is also very specific to kops, but it uses clientcmd.ModifyConfig which looks intriguing.

@errordeveloper
Copy link
Contributor Author

I've reviewed the kubeconfig package in kops, I think we should be able to use it actually, but it might need some refactoring (it uses glog and fmt, but that should be easy to fix upstream). It seems like the package does what we want, I'll need to take another read to confirm that 100%. I'm also very happy to merge this to start with.

@richardcase
Copy link
Contributor

@errordeveloper - you are right KubeconfigBuilder from kops would work. I like the fact that it uses ModifyConfig to do the read of existing and saving of the modified config. As you say very intriguing.

We could refactor to use it directly or change our code to do something similar and call ModifyConfig. I'm happy to do either of these 2 if you want.

@richardcase
Copy link
Contributor

richardcase commented Jun 25, 2018

As you pointed out the kops implementation use glog and fmt and it doesn't support ExecConfig that we require for Heptio Authenticator (unless i've missed this).

I wonder if we should refactor our implementation to use a similar approach to kops and take advantage of clientcmd.NewDefaultPathOptions, clientcmd.ModifyConfig and also clientcmd.GetStartingConfig.

I also like that they do a delete of the cluster, context and authinfos. Our delete logic is a bit crude at the moment (i.e. only delete an autogenerated config).

What do you think?

@errordeveloper
Copy link
Contributor Author

So I've missed ExecConfig part. I think refactoring to make use of clientcmd.NewDefaultPathOptions, clientcmd.ModifyConfig and clientcmd.GetStartingConfig, as you said, is a good idea. Perhaps we can get that in, and open an issue to coverage with kops later.

@richardcase
Copy link
Contributor

I will refactor based on the above against the issue-29-merge-kubeconfig branch.

@richardcase
Copy link
Contributor

As discussed I've updated our implementation to use clientcmd.NewDefaultPathOptions, clientcmd.ModifyConfig and clientcmd.GetStartingConfig.

I will raise an issue with kops to support ExecConfig and link it to an issue here to track and change our implementation.

@errordeveloper
Copy link
Contributor Author

@richardcase I'll test this tomorrow and merge

@@ -23,7 +23,7 @@ func TestCreateNewKubeConfig(t *testing.T) {
"test-context": {AuthInfo: "test-user", Cluster: "test-cluster", Namespace: "test-ns"}},
}

err := kubeconfig.WriteToFile(configFile.Name(), &testConfig, false)
err := kubeconfig.WriteKubeCfg(configFile.Name(), &testConfig, false)

This comment was marked as abuse.

This comment was marked as abuse.

@errordeveloper errordeveloper force-pushed the issue-29-merge-kubeconfig branch from c721dd0 to 7639670 Compare July 4, 2018 16:06
@errordeveloper
Copy link
Contributor Author

I've spotted a couple of issues, turns out NewClientSetWithEmbeddedToken expects current-context to be set, also utils.CheckAllCommands will break... I'm testing a fix a the moment, will push once I confirm it works.

@errordeveloper errordeveloper changed the title Support merging of Kubeconfig and KUBECONFIG Extend kubeconfig writer to support global path with merging Jul 4, 2018
@errordeveloper
Copy link
Contributor Author

@richardcase please take a look

@jstrachan FYI

Copy link
Contributor

@richardcase richardcase left a comment

Choose a reason for hiding this comment

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

This looks good to me.

@@ -79,7 +79,7 @@ func createClusterCmd() *cobra.Command {

fs.BoolVar(&writeKubeconfig, "write-kubeconfig", true, "toggle writing of kubeconfig")
fs.BoolVar(&autoKubeconfigPath, "auto-kubeconfig", false, fmt.Sprintf("save kubconfig file by cluster name, e.g. %q", kubeconfig.AutoPath(exampleClusterName)))
fs.StringVar(&kubeconfigPath, "kubeconfig", "", "path to write kubeconfig (incompatible with --auto-kubeconfig)")
fs.StringVar(&kubeconfigPath, "kubeconfig", kubeconfig.DefaultPath, "path to write kubeconfig (incompatible with --auto-kubeconfig)")

This comment was marked as abuse.

// Write will write Kubernetes client configuration to a file.
// If path isn't specified then the path will be determined by client-go.
// If file pointed to by path doesn't exist it will be created.
// If the file already exists then the configuration will be merged with the existing file.
func Write(path string, newConfig *api.Config, setContext bool) error {
func Write(path string, newConfig *api.Config, setContext bool) (string, error) {

This comment was marked as abuse.

richardcase
richardcase previously approved these changes Jul 4, 2018
@errordeveloper errordeveloper force-pushed the issue-29-merge-kubeconfig branch 2 times, most recently from 960940a to 5381234 Compare July 5, 2018 04:47
richardcase and others added 6 commits July 5, 2018 06:29
When writing the kubeconfig for the new EKS cluster then the following
will happen:
- If --kubeconfig, --auto-kubeconfig or KUBECONFING env var are NOT set
  then the default kube config location will be used (~/.kube/conf).
- If KUBECONFIG is set then we use this (assuming --kubeconfig and
  --auto-kubeconfig aren't specified).
- if KUBECONFIG is set BUT --kubeconfig or --auto-kubeconfig are
  specified then KUBECONFIG will be ignored.
- if a kubeconfig file already exists then we merge in the new EKS
  cluster details with the existing file.
- If `set-context` is true then the current-context will be set to that
  of the new EKS cluster.
- If `set-context` is false (default) then if we are merging with an
  existing configuration file with a current-context set then this won't
  be updated.
- If there is no existing configuration file then current-context will
  be set even if `set-context` is false.

Issue #29
A number of changes in relation to the kube configuration file. The
setting of current-context defaults to true. Also updated the
write-kubeconfig command so that its merges configs as well. Also,
when deleting a cluster it will only try and delete an auto-generated
config file (otherwise it will warn that you need to do this manually).

Issue #29
- rename kubeconf to kubeconfig
- use default kubeconfig path directly
- remove unecessary method for default path and use client-go instead
- other minor cosmetic tweaks
The kubeconfig package has been modified to use `NewDefaultPathOptions`
`ModifyConfig` like kops instead of other handcrafted methods. The
plan will eventually to be re-use KubeConfBuilder from kops when it
supports exec.

Issue #29
The method to write the kube configuration file in the `kubeconfig`
package has been change from `WriteKubeCfg` to `Write` as its simpler
and more readable.
@errordeveloper errordeveloper force-pushed the issue-29-merge-kubeconfig branch from b5f13fa to e4aeef5 Compare July 5, 2018 05:31
- set `--auto-kubeconfig=false` by default
- fix default `--kubeconfig` value
- pass context to command checker
- do not pass `setContext` to client builder
- obtain effective filename
- remove unused import
- improve output messages
  - use more explicit flag syntax in output message
  - add missing message in `eksctl utils write-kubeconfig`
  - clarify kubectl test warning
@errordeveloper errordeveloper force-pushed the issue-29-merge-kubeconfig branch from e4aeef5 to 7e6bf07 Compare July 5, 2018 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants