change node label handle api

This commit is contained in:
barnett 2019-08-01 18:25:27 +08:00
parent fe8eba1fa8
commit fe9cb64a69
4 changed files with 61 additions and 12 deletions

View File

@ -93,13 +93,13 @@ func (list TenantList) Len() int {
}
func (list TenantList) Less(i, j int) bool {
if list[i].MemoryRequest < list[j].MemoryRequest {
if list[i].Tenants.LimitMemory < list[j].Tenants.LimitMemory {
return true
}
if list[i].RunningAppNum < list[j].RunningAppNum {
return true
}
if list[i].Tenants.LimitMemory < list[j].Tenants.LimitMemory {
if list[i].MemoryRequest < list[j].MemoryRequest {
return true
}
return false

View File

@ -133,7 +133,7 @@ func GetRuleNodes(w http.ResponseWriter, r *http.Request) {
rule := chi.URLParam(r, "rule")
allowRule := map[string]struct{}{
"compute": struct{}{},
"manage": struct{}{},
"manage": struct{}{},
"storage": struct{}{},
"gateway": struct{}{},
}
@ -278,12 +278,34 @@ func PutLabel(w http.ResponseWriter, r *http.Request) {
logrus.Errorf("error unmarshal labels ,details %s", error.Error())
return
}
err := nodeService.PutNodeLabel(nodeUID, label)
labels, err := nodeService.PutNodeLabel(nodeUID, label)
if err != nil {
err.Handle(r, w)
return
}
httputil.ReturnSuccess(r, w, nil)
httputil.ReturnSuccess(r, w, labels)
}
//DeleteLabel delete node label
func DeleteLabel(w http.ResponseWriter, r *http.Request) {
nodeUID := strings.TrimSpace(chi.URLParam(r, "node_id"))
var label = make(map[string]string)
in, error := ioutil.ReadAll(r.Body)
if error != nil {
logrus.Errorf("error read from request ,details %s", error.Error())
return
}
error = json.Unmarshal(in, &label)
if error != nil {
logrus.Errorf("error unmarshal labels ,details %s", error.Error())
return
}
labels, err := nodeService.DeleteNodeLabel(nodeUID, label)
if err != nil {
err.Handle(r, w)
return
}
httputil.ReturnSuccess(r, w, labels)
}
//GetLabel get node label

View File

@ -77,8 +77,9 @@ func Routers(mode string) *chi.Mux {
r.Put("/{node_id}/status", controller.UpdateNodeStatus)
r.Put("/{node_id}/unschedulable", controller.Cordon)
r.Put("/{node_id}/reschedulable", controller.UnCordon)
r.Put("/{node_id}/labels", controller.PutLabel)
r.Post("/{node_id}/labels", controller.PutLabel)
r.Get("/{node_id}/labels", controller.GetLabel)
r.Delete("/{node_id}/labels", controller.DeleteLabel)
r.Post("/{node_id}/down", controller.DownNode)
r.Post("/{node_id}/up", controller.UpNode)
r.Get("/{node_id}/instance", controller.Instances)

View File

@ -239,21 +239,47 @@ func (n *NodeService) CordonNode(nodeID string, unschedulable bool) *utils.APIHa
}
//PutNodeLabel update node label
func (n *NodeService) PutNodeLabel(nodeID string, labels map[string]string) *utils.APIHandleError {
func (n *NodeService) PutNodeLabel(nodeID string, labels map[string]string) (map[string]string, *utils.APIHandleError) {
hostNode, apierr := n.GetNode(nodeID)
if apierr != nil {
return apierr
return nil, apierr
}
for k, v := range labels {
hostNode.Labels[k] = v
}
if hostNode.Role.HasRule(client.ComputeNode) {
node, err := n.kubecli.UpdateLabels(nodeID, labels)
node, err := n.kubecli.UpdateLabels(nodeID, hostNode.Labels)
if err != nil {
return utils.CreateAPIHandleError(500, fmt.Errorf("update k8s node labels error,%s", err.Error()))
return nil, utils.CreateAPIHandleError(500, fmt.Errorf("update k8s node labels error,%s", err.Error()))
}
hostNode.UpdateK8sNodeStatus(*node)
}
hostNode.Labels = labels
n.nodecluster.UpdateNode(hostNode)
return nil
return hostNode.Labels, nil
}
//DeleteNodeLabel delete node label
func (n *NodeService) DeleteNodeLabel(nodeID string, labels map[string]string) (map[string]string, *utils.APIHandleError) {
hostNode, apierr := n.GetNode(nodeID)
if apierr != nil {
return nil, apierr
}
newLabels := make(map[string]string)
for k, v := range hostNode.Labels {
if _, ok := labels[k]; !ok {
newLabels[k] = v
}
}
hostNode.Labels = newLabels
if hostNode.Role.HasRule(client.ComputeNode) {
node, err := n.kubecli.UpdateLabels(nodeID, hostNode.Labels)
if err != nil {
return nil, utils.CreateAPIHandleError(500, fmt.Errorf("update k8s node labels error,%s", err.Error()))
}
hostNode.UpdateK8sNodeStatus(*node)
}
n.nodecluster.UpdateNode(hostNode)
return hostNode.Labels, nil
}
//DownNode down node