Пример #1
0
func (this *PodService) PodByName(name string, data k8s_format.Pod) Pod_format {

	var _pod Pod_format
	// _metadata_name_arr := strings.Split(data.Metadata.Name, "-")
	// _metadata_name := strings.Join(_metadata_name_arr[:len(_metadata_name_arr)-1], "-")
	// if _metadata_name == "kube-dns-v3" {
	// 	return _pod
	// }
	_pod.Label = data.Metadata.Labels

	_pod.Container_name = data.Metadata.Name
	_service_name_arr := strings.Split(data.Metadata.GenerateName, "-")
	_service_name := strings.Join(_service_name_arr[:len(_service_name_arr)-1], "-")
	_pod.Service_name = _service_name

	var herd_service herd_format.Data
	herd_service.Containers = append(herd_service.Containers, data.Metadata.Name)

	var getHerd herdModel.GetHerdModel
	getHerd_data := getHerd.PostData(herd_service)
	if len(getHerd_data.Containers) > 0 {
		_herd_container := getHerd_data.Containers[0]
		_pod.Cpu = _herd_container.Cpu
		_pod.Mem = _herd_container.Mem
	} else {
		_pod.Cpu = 0
		_pod.Mem = 0
	}

	_pod.Ports.Phase = data.Status.Phase
	_pod.Ports.Public_ip = data.Status.PodIP
	_pod.Ports.Host_ip = data.Status.HostIP
	// Test(data.Status.PodIP)

	// if data.Status.PodIP != nil {
	// 	_pod.Status.Public_ip = data.Status.PodIP
	// }
	// if data.Status.HostIP != nil {
	// 	_pod.Status.Host_ip = "2"
	// }

	var _container Container
	for _, _spec_container := range data.Spec.Containers {
		_container.Name = _spec_container.Name
		_container.Image = _spec_container.Image
	}
	_pod.Containers = append(_pod.Containers, _container)
	return _pod
}
func (this *ReplicationControllerService) GetReplicationControllerList() ClusterList {
	_replication_controllerModel := k8sModel.ReplicationControllerModel{}
	data := _replication_controllerModel.GetReplicationControllerList()

	_podModel := k8sModel.PodModel{}
	_pod_data := _podModel.GetPodList()

	var _cluster_list ClusterList

	for _, data_items := range data.Items {
		metadata_name := data_items.Metadata.Name
		if metadata_name == "kube-dns-v3" {
			continue
		}

		var _cluster Cluster_format
		_cluster = this.RcByName(data_items)

		replicas := strconv.Itoa(data_items.Status.Replicas)
		_cluster.Status = replicas + "/" + replicas

		var herd_service herd_format.Data
		herd_service.Clusters = append(herd_service.Clusters, metadata_name)

		var scale_herd herdModel.ScaleHerdModel
		scale_herd_data := scale_herd.GetScale(herd_service)

		if len(scale_herd_data.Clusters) > 0 {
			_cluster.Auto_scale = scale_herd_data.Clusters[0].Enable_auto_scale
		}

		for _, _pod_data_item := range _pod_data.Items {
			_metadata_name_arr := strings.Split(_pod_data_item.Metadata.Name, "-")
			_metadata_name := strings.Join(_metadata_name_arr[:len(_metadata_name_arr)-1], "-")
			if metadata_name != _metadata_name {
				continue
			}
			var _pod pod
			_pod.Container_name = _pod_data_item.Metadata.Name
			_pod.Label = _pod_data_item.Metadata.Labels
			_pod.Public_ip = _pod_data_item.Status.PodIP
			_pod.Host_ip = _pod_data_item.Status.HostIP
			_pod.Status = _pod_data_item.Status.Phase

			var getHerd herdModel.GetHerdModel
			getHerd_data := getHerd.PostData(herd_service)

			for _, cluster := range getHerd_data.Clusters {
				if len(cluster.Containers) > 0 {
					for _, _container := range cluster.Containers {
						_cluster.Cpu += _container.Cpu
						_cluster.Mem += _container.Mem
						if _pod_data_item.Metadata.Name == _container.Container_name {
							_pod.Cpu = _container.Cpu
							_pod.Mem = _container.Mem
						}
					}
				}
			}
			var _image_file image_file
			_image_file.Name = _pod_data_item.Spec.Containers[0].Name
			_image_file.Image = _pod_data_item.Spec.Containers[0].Image
			_pod.Image_file = append(_pod.Image_file, _image_file)

			_cluster.Pods = append(_cluster.Pods, _pod)
		}

		_cluster_list.Status = "ok"
		_cluster_list.Data = append(_cluster_list.Data, _cluster)
	}

	return _cluster_list
}
Пример #3
0
//未完成
func (this *OverviewService) GetServiceInfo() OverviewList {
	service_model := k8sModel.ServiceModel{}
	data := service_model.GetServiceList()

	rc_model := k8sModel.ReplicationControllerModel{}
	rc_data := rc_model.GetReplicationControllerList()

	machine_data := this.GetMachineInfo()
	var total_mem float64 = 0
	for _, _machine_data := range machine_data.Data.Machine_info {
		total_mem += _machine_data.Mem
	}

	var _overview_list OverviewList
	_overview_list.Status = "ok"

	for _, data_items := range data.Items {
		metadata_name := data_items.Metadata.Name
		if metadata_name == "kube-dns" || metadata_name == "kubernetes" {
			continue
		}

		var _service_info service_info
		_service_info.Name = metadata_name

		var herd_service herd_format.Data
		herd_service.Services = append(herd_service.Services, metadata_name)

		var getHerd herdModel.GetHerdModel
		getHerd_data := getHerd.PostData(herd_service)

		var cpu int = 0
		var mem int = 0
		for _, service_herd := range getHerd_data.Services {
			if len(service_herd.Clusters) > 0 {
				for _, cluster_herd := range service_herd.Clusters {
					if len(cluster_herd.Containers) > 0 {
						for _, container_herd := range cluster_herd.Containers {
							cpu += container_herd.Cpu
							mem += container_herd.Mem
						}
					}
				}
			}
		}
		_service_info.Cpu = cpu
		_service_info.Mem = (float64(mem) / total_mem) * 100
		for _, _rc_data_itme := range rc_data.Items {
			if data_items.Spec.Selector != nil {
				if data_items.Spec.Selector["name"] == _rc_data_itme.Spec.Selector["name"] {
					_service_info.Container_total += _rc_data_itme.Spec.Replicas
					_service_info.Container_run += _rc_data_itme.Spec.Replicas

				}
			}
		}
		_overview_list.Data.Service_info = append(_overview_list.Data.Service_info, _service_info)
	}

	return _overview_list
}
Пример #4
0
func (this *ServiceService) ServiceByName(name string, data k8s_format.Service) Service_format {
	// service_model := k8sModel.ServiceModel{}
	// data := service_model.GetService(name)
	// var _service_list ServiceList

	var _service Service_format

	metadata_name := data.Metadata.Name
	if metadata_name == "kube-dns" || metadata_name == "kubernetes" {
		// _service.Status = "fail"
		// _service.Errmsg = "no found service"
		return _service
	}

	_service.Service_name = metadata_name
	_service.Label = data.Metadata.Labels

	var herd_service herd_format.Data
	herd_service.Services = append(herd_service.Services, metadata_name)

	var getHerd herdModel.GetHerdModel
	getHerd_data := getHerd.PostData(herd_service)
	var cpuAndMem struct {
		Cpu []int
		Mem []int
	}

	for _, service := range getHerd_data.Services {
		if len(service.Clusters) > 0 {
			for _, _cluster := range service.Clusters {
				if len(_cluster.Containers) > 0 {
					for _, _container := range _cluster.Containers {
						cpuAndMem.Cpu = append(cpuAndMem.Cpu, _container.Cpu)
						cpuAndMem.Mem = append(cpuAndMem.Mem, _container.Mem)
					}
				}
			}
		}
	}

	var _sub_Port sub_Ports
	for _, port := range data.Spec.Ports {
		_sub_Port.Port = port.Port
		_sub_Port.External_port = port.NodePort
		_service.Ports = append(_service.Ports, _sub_Port)
	}

	if len(data.Spec.Selector) < 1 {
		// _service.Status = "Forward"
		// _service.Cpu = "0"
		// _service.Mem = "0"
	} else {
		cpu := strconv.Itoa(len(cpuAndMem.Cpu))
		mem := strconv.Itoa(len(cpuAndMem.Mem))
		_service.Status = cpu + "/" + mem
		_service.Cpu = strconv.Itoa(len(cpuAndMem.Cpu))
		_service.Mem = strconv.Itoa(len(cpuAndMem.Mem))
	}

	return _service
}