-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix setting service account in Kubernetes/Openshift extensions
Fix #32933
- Loading branch information
Showing
2 changed files
with
78 additions
and
14 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
64 changes: 64 additions & 0 deletions
64
...standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesWithServiceAccountTest.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,64 @@ | ||
package io.quarkus.it.kubernetes; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.api.model.ServiceAccount; | ||
import io.fabric8.kubernetes.api.model.apps.Deployment; | ||
import io.quarkus.test.ProdBuildResults; | ||
import io.quarkus.test.ProdModeTestResults; | ||
import io.quarkus.test.QuarkusProdModeTest; | ||
|
||
public class KubernetesWithServiceAccountTest { | ||
|
||
private static final String APP_NAME = "kubernetes-with-service-account"; | ||
private static final String SERVICE_ACCOUNT = "my-service-account"; | ||
|
||
@RegisterExtension | ||
static final QuarkusProdModeTest config = new QuarkusProdModeTest() | ||
.withApplicationRoot((jar) -> jar.addClasses(GreetingResource.class)) | ||
.setApplicationName(APP_NAME) | ||
.setApplicationVersion("0.1-SNAPSHOT") | ||
.overrideConfigKey("quarkus.kubernetes.service-account", SERVICE_ACCOUNT); | ||
|
||
@ProdBuildResults | ||
private ProdModeTestResults prodModeTestResults; | ||
|
||
@Test | ||
public void assertGeneratedResources() throws IOException { | ||
final Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes"); | ||
List<HasMetadata> kubernetesList = DeserializationUtil | ||
.deserializeAsList(kubernetesDir.resolve("kubernetes.yml")); | ||
|
||
Deployment deployment = getDeploymentByName(kubernetesList, APP_NAME).get(); | ||
assertThat(deployment.getSpec().getTemplate().getSpec().getServiceAccountName()).isEqualTo(SERVICE_ACCOUNT); | ||
|
||
Optional<ServiceAccount> serviceAccount = getServiceAccountByName(kubernetesList, SERVICE_ACCOUNT); | ||
assertThat(serviceAccount.isPresent()).isFalse(); | ||
} | ||
|
||
private Optional<Deployment> getDeploymentByName(List<HasMetadata> kubernetesList, String name) { | ||
return getResourceByName(kubernetesList, Deployment.class, name); | ||
} | ||
|
||
private Optional<ServiceAccount> getServiceAccountByName(List<HasMetadata> kubernetesList, String saName) { | ||
return getResourceByName(kubernetesList, ServiceAccount.class, saName); | ||
} | ||
|
||
private <T extends HasMetadata> Optional<T> getResourceByName(List<HasMetadata> kubernetesList, Class<T> clazz, | ||
String name) { | ||
return kubernetesList.stream() | ||
.filter(r -> r.getMetadata().getName().equals(name)) | ||
.filter(clazz::isInstance) | ||
.map(clazz::cast) | ||
.findFirst(); | ||
} | ||
} |