Пример #1
0
func (this *LineService) Delete(data []byte) line {
	fmt.Println("[Delete] LineService")

	var _data_format data_format
	json.Unmarshal(data, &_data_format)

	service_model := k8sModel.ServiceModel{}
	_get_service_data := service_model.GetService(_data_format.Service_name)

	replication_controller_model := k8sModel.ReplicationControllerModel{}
	_get_replication_controller_data := replication_controller_model.GetReplicationController(_data_format.Cluster_name)

	var _line line

	if _get_service_data.Spec.Selector == nil {
		_line.Status = "fail"
		_line.Errmsg = "service no selector"
		return _line
	}

	if _get_service_data.Spec.Selector["name"] != _get_replication_controller_data.Spec.Selector["name"] {
		_line.Status = "fail"
		_line.Errmsg = "service and cluster not same"
		return _line
	}

	_get_service_data.Spec.Selector["name"] = _get_service_data.Spec.Selector["name"] + "-giga"
	service_model.UpdateService(_data_format.Service_name, _get_service_data)

	_line.Status = "ok"
	return _line
}
Пример #2
0
func (this *ServiceService) UpdateService(name string, data []byte) Service {
	type service_format struct {
		Service_name string            `json:"service_name"`
		Port         string            `json:"port"`
		Label        map[string]string `json:"label"`
		NodePort     string            `json:"nodePort"`
	}
	var _service Service

	var _service_format service_format
	json.Unmarshal(data, &_service_format)

	service_model := k8sModel.ServiceModel{}
	_k8s_service := service_model.GetService(name)

	_k8s_service.Metadata.Labels = _service_format.Label
	_k8s_service.Spec.Ports[0].Port, _ = strconv.Atoi(_service_format.Port)
	_k8s_service.Spec.Ports[0].TargetPort, _ = strconv.Atoi(_service_format.Port)
	// _k8s_service.Spec.Selector["name"] = name

	service_model.UpdateService(name, _k8s_service)
	_service.Status = "ok"
	// _service.Metadata.Label = _service_format.Label
	return _service
}
Пример #3
0
func (this *LineService) Post(data []byte) line {
	fmt.Println("[Post] LineService")

	var _data_format data_format
	json.Unmarshal(data, &_data_format)

	service_model := k8sModel.ServiceModel{}
	_get_service_data := service_model.GetService(_data_format.Service_name)

	replication_controller_model := k8sModel.ReplicationControllerModel{}
	_get_replication_controller_data := replication_controller_model.GetReplicationController(_data_format.Cluster_name)

	var _line line

	if _get_service_data.Spec.Selector == nil {
		_line.Status = "fail"
		_line.Errmsg = "service no selector"
		return _line
	}

	_get_service_data.Spec.Selector = _get_replication_controller_data.Spec.Selector

	service_model.UpdateService(_data_format.Service_name, _get_service_data)
	// _put_service_data := service_model.UpdateService(_data_format.Service_name, _get_service_data)
	// if _put_service_data.Status_code == "200" {
	// 	Status = "ok"
	// } else {
	// 	Status = "fail"
	// }
	_line.Status = "ok"
	return _line
}
Пример #4
0
func (this *ServiceService) GetServiceList() ServiceList {
	service_model := k8sModel.ServiceModel{}
	data := service_model.GetServiceList()
	var _service_list ServiceList
	_service_list.Status = "ok"
	for _, data_items := range data.Items {
		metadata_name := data_items.Metadata.Name
		if metadata_name == "kube-dns" || metadata_name == "kubernetes" {
			continue
		}

		data := service_model.GetService(metadata_name)
		_service := this.ServiceByName(metadata_name, data)
		_service_list.Data = append(_service_list.Data, _service)
	}
	return _service_list

	// response, _ := ioutil.ReadAll(_service_list)

	// response, _ := ioutil.ReadAll(js)

	// fmt.Println(data.Items[0].Status.Replicas)

	// fmt.Println(data.ApiVersion)
}
Пример #5
0
func (this *ServiceService) CreateServiceRouting(data []byte) Service {
	type service_format struct {
		Service_name string            `json:"service_name"`
		Routing_ip   string            `json:"routing_ip"`
		Port         string            `json:"port"`
		Label        map[string]string `json:"label"`
		NodePort     string            `json:"nodePort"`
	}

	var _service_format service_format
	json.Unmarshal(data, &_service_format)

	var _k8s_service k8s_format.Service
	_k8s_service.Kind = "Service"
	_k8s_service.Metadata.Name = _service_format.Service_name
	_k8s_service.Metadata.Labels = _service_format.Label
	_k8s_service.Metadata.Labels["name"] = _service_format.Service_name

	var _k8s_service_ports k8s_format.ServicePort
	_k8s_service_ports.Port, _ = strconv.Atoi(_service_format.Port)
	_k8s_service_ports.TargetPort, _ = strconv.Atoi(_service_format.Port)
	_k8s_service_ports.Protocol = "TCP"
	_k8s_service.Spec.Ports = append(_k8s_service.Spec.Ports, _k8s_service_ports)
	_k8s_service.Spec.Type = "NodePort"
	service_model := k8sModel.ServiceModel{}
	// rep := service_model.CreateService(_k8s_service)
	service_model.CreateService(_k8s_service)

	var _k8s_endpoint k8s_format.Endpoints
	_k8s_endpoint.Kind = "Endpoints"
	_k8s_endpoint.Metadata.Name = _service_format.Service_name
	_k8s_endpoint.Metadata.Labels = _service_format.Label
	_k8s_endpoint.Metadata.Labels["name"] = _service_format.Service_name

	var _k8s_endpoint_address k8s_format.EndpointAddress
	_k8s_endpoint_address.IP = _service_format.Routing_ip

	var _k8s_endpoint_port k8s_format.EndpointPort
	_k8s_endpoint_port.Port, _ = strconv.Atoi(_service_format.Port)
	_k8s_endpoint_port.Protocol = "TCP"

	var _k8s_endpoint_subset k8s_format.EndpointSubset
	_k8s_endpoint_subset.Addresses = append(_k8s_endpoint_subset.Addresses, _k8s_endpoint_address)
	_k8s_endpoint_subset.Ports = append(_k8s_endpoint_subset.Ports, _k8s_endpoint_port)

	_k8s_endpoint.Subsets = append(_k8s_endpoint.Subsets, _k8s_endpoint_subset)

	var endpoint_model k8sModel.EndpointModel
	endpoint_model.CreateEndpoint(_k8s_endpoint)

	var _service Service
	// if rep == nil {
	// 	_service.Status = "fail"
	// 	return _service
	// }

	_service.Status = "ok"
	return _service
}
Пример #6
0
func (this *ServiceService) DeleteService(name string) Service {
	service_model := k8sModel.ServiceModel{}
	// _service := service_model.DeleteService(name)
	service_model.DeleteService(name)
	var _service Service
	_service.Status = "ok"

	return _service
}
Пример #7
0
func (this *ServiceService) CreateBoth(data []byte) Service {
	var data_format struct {
		Service_name    string            `json:"service_name"`
		Port            string            `json:"port"`
		Container_count string            `json:"container_count"`
		Cpu             string            `json:"cpu"`
		Image           string            `json:"image"`
		Label           map[string]string `json:"label"`
	}
	json.Unmarshal(data, &data_format)

	var _k8s_service k8s_format.Service
	_k8s_service.Kind = "Service"
	_k8s_service.Metadata.Name = data_format.Service_name
	_k8s_service.Metadata.Labels = data_format.Label
	_k8s_service.Metadata.Labels["name"] = data_format.Service_name
	_k8s_service.Spec.Type = "NodePort"
	_k8s_service.Spec.Selector = map[string]string{"name": data_format.Service_name}

	var _k8s_service_ports k8s_format.ServicePort
	_k8s_service_ports.Port, _ = strconv.Atoi(data_format.Port)
	_k8s_service_ports.TargetPort, _ = strconv.Atoi(data_format.Port)
	_k8s_service_ports.Protocol = "TCP"
	_k8s_service.Spec.Ports = append(_k8s_service.Spec.Ports, _k8s_service_ports)

	var k8s_rc k8s_format.ReplicationController
	k8s_rc.Kind = "ReplicationController"
	k8s_rc.Metadata.Name = data_format.Service_name
	k8s_rc.Metadata.Labels = data_format.Label
	k8s_rc.Metadata.Labels["name"] = data_format.Service_name
	k8s_rc.Spec.Replicas, _ = strconv.Atoi(data_format.Container_count)
	k8s_rc.Spec.Selector = map[string]string{"name": data_format.Service_name}
	k8s_rc.Spec.Template.Metadata.Labels = map[string]string{"name": data_format.Service_name}

	var k8s_rc_container_port k8s_format.ContainerPort
	k8s_rc_container_port.ContainerPort, _ = strconv.Atoi(data_format.Port)

	var k8s_rc_container k8s_format.Container
	k8s_rc_container.Name = data_format.Service_name
	k8s_rc_container.Image = data_format.Image
	k8s_rc_container.Ports = append(k8s_rc_container.Ports, k8s_rc_container_port)
	k8s_rc.Spec.Template.Spec.Containers = append(k8s_rc.Spec.Template.Spec.Containers, k8s_rc_container)

	var rc_model k8sModel.ReplicationControllerModel
	var service_model k8sModel.ServiceModel

	rc_model.CreateReplicationController(k8s_rc)
	service_model.CreateService(_k8s_service)
	// _rc_service := data_format

	var service Service
	service.Status = "ok"
	// service.Data = ""
	return service
}
Пример #8
0
func (this *ServiceService) GetService(name string) Service {
	service_model := k8sModel.ServiceModel{}
	data := service_model.GetService(name)
	// Test(data)
	var _service Service

	_service.Data = this.ServiceByName(name, data)
	_service.Status = "ok"

	return _service
}
Пример #9
0
func (this *BubbleService) GetRelate() bubble {
	fmt.Println("[Get] Relate Service")

	var _bubble bubble
	var _item item

	var service_model k8sModel.ServiceModel
	service_data := service_model.GetServiceList()
	// cluster_to_service := map[string]string{}
	for _, service := range service_data.Items {
		_item.Services = append(_item.Services, service.Metadata.Name)
	}

	var pod_model k8sModel.PodModel
	pod_data := pod_model.GetPodList()
	// Test(pod_data)
	for _, pod := range pod_data.Items {
		// Test(pod)
		_item.Containers = append(_item.Containers, pod.Metadata.Name)
	}

	var rc_model k8sModel.ReplicationControllerModel
	rc_data := rc_model.GetReplicationControllerList()
	for _, rc := range rc_data.Items {
		_item.Clusters = append(_item.Clusters, rc.Metadata.Name)
		var _relate relate
		_relate.Cluster_name = rc.Metadata.Name
		for _, service := range service_data.Items {
			// _item.Services = append(_item.Services, service.Metadata.Name)
			if service.Spec.Selector != nil &&
				service.Spec.Selector["name"] != "" &&
				rc.Spec.Selector["name"] == service.Spec.Selector["name"] {
				_relate.Service = append(_relate.Service, service.Metadata.Name)
			}
		}

		for _, pod := range pod_data.Items {
			if pod.Metadata.Labels != nil &&
				pod.Metadata.Labels["name"] != "" &&
				rc.Spec.Selector["name"] == pod.Metadata.Labels["name"] {
				_relate.Container = append(_relate.Container, pod.Metadata.Name)
			}
		}

		_bubble.Data.Relates = append(_bubble.Data.Relates, _relate)
	}

	_bubble.Data.Items = _item
	_bubble.Status = "ok"
	return _bubble
}
Пример #10
0
func (this *OverviewService) GetServiceCount() OverviewList {
	service_model := k8sModel.ServiceModel{}
	data := service_model.GetServiceList()

	var _overview_list OverviewList
	_overview_list.Status = "ok"

	var i int = 0
	for _, data_items := range data.Items {
		metadata_name := data_items.Metadata.Name
		if metadata_name == "kube-dns" || metadata_name == "kubernetes" {
			continue
		}
		i = i + 1
	}
	_overview_list.Data.Service_count = i
	return _overview_list
}
Пример #11
0
func (this *ServiceService) CreateService(data []byte) Service {
	type service_format struct {
		Service_name string            `json:"service_name"`
		Port         string            `json:"port"`
		Label        map[string]string `json:"label"`
		NodePort     string            `json:"nodePort"`
	}

	var _service_format service_format
	json.Unmarshal(data, &_service_format)

	var _k8s_service k8s_format.Service
	var _k8s_service_ports k8s_format.ServicePort
	_k8s_service.Kind = "Service"
	// _k8s_service.APIVersion = "v1beta3"
	_k8s_service.Metadata.Name = _service_format.Service_name
	_k8s_service.Metadata.Labels = _service_format.Label
	_k8s_service.Metadata.Labels["name"] = _service_format.Service_name

	_k8s_service_ports.Port, _ = strconv.Atoi(_service_format.Port)
	_k8s_service_ports.TargetPort, _ = strconv.Atoi(_service_format.Port)
	_k8s_service_ports.Protocol = "TCP"
	_k8s_service.Spec.Ports = append(_k8s_service.Spec.Ports, _k8s_service_ports)

	_k8s_service.Spec.Selector = map[string]string{"name": _service_format.Service_name}
	_k8s_service.Spec.Type = "NodePort"

	service_model := k8sModel.ServiceModel{}
	// rep := service_model.CreateService(_k8s_service)
	service_model.CreateService(_k8s_service)
	var _service Service
	// if rep == nil {
	// 	_service.Status = "fail"
	// 	return _service
	// }

	_service.Status = "ok"
	return _service
}
Пример #12
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
}