Skip to content

Commit

Permalink
refactor(gce): introduce a WaitableComputeOperation (#3715)
Browse files Browse the repository at this point in the history
* refactor(gce): move the GoogleServerGroupManagers and related classes

* refactor(gce): introduce a WaitableComputeOperation
  • Loading branch information
plumpy authored and jtk54 committed May 23, 2019
1 parent 4295373 commit 55b8da0
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.netflix.spinnaker.clouddriver.google.deploy.instancegroups;
package com.netflix.spinnaker.clouddriver.google.compute;

import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.services.compute.ComputeRequest;
Expand Down Expand Up @@ -26,16 +26,16 @@ public abstract class AbstractGoogleServerGroupManagers implements GoogleServerG
}

@Override
public Operation abandonInstances(List<String> instances) throws IOException {
return timeExecute(performAbandonInstances(instances), "abandonInstances");
public WaitableComputeOperation abandonInstances(List<String> instances) throws IOException {
return wrapOperation(timeExecute(performAbandonInstances(instances), "abandonInstances"));
}

abstract ComputeRequest<Operation> performAbandonInstances(List<String> instances)
throws IOException;

@Override
public Operation delete() throws IOException {
return timeExecute(performDelete(), "delete");
public WaitableComputeOperation delete() throws IOException {
return wrapOperation(timeExecute(performDelete(), "delete"));
}

abstract ComputeRequest<Operation> performDelete() throws IOException;
Expand All @@ -48,12 +48,14 @@ public InstanceGroupManager get() throws IOException {
abstract ComputeRequest<InstanceGroupManager> performGet() throws IOException;

@Override
public Operation update(InstanceGroupManager content) throws IOException {
return timeExecute(performUpdate(content), "update");
public WaitableComputeOperation update(InstanceGroupManager content) throws IOException {
return wrapOperation(timeExecute(performUpdate(content), "update"));
}

abstract ComputeRequest<Operation> performUpdate(InstanceGroupManager content) throws IOException;

abstract WaitableComputeOperation wrapOperation(Operation operation);

private <T> T timeExecute(AbstractGoogleClientRequest<T> request, String api) throws IOException {
return GoogleExecutor.timeExecute(
registry,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.netflix.spinnaker.clouddriver.google.deploy.instancegroups;
package com.netflix.spinnaker.clouddriver.google.compute;

import com.google.api.services.compute.Compute.InstanceGroupManagers;
import com.google.api.services.compute.Compute.RegionInstanceGroupManagers;
import com.google.api.services.compute.model.InstanceGroupManager;
import com.google.api.services.compute.model.Operation;
import com.netflix.spinnaker.clouddriver.google.model.GoogleServerGroup;
import java.io.IOException;
import java.util.List;
Expand All @@ -14,13 +13,11 @@
*/
public interface GoogleServerGroupManagers {

Operation abandonInstances(List<String> instances) throws IOException;
WaitableComputeOperation abandonInstances(List<String> instances) throws IOException;

Operation delete() throws IOException;
WaitableComputeOperation delete() throws IOException;

InstanceGroupManager get() throws IOException;

Operation update(InstanceGroupManager content) throws IOException;

GoogleServerGroupOperationPoller getOperationPoller();
WaitableComputeOperation update(InstanceGroupManager content) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.netflix.spinnaker.clouddriver.google.deploy.instancegroups;
package com.netflix.spinnaker.clouddriver.google.compute;

import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.clouddriver.google.deploy.GoogleOperationPoller;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.netflix.spinnaker.clouddriver.google.deploy.instancegroups;
package com.netflix.spinnaker.clouddriver.google.compute;

import com.google.api.services.compute.model.Operation;
import com.netflix.spinnaker.clouddriver.data.task.Task;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.netflix.spinnaker.clouddriver.google.deploy.instancegroups;
package com.netflix.spinnaker.clouddriver.google.compute;

import com.google.api.services.compute.Compute;
import com.google.api.services.compute.ComputeRequest;
Expand All @@ -7,7 +7,6 @@
import com.google.api.services.compute.model.RegionInstanceGroupManagersAbandonInstancesRequest;
import com.google.common.collect.ImmutableList;
import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.clouddriver.data.task.Task;
import com.netflix.spinnaker.clouddriver.google.GoogleExecutor;
import com.netflix.spinnaker.clouddriver.google.deploy.GoogleOperationPoller;
import com.netflix.spinnaker.clouddriver.google.security.GoogleNamedAccountCredentials;
Expand All @@ -17,7 +16,7 @@
class RegionGoogleServerGroupManagers extends AbstractGoogleServerGroupManagers {

private final Compute.RegionInstanceGroupManagers managers;
private final RegionGoogleServerGroupOperationPoller operationPoller;
private final GoogleOperationPoller operationPoller;
private final String region;

RegionGoogleServerGroupManagers(
Expand All @@ -28,7 +27,7 @@ class RegionGoogleServerGroupManagers extends AbstractGoogleServerGroupManagers
String region) {
super(credentials, registry, instanceGroupName);
this.managers = credentials.getCompute().regionInstanceGroupManagers();
this.operationPoller = new RegionGoogleServerGroupOperationPoller(operationPoller);
this.operationPoller = operationPoller;
this.region = region;
}

Expand Down Expand Up @@ -57,8 +56,8 @@ ComputeRequest<Operation> performUpdate(InstanceGroupManager content) throws IOE
}

@Override
public GoogleServerGroupOperationPoller getOperationPoller() {
return operationPoller;
WaitableComputeOperation wrapOperation(Operation operation) {
return new RegionalOperation(operation, getCredentials(), operationPoller);
}

@Override
Expand All @@ -74,25 +73,4 @@ List<String> getRegionOrZoneTags() {
GoogleExecutor.getTAG_REGION(),
region);
}

private class RegionGoogleServerGroupOperationPoller extends GoogleServerGroupOperationPoller {

private RegionGoogleServerGroupOperationPoller(GoogleOperationPoller poller) {
super(poller);
}

@Override
public void waitForOperation(Operation operation, Long timeout, Task task, String phase) {
getPoller()
.waitForRegionalOperation(
getCredentials().getCompute(),
getProject(),
region,
operation.getName(),
timeout,
task,
"regional instance group " + getInstanceGroupName(),
phase);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.netflix.spinnaker.clouddriver.google.compute;

import com.google.api.services.compute.model.Operation;
import com.netflix.spinnaker.clouddriver.data.task.Task;
import com.netflix.spinnaker.clouddriver.google.deploy.GCEUtil;
import com.netflix.spinnaker.clouddriver.google.deploy.GoogleOperationPoller;
import com.netflix.spinnaker.clouddriver.google.security.GoogleNamedAccountCredentials;

class RegionalOperation implements WaitableComputeOperation {

private final Operation operation;
private final GoogleNamedAccountCredentials credentials;
private final GoogleOperationPoller poller;

RegionalOperation(
Operation operation,
GoogleNamedAccountCredentials credentials,
GoogleOperationPoller poller) {
this.operation = operation;
this.credentials = credentials;
this.poller = poller;
}

@Override
public Operation waitForDone(Task task, String phase) {
return poller.waitForRegionalOperation(
credentials.getCompute(),
credentials.getProject(),
operation.getRegion(),
operation.getName(),
/* timeoutSeconds= */ null,
task,
GCEUtil.getLocalName(operation.getTargetLink()),
phase);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.netflix.spinnaker.clouddriver.google.compute;

import com.google.api.services.compute.model.Operation;
import com.netflix.spinnaker.clouddriver.data.task.Task;

public interface WaitableComputeOperation {

Operation waitForDone(Task task, String phase);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.netflix.spinnaker.clouddriver.google.compute;

import com.google.api.services.compute.model.Operation;
import com.netflix.spinnaker.clouddriver.data.task.Task;
import com.netflix.spinnaker.clouddriver.google.deploy.GCEUtil;
import com.netflix.spinnaker.clouddriver.google.deploy.GoogleOperationPoller;
import com.netflix.spinnaker.clouddriver.google.security.GoogleNamedAccountCredentials;

class ZonalOperation implements WaitableComputeOperation {

private final Operation operation;
private final GoogleNamedAccountCredentials credentials;
private final GoogleOperationPoller poller;

ZonalOperation(
Operation operation,
GoogleNamedAccountCredentials credentials,
GoogleOperationPoller poller) {
this.operation = operation;
this.credentials = credentials;
this.poller = poller;
}

@Override
public Operation waitForDone(Task task, String phase) {
return poller.waitForZonalOperation(
credentials.getCompute(),
credentials.getProject(),
GCEUtil.getLocalName(operation.getZone()),
operation.getName(),
/* timeoutSeconds= */ null,
task,
GCEUtil.getLocalName(operation.getTargetLink()),
phase);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.netflix.spinnaker.clouddriver.google.deploy.instancegroups;
package com.netflix.spinnaker.clouddriver.google.compute;

import com.google.api.services.compute.Compute;
import com.google.api.services.compute.ComputeRequest;
Expand All @@ -7,7 +7,6 @@
import com.google.api.services.compute.model.Operation;
import com.google.common.collect.ImmutableList;
import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.clouddriver.data.task.Task;
import com.netflix.spinnaker.clouddriver.google.GoogleExecutor;
import com.netflix.spinnaker.clouddriver.google.deploy.GoogleOperationPoller;
import com.netflix.spinnaker.clouddriver.google.security.GoogleNamedAccountCredentials;
Expand All @@ -17,7 +16,7 @@
class ZoneGoogleServerGroupManagers extends AbstractGoogleServerGroupManagers {

private final Compute.InstanceGroupManagers managers;
private final ZoneGoogleServerGroupOperationPoller operationPoller;
private final GoogleOperationPoller operationPoller;
private final String zone;

ZoneGoogleServerGroupManagers(
Expand All @@ -28,7 +27,7 @@ class ZoneGoogleServerGroupManagers extends AbstractGoogleServerGroupManagers {
String zone) {
super(credentials, registry, instanceGroupName);
this.managers = credentials.getCompute().instanceGroupManagers();
this.operationPoller = new ZoneGoogleServerGroupOperationPoller(operationPoller);
this.operationPoller = operationPoller;
this.zone = zone;
}

Expand Down Expand Up @@ -57,8 +56,8 @@ ComputeRequest<Operation> performUpdate(InstanceGroupManager content) throws IOE
}

@Override
public GoogleServerGroupOperationPoller getOperationPoller() {
return operationPoller;
WaitableComputeOperation wrapOperation(Operation operation) {
return new ZonalOperation(operation, getCredentials(), operationPoller);
}

@Override
Expand All @@ -74,25 +73,4 @@ List<String> getRegionOrZoneTags() {
GoogleExecutor.getTAG_ZONE(),
zone);
}

private class ZoneGoogleServerGroupOperationPoller extends GoogleServerGroupOperationPoller {

private ZoneGoogleServerGroupOperationPoller(GoogleOperationPoller poller) {
super(poller);
}

@Override
public void waitForOperation(Operation operation, Long timeout, Task task, String phase) {
getPoller()
.waitForZonalOperation(
getCredentials().getCompute(),
getProject(),
zone,
operation.getName(),
timeout,
task,
"zonal instance group " + getInstanceGroupName(),
phase);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.netflix.spinnaker.clouddriver.google.deploy.converters;

import com.netflix.spinnaker.clouddriver.google.GoogleOperation;
import com.netflix.spinnaker.clouddriver.google.compute.GoogleServerGroupManagersFactory;
import com.netflix.spinnaker.clouddriver.google.deploy.description.SetStatefulDiskDescription;
import com.netflix.spinnaker.clouddriver.google.deploy.instancegroups.GoogleServerGroupManagersFactory;
import com.netflix.spinnaker.clouddriver.google.deploy.ops.SetStatefulDiskAtomicOperation;
import com.netflix.spinnaker.clouddriver.google.provider.view.GoogleClusterProvider;
import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package com.netflix.spinnaker.clouddriver.google.deploy.ops
import com.netflix.spinnaker.clouddriver.data.task.Task
import com.netflix.spinnaker.clouddriver.data.task.TaskRepository
import com.netflix.spinnaker.clouddriver.google.deploy.GCEUtil
import com.netflix.spinnaker.clouddriver.google.deploy.instancegroups.GoogleServerGroupManagersFactory
import com.netflix.spinnaker.clouddriver.google.compute.GoogleServerGroupManagersFactory
import com.netflix.spinnaker.clouddriver.google.deploy.description.AbandonAndDecrementGoogleServerGroupDescription
import com.netflix.spinnaker.clouddriver.google.provider.view.GoogleClusterProvider
import org.springframework.beans.factory.annotation.Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.netflix.spinnaker.clouddriver.google.deploy.GCEUtil
import com.netflix.spinnaker.clouddriver.google.deploy.GoogleOperationPoller
import com.netflix.spinnaker.clouddriver.google.deploy.SafeRetry
import com.netflix.spinnaker.clouddriver.google.deploy.description.DestroyGoogleServerGroupDescription
import com.netflix.spinnaker.clouddriver.google.deploy.instancegroups.GoogleServerGroupManagersFactory
import com.netflix.spinnaker.clouddriver.google.compute.GoogleServerGroupManagersFactory
import com.netflix.spinnaker.clouddriver.google.model.GoogleServerGroup
import com.netflix.spinnaker.clouddriver.google.provider.view.GoogleClusterProvider
import com.netflix.spinnaker.clouddriver.google.provider.view.GoogleLoadBalancerProvider
Expand Down Expand Up @@ -245,7 +245,7 @@ class DestroyGoogleServerGroupAtomicOperation extends GoogleAtomicOperation<Void

task.updateStatus BASE_PHASE, "Waiting on delete operation for managed instance group..."

serverGroupManagers.operationPoller.waitForOperation(deleteOperation, /* timeout= */ null, task, BASE_PHASE)
deleteOperation.waitForDone(task, BASE_PHASE)
null
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.netflix.spinnaker.clouddriver.google.deploy.ops;

import com.google.api.services.compute.model.InstanceGroupManager;
import com.google.api.services.compute.model.Operation;
import com.google.api.services.compute.model.StatefulPolicy;
import com.google.api.services.compute.model.StatefulPolicyPreservedState;
import com.google.api.services.compute.model.StatefulPolicyPreservedStateDiskDevice;
import com.google.common.annotations.VisibleForTesting;
import com.netflix.spinnaker.clouddriver.data.task.Task;
import com.netflix.spinnaker.clouddriver.data.task.TaskRepository;
import com.netflix.spinnaker.clouddriver.google.compute.GoogleServerGroupManagers;
import com.netflix.spinnaker.clouddriver.google.compute.GoogleServerGroupManagersFactory;
import com.netflix.spinnaker.clouddriver.google.compute.WaitableComputeOperation;
import com.netflix.spinnaker.clouddriver.google.deploy.GCEUtil;
import com.netflix.spinnaker.clouddriver.google.deploy.description.SetStatefulDiskDescription;
import com.netflix.spinnaker.clouddriver.google.deploy.instancegroups.GoogleServerGroupManagers;
import com.netflix.spinnaker.clouddriver.google.deploy.instancegroups.GoogleServerGroupManagersFactory;
import com.netflix.spinnaker.clouddriver.google.model.GoogleServerGroup;
import com.netflix.spinnaker.clouddriver.google.provider.view.GoogleClusterProvider;
import java.io.IOException;
Expand Down Expand Up @@ -77,14 +77,11 @@ public Void operate(List priorOutputs) {

task.updateStatus(BASE_PHASE, "Storing updated instance group definition");

Operation operation = managers.update(instanceGroupManager);
WaitableComputeOperation operation = managers.update(instanceGroupManager);

task.updateStatus(BASE_PHASE, "Waiting for update to complete");

managers
.getOperationPoller()
.waitForOperation(
operation, /* timeout= */ null, TaskRepository.threadLocalTask.get(), BASE_PHASE);
operation.waitForDone(TaskRepository.threadLocalTask.get(), BASE_PHASE);

return null;
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spinnaker.clouddriver.google.compute.GoogleServerGroupManagersFactory;
import com.netflix.spinnaker.clouddriver.google.deploy.description.SetStatefulDiskDescription;
import com.netflix.spinnaker.clouddriver.google.deploy.instancegroups.GoogleServerGroupManagersFactory;
import com.netflix.spinnaker.clouddriver.google.deploy.ops.SetStatefulDiskAtomicOperation;
import com.netflix.spinnaker.clouddriver.google.provider.view.GoogleClusterProvider;
import com.netflix.spinnaker.clouddriver.google.security.FakeGoogleCredentials;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.netflix.spinnaker.clouddriver.data.task.TaskRepository
import com.netflix.spinnaker.clouddriver.google.deploy.GCEUtil
import com.netflix.spinnaker.clouddriver.google.deploy.GoogleOperationPoller
import com.netflix.spinnaker.clouddriver.google.deploy.description.AbandonAndDecrementGoogleServerGroupDescription
import com.netflix.spinnaker.clouddriver.google.deploy.instancegroups.GoogleServerGroupManagersFactory
import com.netflix.spinnaker.clouddriver.google.compute.GoogleServerGroupManagersFactory
import com.netflix.spinnaker.clouddriver.google.model.GoogleInstance
import com.netflix.spinnaker.clouddriver.google.model.GoogleServerGroup
import com.netflix.spinnaker.clouddriver.google.provider.view.GoogleClusterProvider
Expand Down
Loading

0 comments on commit 55b8da0

Please sign in to comment.