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

✨ add VM naming strategy for VMs in govmomi mode #3286

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

viveksyngh
Copy link

  • Add VM naming stragey for vsphere vms and VMs in govami mode

What this PR does / why we need it:

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Dec 8, 2024
@k8s-ci-robot
Copy link
Contributor

Welcome @viveksyngh!

It looks like this is your first PR to kubernetes-sigs/cluster-api-provider-vsphere 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/cluster-api-provider-vsphere has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Dec 8, 2024
@k8s-ci-robot k8s-ci-robot requested review from MaxRink and srm09 December 8, 2024 16:20
@k8s-ci-robot
Copy link
Contributor

Hi @viveksyngh. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Dec 8, 2024
@viveksyngh viveksyngh changed the title feat: add VM naming strategy for VMs in govami mode ✨ add VM naming strategy for VMs in govami mode Dec 8, 2024
@chrischdi chrischdi changed the title ✨ add VM naming strategy for VMs in govami mode ✨ add VM naming strategy for VMs in govmomi mode Dec 9, 2024
Copy link
Member

@neolit123 neolit123 left a comment

Choose a reason for hiding this comment

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

please give a ping once this is no longer a draft and is ready for review.

/cc

@neolit123
Copy link
Member

make sure you add a test.

@viveksyngh viveksyngh marked this pull request as ready for review December 16, 2024 04:31
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 16, 2024
@@ -68,6 +68,30 @@ type VSphereMachineSpec struct {
//
// +optional
GuestSoftPowerOffTimeout *metav1.Duration `json:"guestSoftPowerOffTimeout,omitempty"`

// NamingStrategy allows configuring the naming strategy used when calculating the name of the VSphereVM.
Copy link
Member

@neolit123 neolit123 Dec 16, 2024

Choose a reason for hiding this comment

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

calculating -> preparing
as it's not a math calculation.

EDIT: seems consistent with the supervisor PR. so you can leave it as is.

name of the VSphereVM.

this field is inside VSphereMachineSpec not VSphereVMSpec

Comment on lines +77 to +78
// VSphereVMNamingStrategy defines the naming strategy for the VSphereVMs.
type VSphereVMNamingStrategy struct {
Copy link
Member

Choose a reason for hiding this comment

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

should this be named
VSphereMachineNamingStrategy

Copy link
Member

Choose a reason for hiding this comment

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

+1, as this struct is used at the VSphereMachine

apis/v1beta1/vspheremachine_types.go Show resolved Hide resolved
apis/v1beta1/vspheremachine_types.go Show resolved Hide resolved
return nil, AggregateObjErrors(newTyped.GroupVersionKind().GroupKind(), newTyped.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (webhook *VSphereMachineTemplateWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
return nil, nil
}

func validateVSphereVMNamingTemplate(_ context.Context, vsphereMachineTemplate *infrav1.VSphereMachineTemplate) field.ErrorList {
Copy link
Member

Choose a reason for hiding this comment

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

is this following some code that core CAPI already has or is this done from scratch as a use case for CAPV?
if the former, could you link to that code for reviewer's reference?

Copy link
Author

Choose a reason for hiding this comment

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

This is based on this MR which was added to support this in supervisor mode #3099

In supervisor mode, the object is called VirtualMachine which drives the creation of vSphere VMs while in govami mode this object is called VSphereVM.

Copy link
Member

Choose a reason for hiding this comment

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

looks like a lot comes from that PR.
why not also name it VirtualMachineNamingStrategy here? that seems more consistent despite the diff backend objects.

Copy link
Author

Choose a reason for hiding this comment

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

I can do that if we prefer that. I just to wanted to call it what it is being used for.

Copy link
Member

Choose a reason for hiding this comment

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

that would be my preference. not sure what other reviewers will comment on this.

Copy link
Member

Choose a reason for hiding this comment

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

👍 to keep it as VSphereVM, because that's the object which gets created from a VSphereMachine.

maxNameLength = 63
)

// Note: Inlining these functions from sprig to avoid introducing a dependency.
Copy link
Member

Choose a reason for hiding this comment

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

sprig is a typo?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

@neolit123 neolit123 Jan 9, 2025

Choose a reason for hiding this comment

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

you can't just copy code from another project without attribution. also CNCF has special handling of MIT and some other licenses. see https://github.com/cncf/foundation/blob/main/allowed-third-party-license-policy.md#cncf-allowlist-license-policy

please add the entire dependency and let the go compiler optimize.

Copy link
Member

Choose a reason for hiding this comment

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

/hold

Copy link
Member

Choose a reason for hiding this comment

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

Note: we don't want to simply import the whole sprig library and use the function from there to prevent the transitive dependencies. Instead we should better outline the license part here and propably move the inlined functions to a separate package.

Copy link
Member

@neolit123 neolit123 Jan 10, 2025

Choose a reason for hiding this comment

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

given it will be a fork the project can copy only the bits that it needs. a readme or just a code comment clarifing purpose and origin of the forked bits would suffice. original license file and license header too.

in the k/k third-party dir there are cases like that.

Copy link
Member

Choose a reason for hiding this comment

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

Took a look at the CAPI go.mod file. We already have a direct dependency to sprig there. I think it's okay to have the same in CAPV. So I would just take the direct dependency and we're good.

(there's not much point to avoiding the dependency in CAPV if we have it in CAPI)

Copy link
Author

Choose a reason for hiding this comment

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

This is already in the main and we are using it. https://github.com/kubernetes-sigs/cluster-api-provider-vsphere/blob/main/pkg/services/vmoperator/vmopmachine.go#L298

I could not reuse it because they were ware private and not exported. Depending what we decide, I can make the change.

Copy link
Member

@neolit123 neolit123 Jan 10, 2025

Choose a reason for hiding this comment

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

This is already in the main and we are using it. https://github.com/kubernetes-sigs/cluster-api-provider-vsphere/blob/main/pkg/services/vmoperator/vmopmachine.go#L298

I could not reuse it because they were ware private and not exported. Depending what we decide, I can make the change.

all copied code from a different license without attribution must be removed.
import the library and use its exported functions.
if what you need from the library is not exported, go with the minimal fork approach.

Copy link
Author

Choose a reason for hiding this comment

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

I have update the MR where I imported the function sprig library and use only those functions which are required.

Please take a look.

- op: add
path: /spec/template/spec/namingStrategy
value:
template: '{{ if le (len .machine.name) 20 }}{{ .machine.name }}{{else}}{{ trimSuffix "-" (trunc 14 .machine.name) }}-{{ trunc -5 .machine.name }}{{end}}'
Copy link
Member

Choose a reason for hiding this comment

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

add missing new line at the end.

@viveksyngh
Copy link
Author

This MR is more or less similar code from this MR which added the functionality in the supervisor mode. #3099

I can make the requested changes but I have tried to align this as much as possible with supervisor mode changes.

@neolit123
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Dec 16, 2024
@neolit123
Copy link
Member

This MR is more or less similar code from this MR which added the functionality in the supervisor mode. #3099

I can make the requested changes but I have tried to align this as much as possible with supervisor mode changes.

#3286 (comment)

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jan 9, 2025
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 13, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign vincepri for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 13, 2025
Comment on lines +49 to +51
dario.cat/mergo v1.0.1 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.3.0 // indirect
Copy link
Member

Choose a reason for hiding this comment

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

These dependencies are something we might want to prevent having.

Copy link
Author

@viveksyngh viveksyngh Jan 15, 2025

Choose a reason for hiding this comment

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

I think these came up from sprig library.

Copy link
Member

Choose a reason for hiding this comment

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

Just noted: This now adds a new dependency to github.com/Masterminds/sprig/v3, while we currently only have a go dependency to github.com/go-task/slim-sprig/v3.

Comment on lines +66 to +67
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
Copy link
Member

Choose a reason for hiding this comment

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

These dependencies are even archived and not maintained anymore.

Copy link
Member

Choose a reason for hiding this comment

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

cc @sbueringer , not sure if we should instead do the third_party approach with "minimal" change 🤔

Copy link
Member

@chrischdi chrischdi Jan 15, 2025

Choose a reason for hiding this comment

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

xref #3286 (comment)

We want to import a different sprig so this may be gone then

Comment on lines +105 to +108
{
name: "successful VSphereMachineTemplate creation with namingStrategy not set",
vsphereMachine: createVSphereMachineTemplate("foo.com", "vmx-17", nil, "", []string{}, nil, nil),
},
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
{
name: "successful VSphereMachineTemplate creation with namingStrategy not set",
vsphereMachine: createVSphereMachineTemplate("foo.com", "vmx-17", nil, "", []string{}, nil, nil),
},

Duplicate test case to above, let's adjust the name above though.

Copy link
Member

@chrischdi chrischdi left a comment

Choose a reason for hiding this comment

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

/hold due to the breakage of findVSphereVM

pkg/services/vimmachine.go Show resolved Hide resolved
var nameTpl = template.New("name generator").Funcs(nameTemplateFuncs).Option("missingkey=error")

// GenerateVSphereVMName generates the name of a VSphereVM based on the naming strategy.
func GenerateVSphereVMName(machineName string, namingStrategy *infrav1.VSphereVMNamingStrategy) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

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

This function seems to be the same as we have for supervisor?

Let's move this to pkg/util to a separate go file and use it at both places.

@chrischdi
Copy link
Member

/hold cancel

As above noted #3286 (comment)

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants