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

Stop hard-coding underscore in clone names #93

Merged
merged 3 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public class vSphereCloudSlaveTemplate implements Describable<vSphereCloudSlaveT
protected static final SchemeRequirement HTTP_SCHEME = new SchemeRequirement("http");
protected static final SchemeRequirement HTTPS_SCHEME = new SchemeRequirement("https");

private final String cloneNamePrefix;
private int configVersion;
private static final int CURRENT_CONFIG_VERSION = 1;
private String cloneNamePrefix; // almost final
private final String masterImageName;
private Boolean useSnapshot; // almost final
private final String snapshotName;
Expand Down Expand Up @@ -148,6 +150,7 @@ public vSphereCloudSlaveTemplate(final String cloneNamePrefix,
final RetentionStrategy<?> retentionStrategy,
final List<? extends NodeProperty<?>> nodeProperties,
final List<? extends VSphereGuestInfoProperty> guestInfoProperties) {
this.configVersion = CURRENT_CONFIG_VERSION;
this.cloneNamePrefix = cloneNamePrefix;
this.masterImageName = masterImageName;
this.snapshotName = snapshotName;
Expand Down Expand Up @@ -349,6 +352,38 @@ protected Object readResolve() {
LOGGER.log(Level.CONFIG, " - Failed to reconfigure strategy", ex);
}
}
// Earlier versions of the code may have stored configuration in a
// different way but in the same kind of fields, so we need an explicit
// versioning to know how to mutate the data.
if( configVersion<=0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: inconsistent parens (looks like this project generally goes for the standard-ish if (...) { with space after the if and space after the close paren.)

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm quite happy with nit-picking comments; I'll sort those out - I dislike messy/inconsistent code too.

LOGGER.log(Level.CONFIG,
"{0} loaded old configuration that had hard-coded underscore at the end of the cloneNamePrefix.",
this);
// In version one, the underscore was removed from the code so we
// have to put it in the data (the user is then free to change it to
// something else if they want to).
this.cloneNamePrefix = this.cloneNamePrefix + "_";
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this.cloneNamePrefix be empty going into this method, e.g. perhaps if there was no config object at all to begin with? Is readResolve() only called if there is an existing config, and if so, is cloneNamePrefix guaranteed to be non-null and non-empty?

Copy link
Member Author

Choose a reason for hiding this comment

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

There are no guarantees.

  • If it was empty then the slave node names would previously have been named "_someRandomId" and this code will ensure that this remains the case.
  • If it was null then the slave node names would previously have been named "null_someRandomId" and this code will ensure that this remains the case.
    i.e. The old behavior is maintained, but a bad config won't be magically made good.

In an ideal world, we'd have some sort of "post deserialization fix-up" at the cloud level that ensured that, within the vSphereCloud, the prefix was non-null, non-empty and unique for each template, but I'm not 100% sure where that code would have to go / get-called-from in order to make it work in all situations.

configVersion = 1;
}
// Note: Subsequent changes dependent on configVersion should go above
// this line.
if (configVersion < CURRENT_CONFIG_VERSION) {
throw new IllegalStateException("Internal error: configVersion==" + configVersion
+ " at end of readResolve method, but the current config version should be "
+ CURRENT_CONFIG_VERSION
+ ". Either CURRENT_CONFIG_VERSION is incorrect or the readResolve method is not setting configVersion when it upgrades the data.");
}
if( configVersion > CURRENT_CONFIG_VERSION ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: paren spacing

LOGGER.log(Level.WARNING,
"{0} was defined by a later version of the plugin "
+ "(one that saved with configVersion={1}, whereas this version of the plugin is expecting {2}). "
+ "The code may not function as expected.",
new Object[]{
this,
configVersion,
CURRENT_CONFIG_VERSION
});
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ public static CloudProvisioningRecord findTemplateWithMostFreeCapacity(
}

/**
* Chooses a name for a new node.
* Chooses a name for a new node. The name will start with the
* {@link vSphereCloudSlaveTemplate#getCloneNamePrefix()}.
* <ul>
* <li>If the template has a limited number of instances available then the
* name will be of the form "prefix_number" where "number" is a number that
* should be between 1 and the number of permitted instances.</li>
* name will be of the form "prefix<i>number</i>" where "<i>number</i>" is a
* number that should be between 1 and the number of permitted instances.
* </li>
* <li>If the template has an unlimited number of instances available then
* the name will be of the form "prefix_random" where "random" is a random
* UUID's 32-byte number (rendered using a high radix to keep the string
* short).</li>
* the name will be of the form "prefix<i>random</i>" where "<i>random</i>"
* is a random UUID's 32-byte (128 bit) number (rendered using a high radix
* to keep the string short).</li>
* </ul>
*
* @param record
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div>
Controls the name by which the virtual machine will be known to vSphere.
The plug-in will append an underscore and a unique ID to this prefix in order to generate unique names.<p>
The plug-in will append a unique ID to this prefix in order to generate unique names.<p>
Copy link
Contributor

Choose a reason for hiding this comment

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

HTML nit (maybe applies throughout) -- do this

<p>stuff</p>

<p>more stuff</p>

Copy link
Member Author

Choose a reason for hiding this comment

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

Personally, I'm not much of a fan of putting formatting/layout elements at the start/end of lines that contain content. I prefer to keep layout and content on their own lines. e.g.

<p>
stuff
</p>
<p>
more stuff
</p>

However, I did try wrapping the first line of text (line 2 in the file) in <p> ... </p> and found that this then added an unsightly amount of vertical whitespace in the help text (between the top border of the help box and the top of the first line of text).
There's a border around the existing help-text that's about as wide & tall as an "M" character, and the word "Controls" at the start and the "(from vSphere Plugin)" text at the end are both indented by equal amounts.
If we make the initial text a paragraph then the layout becomes unbalanced and there's "two Ms" worth of vertical whitespace before the first word "Controls".
i.e. it might be "more correct" HTML but it's also more ugly.

I suspect that the HTML generated by Jenkins to wrap the help text was designed more for "simple text" than HTML containing lots of layout formatting. I suspect that it was designed to make things "look right" if they start and end with no vertical whitespace.

I'm not an HTML expert, so I did mvn hpi:run and did some experimentation with the layout ... and I found that if we remove <p> and </p> completely and instead separate the initial text (lines 2 and 3 in this file) from the "Note" (line 4) by <br><br> then that looks good too (in my opinion), and it also uses half an M less vertical whitespace between the "Note" text and the "(from vSphere Plugin)" text, which appeals to my sense of neatness.

...also (a brief google later), doing <p style="margin-top:0;">stuff</p> for the first makes it look acceptable when rendered too, but I suspect that messing with styling here would be considered worse than just using <br><br> would be.

I'm open to ideas but unless we can settle on something that's both "good HTML practice" and also "looks correct when rendered" then I think it may be best to leave that code as-is.

Note: You must ensure that no virtual machines (other than those created by this template) have names starting with this prefix.
</p>
</div>