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

Set hostname for dhcp networks #714

Merged
merged 1 commit into from
Mar 11, 2020
Merged

Conversation

pablochacin
Copy link
Contributor

For dhcp networks, ensure the hostname is set when the ip address is allocated.

Unify behavior when network is referenced either by name or by id.

Fix bug using network after it was freed

Improve test for covering new behavior

Fixes #708

Please make sure you read the contributor documentation before opening a Pull Request.

@pablochacin pablochacin force-pushed the dhcp-hostname branch 2 times, most recently from ee36ca0 to 96bc263 Compare March 6, 2020 08:36
@MalloZup MalloZup self-requested a review March 6, 2020 11:31
Copy link
Collaborator

@flavio flavio left a comment

Choose a reason for hiding this comment

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

Good work. The whole code is needs a lot of refactoring to clean it up, but this is something that should be done later, with a dedicated set or PRs.

Some overall comments/requests:

  • Please do not conflate all the changes into a single commit. You're solving different problems with the same PR (which is acceptable), but you should have multiple commits (eg: the fixes about the network resource being released twice should be part of a different commit)
  • I preferred to leave the deallocation of the network instances to the caller of the setNetworkInterfaces function. Yes, we can build on top of the GC offered by the libvirt library (using the Ref() method), but I would prefer not to do that because 1) I don't know how much we can trust that to work 2) IMHO it makes the code less idiomatic

Thanks a lot for your fixes, I know it has been a hard one 👏

libvirt/domain.go Outdated Show resolved Hide resolved
libvirt/domain.go Outdated Show resolved Hide resolved
libvirt/domain.go Show resolved Hide resolved
libvirt/domain.go Outdated Show resolved Hide resolved
libvirt/resource_libvirt_domain.go Outdated Show resolved Hide resolved
@pablochacin
Copy link
Contributor Author

  • Please do not conflate all the changes into a single commit. You're solving different problems with the same PR (which is acceptable), but you should have multiple commits (eg: the fixes about the network resource being released twice should be part of a different commit)

I tend to do this except that in this case, without this change, the refactoring for setting the hostname doesn't work. The test fails. I generally don't like to make commits that don't work.

On the other hand, the way the reference to the network is freed depends on the re-organization of the code introduced. I could make a fix for that under the actual code structure, but that change will be completely lost in the second commit.

@MalloZup
Copy link
Collaborator

MalloZup commented Mar 6, 2020

thx @flavio and @pablochacin for this. i will have a look to this PR also next week

@flavio
Copy link
Collaborator

flavio commented Mar 9, 2020

To elaborate more on why I'm not so convinced about using the Ref() method.

When dealing with resource cleanup a Go developer doesn't have a double garbage collector running like in this case, where we have Go's GC and the one implemented inside of libvirt.

When dealing with resources like a database object a Go developer has still to take care of invoking some finalization code before Go's GC takes the object instance out of reach. The developer does that by leveraging the defer construct and by taking a close look at how return statements are used.

I would prefer to ignore the Ref() method and take a more "idiomatic" approach to this problem. I would also, with a later series of PRs, refactor this portion of code to make it more manageable. Right now I admit that understanding when to do a defer/Free/nothing at all is complicated, but that's caused by the incredible length and branching we have in this area of the codebase.

@pablochacin
Copy link
Contributor Author

pablochacin commented Mar 9, 2020

@flavio I gave a try to your proposal and the resulting code is extremely convoluted and hard to maintain because it must ensure it will work on every possible code execution path. It is a lot of changes for something that we expect to solve by refactoring the code.

One alternative could be to change the pendingMapping structure and pass the network name or ID instead of the reference to the network and then whoever takes care of these pending mappings will obtain and release the network reference from this name or ID. I think this is a tradeoff between a non-idiomatic solution using Ref and refactoring the code (it is a minor change in the code)

If you disagree with this alternative, then do you want to include this refactoring in the present PR? I would prefer not, but this is the only alternative to introducing multiple Free in the code, I prefer to refactor the code.

@flavio
Copy link
Collaborator

flavio commented Mar 10, 2020

Let's go ahead with the Ref() approach for now. We can try to get rid of them with a future PR


resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibvirtDomainDestroy,
Steps: []resource.TestStep{
{
Config: config,
Config: config,
ExpectNonEmptyPlan: false, // TODO: find why if set to true, the test fails
Copy link
Collaborator

Choose a reason for hiding this comment

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

we might need to check this before mergin because it could be a code smell of regression.

same also the Destroy I think we never used in Testacc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. I was unable to identify the reason, except that before the plan had the hostname but after the apply, it was empty. Terraform complained about this. I pointed this out in the issue #708 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, not expecting nonempty plans is good:

Expecting errors or non-empty plans:

It is possible for scenarios to exist where a valid configuration (no errors during plan) would result in a non-empty plan after successfully running terraform apply. This is typically due to a valid but otherwise misconfiguration of the resource, and is generally undesirable.

@MalloZup
Copy link
Collaborator

@pablochacin @flavio regarding the Ref or not Ref approach.

I'm fine with both approaches, BUT please add some comment on the design.

I remember to have looked in the past how we process networks and until you don't dive in actively in the code, you can't understand ( the code is also pretty complicated for good and bad reasons).

So if we can have comments explaining how we use the REF and why, this will be appreciate, because I have already forgotten that part of code. 😁

A part of this, I think we might check 2 times the testacc because imho we are modifying them to pass in an usual way and this could be there is a regression since we need to modify them to make them pass see my comments.

Thx sofar.. 🌞

For dhcp networks, ensure the hostname is set
when the ip address is allocated.

Unify behavior when network is referenced either by name
or by id.

Fix bug using network after it was freed

Improve test for covering new behavior

Signed-off-by: Pablo Chacin <pchacin@suse.com>
@pablochacin
Copy link
Contributor Author

pablochacin commented Mar 10, 2020

I have updated the PR. I think I've addressed all the concerns except the change in the test, as @MalloZup pointed out.

I finally changed the code not to pass a reference to the network in the structure of the pending interface, but the network name. I think this way we delegate to the routine that handles the pending interfaces when to acquire an release the network reference.

Copy link
Collaborator

@flavio flavio left a comment

Choose a reason for hiding this comment

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

This looks so much better!

@flavio
Copy link
Collaborator

flavio commented Mar 11, 2020

@MalloZup I would be fine approving this PR. What do you think?

Also, once merged, how can we tag a new release including this fix?

Copy link
Collaborator

@MalloZup MalloZup left a comment

Choose a reason for hiding this comment

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

thx!

@MalloZup MalloZup merged commit da06479 into dmacvicar:master Mar 11, 2020
@MalloZup
Copy link
Collaborator

@flavio ok I will prepare a new tag including this fix. Then we can build the RPM . @dmacvicar is ok for you at this point to do a new release? I think we have a release draft but is more a sketch. I will need to go trough the PR merged and summarize them.

Sofar thx lot for helping and contributing. Looking forward 🌻 🎸

@MalloZup
Copy link
Collaborator

MalloZup commented Mar 11, 2020

see https://github.com/dmacvicar/terraform-provider-libvirt/releases/tag/untagged-30bccf4e7c1526ff1438 but is not complete.

If you want to help feel free @flavio , otherwise I will do it during the day.

@pablochacin @flavio if you want a RPM package from master you could use the unstable repo just trigger it from the service.

If you wanna right there feel free to ask via obs I will accept.

https://build.opensuse.org/package/show/systemsmanagement:terraform:unstable/terraform-provider-libvirt

otherwise for the "official" normally the process is to :

  • create release draft
  • release it
  • adjust OBS pkgs to point to release version and build them.
  • announcement on social medias etc ( this is done by @dmacvicar since he write more convincing English then me 😁 )

pablochacin pushed a commit to pablochacin/terraform-provider-libvirt that referenced this pull request Mar 16, 2020
PR dmacvicar#714 introduced spurious changes in the test configuration
for `resource_libvirt_domain_test.go`. The added `Destroy` parameter
has no effect and the comment regarding the `ExpectNonEmptyPlan` are
outdated.

dmacvicar#714

Signed-off-by: Pablo Chacin <pchacin@suse.com>
@pablochacin
Copy link
Contributor Author

Thanks a lot, @MalloZup. Not sure what can/should/must do here.

@MalloZup
Copy link
Collaborator

we need to release a new version. I will make it happen

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.

hostname is not set if dhcp is used for assigning addresses
3 participants