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

CLI: Rework cmpInstanceKeys for contextual completions based on instance type #14684

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kadinsayani
Copy link
Contributor

@kadinsayani kadinsayani commented Dec 18, 2024

Fixes #14689.

Summary of changes:

  • Adds completions for cloud-init.* config keys.
  • (WIP) Correctly provides completions based on instance type.

@kadinsayani kadinsayani changed the title lxc storage volume completion fixes and description updates CLI: lxc storage volume completion fixes and description updates Dec 18, 2024
@kadinsayani kadinsayani force-pushed the 14682-storage-volume-completion branch from 1c67d9f to 2f53f43 Compare December 18, 2024 00:09
@kadinsayani kadinsayani changed the title CLI: lxc storage volume completion fixes and description updates CLI: lxc storage volume completion fixes and command description updates Dec 18, 2024
@kadinsayani kadinsayani force-pushed the 14682-storage-volume-completion branch from 2f53f43 to 14d9643 Compare December 18, 2024 16:48
@kadinsayani kadinsayani changed the title CLI: lxc storage volume completion fixes and command description updates CLI: Completion fixes and command description updates Dec 18, 2024
@kadinsayani kadinsayani linked an issue Dec 18, 2024 that may be closed by this pull request
@kadinsayani kadinsayani force-pushed the 14682-storage-volume-completion branch 3 times, most recently from 463ac01 to 33c5c93 Compare December 18, 2024 21:05
@kadinsayani kadinsayani marked this pull request as ready for review December 18, 2024 22:01
lxc/completion.go Outdated Show resolved Hide resolved
@kadinsayani kadinsayani force-pushed the 14682-storage-volume-completion branch 2 times, most recently from 9a7d059 to 5495fc8 Compare December 19, 2024 15:06
@@ -274,7 +274,7 @@ func (g *cmdGlobal) cmpInstanceKeys(instanceName string) ([]string, cobra.ShellC
configKey = strings.TrimSuffix(configKey, "*")

// InstanceTypeAny config keys.
if configKeyField.Condition == "" {
if configKeyField.Condition == "" || configKeyField.Condition == "If supported by image" {
Copy link
Member

Choose a reason for hiding this comment

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

this looks fishy, whats it comparing to?

Copy link
Contributor Author

@kadinsayani kadinsayani Dec 19, 2024

Choose a reason for hiding this comment

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

The condition field for a cloud-init.* instance config key:

			"cloud-init": {
				"keys": [
					{
						"cloud-init.network-config": {
							"condition": "If supported by image",
							"defaultdesc": "`DHCP on eth0`",
							"liveupdate": "no",
							"longdesc": "The content is used as seed value for `cloud-init`.",
							"shortdesc": "Network configuration for `cloud-init`",
							"type": "string"
						}
					},

Alternatively, we could add all keys under the cloud-init map.

Copy link
Member

Choose a reason for hiding this comment

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

why are we needing to consider the Condition field?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We check the condition field to determine if a key is valid for instance type any, container, or virtual-machine.

Copy link
Member

Choose a reason for hiding this comment

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

ok, so we really dont want to be comparing to arbitrary free-text values.

This seem super hacky to me.

Copy link
Member

Choose a reason for hiding this comment

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

Currently, we add all keys from every config key group as long as they match the condition of instance type.

Do we make clear the link anywhere between the value of condition and how it affects auto complete?
It feels like this link has been only recently established in these changes and could easily be broken due to not making clear the link, and hard to detect (due to there being no tests for auto complete).

I wonder if lxd-metadata should be updated to test for valid "conditions" values from a slice it has so we can only use accepted ones.

Then we could expose those as constants and have them used as checks for the auto complete.

If we want to move away from comparing to strings found in metadata, we can grab the keys from instancetype.InstanceConfigKeysAny/VM/Container.

But that would then be restricted to keys known about by the client, and not by the server leading to situations where the client may suggest something the server doesnt support or miss out on suggesting something it does support.

Also, by checking for condition as it stands today, you're missing out on other instance fields like raw.idmap and security.idmap.* which have a condition of unprivileged container.

One acceptable workaround would be to have a list of prefixes that you can accept from the metadata API irrespective of condition.

Copy link
Contributor Author

@kadinsayani kadinsayani Dec 19, 2024

Choose a reason for hiding this comment

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

I'm in agreement with moving away from checking "condition" values.

But that would then be restricted to keys known about by the client, and not by the server leading to situations where the client may suggest something the server doesnt support or miss out on suggesting something it does support.

We can also consider checking the keys from the metadata API against instancetype.InstanceConfigKeysAny/VM/Container to deduce compatible instance types. Aren't the keys generated by lxd-metadata sourced from lxd/instance/instancetype/instance.go?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but remember lxc would be built against a list at a point in time, but the server maybe newer and have additional keys only in metadata api

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What about adding a "supported instance type" field to the metadata? I'm trying to avoid hard coding a list of accepted keys based on instance type in the client, since these may not line up with the server. Also, there are cases where a prefix contains keys supported by containers and VMs. For example, the migration prefix contains migration.incremental.memory which is only supported by containers, and migration.stateful which is only supported by VMs.

Copy link
Contributor Author

@kadinsayani kadinsayani Dec 20, 2024

Choose a reason for hiding this comment

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

Another consideration for adding an instance type field to config keys in the metadata is the potential usability improvements that could be achieved in lxd-ui. I don't use it much so I'm not sure if completions are served when setting profile or instance config keys, but having an instance type field in the metadata could facilitate smart completions for config keys in the ui.

@tomponline
Copy link
Member

Is it possible to split this pr into two, so we can merge the non contentious parts?

@kadinsayani kadinsayani force-pushed the 14682-storage-volume-completion branch from 5495fc8 to 4307f76 Compare December 19, 2024 22:20
@kadinsayani kadinsayani force-pushed the 14682-storage-volume-completion branch from 4307f76 to fb65418 Compare December 19, 2024 22:22
@kadinsayani kadinsayani changed the title CLI: Completion fixes and command description updates CLI: Completion fixes Dec 19, 2024
@kadinsayani kadinsayani marked this pull request as draft December 19, 2024 22:32
@kadinsayani
Copy link
Contributor Author

kadinsayani commented Dec 19, 2024

Is it possible to split this pr into two, so we can merge the non contentious parts?

Sure thing. I've updated this PR and created #14702. Please note the addition of 5b7d222.

@kadinsayani kadinsayani changed the title CLI: Completion fixes CLI: Rework cmpInstanceKeys for contextual completions based on instance type Dec 19, 2024
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.

cloud-init.* keys are not suggested during tab completion
3 participants