/*
 * Copyright (C) 2016 to the original authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.springframework.cloud.kubernetes.zipkin;

import io.fabric8.kubernetes.api.model.Endpoints;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.utils.Utils;
import org.springframework.cloud.kubernetes.discovery.KubernetesServiceInstance;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.sleuth.metric.SpanMetricReporter;
import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter;
import org.springframework.cloud.sleuth.zipkin.ZipkinAutoConfiguration;
import org.springframework.cloud.sleuth.zipkin.ZipkinProperties;
import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import zipkin.Span;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Configuration
@EnableConfigurationProperties(KubernetesZipkinDiscoveryProperties.class)
@ConditionalOnProperty(value = "spring.cloud.kubernetes.zipkin.discovery.enabled", matchIfMissing = true)
@AutoConfigureBefore(ZipkinAutoConfiguration.class)
public class ZipkinKubernetesAutoConfiguration {


    @Bean
    public ZipkinSpanReporter reporter(KubernetesClient client, KubernetesZipkinDiscoveryProperties discoveryProperties, SpanMetricReporter spanMetricReporter, ZipkinProperties zipkin) {
        String serviceName = discoveryProperties.getServiceName();
        String serviceNamespace = Utils.isNotNullOrEmpty(discoveryProperties.getServiceNamespace()) ? discoveryProperties.getServiceNamespace() : client.getNamespace();

        List<ServiceInstance> services = getInstances(client, serviceName, serviceNamespace);
        String serviceUrl = services.stream()
                .findFirst()
                .map(s -> s.getUri().toString())
                .orElse(null);

        return serviceUrl == null || serviceUrl.isEmpty()
                ? new NullZipkinSpanReporter()
                : new HttpZipkinSpanReporter(serviceUrl, zipkin.getFlushInterval(), zipkin.getCompression().isEnabled(), spanMetricReporter);
    }

    private static List<ServiceInstance> getInstances(KubernetesClient client, String name, String namespace) {
        Assert.notNull(name, "[Assertion failed] - the service name must not be null");

        return Optional.ofNullable(client.endpoints().inNamespace(namespace).withName(name).get())
                .orElse(new Endpoints())
                .getSubsets()
                .stream()
                .flatMap(s -> s.getAddresses().stream().map(a -> (ServiceInstance) new KubernetesServiceInstance(name, a ,s.getPorts().stream().findFirst().orElseThrow(IllegalStateException::new), false)))
                .collect(Collectors.toList());
    }

    static final class NullZipkinSpanReporter implements ZipkinSpanReporter {

        @Override
        public void report(Span span) {

        }
    }
}

@@ -1245,32 +1245,116 @@
 MetricReporter;%0A
+import org.springframework.cloud.sleuth.zipkin.DefaultZipkinRestTemplateCustomizer;%0A
 import org.sprin
@@ -1400,32 +1400,32 @@
 inSpanReporter;%0A
-
 import org.sprin
@@ -1765,16 +1765,68 @@
 Assert;%0A
+import org.springframework.web.client.RestTemplate;%0A
 import z
@@ -3057,16 +3057,47 @@
 eporter(
+restTemplateWithCompression(), 
 serviceU
@@ -3130,45 +3130,8 @@
 l(),
- zipkin.getCompression().isEnabled(),
  spa
@@ -3960,16 +3960,465 @@
       %7D%0A
-
     %7D
+%0A%0A%09private RestTemplate restTemplateWithCompression() %7B%0A%09%09ZipkinProperties zipkinProperties = new ZipkinProperties();%0A%09%09zipkinProperties.getCompression().setEnabled(true);%0A%09%09return restTemplate(zipkinProperties);%0A%09%7D%0A%0A%09private RestTemplate restTemplate(ZipkinProperties zipkinProperties) %7B%0A%09%09RestTemplate restTemplate = new RestTemplate();%0A%09%09new DefaultZipkinRestTemplateCustomizer(zipkinProperties).customize(restTemplate);%0A%09%09return restTemplate;%0A%09%7D
 %0A%7D%0A

Refactor code to pass the correct parameters for HttpZipkinSpanReporter