Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

cli/cmd: implement kubeconfig fallback to Terraform state #701

Merged
merged 7 commits into from
Sep 3, 2020

Conversation

invidian
Copy link
Member

@invidian invidian commented Jul 3, 2020

This PR includes commits from #631, please review this one first and don't leave commit comments for this PR here.

TODO:

  • Running lokoctl health is now slower, as on every run, it initializes the Terraform and reads the output. Should we try to read file from assets dir first and then fallback to Terraform output? Maybe we should cache somehow the kubeconfig file content?
  • Update and add new tests

Closes #608

Signed-off-by: Mateusz Gozdek mateusz@kinvolk.io

@invidian
Copy link
Member Author

invidian commented Aug 4, 2020

Running lokoctl health is now slower, as on every run, it initializes the Terraform and reads the output. Should we try to read file from assets dir first and then fallback to Terraform output? Maybe we should cache somehow the kubeconfig file content?

It seems caching is more complex than I thought, as the optimal sequence of execution is against our abstraction. What we need:

  • Try loading platform configuration
  • Check if kubeconfig file exists in platform assets directory (if it does, great, we're done
  • initialize Terraform
  • read Terraform output

And what we have right now:

  • Try loading platform configuration
  • initialize Terraform
  • read Terraform output

@invidian invidian force-pushed the invidian/kubeconfig-content-part3 branch from 127725b to 9a4c8bc Compare August 4, 2020 11:14
@invidian invidian force-pushed the invidian/kubeconfig-content-part3 branch 2 times, most recently from af5faa8 to 0676578 Compare August 21, 2020 14:40
@invidian invidian marked this pull request as ready for review August 21, 2020 14:48
@invidian invidian force-pushed the invidian/kubeconfig-content-part3 branch from 0676578 to b5d3d64 Compare August 21, 2020 14:52
@invidian invidian changed the title Use kubeconfig from Terraform output, not from assets directory cli/cmd: implement kubeconfig fallback to Terraform state Aug 21, 2020
@johananl
Copy link
Member

This PR includes commits from #631, please review this one first and don't leave commit comments for this PR here.

What do you mean by "this one" and "this PR"? #631 or #701? Should #701 be reviewed?

@invidian
Copy link
Member Author

What do you mean by "this one" and "this PR"? #631 or #701? Should #701 be reviewed?

It's not relevant anymore, as #631 has been merged already :) And yes, I requested reviews on this PR, so it should be reviewed.

@invidian invidian force-pushed the invidian/kubeconfig-content-part3 branch from b5d3d64 to 44c3c0c Compare August 27, 2020 10:53
@invidian
Copy link
Member Author

Rebased to resolve conflicts.

knrt10
knrt10 previously approved these changes Aug 31, 2020
Copy link
Member

@knrt10 knrt10 left a comment

Choose a reason for hiding this comment

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

Looks great.

@surajssd
Copy link
Member

surajssd commented Sep 2, 2020

This failed as we want:

$ lokoctl component apply
WARN[0000] Kubeconfig file not found in assets directory, pulling kubeconfig from Terraform state, this might be slow. Run 'lokoctl cluster apply' to fix it.  args="[]" command="lokoctl component apply"
INFO[0004] Executing step "initialize Terraform"         phase=infrastructure

You can find the logs in "assets/terraform/logs/299669.log"
FATA[0005] Error in finding kubeconfig file: reading kubeconfig file content from Terraform state: failed getting Terraform output for key "kubeconfig": exit status 1  args="[]" command="lokoctl component apply"
$ cat assets/terraform/logs/299669.log
Initializing modules...

Initializing the backend...

Initializing provider plugins...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Can we give more informational message to the user?

@invidian
Copy link
Member Author

invidian commented Sep 2, 2020

Can we give more informational message to the user?

Hmm, should we replace top-level message (Error in finding kubeconfig file: reading kubeconfig file content from Terraform state: failed getting Terraform output for key "kubeconfig": exit status 1) entirely then?

Something like Finding kubeconfig file failed. Did you run "lokoctl cluster apply" ? ?

@invidian invidian added this to the v0.4.0 milestone Sep 2, 2020
@surajssd
Copy link
Member

surajssd commented Sep 2, 2020

@invidian that should do for now :-) It is better than the terraform one.

@invidian
Copy link
Member Author

invidian commented Sep 2, 2020

Maybe we can keep existing message in debug verbosity?

@surajssd
Copy link
Member

surajssd commented Sep 2, 2020

Sounds ok @invidian

As part of #623, which is about using kubeconfig file content rather
than kubeconfig path around, this commits adds Terraform output with
content of kubeconfig file, which can be then pulled during execution
using 'terraform output' command.

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
As number of tests we have has grown, currently running it with verbose
makes it very hard to find which test failed. Removing '-v' flag
improves that a bit.

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
Closes #608.

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
@invidian
Copy link
Member Author

