-
Notifications
You must be signed in to change notification settings - Fork 261
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
✨ Refactor: Don't use infrav1.Instance internally #971
✨ Refactor: Don't use infrav1.Instance internally #971
Conversation
@@ -196,24 +196,34 @@ func deleteBastion(log logr.Logger, osProviderClient *gophercloud.ProviderClient | |||
return err | |||
} | |||
|
|||
instance, err := computeService.GetInstanceByName(openStackCluster, fmt.Sprintf("%s-bastion", cluster.Name)) | |||
instanceStatus, err := computeService.GetInstanceByName(openStackCluster, fmt.Sprintf("%s-bastion", cluster.Name)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure we need rename to GetInstanceStatusByName?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm inclined to leave it for now because it's already getting a bit long, but it's easy to change if we decide it's confusing. I think it should be ok, though, because now everything returns InstanceStatus.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for rename it to GetInstanceStatusByName
@@ -0,0 +1,171 @@ | |||
/* | |||
Copyright 2018 The Kubernetes Authors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit : 2021
} | ||
|
||
// APIInstance returns an infrav1.Instance object for use by the API. | ||
func (is *InstanceStatus) APIInstance() (*infrav1.Instance, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like we have more param for Metadata, ConfigDrive etc for Infrav1.Instance
will it be inited here or somewhere else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the intention, although I don't currently think we'll add any paramaters to APIInstance() specifically. We will almost definitely add parameters to NetworkStatus(), though.
/lgtm |
4f39d29
to
3d794dc
Compare
I've rebased on to master and fixed the copyright nit. No other changes. |
/approve ok, I think we can do additional update over time, so far it looks great |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: jichenjc, mdbooth The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found a few nits. But all in all it looks good :)
@@ -196,24 +196,34 @@ func deleteBastion(log logr.Logger, osProviderClient *gophercloud.ProviderClient | |||
return err | |||
} | |||
|
|||
instance, err := computeService.GetInstanceByName(openStackCluster, fmt.Sprintf("%s-bastion", cluster.Name)) | |||
instanceStatus, err := computeService.GetInstanceByName(openStackCluster, fmt.Sprintf("%s-bastion", cluster.Name)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for rename it to GetInstanceStatusByName
instanceNS, err := instanceStatus.NetworkStatus() | ||
if err != nil { | ||
return err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should move this block inside the instanceStatus != nil
condition, as it's only used there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would also have been a bug!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nil pointer incoming :)
floatingIP := instanceNS.FloatingIP() | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't define err
here, you can remove the err handling as no error will be returned
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops. FloatingIP() returned an error in an earlier version and I missed this!
ns, err := instance.NetworkStatus() | ||
if err != nil { | ||
handleUpdateMachineError(logger, openStackMachine, errors.Errorf("error getting network status for OpenStack instance %s with ID %s: %v", instance.Name(), instance.ID(), err)) | ||
return ctrl.Result{}, nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need the current status here? If yes, you should call GetInstanceByName
(or GetInstanceStatusByName
) again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also instance
should be called instanceStatus
:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we want to fetch again here. Were you thinking of some specific reason we might need to? We'd potentially race with the prior delete, so it might even be a bug to re-fetch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we delete the instance and then check the status of the instance? Shouldn't we first check the status (i.e., to get the fips) and if that fails, we don't want to delete the instance? Otherwise we could have orphaned fips, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're creating a InstanceNetworkStatus here so we can get the FloatingIP. This doesn't go back to OpenStack; it's just doing some extra work on the previously returned server object. The only reason it can fail is json parsing. All we need to know is which FloatingIP to delete.
The previous code also had this failure mode, but it was in the return of DeleteInstance()->GetInstance()->serverToInstance()->GetIPFromInstance(). Moving that call up to the 'top level' in the controller is the primary purpose of this refactor, because it means we can pass additional arguments to NetworkStatus() so we can have more context. I don't think it currently matters for FloatingIP(), for for IP() it means we can pass OpenStackCluster as an argument here and use the additional context to determine the correct 'primary' IP from those returned by OpenStack.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incidentally, for robustness we should probably reverse the order of these deletions. The reason is that if deletion of the server succeeds but the deletion of the floating ip fails, the next reconcile will short-cut because there's no server and we'll leak the floating ip.
This patch is just a refactor, though, so I don't want to fix that here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's exactly the point I was worried about. We should first delete the floating IPs and the instance right after the successful deletion.
But okay for me to fix that after this refactor PR.
Further, we could maybe improve the error message for the failed json parsing. error getting network status for OpenStack instance
is a bit misleading, tbh.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And starting errors with error
is redundant. But we have to fix that in the complete repo anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for a thorough review, @tobiasgiese! I'm not in a hurry to land this as I still haven't written the fix for the actual multi-network bug, so I'd prefer to address these before merging.
With 2 people thinking I should rename the GetInstance functions I'll do that, too.
floatingIP := instanceNS.FloatingIP() | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops. FloatingIP() returned an error in an earlier version and I missed this!
ns, err := instance.NetworkStatus() | ||
if err != nil { | ||
handleUpdateMachineError(logger, openStackMachine, errors.Errorf("error getting network status for OpenStack instance %s with ID %s: %v", instance.Name(), instance.ID(), err)) | ||
return ctrl.Result{}, nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we want to fetch again here. Were you thinking of some specific reason we might need to? We'd potentially race with the prior delete, so it might even be a bug to re-fetch.
/lgtm |
/lgtm cancel waiting for the findings to be updated |
This is predominantly a non-functional change. It contains some minor changes to log messages, and omits a check for instance name being empty prior to delete in OpenStackMachine controller because this check is no longer relevant when we delete by ID. Apart from that it contains no (deliberate) functional changes. Currently we use infrav1.Instance as our internal representation of an OpenStack instance. There are several problems with this: * infrav1.Instance is part of the API, so is hard to change for internal use * infrav1.Instance is used to represent both the 'spec' and 'status' of an instance * When used as a spec for the Bastion host, it allows fields to be set which will be ignored * When used as the status of an instance fetched from OpenStack not all fields are populated, leading to potential accidental usage errors This change creates several new types representing different types of instance data purely for internal use. This has the following advantages: * As they are not API types they can be updated easily * When used in a function signature they make it clear what data is being used * The type system will prevent accidental use of uninitialised data The new types are: * InstanceSpec * InstanceIdentifier * InstanceStatus * InstanceNetworkStatus They are defined and described in `instance_types.go`. The primary driver for this work is to eventually fix the documented errors in InstanceNetworkStatus in IP() and FloatingIP().
3d794dc
to
6ef256a
Compare
That change is intended to address all review comments. Please check! |
/lgtm |
/hold cancel |
This is predominantly a non-functional change. It contains some minor
changes to log messages, and omits a check for instance name being empty
prior to delete in OpenStackMachine controller because this check is no
longer relevant when we delete by ID. Apart from that it contains no
(deliberate) functional changes.
Currently we use infrav1.Instance as our internal representation of an
OpenStack instance. There are several problems with this:
use
an instance
which will be ignored
fields are populated, leading to potential accidental usage errors
This change creates several new types representing different types of
instance data purely for internal use. This has the following
advantages:
being used
The new types are:
They are defined and described in
instance_types.go
.The primary driver for this work is to eventually fix the documented
errors in InstanceNetworkStatus in IP() and FloatingIP().
What this PR does / why we need it:
This is a refactor in support of a future change to fix #926. I would like to merge these in separate PRs to separate potential errors introduced by a refactor from the functional changes required to fix the issue.
Which issue(s) this PR fixes (optional, in
fixes #<issue number>(, fixes #<issue_number>, ...)
format, will close the issue(s) when PR gets merged):Special notes for your reviewer:
I suggest the easiest way to read this change is:
/hold