|
| 1 | +package io.javaoperatorsdk.operator.springboot.starter.test; |
| 2 | + |
| 3 | +import io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition; |
| 4 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 5 | +import io.fabric8.kubernetes.client.server.mock.KubernetesCrudDispatcher; |
| 6 | +import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; |
| 7 | +import io.fabric8.kubernetes.client.utils.Serialization; |
| 8 | +import io.fabric8.mockwebserver.Context; |
| 9 | +import java.io.FileInputStream; |
| 10 | +import java.io.FileNotFoundException; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.List; |
| 14 | +import okhttp3.mockwebserver.MockWebServer; |
| 15 | +import org.springframework.beans.factory.annotation.Value; |
| 16 | +import org.springframework.context.annotation.Bean; |
| 17 | +import org.springframework.context.annotation.Configuration; |
| 18 | +import org.springframework.util.ResourceUtils; |
| 19 | + |
| 20 | +@Configuration |
| 21 | +public class TestConfiguration { |
| 22 | + |
| 23 | + @Value("${io.javaoperatorsdk.test.crdPaths}") |
| 24 | + private List<String> crdPaths; |
| 25 | + |
| 26 | + @Bean |
| 27 | + public KubernetesMockServer k8sMockServer() { |
| 28 | + final var server = new KubernetesMockServer(new Context(), new MockWebServer(), new HashMap<>(), |
| 29 | + new KubernetesCrudDispatcher(Collections.emptyList()), true); |
| 30 | + server.init(); |
| 31 | + return server; |
| 32 | + } |
| 33 | + |
| 34 | + @Bean |
| 35 | + public KubernetesClient kubernetesClient(KubernetesMockServer server) |
| 36 | + throws FileNotFoundException { |
| 37 | + final var client = server.createClient(); |
| 38 | + |
| 39 | + crdPaths.forEach(crdPath -> { |
| 40 | + CustomResourceDefinition crd = null; |
| 41 | + try { |
| 42 | + crd = Serialization |
| 43 | + .unmarshal(new FileInputStream(ResourceUtils.getFile("classpath:test-crd.yaml"))); |
| 44 | + } catch (FileNotFoundException e) { |
| 45 | + e.printStackTrace(); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + client.apiextensions().v1beta1() |
| 50 | + .customResourceDefinitions() |
| 51 | + .create(crd); |
| 52 | + }); |
| 53 | + |
| 54 | + return client; |
| 55 | + } |
| 56 | +} |
0 commit comments