-
Notifications
You must be signed in to change notification settings - Fork 986
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
Credentials changes for v2.0.0 #1052
Conversation
153f7d0
to
3be7d12
Compare
bc45567
to
9602d64
Compare
if v, ok := d.Get("config_paths").([]string); ok && len(v) > 0 { | ||
configPaths = v | ||
} else if v := os.Getenv("KUBE_CONFIG_PATHS"); v != "" { | ||
// NOTE we have to do this here because the schema |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an open issue for this hashicorp/terraform-plugin-sdk#142
940af6a
to
7cc1c12
Compare
7cc1c12
to
af0af77
Compare
} | ||
if err := os.Unsetenv("KUBE_LOAD_CONFIG_FILE"); err != nil { | ||
t.Fatalf("Error unsetting env var KUBE_LOAD_CONFIG_FILE: %s", err) | ||
envVars := map[string]string{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mopped this up because it was tedious. 🧹
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I actually did the same thing here, but I removed the whole section. I found it wasn't being used, or at least, it had no visible effect when I removed it. That's ok though, I'll remove this from my PR and test again once yours merges.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to be used in this one test that doesn't actually run as part of the acceptance tests: https://github.com/hashicorp/terraform-provider-kubernetes/blob/master/kubernetes/provider_test.go#L70
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I saw that it didn't run during acceptance tests and that nothing was calling it... but I think I understand its purpose now. Thanks for showing me. I was misinterpreting it as a "TestProvider" configure function. But it actually tests the "provider configure" code, which does serve a purpose. So that's worth keeping.
af0af77
to
58883a6
Compare
58883a6
to
f82e5f2
Compare
kubernetes/provider.go
Outdated
} else if v := os.Getenv("KUBE_CONFIG_PATHS"); v != "" { | ||
// NOTE we have to do this here because the schema | ||
// does not yet allow you to set a default for a TypeList | ||
configPaths = strings.Split(v, ":") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Splitting the string by :
will work on Linux and MacOS, but Windows uses a semi-colon as the delimiter for its PATH. Maybe os.PathListSeparator
would be better to use here than :
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call – I didn't know about os.PathListSeparator. I just had a poke at the kubectl code and it uses filepath.SplitList, which uses os.PathListSeparator underneath so I'm going to update to use that.
Just realized I haven't owned a Windows machine in about 10 years 😅.
Would it be possible to add an error message to let users know why the provider can no longer find their
It looks like we might need an additional check for the configuration during acceptance tests:
|
kubernetes/provider.go
Outdated
Type: schema.TypeString, | ||
Optional: true, | ||
DefaultFunc: schema.EnvDefaultFunc("KUBE_CONFIG_PATH", ""), | ||
Description: "Path to the kube config file. Can be set with KUBE_CONFIG_PATH.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great! Could we also add ConflictsWith = []string{"config_paths"}
? And the equivalent for config_path, so that the SDK lets a user know if they've specified both? Otherwise we're selecting one of the two at random, and the user might find it unexpected. In my testing, it will yield this result:
$ terraform validate
Error: ConflictsWith
"config_paths": conflicts with config_path
Error: ConflictsWith
"config_path": conflicts with config_paths
However, if specified as environment variables, it doesn't give the user any error message:
$ KUBE_CONFIG_PATH=$KUBECONFIG KUBE_CONFIG_PATHS=$KUBECONFIG terraform init
$ KUBE_CONFIG_PATH=$KUBECONFIG KUBE_CONFIG_PATHS=$KUBECONFIG terraform validate
Success! The configuration is valid.
$ KUBE_CONFIG_PATH=$KUBECONFIG KUBE_CONFIG_PATHS=$KUBECONFIG terraform apply --auto-approve
kubernetes_pod.main: Creating...
kubernetes_pod.main: Creation complete after 6s [id=default/test-pod]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
That validation difference between the environment variables and the equivalent in HCL might be an SDK bug.
EDIT: I see, this is the same bug you linked in this PR. It affects setting the default, which affects ConflictsWith.
Co-authored-by: Stef Forrester <[email protected]>
So this looks to be because of this conditional: https://github.com/hashicorp/terraform-provider-kubernetes/blob/master/kubernetes/provider.go#L244 We could explicitly check the attributes supplied to the provider block but the tricky thing here is, to know for sure what's going on, we have to check:
If we also add cc @alexsomesan |
Check kubeconfig files exist at configure time
The error message for this is super nice! I did some testing with it today and it was a smooth user experience. So this is looking really good so far. I tested a new scenario that might need some work: the ability to detect when one cluster has gone away. Here's the scenario: I have two clusters. By default, the provider will apply all my kubernetes configurations to the first cluster listed in
Here's my provider block:
When I deleted the minikube cluster named |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally we get rid of load_config_file
... Nicely done!
I've had a look around and I found a spot around the InCluster stuff that I think might cause some trouble. Left some comments in there.
"token", | ||
"exec", | ||
} | ||
for _, a := range atLeastOneOf { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand this correctly, but it looks like having just "token" or "client_certificate" set would count as valid configuration. Not sure if I'm missing something else.
This could be confusing if not covering all combinations that make a valid configuration.
Also (not 100% sure) but it might be falsely triggered by progressive apply situations. Can we rule those out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intent of this code is not to cover all valid possible configurations, it is to rule out that:
- The user has left the provider block is empty
- Terraform is running inside a cluster.
The purpose of this is so we can tell the user to explicitly configure instead of generating the confusing "could not connect" error at apply time when the client actually gets initialized. Once we fix this we should be able to generate more meaningful errors that come from client go, as it will warn you about these problems.
We can use ConflictsWith
and RequiredWith
to validate that the right combination of attributes is being used together, but I would propose doing that in another pull request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. I didn't catch those nuances while reading through it the first time. Let's try this approach out and see how it improves things. Looks like hashicorp/terraform#24055 is confirmed fixed so we're clear to move in that regard.
I agree with doing things incrementally. Let's re-evaluate after this is released.
} | ||
log.Printf("[DEBUG] Using overidden context: %#v", overrides.Context) | ||
if _, err := os.Stat(path); err != nil { | ||
return nil, fmt.Errorf("could not open kubeconfig %q: %v", p, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this fail to configure the provider if a single file is missing?
IMO, we should consider all entries in the list in a best effort to find valid credentials.
Also, in case of multiple files present it could be argued that a valid CurrentContext should be present to eliminate ambiguities about which cluster would be chosen to connect to. Otherwise we might end up applying changes to an undesirable destination.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The behaviour of kubectl when using a KUBECONFIG
with multiple paths is not to error if one of the paths does not exist. That being said I think if we're erring on the side of explicitness by not supporting KUBECONFIG
anymore (and allowing this to be set as a list in the provider block) then we should also be more explicit here and tell the user that one of the paths they specified does not exist rather than silently skipping it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good then. Let's go with this!
We might need to keep an eye out for corner cases where this list would be set but not all entries might be actually present in the file system (yet). It's a stretch and quite unlikely, but we've seen funny things before.
What do you think about requiring a CurrentContext to be set when this list has more than one entry? Just as a thought experiment for now. Doesn't need to be part of this PR.
@dak1n1 Did you confirm this is what's supposed to happen when you use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All good on my side. This looks like it's going to improve quality of life for everyone.
Excited to try it out!
"token", | ||
"exec", | ||
} | ||
for _, a := range atLeastOneOf { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. I didn't catch those nuances while reading through it the first time. Let's try this approach out and see how it improves things. Looks like hashicorp/terraform#24055 is confirmed fixed so we're clear to move in that regard.
I agree with doing things incrementally. Let's re-evaluate after this is released.
} | ||
log.Printf("[DEBUG] Using overidden context: %#v", overrides.Context) | ||
if _, err := os.Stat(path); err != nil { | ||
return nil, fmt.Errorf("could not open kubeconfig %q: %v", p, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good then. Let's go with this!
We might need to keep an eye out for corner cases where this list would be set but not all entries might be actually present in the file system (yet). It's a stretch and quite unlikely, but we've seen funny things before.
What do you think about requiring a CurrentContext to be set when this list has more than one entry? Just as a thought experiment for now. Doesn't need to be part of this PR.
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks! |
Description
Removes the load_config_file attribute and drops implicit support for
KUBECONFIG
.Acceptance tests
Have you added an acceptance test for the functionality being added?Release Note
Release note for CHANGELOG:
References
Community Note