Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 committed Dec 21, 2024
1 parent a5564fb commit d549bf2
Showing 1 changed file with 0 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import io.fabric8.kubernetes.api.model.ConfigMap;
Expand All @@ -32,17 +30,13 @@
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.networking.v1.Ingress;
import io.fabric8.kubernetes.api.model.networking.v1.IngressLoadBalancerIngress;
import io.fabric8.kubernetes.api.model.rbac.ClusterRole;
import io.fabric8.kubernetes.api.model.rbac.Role;
import io.fabric8.kubernetes.api.model.rbac.RoleBinding;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.dsl.base.PatchContext;
import io.fabric8.kubernetes.client.dsl.base.PatchType;
import io.fabric8.kubernetes.client.utils.Serialization;
import jakarta.annotation.Nullable;
import org.apache.commons.logging.Log;
Expand All @@ -53,7 +47,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 Down Expand Up @@ -228,40 +221,6 @@ public void deleteNamespace(String name) {

}

public void setUpClusterWide(String serviceAccountNamespace, Set<String> namespaces) {
InputStream clusterRoleBindingAsStream = inputStream("cluster/cluster-role.yaml");
InputStream serviceAccountAsStream = inputStream("cluster/service-account.yaml");
InputStream roleBindingAsStream = inputStream("cluster/role-binding.yaml");

ClusterRole clusterRole = client.rbac().clusterRoles().load(clusterRoleBindingAsStream).item();
if (client.rbac().clusterRoles().withName(clusterRole.getMetadata().getName()).get() == null) {
client.rbac().clusterRoles().resource(clusterRole).create();
}

ServiceAccount serviceAccountFromStream = client.serviceAccounts().load(serviceAccountAsStream).item();
serviceAccountFromStream.getMetadata().setNamespace(serviceAccountNamespace);
if (client.serviceAccounts()
.inNamespace(serviceAccountNamespace)
.withName(serviceAccountFromStream.getMetadata().getName())
.get() == null) {
client.serviceAccounts().inNamespace(serviceAccountNamespace).resource(serviceAccountFromStream).create();
}

RoleBinding roleBindingFromStream = client.rbac().roleBindings().load(roleBindingAsStream).item();
namespaces.forEach(namespace -> {
roleBindingFromStream.getMetadata().setNamespace(namespace);

if (client.rbac()
.roleBindings()
.inNamespace(namespace)
.withName(roleBindingFromStream.getMetadata().getName())
.get() == null) {
client.rbac().roleBindings().inNamespace(namespace).resource(roleBindingFromStream).create();
}
});

}

public void createAndWait(String namespace, @Nullable ConfigMap configMap, @Nullable Secret secret) {
if (configMap != null) {
client.configMaps().resource(configMap).create();
Expand Down Expand Up @@ -469,59 +428,6 @@ public void waitForIngress(String namespace, Ingress ingress) {

}

public void patchWithReplace(String imageName, String deploymentName, String namespace, String patchBody,
Map<String, String> labels) {
String body = patchBody.replace("image_name_here", imageName);

client.apps()
.deployments()
.inNamespace(namespace)
.withName(deploymentName)
.patch(PatchContext.of(PatchType.JSON_MERGE), body);

waitForDeploymentAfterPatch(deploymentName, namespace, labels);
}

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 @@ -574,8 +480,4 @@ private String secretName(Secret secret) {
return secret.getMetadata().getName();
}

public KubernetesClient client() {
return client;
}

}

0 comments on commit d549bf2

Please sign in to comment.