|
| 1 | +package io.javaoperatorsdk.operator.springboot.starter.test; |
| 2 | + |
| 3 | +import io.fabric8.kubernetes.api.model.apiextensions.v1.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.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | +import org.springframework.beans.factory.annotation.Value; |
| 18 | +import org.springframework.context.annotation.Bean; |
| 19 | +import org.springframework.context.annotation.Configuration; |
| 20 | +import org.springframework.util.ResourceUtils; |
| 21 | + |
| 22 | +@Configuration |
| 23 | +public class TestConfiguration { |
| 24 | + |
| 25 | + private static final Logger log = LoggerFactory.getLogger(TestConfiguration.class); |
| 26 | + |
| 27 | + @Value("${io.javaoperatorsdk.test.crdPaths}") |
| 28 | + private List<String> crdPaths; |
| 29 | + |
| 30 | + @Bean |
| 31 | + public KubernetesMockServer k8sMockServer() { |
| 32 | + final var server = |
| 33 | + new KubernetesMockServer( |
| 34 | + new Context(), |
| 35 | + new MockWebServer(), |
| 36 | + new HashMap<>(), |
| 37 | + new KubernetesCrudDispatcher(Collections.emptyList()), |
| 38 | + true); |
| 39 | + server.init(); |
| 40 | + return server; |
| 41 | + } |
| 42 | + |
| 43 | + @Bean |
| 44 | + public KubernetesClient kubernetesClient(KubernetesMockServer server) { |
| 45 | + final var client = server.createClient(); |
| 46 | + |
| 47 | + crdPaths.forEach( |
| 48 | + crdPath -> { |
| 49 | + CustomResourceDefinition crd = null; |
| 50 | + try { |
| 51 | + crd = Serialization.unmarshal(new FileInputStream(ResourceUtils.getFile(crdPath))); |
| 52 | + } catch (FileNotFoundException e) { |
| 53 | + log.warn("CRD with path {} not found!", crdPath); |
| 54 | + e.printStackTrace(); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + client.apiextensions().v1().customResourceDefinitions().create(crd); |
| 59 | + }); |
| 60 | + |
| 61 | + return client; |
| 62 | + } |
| 63 | +} |
0 commit comments