invidian commented Sep 2, 2020

Sounds ok @invidian

😕 I had to add --debug flag, so debug messages can be actually displayed. I could just set it without the flag too, but I think it's a reasonable addition. And I would really prefer not to hide the error. Alternative would be to print exact error after friendly error message.

@invidian invidian force-pushed the invidian/kubeconfig-content-part3 branch from 7b8e721 to f35178e Compare September 2, 2020 12:14
@invidian invidian requested a review from knrt10 September 2, 2020 12:16
@invidian invidian force-pushed the invidian/kubeconfig-content-part3 branch from f35178e to 2b9507a Compare September 2, 2020 13:00
@knrt10
Copy link
Member

knrt10 commented Sep 2, 2020

Can you please update the docs again, it's failing because of the init function change

So debug messages can be displayed if --debug flag is specified. This is
useful for printing not very UX friendly messages to the user if some
issue occurs.

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
For users, we display nice error message which suggests running 'lokoctl
cluster apply', while in debug logs we print exact message.

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
@invidian invidian force-pushed the invidian/kubeconfig-content-part3 branch from 2b9507a to b2d084a Compare September 2, 2020 13:28
@invidian
Copy link
Member Author

invidian commented Sep 2, 2020

Can you please update the docs again, it's failing because of the init function change

🤦 done 😄

cli/cmd/health.go Outdated Show resolved Hide resolved
Copy link
Member

@knrt10 knrt10 left a comment

Choose a reason for hiding this comment

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

Rest lgtm

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
knrt10
knrt10 previously approved these changes Sep 2, 2020
Copy link
Member

@knrt10 knrt10 left a comment

Choose a reason for hiding this comment

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

+1

@surajssd
Copy link
Member

surajssd commented Sep 3, 2020

This failed as follows:

$ lokoctl health --kubeconfig-file $HOME/.kube/config
ERRO[0000] <nil>: no platform configured;                args="[]" command="lokoctl health"
FATA[0000] Suitable kubeconfig file not found. Did you run 'lokoctl cluster apply' ?  args="[]" command="lokoctl health"

$ lokoctl version
v0.3.0-207-gf7f5eded9

$ ll
.rw-rw-r--@ 35 surajd  3 Sep 12:45 foobar.lokocfg

$ cat foobar.lokocfg 
component "prometheus-operator" {}

lokoctl still tries to get the file from state, even after explicit mention.

@invidian
Copy link
Member Author

invidian commented Sep 3, 2020

lokoctl still tries to get the file from state, even after explicit mention.

This is as intended. health command should be only run on L8e clusters as far as I'm aware.

@surajssd
Copy link
Member

surajssd commented Sep 3, 2020

Why do we have a --kubeconfig-file flag for this? Also the help says that it has a fallback mechanism, so it is confusing to the user. I understand the technicalities behind why it shows up here, but then for a user it can be confusing. Either we fail if we have a flag mentioned or this flag should not be here:

$ lokoctl health -h
Get the health of a cluster

Usage:
  lokoctl health [flags]

Flags:
      --debug   Print debug messages
  -h, --help    help for health

Global Flags:
      --kubeconfig-file string   Path to a kubeconfig file. If empty, the following precedence order is used:
                                   1. Cluster asset dir when a lokocfg file is present in the current directory.
                                   2. KUBECONFIG environment variable.
                                   3. ~/.kube/config file.
...

@knrt10 knrt10 removed this from the v0.4.0 milestone Sep 3, 2020
@invidian
Copy link
Member Author

invidian commented Sep 3, 2020

Why do we have a --kubeconfig-file flag for this? Also the help says that it has a fallback mechanism, so it is confusing to the user. I understand the technicalities behind why it shows up here, but then for a user it can be confusing. Either we fail if we have a flag mentioned or this flag should not be here:

Because it's a global flag... :( And yes, entire CLI code should probably be reworked to clean those things up..

Other commands don't really need this flag and commands like 'cluster
apply' or 'health' should only be executed in the scope of L8e cluster,
so they shouldn't allow overriding used kubeconfig file, as it might be
very confusing.

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
@invidian invidian force-pushed the invidian/kubeconfig-content-part3 branch from aa9c70f to 8fa88da Compare September 3, 2020 08:46
@invidian
Copy link
Member Author

invidian commented Sep 3, 2020

@surajssd I pushed the fix with moving --kubeconfig-file flag. Please have a look.

Copy link
Member

@surajssd surajssd left a comment

Choose a reason for hiding this comment

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

Verified the lokoctl health functionality works.

@invidian invidian requested a review from knrt10 September 3, 2020 10:18
@invidian invidian merged commit 68bebe7 into master Sep 3, 2020
@invidian invidian deleted the invidian/kubeconfig-content-part3 branch September 3, 2020 11:13
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lokoctl health does not fallback for kubeconfig
4 participants