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

Allow users to define vm naming logic #369

Merged
merged 5 commits into from
Jan 31, 2023

Conversation

erkanerol
Copy link
Contributor

@erkanerol erkanerol commented Dec 29, 2022

Description

Now, VMs are named as machine.Name. In some cases, we need to name VMs with a custom logic.

This PR adds a new field into VCDMachine CR so that users can define templates to name VMs by using Go templates and Sprig functions. .machine and .vcdMachine will refer to Machine and VCDMachine CRs in the templates.

Example

spec:
  vmNamingTemplate: 'mycustomprefix{{.machine.Name |sha256sum | trunc 7}}'

Important Points
This field must be immutable. Otherwise, users can change it and the controller will not be able to know existing VMs.
Immutability is enforced by the new validating webhook for VCDMachine.

Checklist

  • tested locally
  • updated any relevant dependencies
  • updated any relevant documentation or examples

API Changes

Are there API changes?

  • Yes
  • No

If yes, please fill in the below

  1. Updated conversions?
    • Yes
    • No
    • N/A
  2. Updated CRDs?
    • Yes
    • No
    • N/A
  3. Updated infrastructure-components.yaml?
    • Yes
    • No
    • N/A
  4. Updated ./examples/capi-quickstart.yaml?
    • Yes
    • No
    • N/A
  5. Updated necessary files under ./infrastructure-vcd/v1.0.0/?
    • Yes
    • No
    • N/A

This change is Reviewable

@erkanerol erkanerol marked this pull request as ready for review December 29, 2022 11:56
@erkanerol erkanerol force-pushed the vm-naming-with-template branch from d4dfafe to d70600d Compare January 9, 2023 10:41
@erkanerol
Copy link
Contributor Author

@arunmk I added an immutability check to the webhook. This is ready for review.

@vxav vxav mentioned this pull request Jan 10, 2023
Copy link
Collaborator

@arunmk arunmk left a comment

Choose a reason for hiding this comment

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

lgtm. I have only one ask of a comment

api/v1beta1/vcdmachine_types.go Outdated Show resolved Hide resolved
Copy link
Contributor

@Anirudh9794 Anirudh9794 left a comment

Choose a reason for hiding this comment

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

Apart from the error check, which seems like which was added by mistake, the rest of the PR looks good to me.

@@ -441,7 +449,10 @@ func (r *VCDMachineReconciler) reconcileNormal(ctx context.Context, cluster *clu
cloudInitInput.HTTPProxy = vcdCluster.Spec.ProxyConfigSpec.HTTPProxy
cloudInitInput.HTTPSProxy = vcdCluster.Spec.ProxyConfigSpec.HTTPSProxy
cloudInitInput.NoProxy = vcdCluster.Spec.ProxyConfigSpec.NoProxy
cloudInitInput.MachineName = machine.Name
cloudInitInput.MachineName = vmName
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

It is not sure what error is being checked here. Can you please make sure this is not added by mistake?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@erkanerol could you please handle this? we should be able to merge immediately after it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is leftover after a refactoring. Just deleted the check. Thanks for catching this.

@erkanerol erkanerol force-pushed the vm-naming-with-template branch from 326e6ba to 20b05b8 Compare January 27, 2023 15:20
Erkan Erol added 4 commits January 30, 2023 11:22
Now, VMs are named as machine.Name. In some cases,
we need to name VMs with a custom logic.

This PR adds a new field into VCDMachine CR so that
users can define templates to name VMs by using Go templates
and Sprig functions. ".machine" and ".vcdMachine" will refer
to Machine and VCDMachine CRs in the templates.
@erkanerol erkanerol force-pushed the vm-naming-with-template branch from 34db163 to 7603f5a Compare January 30, 2023 08:24
@erkanerol
Copy link
Contributor Author

@arunmk @Anirudh9794 I rebased the PR. FYI.


// VmNamingTemplate is go template to generate VM names based on Machine and VCDMachine CRs.
// Functions of Sprig library are supported. See https://github.com/Masterminds/sprig
// Immutable field. machine.Name is used as VM name when this field is empty.
// +optional
VmNamingTemplate string `json:"vmNamingTemplate"`
VmNamingTemplate string `json:"vmNamingTemplate,omitempty"`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Users should be able to create VCDMachine CRs without providing empty values for these fields. Missing omitempty tags lead to errors in CAPI controllers while creating VCDMachine by using VCDMachineTemplate.

@arunmk
Copy link
Collaborator

arunmk commented Jan 31, 2023

I'll merge this since it has 2 approvals.

@arunmk arunmk merged commit c4c22d3 into vmware:main Jan 31, 2023
@erkanerol erkanerol deleted the vm-naming-with-template branch February 20, 2023 09:58
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.

3 participants