package com.css.simulation.resource.scheduler.infra.configuration.kubernetes;

import com.css.simulation.resource.scheduler.infra.configuration.entity.NodeEntity;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.KubeConfig;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileReader;
import java.util.List;

@Configuration
@ConfigurationProperties(prefix = "kubernetes")
@Data
public class KubernetesConfiguration {

    private String namespace;
    private String podTimeout;
    private String imageVtdGpu;
    private String commandVtdGpu;
    private String commandVtdCarsimGpu;
    private String imageVtdNogpu;
    private String commandVtdNogpu;
    private String commandVtdCarsimNogpu;
    private String carsimImage;
    private String carsimCommand;
//    private List<NodeModel> nodeList;
    private List<NodeEntity> cpuNodeList;
    private List<NodeEntity> gpuNodeList;


    /**
     * **************************以下是多模式仿真**********************
     */

    private String multiNamespace;

    private String multiImageVtdGpu;

    private String multiCommandVtdGpu;


    private List<NodeEntity> multiGpuNodeList;

    private List<NodeEntity> multiCpuNodeList;

    @Bean
    @SneakyThrows
    public ApiClient apiClient() {
//        File config = ResourceUtils.getFile("classpath:kubernetes/config");  // 开发环境可用，生产环境不行，无法从jar 包读取
//        File config = new File("E:\\project\\simulation-cloud\\simulation-resource-scheduler\\src\\main\\resources\\kubernetes\\config");  //windows
        File config = new File("/root/.kube/config");   //linux
//
//        ClassPathResource classPathResource = new ClassPathResource("kubernetes/config");
//        InputStream inputStream = classPathResource.getInputStream();
//        FileUtil.writeInputStreamToLocalFile();
        FileReader fileReader = new FileReader(config);
        return ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(fileReader)).build();
    }

}
