Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 committed Dec 21, 2024
2 parents a34911f + f676fd2 commit bc6d0c6
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@

import org.assertj.core.api.Assertions;
import org.awaitility.Awaitility;
import reactor.netty.http.client.HttpClient;
import reactor.util.retry.Retry;
import reactor.util.retry.RetryBackoffSpec;

import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.cloud.kubernetes.commons.discovery.EndpointNameAndNamespace;
Expand All @@ -34,11 +31,12 @@
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.ResolvableType;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.builder;
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.retrySpec;

/**
* @author wind57
Expand Down Expand Up @@ -116,12 +114,4 @@ static void invokeAndAssert(Util util, Set<String> namespaces, int port, String

}

private static WebClient.Builder builder() {
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create()));
}

private static RetryBackoffSpec retrySpec() {
return Retry.fixedDelay(15, Duration.ofSeconds(1)).filter(Objects::nonNull);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
*/
class Fabric8DiscoveryAllServicesIT extends Fabric8DiscoveryBase {


private static Service externalServiceName;

@BeforeAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
@TestPropertySource(properties = { "spring.cloud.kubernetes.discovery.namespaces[0]=a-uat",
"spring.cloud.kubernetes.discovery.namespaces[1]=b-uat",
"spring.cloud.kubernetes.discovery.filter=#root.metadata.namespace matches 'a-uat$'",
"logging.level.org.springframework.cloud.kubernetes.fabric8.discovery=DEBUG"
})
"logging.level.org.springframework.cloud.kubernetes.fabric8.discovery=DEBUG" })
class Fabric8DiscoveryFilterMatchOneNamespaceIT extends Fabric8DiscoveryBase {

private static final String NAMESPACE_A_UAT = "a-uat";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
@TestPropertySource(properties = { "spring.cloud.kubernetes.discovery.namespaces[0]=a-uat",
"spring.cloud.kubernetes.discovery.namespaces[1]=b-uat",
"spring.cloud.kubernetes.discovery.filter=#root.metadata.namespace matches '^.*uat$'",
"logging.level.org.springframework.cloud.kubernetes.fabric8.discovery=DEBUG"
})
"logging.level.org.springframework.cloud.kubernetes.fabric8.discovery=DEBUG" })
class Fabric8DiscoveryFilterMatchTwoNamespacesIT extends Fabric8DiscoveryBase {

private static final String NAMESPACE_A_UAT = "a-uat";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,20 @@
import java.util.Map;
import java.util.Objects;

import reactor.netty.http.client.HttpClient;
import reactor.util.retry.Retry;
import reactor.util.retry.RetryBackoffSpec;

import org.springframework.boot.test.json.BasicJsonTester;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;

import static java.util.AbstractMap.SimpleEntry;
import static java.util.Map.Entry;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.builder;
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.retrySpec;
import static org.testcontainers.shaded.org.awaitility.Awaitility.await;

/**
Expand Down Expand Up @@ -321,12 +318,4 @@ private static void waitForLogStatement(CapturedOutput output, String message) {
.until(() -> output.getOut().contains(message));
}

private static WebClient.Builder builder() {
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create()));
}

private static RetryBackoffSpec retrySpec() {
return Retry.fixedDelay(15, Duration.ofSeconds(1)).filter(Objects::nonNull);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package org.springframework.cloud.kubernetes.fabric8.client.istio;

import java.io.InputStream;
import java.time.Duration;
import java.util.List;
import java.util.Objects;

import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.apps.Deployment;
Expand All @@ -31,18 +29,17 @@
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.Container;
import org.testcontainers.k3s.K3sContainer;
import reactor.netty.http.client.HttpClient;
import reactor.util.retry.Retry;
import reactor.util.retry.RetryBackoffSpec;

import org.springframework.cloud.kubernetes.integration.tests.commons.Commons;
import org.springframework.cloud.kubernetes.integration.tests.commons.Images;
import org.springframework.cloud.kubernetes.integration.tests.commons.Phase;
import org.springframework.cloud.kubernetes.integration.tests.commons.fabric8_client.Util;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;

import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.builder;
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.retrySpec;

/**
* @author wind57
*/
Expand Down Expand Up @@ -133,14 +130,6 @@ private static void appManifests(Phase phase) {

}

private WebClient.Builder builder() {
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create()));
}

