Skip to content
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

feat(deleteSnapshot): Adding deleteSnapshot stage and deleteSnapshot … #2769

Merged
merged 9 commits into from
Apr 8, 2019
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2019 Netflix, 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.snapshot;

import java.util.Set;
import javax.validation.constraints.NotNull;
import com.netflix.spinnaker.orca.clouddriver.tasks.MonitorKatoTask;
import com.netflix.spinnaker.orca.clouddriver.tasks.snapshot.DeleteSnapshotTask;
import com.netflix.spinnaker.orca.pipeline.StageDefinitionBuilder;
import com.netflix.spinnaker.orca.pipeline.TaskNode;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import org.springframework.stereotype.Component;

@Component
public class DeleteSnapshotStage implements StageDefinitionBuilder {
@Override
public void taskGraph(@NotNull Stage stage, @NotNull TaskNode.Builder builder) {
builder
.withTask("deleteSnapshot", DeleteSnapshotTask.class)
.withTask("monitorDeleteSnapshot", MonitorKatoTask.class);
}

public static class DeleteSnapshotRequest {
@NotNull
private String credentials;

@NotNull
private String cloudProvider;

@NotNull
private String region;

@NotNull
private Set<String> snapshotIds;

public String getCredentials() {
return credentials;
}

public void setCredentials(String credentials) {
this.credentials = credentials;
}

public String getCloudProvider() {
return cloudProvider;
}

public void setCloudProvider(String cloudProvider) {
this.cloudProvider = cloudProvider;
}

public String getRegion() {
return region;
}

public void setRegion(String region) {
this.region = region;
}

public Set<String> getSnapshotIds() {
return snapshotIds;
}

public void setSnapshotIds(Set<String> snapshotIds) {
this.snapshotIds = snapshotIds;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2019 Netflix, 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.snapshot;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import com.netflix.spinnaker.orca.ExecutionStatus;
import com.netflix.spinnaker.orca.RetryableTask;
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.snapshot.DeleteSnapshotStage;
import com.netflix.spinnaker.orca.clouddriver.tasks.AbstractCloudProviderAwareTask;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import static java.util.stream.Collectors.toList;

@Component
public class DeleteSnapshotTask extends AbstractCloudProviderAwareTask implements RetryableTask {
private final KatoService katoService;

@Autowired
public DeleteSnapshotTask(KatoService katoService) {
this.katoService = katoService;
}

@Override
public TaskResult execute(@Nonnull Stage stage) {
DeleteSnapshotStage.DeleteSnapshotRequest deleteSnapshotRequest = stage.mapTo(DeleteSnapshotStage.DeleteSnapshotRequest.class);

List<Map<String, Map>> operations = deleteSnapshotRequest
.getSnapshotIds()
.stream()
.map(snapshotId -> {
Map<String, Object> operation = new HashMap<>();
operation.put("credentials", deleteSnapshotRequest.getCredentials());
operation.put("region", deleteSnapshotRequest.getRegion());
operation.put("snapshotId", snapshotId);
return Collections.<String, Map>singletonMap("deleteSnapshot", operation);
}
).collect(toList());

TaskId taskId = katoService
.requestOperations(deleteSnapshotRequest.getCloudProvider(), operations).toBlocking().first();

Map<String, Object> outputs = new HashMap<>();
outputs.put("notification.type", "deleteSnapshot");
outputs.put("kato.last.task.id", taskId);
Copy link
Contributor

Choose a reason for hiding this comment

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

After some thought I think we should have a monitor task that monitors the keto task for no longer than a minute or two. I'd like you to check the status of the task and reflect if we couldn't delete the snapshot because it's still in use.

Copy link
Contributor Author

@aravindmd aravindmd Mar 18, 2019

Choose a reason for hiding this comment

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

Added a generic monitor keto task to the stage

outputs.put("delete.region", deleteSnapshotRequest.getRegion());
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure why these need to be in outputs?

I'm cautious about adding things to outputs unless there's a legitimate downstream need to have access to them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had a chat with @ajordens about this and concluded that we need this in here

Copy link
Contributor

Choose a reason for hiding this comment

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

(I think the conclusion was that I still didn't think it was needed but that it was unlikely to cause any problems)

outputs.put("delete.account.name", deleteSnapshotRequest.getCredentials());

return new TaskResult(ExecutionStatus.SUCCEEDED, outputs);
}

@Override
public long getBackoffPeriod() {
return TimeUnit.SECONDS.toMillis(10);
}

@Override
public long getTimeout() {
return TimeUnit.MINUTES.toMillis(2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2019 Netflix, 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.snapshot

import com.netflix.spinnaker.orca.ExecutionStatus
import com.netflix.spinnaker.orca.clouddriver.KatoService
import com.netflix.spinnaker.orca.clouddriver.model.TaskId
import com.netflix.spinnaker.orca.pipeline.model.Execution
import com.netflix.spinnaker.orca.pipeline.model.Stage
import spock.lang.Specification

class DeleteSnapshotTaskSpec extends Specification {

def "Should delete a snapshot"() {
given:
def context = [
cloudProvider: "aws",
credentials : "test",
region : "us-east-1",
snapshotIds : ["snap-08e97a12bceb0b750"]
]

def stage = new Stage(Execution.newPipeline("orca"), "deleteSnapshot", context)

and:
List<Map> operations = []
def katoService = Mock(KatoService) {
1 * requestOperations("aws", _) >> {
operations = it[1]
rx.Observable.from(new TaskId(UUID.randomUUID().toString()))
}
}
def task = new DeleteSnapshotTask(katoService)

when:
def result = task.execute(stage)

then:
operations.size() == 1
operations[0].deleteSnapshot.snapshotId == stage.context.snapshotIds[0]
result.status == ExecutionStatus.SUCCEEDED
}
}