Skip to content
This repository was archived by the owner on Sep 16, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class ITSystemTest {
@BeforeClass
public static void beforeClass() throws Exception {
client = ClusterManagerClient.create();
Util.cleanUpExistingInstanceCluster(PROJECT_ID, ZONE, client);

/** create node pool* */
NodePool nodePool =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2022 Google LLC
*
* 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.google.cloud.container.v1.it;

import com.google.cloud.container.v1.ClusterManagerClient;
import com.google.container.v1.Cluster;
import com.google.container.v1.ListClustersResponse;
import java.io.IOException;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class Util {
// Cleans existing test resources if any.
private static final int DELETION_THRESHOLD_TIME_HOURS = 24;

/** tear down any clusters that are older than 24 hours * */
Comment thread
xtineskim marked this conversation as resolved.
public static void cleanUpExistingInstanceCluster(
String projectId, String zone, ClusterManagerClient client)
throws IOException, ExecutionException, InterruptedException {

ListClustersResponse clustersResponse = client.listClusters(projectId, zone);
List<Cluster> clusters = clustersResponse.getClustersList();

for (Cluster cluster : clusters) {
if (isCreatedBeforeThresholdTime(cluster.getCreateTime())) {
client.deleteCluster(projectId, zone, cluster.getName());
Comment thread
xtineskim marked this conversation as resolved.
}
}
}

private static boolean isCreatedBeforeThresholdTime(String timestamp) {
return OffsetDateTime.parse(timestamp)
.toInstant()
.isBefore(Instant.now().minus(DELETION_THRESHOLD_TIME_HOURS, ChronoUnit.HOURS));
}
}