-
Notifications
You must be signed in to change notification settings - Fork 809
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gce): Add StatefullyUpdateBootDisk{Task,Stage} (#3007)
* feat(gce): Add StatefullyUpdateBootDisk{Task,Stage} * style(google): add missing @nonnull * style(gce): Fix copyright headers
- Loading branch information
Showing
7 changed files
with
279 additions
and
53 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
23 changes: 10 additions & 13 deletions
23
...y/com/netflix/spinnaker/orca/clouddriver/pipeline/providers/gce/SetStatefulDiskStage.java
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
57 changes: 57 additions & 0 deletions
57
...lix/spinnaker/orca/clouddriver/pipeline/providers/gce/StatefullyUpdateBootImageStage.java
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,57 @@ | ||
/* | ||
* Copyright 2019 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.orca.clouddriver.pipeline.providers.gce; | ||
|
||
import com.netflix.spinnaker.kork.dynamicconfig.DynamicConfigService; | ||
import com.netflix.spinnaker.orca.clouddriver.tasks.providers.gce.StatefullyUpdateBootImageTask; | ||
import com.netflix.spinnaker.orca.clouddriver.tasks.servergroup.ServerGroupCacheForceRefreshTask; | ||
import com.netflix.spinnaker.orca.pipeline.StageDefinitionBuilder; | ||
import com.netflix.spinnaker.orca.pipeline.TaskNode; | ||
import com.netflix.spinnaker.orca.pipeline.model.Stage; | ||
import javax.annotation.Nonnull; | ||
import lombok.Data; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public final class StatefullyUpdateBootImageStage implements StageDefinitionBuilder { | ||
|
||
private final DynamicConfigService dynamicConfigService; | ||
|
||
@Autowired | ||
StatefullyUpdateBootImageStage(DynamicConfigService dynamicConfigService) { | ||
this.dynamicConfigService = dynamicConfigService; | ||
} | ||
|
||
@Override | ||
public void taskGraph(@Nonnull Stage stage, TaskNode.Builder builder) { | ||
builder.withTask("statefullyUpdateBootDisk", StatefullyUpdateBootImageTask.class); | ||
|
||
if (isForceCacheRefreshEnabled(dynamicConfigService)) { | ||
builder.withTask("forceCacheRefresh", ServerGroupCacheForceRefreshTask.class); | ||
} | ||
} | ||
|
||
@Data | ||
public static class StageData { | ||
|
||
public String accountName; | ||
public String serverGroupName; | ||
public String region; | ||
public String bootImage; | ||
} | ||
} |
23 changes: 10 additions & 13 deletions
23
...roovy/com/netflix/spinnaker/orca/clouddriver/tasks/providers/gce/SetStatefulDiskTask.java
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
95 changes: 95 additions & 0 deletions
95
...netflix/spinnaker/orca/clouddriver/tasks/providers/gce/StatefullyUpdateBootImageTask.java
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,95 @@ | ||
/* | ||
* Copyright 2019 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.orca.clouddriver.tasks.providers.gce; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Preconditions.checkState; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.netflix.spinnaker.orca.ExecutionStatus; | ||
import com.netflix.spinnaker.orca.TaskResult; | ||
import com.netflix.spinnaker.orca.clouddriver.KatoService; | ||
import com.netflix.spinnaker.orca.clouddriver.model.TaskId; | ||
import com.netflix.spinnaker.orca.clouddriver.pipeline.providers.gce.StatefullyUpdateBootImageStage.StageData; | ||
import com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.support.TargetServerGroup; | ||
import com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.support.TargetServerGroupResolver; | ||
import com.netflix.spinnaker.orca.clouddriver.tasks.AbstractCloudProviderAwareTask; | ||
import com.netflix.spinnaker.orca.pipeline.model.Stage; | ||
import java.util.List; | ||
import java.util.Map; | ||
import javax.annotation.Nonnull; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import rx.Observable; | ||
|
||
@Component | ||
public final class StatefullyUpdateBootImageTask extends AbstractCloudProviderAwareTask { | ||
|
||
private static final String KATO_OP_NAME = "statefullyUpdateBootImage"; | ||
|
||
private final KatoService katoService; | ||
private final TargetServerGroupResolver resolver; | ||
|
||
@Autowired | ||
StatefullyUpdateBootImageTask(KatoService katoService, TargetServerGroupResolver resolver) { | ||
this.katoService = katoService; | ||
this.resolver = resolver; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public TaskResult execute(@Nonnull Stage stage) { | ||
StageData data = stage.mapTo(StageData.class); | ||
|
||
List<TargetServerGroup> resolvedServerGroups = resolver.resolve(stage); | ||
checkArgument( | ||
resolvedServerGroups.size() > 0, | ||
"Could not find a server group named %s for %s in %s", | ||
data.serverGroupName, | ||
data.accountName, | ||
data.region); | ||
checkState( | ||
resolvedServerGroups.size() == 1, | ||
"Found multiple server groups named %s for %s in %s", | ||
data.serverGroupName, | ||
data.accountName, | ||
data.region); | ||
TargetServerGroup serverGroup = resolvedServerGroups.get(0); | ||
|
||
ImmutableMap<String, String> opData = | ||
ImmutableMap.of( | ||
"credentials", getCredentials(stage), | ||
"serverGroupName", serverGroup.getName(), | ||
"region", data.getRegion(), | ||
"bootImage", data.bootImage); | ||
|
||
Map<String, Map> operation = ImmutableMap.of(KATO_OP_NAME, opData); | ||
Observable<TaskId> observable = | ||
katoService.requestOperations(getCloudProvider(stage), ImmutableList.of(operation)); | ||
observable.toBlocking().first(); | ||
|
||
ImmutableMap<String, ImmutableList<String>> modifiedServerGroups = | ||
ImmutableMap.of(data.getRegion(), ImmutableList.of(serverGroup.getName())); | ||
ImmutableMap<String, Object> context = | ||
ImmutableMap.of( | ||
"notification.type", KATO_OP_NAME.toLowerCase(), | ||
"serverGroupName", serverGroup.getName(), | ||
"deploy.server.groups", modifiedServerGroups); | ||
return TaskResult.builder(ExecutionStatus.SUCCEEDED).context(context).build(); | ||
} | ||
} |
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
84 changes: 84 additions & 0 deletions
84
...netflix/spinnaker/orca/clouddriver/tasks/providers/gce/StatefullyUpdateBootImageTest.java
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,84 @@ | ||
/* | ||
* Copyright 2019 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.orca.clouddriver.tasks.providers.gce; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.netflix.spinnaker.orca.TaskResult; | ||
import com.netflix.spinnaker.orca.clouddriver.KatoService; | ||
import com.netflix.spinnaker.orca.clouddriver.model.TaskId; | ||
import com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.support.TargetServerGroup; | ||
import com.netflix.spinnaker.orca.clouddriver.pipeline.servergroup.support.TargetServerGroupResolver; | ||
import com.netflix.spinnaker.orca.pipeline.model.Stage; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import rx.Observable; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
final class StatefullyUpdateBootImageTest { | ||
|
||
private StatefullyUpdateBootImageTask task; | ||
|
||
@Mock private KatoService katoService; | ||
@Mock private TargetServerGroupResolver resolver; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
task = new StatefullyUpdateBootImageTask(katoService, resolver); | ||
} | ||
|
||
@Test | ||
void success() { | ||
when(resolver.resolve(any())) | ||
.thenReturn( | ||
ImmutableList.of(new TargetServerGroup(ImmutableMap.of("name", "testapp-v000")))); | ||
when(katoService.requestOperations(any(), any())) | ||
.thenReturn(Observable.just(new TaskId("10111"))); | ||
|
||
Stage stage = new Stage(); | ||
stage.getContext().put("cloudProvider", "gce"); | ||
stage.getContext().put("credentials", "spinnaker-test"); | ||
stage.getContext().put("serverGroupName", "testapp-v000"); | ||
stage.getContext().put("region", "us-desertoasis1"); | ||
stage.getContext().put("bootImage", "new-kool-os"); | ||
|
||
TaskResult result = task.execute(stage); | ||
|
||
ImmutableMap<String, String> operationParams = | ||
ImmutableMap.of( | ||
"credentials", "spinnaker-test", | ||
"serverGroupName", "testapp-v000", | ||
"region", "us-desertoasis1", | ||
"bootImage", "new-kool-os"); | ||
verify(katoService) | ||
.requestOperations( | ||
"gce", ImmutableList.of(ImmutableMap.of("statefullyUpdateBootImage", operationParams))); | ||
|
||
assertThat(result.getContext().get("notification.type")).isEqualTo("statefullyupdatebootimage"); | ||
assertThat(result.getContext().get("serverGroupName")).isEqualTo("testapp-v000"); | ||
assertThat(result.getContext().get("deploy.server.groups")) | ||
.isEqualTo(ImmutableMap.of("us-desertoasis1", ImmutableList.of("testapp-v000"))); | ||
} | ||
} |