Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Update remove-clusters to remove clusters from urlmaps status as well #151

Merged
merged 1 commit into from
Mar 29, 2018

Conversation

nikhiljindal
Copy link
Contributor

Ref #58 (remove-clusters) and #145 (storing status on url map instead of forwarding rule).

Follow up to #146 which added a kubemci remove-clusters command. That PR updated the status stored on forwarding rule.
This PR updates that code to update the status on url map as well.

The status could be stored on forwarding rule (for old MCIs) or url map (for new MCIs).

cc @G-Harmon @csbell @madhusudancs

@perotinus
Copy link
Contributor

I'll take a look at this one.

return fmt.Errorf("errors in updating both http and https forwarding rules, http rule error: %s, https rule error: %s", httpErr, httpsErr)
}

// canIgnoreStatusUpdateError returns true iff either of the following is true:
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I think it might be better to expand "iff" to "if and only if". I'm always concerned that people will think that "iff" is a typo and miss out on the meaning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

// * err is nil
// * err has http.StatusNotFound code
func canIgnoreStatusUpdateError(err error) bool {
if err == nil || utils.IsHTTPErrorCode(err, http.StatusNotFound) {
Copy link
Contributor

Choose a reason for hiding this comment

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

return err == nil || utils.IsHTTPErrorCode(err, http.StatusNotFound)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right :) Updated!

httpErr := s.removeClustersFromStatus("http", s.namer.HttpForwardingRuleName(), clusters)
// If both are not found, then return that.
if utils.IsHTTPErrorCode(httpErr, http.StatusNotFound) && utils.IsHTTPErrorCode(httpsErr, http.StatusNotFound) {
return httpErr
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be a combined error that explains that both are not found?

Also, why is it OK if only one is not found?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By default users create http only ingress, so only HttpForwardingRule will exist.
Similarly for https only ingress, only HttpsForwardingRule will exist.
Generally speaking, if this was able to update the status's that it found, it returns success.

For combining: the error only has the NotFound status code, so combining wont change anything.
We handle both NotFound specially since we want to surface "load balancer does not exist" errors.

fmt.Println(httpProtocol, "forwarding rule not found. Nothing to update. Continuing")
return nil
// Return this error as is.
return err
}
err := fmt.Errorf("error in fetching existing forwarding rule %s: %s", name, err)
fmt.Println(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be Printf("%v\n", err)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

um why?
I dont need any special formatting. Println will print the error fine.
Example: https://play.golang.org/p/CEl9eX_esTh

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, OK. I think this was a mental holdover on my part from doing C-based language work for years, where the arguments to "PrintX" methods were always strings. Apologies!

if err := frs.RemoveClustersFromStatus([]string{"cluster1"}); err != nil {
t.Errorf("unexpected error in updating status to remove clusters: %s", err)
err := frs.RemoveClustersFromStatus([]string{"cluster1"})
if c.shouldErr != (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.

Maybe create a local variable for err != nil

hasErr := err != nil
if c.ShouldErr != hasErr

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 think its fine here without it but good point to keep in mind for places with more complex code.

return err
}

func (l *LoadBalancerSyncer) removeClustersFromStatus(lbName string, removeClusters []string) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Docs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

// Also note that it continues silently if either HTTP or HTTPS forwarding rules do not exist.
if err := s.removeClustersFromStatus("https", s.namer.HttpsForwardingRuleName(), clusters); err != nil {
return fmt.Errorf("unexpected error in updating the https forwarding rule status: %s", err)
httpsErr := s.removeClustersFromStatus("https", s.namer.HttpsForwardingRuleName(), clusters)
Copy link
Contributor

Choose a reason for hiding this comment

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

How does this relate to the URL map change? It seems independent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Strictly speaking yes its independent. The reason its here is that the interface is tighter now and consistent with UrlMapSyncer.RemoveClustersFromStatus. Both return NotFound if the urlmap or forwarding rule does not exist.


// RemoveClusters removes the given list of clusters from the given status string.
// Returns NotFoundError if the given statusStr is not a valid LoadBalancerStatus.
func RemoveClusters(statusStr string, removeClusters []string) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: clustersToRemove

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, here and everywhere

status.Clusters = newClusters
newDesc, err := status.ToString()
if err != nil {
return statusStr, fmt.Errorf("unexpected error in converting status to string. status: %s, err: %s", status, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a NotFoundError?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. This is error in serializing Status struct to string. This should ideally never be hit.

@@ -117,6 +118,32 @@ func (s *URLMapSyncer) DeleteURLMap() error {
}
}

// See interface for comment.
func (s *URLMapSyncer) RemoveClustersFromStatus(clusters []string) error {
fmt.Println("Removing clusters", clusters, "from url map")
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this use Printf?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

um why? We use Println throughout the code base for simple output strings that dont require special formatting.

@nikhiljindal
Copy link
Contributor Author

Thanks for the review @perotinus
Pushed a new commit as per comments.
PTAL. Will squash commits after lgtm before merging.

@nikhiljindal nikhiljindal force-pushed the removeCluster branch 2 times, most recently from 275da99 to 3f49d05 Compare March 28, 2018 21:58
@nikhiljindal
Copy link
Contributor Author

Rebased to remove merge conflicts and fixed test.
@perotinus PTAL

fmt.Println(httpProtocol, "forwarding rule not found. Nothing to update. Continuing")
return nil
// Return this error as is.
return err
}
err := fmt.Errorf("error in fetching existing forwarding rule %s: %s", name, err)
fmt.Println(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, OK. I think this was a mental holdover on my part from doing C-based language work for years, where the arguments to "PrintX" methods were always strings. Apologies!

t.Errorf("unexpected error in updating status to remove clusters: %s", err)
err := frs.RemoveClustersFromStatus([]string{"cluster1"})
if c.shouldErr != (err != nil) {
t.Errorf("unexpected error in updating status to remove clusters, expected err != nil: %v, actual err: %s", c.shouldErr, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm. I think the test output and this code would be more readable if you had two branches. If this test failed and I had to look at it without having seen it before, it would take me some time to parse out all of the boolean conditions to actually understand the failure, which is not ideal in a test.

If you wanted to leave one branch, I think the failure output would be clearer if you replaced expected err != nil: %v with expected err %s nil: and a branch that set either == or != as the replacement for %s.

@perotinus
Copy link
Contributor

/lgtm

@nikhiljindal
Copy link
Contributor Author

Thanks for the review @perotinus

Updated as per comments and squashed commits.
Will merge once tests pass

@nikhiljindal nikhiljindal merged commit 4b94013 into GoogleCloudPlatform:master Mar 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants