-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from spinnaker/kato_clouddriver_rebase
Kato clouddriver rebase
- Loading branch information
Showing
32 changed files
with
493 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 0 additions & 105 deletions
105
kato/kato-gce/src/main/groovy/com/netflix/spinnaker/kato/gce/deploy/GCEOperationUtil.groovy
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...to-gce/src/main/groovy/com/netflix/spinnaker/kato/gce/deploy/GoogleOperationPoller.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright 2015 Google, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.kato.gce.deploy | ||
|
||
import com.google.api.services.compute.Compute | ||
import com.google.api.services.compute.model.Operation | ||
import com.google.api.services.replicapool.Replicapool | ||
import com.netflix.spinnaker.kato.data.task.Task | ||
import com.netflix.spinnaker.kato.gce.deploy.config.GoogleConfig | ||
import com.netflix.spinnaker.kato.gce.deploy.exception.GoogleOperationException | ||
import com.netflix.spinnaker.kato.gce.deploy.exception.GoogleOperationTimedOutException | ||
import com.netflix.spinnaker.kato.gce.deploy.exception.GoogleResourceNotFoundException | ||
import org.springframework.beans.factory.annotation.Autowired | ||
|
||
class GoogleOperationPoller { | ||
|
||
// This only exists to facilitate testing. | ||
static class ThreadSleeper { | ||
void sleep(long seconds) { | ||
Thread.currentThread().sleep(seconds * 1000) | ||
} | ||
} | ||
|
||
@Autowired | ||
GoogleConfig.GoogleConfigurationProperties googleConfigurationProperties | ||
|
||
private ThreadSleeper threadSleeper = new ThreadSleeper() | ||
|
||
// The methods below are used to wait on the operation specified in |operationName|. This is used in practice to | ||
// turn the asynchronous GCE client operations into synchronous calls. Will poll the state of the operation until | ||
// either state is DONE or |timeoutSeconds| is reached. | ||
Operation waitForRegionalOperation(Compute compute, String projectName, String region, String operationName, | ||
Long timeoutSeconds, Task task, String resourceString, String basePhase) { | ||
return handleFinishedAsyncOperation( | ||
waitForOperation({compute.regionOperations().get(projectName, region, operationName).execute()}, | ||
getTimeout(timeoutSeconds)), task, resourceString, basePhase) | ||
} | ||
|
||
Operation waitForGlobalOperation(Compute compute, String projectName, String operationName, | ||
Long timeoutSeconds, Task task, String resourceString, String basePhase) { | ||
return handleFinishedAsyncOperation( | ||
waitForOperation({compute.globalOperations().get(projectName, operationName).execute()}, | ||
getTimeout(timeoutSeconds)), task, resourceString, basePhase) | ||
} | ||
|
||
// This method is like the two above except that it operates using a Replicapool object (rather than Compute), which | ||
// is the base class for operations relating to managed instance groups. | ||
void waitForZoneOperation(Replicapool replicapool, String projectName, String zone, String operationName, | ||
Long timeoutSeconds, Task task, String resourceString, String basePhase) { | ||
handleFinishedAsyncOperation( | ||
waitForOperation({replicapool.zoneOperations().get(projectName, zone, operationName).execute()}, | ||
getTimeout(timeoutSeconds)), task, resourceString, basePhase) | ||
} | ||
|
||
private long getTimeout(Long timeoutSeconds) { | ||
// Note that we cannot use an Elvis operator here because we might have a timeoutSeconds value of | ||
// zero. In that case, we still want to pass that value. So we use null comparison here instead. | ||
Math.max(timeoutSeconds != null ? timeoutSeconds : googleConfigurationProperties.asyncOperationTimeoutSecondsDefault, 0) | ||
} | ||
|
||
private static handleFinishedAsyncOperation(Operation operation, Task task, String resourceString, String basePhase) { | ||
if (!operation) { | ||
String errorMsg = "Operation on $resourceString timed out." | ||
task.updateStatus basePhase, errorMsg | ||
throw new GoogleOperationTimedOutException(errorMsg) | ||
} | ||
|
||
if (operation.getError()) { | ||
def error = operation?.getError()?.getErrors()?.get(0) | ||
String errorMsg = "Failed to complete operation on $resourceString with error: $error" | ||
task.updateStatus basePhase, errorMsg | ||
throw new GoogleOperationException(errorMsg) | ||
} | ||
|
||
task.updateStatus basePhase, "Done operating on $resourceString." | ||
} | ||
|
||
/* | ||
This method does not correct for potential drift at each interval (we trade some precision for readability). | ||
The timeoutSeconds parameter is really treated as a lower-bound. We will poll until the operation reaches a DONE | ||
state or until <em>at least</em> that many seconds have passed. | ||
*/ | ||
private Operation waitForOperation(Closure getOperation, long timeoutSeconds) { | ||
int totalTimePollingSeconds = 0 | ||
boolean timeoutExceeded = false | ||
|
||
// Fibonacci backoff in seconds, up to googleConfigurationProperties.asyncOperationMaxPollingIntervalSeconds interval. | ||
int pollInterval = 1 | ||
int pollIncrement = 0 | ||
|
||
while (!timeoutExceeded) { | ||
threadSleeper.sleep(pollInterval) | ||
|
||
totalTimePollingSeconds += pollInterval | ||
|
||
Operation operation = getOperation() | ||
|
||
if (operation.getStatus() == "DONE") { | ||
return operation | ||
} | ||
|
||
if (totalTimePollingSeconds > timeoutSeconds) { | ||
timeoutExceeded = true | ||
} else { | ||
// Update polling interval. | ||
int oldIncrement = pollIncrement | ||
pollIncrement = pollInterval | ||
pollInterval += oldIncrement | ||
pollInterval = Math.min(pollInterval, googleConfigurationProperties.asyncOperationMaxPollingIntervalSeconds) | ||
} | ||
} | ||
|
||
return null | ||
} | ||
} |
Oops, something went wrong.