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

job: import jobs from non-default namespace #408

Merged
merged 5 commits into from
Dec 19, 2023
Merged

Conversation

lgfa29
Copy link
Contributor

@lgfa29 lgfa29 commented Dec 16, 2023

nomad_job, nomad_csi_volume, and nomad_csi_volume_registration do not have the namespace as part of their state ID. This makes it impossible to import them if they are registered in a non-default namespace since only the ID is provided to the import function.

This commit adds a new helper that imports using an ID with the format <id>@<namespace>. Since @ is not a valid character for namespaces, the last @ character (if found) represents the separator.

Implements the approach suggested in #375 (comment).

lgfa29 added a commit that referenced this pull request Dec 16, 2023
@lgfa29
Copy link
Contributor Author

lgfa29 commented Dec 16, 2023

Tests seems to have started failing in a81b5f7. I will investigate in a follow-up PR.

@lgfa29
Copy link
Contributor Author

lgfa29 commented Dec 16, 2023

Oh, I think #407 fixes the tests. We'll see once all PRs are merged 😅

@lgfa29 lgfa29 requested a review from gulducat December 18, 2023 22:55
Copy link
Member

@gulducat gulducat left a comment

Choose a reason for hiding this comment

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

some food for thought, but LGTM!

CHANGELOG.md Outdated Show resolved Hide resolved
nomad/helper/import.go Outdated Show resolved Hide resolved
@@ -137,5 +137,22 @@ configuration options.
- `create` `(string: "10m")` - Timeout when creating or updating a new CSI volume.
- `delete` `(string: "10m")` - Timeout when deleting a CSI volume.

## Importing CSI Volumes

CSI volumes are imported using the pattern `<volume ID>@<namespace>` .
Copy link
Member

Choose a reason for hiding this comment

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

I think these might benefit from some info on how to get the namespace, or short of that, saying ~"If you're not sure, it's probably the default, which is @default" and maybe using @default in the examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hum...not sure about this. If you're importing a resource you must know its namespace. If you don't, you really shouldn't be importing it 😬

You will also need to set the namespace in the TF config, so you really need to know it if you're trying to import it.

Copy link
Member

Choose a reason for hiding this comment

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

For jobs, the namespace can be excluded from the jobspec, and it isn't an argument in the TF resource. For CSI volumes, the namespace is a TF resource argument, but can also be left out and defaults to "default"

So our just-getting-started type folks, or even experienced users who just never needed namespaces for some reason, could totally get away with never thinking about namespaces at all -- the default "just works" -- so I'm thinking this might be a little learning opportunity where they could use just a bit of extra help.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But importing a resource in Terraform is not targeted at just-getting-started folks, it's more like a day 10321 type of thing 😅

return nil, missingIDImportErr
}

d.SetId(id)
Copy link
Member

Choose a reason for hiding this comment

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

I like that this retains the same non-namespaced ID in state, so no state migration necessary 👍

`nomad_job`, `nomad_csi_volume`, and `nomad_csi_volume_registration` do
not have the namespace as part of their state ID. This makes it
impossible to import them if they are registered in a non-default
namespace since only the ID is provided to the import function.

This commit adds a new helper that imports using an ID with the format
`<id>@<namespace>`. Since `@` is not a valid character for namespaces,
the last `@` character (if found) represents the separator.
Importing namespaced resources require a namespace to be passed. Without
this requirement jobs such as `a@b` become ambiguous as it's not
possible to determine if the user intention was to import the job named
`a` from namespace `b` or job `a@b` on namespace `default`. By requiring
a namespace to be set this ambiguity is solved.

Additionally, Terraform automatically refreshes the state after an
import, so there is no need to call the `Read` method manually on
import. Doing so can also have unintended consequences, as the provider
deletes resources from state in case of a `404`, resulting in a
Terraform error when trying to import resources that don't exist.
@lgfa29 lgfa29 merged commit 1b74304 into main Dec 19, 2023
3 checks passed
@lgfa29 lgfa29 deleted the f-namespaced-importer branch December 19, 2023 23:03
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.

None yet

2 participants