yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/kube_node.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aliyun
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  
    22  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    23  	"yunion.io/x/cloudmux/pkg/multicloud"
    24  )
    25  
    26  type SKubeNode struct {
    27  	multicloud.SResourceBase
    28  	AliyunTags
    29  
    30  	cluster *SKubeCluster
    31  
    32  	HostName           string
    33  	ImageId            string
    34  	InstanceChargeType string
    35  	InstanceId         string
    36  	InstanceName       string
    37  	InstanceRole       string
    38  	InstanceStatus     string
    39  	InstanceType       string
    40  	InstanceTypeFamily string
    41  	IpAddress          []string
    42  	IsAliyunNode       bool
    43  	NodeName           string
    44  	NodeStatus         string
    45  	NodepoolId         string
    46  	Source             string
    47  	State              string
    48  	SpotStrategy       string
    49  }
    50  
    51  func (self *SKubeNode) GetGlobalId() string {
    52  	return self.NodeName
    53  }
    54  
    55  func (self *SKubeNode) GetId() string {
    56  	return self.NodeName
    57  }
    58  
    59  func (self *SKubeNode) GetName() string {
    60  	return self.NodeName
    61  }
    62  
    63  func (self *SKubeNode) GetStatus() string {
    64  	return self.NodeStatus
    65  }
    66  
    67  func (self *SKubeNode) GetINodePoolId() string {
    68  	return self.NodepoolId
    69  }
    70  
    71  func (self *SRegion) GetKubeNodes(clusterId string, pageSize, pageNumber int) ([]SKubeNode, int, error) {
    72  	if pageSize < 1 || pageSize > 100 {
    73  		pageSize = 100
    74  	}
    75  	if pageNumber < 1 {
    76  		pageNumber = 1
    77  	}
    78  	params := map[string]string{
    79  		"page_size":   fmt.Sprintf("%d", pageSize),
    80  		"page_number": fmt.Sprintf("%d", pageNumber),
    81  		"PathPattern": fmt.Sprintf("/clusters/%s/nodes", clusterId),
    82  	}
    83  	resp, err := self.k8sRequest("DescribeClusterNodes", params)
    84  	if err != nil {
    85  		return nil, 0, errors.Wrapf(err, "DescribeClusterNodes")
    86  	}
    87  	nodes := []SKubeNode{}
    88  	err = resp.Unmarshal(&nodes, "nodes")
    89  	if err != nil {
    90  		return nil, 0, errors.Wrapf(err, "resp.Unmarshal")
    91  	}
    92  	totalCnt, _ := resp.Int("page", "total_count")
    93  	return nodes, int(totalCnt), nil
    94  }
    95  
    96  func (self *SKubeCluster) GetIKubeNodes() ([]cloudprovider.ICloudKubeNode, error) {
    97  	nodes := []SKubeNode{}
    98  	for {
    99  		part, total, err := self.region.GetKubeNodes(self.ClusterId, 100, len(nodes)/100)
   100  		if err != nil {
   101  			return nil, errors.Wrapf(err, "GetKubeNodes")
   102  		}
   103  		nodes = append(nodes, part...)
   104  		if len(nodes) >= total || len(part) == 0 {
   105  			break
   106  		}
   107  	}
   108  	ret := []cloudprovider.ICloudKubeNode{}
   109  	for i := range nodes {
   110  		nodes[i].cluster = self
   111  		ret = append(ret, &nodes[i])
   112  	}
   113  	return ret, nil
   114  }