private RetryBackoffSpec retrySpec() {
return Retry.fixedDelay(15, Duration.ofSeconds(1)).filter(Objects::nonNull);
}

private static String istioctlPodName() {
try {
return K3S
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.springframework.cloud.kubernetes.fabric8.client.reload.TestAssertions.builder;
import static org.springframework.cloud.kubernetes.fabric8.client.reload.TestAssertions.manifests;
import static org.springframework.cloud.kubernetes.fabric8.client.reload.TestAssertions.retrySpec;
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.builder;
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.retrySpec;

/**
* @author wind57
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.springframework.cloud.kubernetes.fabric8.client.reload.TestAssertions.builder;
import static org.springframework.cloud.kubernetes.fabric8.client.reload.TestAssertions.manifests;
import static org.springframework.cloud.kubernetes.fabric8.client.reload.TestAssertions.retrySpec;
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.builder;
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.retrySpec;

/**
* @author wind57
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.InputStream;
import java.time.Duration;
import java.util.Objects;

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.Secret;
Expand All @@ -27,15 +26,10 @@
import io.fabric8.kubernetes.api.model.networking.v1.Ingress;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.utils.Serialization;
import reactor.netty.http.client.HttpClient;
import reactor.util.retry.Retry;
import reactor.util.retry.RetryBackoffSpec;

import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.cloud.kubernetes.integration.tests.commons.Phase;
import org.springframework.cloud.kubernetes.integration.tests.commons.fabric8_client.Util;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;

import static org.testcontainers.shaded.org.awaitility.Awaitility.await;

Expand Down Expand Up @@ -116,12 +110,4 @@ static void manifests(Phase phase, Util util, String namespace) {

}

static WebClient.Builder builder() {
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create()));
}

static RetryBackoffSpec retrySpec() {
return Retry.fixedDelay(120, Duration.ofSeconds(2)).filter(Objects::nonNull);
}

}
4 changes: 4 additions & 0 deletions spring-cloud-kubernetes-test-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java-extended</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

Expand All @@ -38,10 +39,15 @@
import org.junit.jupiter.api.Assertions;
import org.testcontainers.containers.Container;
import org.testcontainers.k3s.K3sContainer;
import reactor.netty.http.client.HttpClient;
import reactor.util.retry.Retry;
import reactor.util.retry.RetryBackoffSpec;

import org.springframework.core.io.ClassPathResource;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.function.client.WebClient;

import static org.awaitility.Awaitility.await;
import static org.springframework.cloud.kubernetes.integration.tests.commons.Constants.KUBERNETES_VERSION_FILE;
Expand Down Expand Up @@ -264,6 +270,14 @@ public static void waitForLogStatement(String message, K3sContainer k3sContainer

}

public static WebClient.Builder builder() {
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create()));
}

public static RetryBackoffSpec retrySpec() {
return Retry.fixedDelay(15, Duration.ofSeconds(1)).filter(Objects::nonNull);
}

private static void loadImageFromPath(String tarName, K3sContainer container) {
await().atMost(Duration.ofMinutes(2)).pollInterval(Duration.ofSeconds(1)).until(() -> {
Container.ExecResult result = container.execInContainer("ctr", "i", "import", TMP_IMAGES + "/" + tarName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class FixedPortsK3sContainer extends K3sContainer {
* Command to use when starting rancher. Without "server" option, traefik is not
* installed
*/
private static final String RANCHER_COMMAND = "server";
private static final String RANCHER_COMMAND = "server --disable=metric-server";

static final K3sContainer CONTAINER = new FixedPortsK3sContainer(DockerImageName.parse(RANCHER_VERSION))
.configureFixedPorts()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import io.fabric8.kubernetes.api.model.APIService;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.fabric8.kubernetes.api.model.Pod;
Expand All @@ -31,7 +31,6 @@
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceAccount;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentList;
import io.fabric8.kubernetes.api.model.rbac.Role;
import io.fabric8.kubernetes.api.model.rbac.RoleBinding;
import io.fabric8.kubernetes.client.Config;
Expand All @@ -47,7 +46,6 @@
import org.springframework.cloud.kubernetes.integration.tests.commons.Phase;

import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.fail;
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.loadImage;
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.pomVersion;
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.pullImage;
Expand All @@ -69,6 +67,10 @@ public Util(K3sContainer container) {
.build();
}

public KubernetesClient client() {
return client;
}

/**
* This is the preferred method to use when creating a deployment alongside with a
* service. It creates the given resources as-well as waits for them to be created.
Expand Down Expand Up @@ -193,6 +195,21 @@ public void createNamespace(String name) {

public void deleteNamespace(String name) {
try {

// sometimes we get errors like :

// "message": "Discovery failed for some groups,
// 1 failing: unable to retrieve the complete list of server APIs:
// metrics.k8s.io/v1beta1: stale GroupVersion discovery: metrics.k8s.io/v1beta1"

// but even when it works OK, the finalizers are slowing down the deletion
List<APIService> apiServices = client.apiServices().list().getItems();
apiServices.stream()
.map(apiService -> apiService.getMetadata().getName())
.filter(apiServiceName -> apiServiceName.contains("metrics.k8s.io"))
.findFirst()
.ifPresent(apiServiceName -> client.apiServices().withName(apiServiceName).delete());

client.namespaces()
.resource(new NamespaceBuilder().withNewMetadata().withName(name).and().build())
.delete();
Expand Down Expand Up @@ -354,46 +371,6 @@ private boolean isDeploymentReady(String namespace, String deploymentName) {
return availableReplicas != null && availableReplicas >= 1;
}

private void waitForDeploymentAfterPatch(String deploymentName, String namespace, Map<String, String> labels) {
try {
await().pollDelay(Duration.ofSeconds(4))
.pollInterval(Duration.ofSeconds(3))
.atMost(60, TimeUnit.SECONDS)
.until(() -> isDeploymentReadyAfterPatch(deploymentName, namespace, labels));
}
catch (Exception e) {
throw new RuntimeException(e);
}

}

private boolean isDeploymentReadyAfterPatch(String deploymentName, String namespace, Map<String, String> labels) {

DeploymentList deployments = client.apps().deployments().inNamespace(namespace).list();

if (deployments.getItems().isEmpty()) {
fail("No deployment with name " + deploymentName);
}

Deployment deployment = deployments.getItems()
.stream()
.filter(x -> x.getMetadata().getName().equals(deploymentName))
.findFirst()
.orElseThrow();
// if no replicas are defined, it means only 1 is needed
int replicas = Optional.ofNullable(deployment.getSpec().getReplicas()).orElse(1);

int numberOfPods = client.pods().inNamespace(namespace).withLabels(labels).list().getItems().size();

if (numberOfPods != replicas) {
LOG.info("number of pods not yet stabilized");
return false;
}

return replicas == Optional.ofNullable(deployment.getStatus().getReadyReplicas()).orElse(0);

}

private void innerSetup(String namespace, InputStream serviceAccountAsStream, InputStream roleBindingAsStream,
InputStream roleAsStream) {
ServiceAccount serviceAccountFromStream = client.serviceAccounts()
Expand Down Expand Up @@ -442,8 +419,4 @@ private String secretName(Secret secret) {
return secret.getMetadata().getName();
}

public KubernetesClient client() {
return client;
}

}

0 comments on commit bc6d0c6

Please sign in to comment.