package com.microservices.mon.k8s.domain.vo;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.utils.bean.BeanUtils;
import com.microservices.mon.k8s.domain.K8sApplication;
import com.microservices.mon.k8s.domain.K8sClusterNode;
import com.microservices.mon.k8s.domain.K8sNamespace;
import lombok.Data;

import java.util.Date;

@Data
public class RancherClusterNodeVo {
    // 标识
    String id;
    // 元数据
    RancherMetadataVo metadata;
    // 节点状态
    RancherClusterNodeStateVo status;

    public K8sClusterNode toK8sClusterNode() {
        K8sClusterNode target = new K8sClusterNode();
        BeanUtils.copyProperties(this, target);
        if (metadata.getLabels() != null && metadata.getLabels().containsKey("node-role.kubernetes.io/control-plane")) {
            target.setRole("Master");
        }else{
            target.setRole("Worker");
        }
        target.setName(metadata.getName());
        if(status!=null&&status.getAddresses()!=null){
            JSONArray addresses=status.getAddresses();
            for (int i = 0; i < addresses.size(); i++) {
                JSONObject jsonObject = addresses.getJSONObject(i);
                if (jsonObject.getString("type").equals("InternalIP")) {
                    target.setIp(jsonObject.getString("address"));
                }
            }
            JSONArray conditions=status.getConditions();
            for (int i = 0; i < conditions.size(); i++) {
                JSONObject jsonObject = conditions.getJSONObject(i);
                if (jsonObject.getString("type").equals("Ready")) {
                    target.setReady(jsonObject.getBoolean("status"));
                }
            }
        }
        return target;
    }
}
