// Path: breakerbox-turbine/src/main/java/com/yammer/breakerbox/turbine/KubernetesInstanceDiscovery.java
// public class KubernetesInstanceDiscovery implements InstanceDiscovery {
// 
//     private static final Logger LOGGER = LoggerFactory.getLogger(KubernetesInstanceDiscovery.class);
// 
//     public static final String PORT_ANNOTATION_KEY = "breakerbox-port";
//     public static final String POD_HASH_LABEL_KEY = "pod-template-hash";
// 
//     private final KubernetesClient client;
// 
//     public KubernetesInstanceDiscovery() {
//         this.client = new DefaultKubernetesClient();
//     }
// 
//     public KubernetesInstanceDiscovery(KubernetesClient client) {
//         this.client = client;
//     }
// 
//     @Override
//     public Collection<Instance> getInstanceList() throws Exception {
//         LOGGER.info("Starting Kubernetes instance discovery using master URL: {}", client.getMasterUrl());
//         return client.pods().inAnyNamespace()
//                 .list()
//                 .getItems().stream()
//                 .filter(pod -> pod.getMetadata().getAnnotations() != null)  // Ignore pods without annotations
//                 .filter(pod -> pod.getMetadata().getAnnotations().containsKey(PORT_ANNOTATION_KEY))
//                 .map(pod -> {
//                     String portString = pod.getMetadata().getAnnotations().get(PORT_ANNOTATION_KEY);
//                     if (!Pattern.compile("^[0-9]{2,5}$").matcher(portString).matches()) {
//                         LOGGER.warn("Invalid port annotation for pod '{}': {}", pod.getMetadata().getName(), portString);
//                         return null;
//                     } else {
//                         String host = String.format("%s:%s", pod.getStatus().getPodIP(), portString);
//                         boolean running = pod.getStatus().getPhase().equals("Running");
//                         LOGGER.info("Found Kubernetes Pod {} at address {}", pod.getMetadata().getName(), host);
//                         return new Instance(host, extractClusterNameFor(pod), running);
//                     }
//                 })
//                 .filter(Objects::nonNull)
//                 .collect(Collectors.toList());
//     }
// 
//     private static String extractClusterNameFor(Pod pod) {
//         String podBaseName = pod.getMetadata().getGenerateName();
//         // Remove auto-generated hashes, if there are any
//         if (pod.getMetadata().getLabels() != null && pod.getMetadata().getLabels().containsKey(POD_HASH_LABEL_KEY)) {
//             String hash = pod.getMetadata().getLabels().get(POD_HASH_LABEL_KEY);
//             podBaseName = podBaseName.replace(hash + "-", "");
//         }
//         // Pod's base names always end with a '-', remove it
//         podBaseName = podBaseName.substring(0, podBaseName.length()-1);
//         return String.format("%s-%s", pod.getMetadata().getNamespace(), podBaseName);
//     }
// }


import com.google.common.collect.Lists;
import com.netflix.turbine.discovery.Instance;
import com.yammer.breakerbox.turbine.KubernetesInstanceDiscovery;
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.dsl.ClientMixedOperation;
import io.fabric8.kubernetes.client.dsl.ClientPodResource;
import org.assertj.core.util.Maps;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.stub;
package com.yammer.breakerbox.turbine.tests;




@RunWith(MockitoJUnitRunner.class)
public class KubernetesInstanceDiscoveryTest {

    @Mock DefaultKubernetesClient client;
    @Mock ClientMixedOperation<Pod, PodList, DoneablePod, ClientPodResource<Pod, DoneablePod>> podsOperation;
    @Mock ClientMixedOperation<Pod, PodList, DoneablePod, ClientPodResource<Pod, DoneablePod>> podsAnyNsOperation;
    @Mock PodList podList;

// Path: breakerbox-turbine/src/main/java/com/yammer/breakerbox/turbine/KubernetesInstanceDiscovery.java
// public class KubernetesInstanceDiscovery implements InstanceDiscovery {
// 
//     private static final Logger LOGGER = LoggerFactory.getLogger(KubernetesInstanceDiscovery.class);
// 
//     public static final String PORT_ANNOTATION_KEY = "breakerbox-port";
//     public static final String POD_HASH_LABEL_KEY = "pod-template-hash";
// 
//     private final KubernetesClient client;
// 
//     public KubernetesInstanceDiscovery() {
//         this.client = new DefaultKubernetesClient();
//     }
// 
//     public KubernetesInstanceDiscovery(KubernetesClient client) {
//         this.client = client;
//     }
// 
//     @Override
//     public Collection<Instance> getInstanceList() throws Exception {
//         LOGGER.info("Starting Kubernetes instance discovery using master URL: {}", client.getMasterUrl());
//         return client.pods().inAnyNamespace()
//                 .list()
//                 .getItems().stream()
//                 .filter(pod -> pod.getMetadata().getAnnotations() != null)  // Ignore pods without annotations
//                 .filter(pod -> pod.getMetadata().getAnnotations().containsKey(PORT_ANNOTATION_KEY))
//                 .map(pod -> {
//                     String portString = pod.getMetadata().getAnnotations().get(PORT_ANNOTATION_KEY);
//                     if (!Pattern.compile("^[0-9]{2,5}$").matcher(portString).matches()) {
//                         LOGGER.warn("Invalid port annotation for pod '{}': {}", pod.getMetadata().getName(), portString);
//                         return null;
//                     } else {
//                         String host = String.format("%s:%s", pod.getStatus().getPodIP(), portString);
//                         boolean running = pod.getStatus().getPhase().equals("Running");
//                         LOGGER.info("Found Kubernetes Pod {} at address {}", pod.getMetadata().getName(), host);
//                         return new Instance(host, extractClusterNameFor(pod), running);
//                     }
//                 })
//                 .filter(Objects::nonNull)
//                 .collect(Collectors.toList());
//     }
// 
//     private static String extractClusterNameFor(Pod pod) {
//         String podBaseName = pod.getMetadata().getGenerateName();
//         // Remove auto-generated hashes, if there are any
//         if (pod.getMetadata().getLabels() != null && pod.getMetadata().getLabels().containsKey(POD_HASH_LABEL_KEY)) {
//             String hash = pod.getMetadata().getLabels().get(POD_HASH_LABEL_KEY);
//             podBaseName = podBaseName.replace(hash + "-", "");
//         }
//         // Pod's base names always end with a '-', remove it
//         podBaseName = podBaseName.substring(0, podBaseName.length()-1);
//         return String.format("%s-%s", pod.getMetadata().getNamespace(), podBaseName);
//     }
// }


// Path: breakerbox-turbine/src/test/java/com/yammer/breakerbox/turbine/tests/KubernetesInstanceDiscoveryTest.java
import com.google.common.collect.Lists;
import com.netflix.turbine.discovery.Instance;
import com.yammer.breakerbox.turbine.KubernetesInstanceDiscovery;
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.dsl.ClientMixedOperation;
import io.fabric8.kubernetes.client.dsl.ClientPodResource;
import org.assertj.core.util.Maps;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.stub;

package com.yammer.breakerbox.turbine.tests;




@RunWith(MockitoJUnitRunner.class)
public class KubernetesInstanceDiscoveryTest {

    @Mock DefaultKubernetesClient client;
    @Mock ClientMixedOperation<Pod, PodList, DoneablePod, ClientPodResource<Pod, DoneablePod>> podsOperation;
    @Mock ClientMixedOperation<Pod, PodList, DoneablePod, ClientPodResource<Pod, DoneablePod>> podsAnyNsOperation;
    @Mock PodList podList;

    private KubernetesInstanceDiscovery discovery;