package io.quarkus.test.kubernetes.client;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.NamespacedKubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;

public class KubernetesMockServerTestResource implements QuarkusTestResourceLifecycleManager {

    private KubernetesMockServer mockServer;

    @Override
    public Map<String, String> start() {
        mockServer = new KubernetesMockServer(useHttps());
        mockServer.init();

        final Map<String, String> systemProps = new HashMap<>();
        try (NamespacedKubernetesClient client = mockServer.createClient()) {
            systemProps.put(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, client.getConfiguration().getMasterUrl());
        }
        systemProps.put(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
        systemProps.put(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
        systemProps.put(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false");
        systemProps.put(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test");

        return systemProps;
    }

    @Override
    public void stop() {
        mockServer.destroy();
    }

    @Override
    public void inject(Object testInstance) {
        Class<?> c = testInstance.getClass();
        while (c != Object.class) {
            for (Field f : c.getDeclaredFields()) {
                if (f.getAnnotation(MockServer.class) != null) {
                    if (!KubernetesMockServer.class.isAssignableFrom(f.getType())) {
                        throw new RuntimeException("@MockServer can only be used on fields of type KubernetesMockServer");
                    }

                    f.setAccessible(true);
                    try {
                        f.set(testInstance, mockServer);
                        return;
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            c = c.getSuperclass();
        }
    }

    private boolean useHttps() {
        return Boolean.getBoolean("quarkus.kubernetes-client.test.https");
    }
}
@@ -573,234 +573,65 @@
     
-mockServer = new KubernetesMockServer(useHttps());%0A        mockServer.init();%0A%0A        final Map%3CString, String%3E systemProps = new HashMap%3C%3E();%0A        try (NamespacedKubernetesClient client = mockServer.createClient()) %7B%0A    
+final Map%3CString, String%3E systemProps = new HashMap%3C%3E();%0A
     
@@ -672,14 +672,18 @@
 TES_
-MAST
+TRUST_C
 ER
+T
 _SYS
@@ -700,60 +700,16 @@
 TY, 
-client.getConfiguration().getMasterUrl());%0A        %7D
+%22true%22);
 %0A   
@@ -743,34 +743,42 @@
 .KUBERNETES_
-TRUST_CERT
+AUTH_TRYKUBECONFIG
 _SYSTEM_PROP
@@ -776,35 +776,36 @@
 STEM_PROPERTY, %22
-tru
+fals
 e%22);%0A        sys
@@ -839,34 +839,38 @@
 TES_AUTH_TRY
-KUBECONFIG
+SERVICEACCOUNT
 _SYSTEM_PROP
@@ -931,58 +931,213 @@
 TES_
-AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, %22false%22);%0A
+NAMESPACE_SYSTEM_PROPERTY, %22test%22);%0A%0A        mockServer = new KubernetesMockServer(useHttps());%0A        mockServer.init();%0A        try (NamespacedKubernetesClient client = mockServer.createClient()) %7B%0A    
     
@@ -1170,33 +1170,30 @@
 .KUBERNETES_
-NAMESPACE
+MASTER
 _SYSTEM_PROP
@@ -1194,32 +1194,76 @@
 M_PROPERTY, 
-%22test%22);
+client.getConfiguration().getMasterUrl());%0A        %7D
 %0A%0A        re

fix (#7460) Setup mock before creating the client