yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/kube_clusters.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 qcloud
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  type SKubeCluster struct {
    29  	multicloud.SResourceBase
    30  	QcloudTags
    31  	region *SRegion
    32  
    33  	ClusterId              string
    34  	ClusterName            string
    35  	ClusterDescription     string
    36  	ClusterVersion         string
    37  	ClusterOs              string
    38  	ClusterType            string
    39  	ClusterNetworkSettings struct {
    40  	}
    41  	ClusterNodeNum   int
    42  	ProjectId        string
    43  	TagSpecification []struct {
    44  	}
    45  	ClusterStatus       string
    46  	Property            string
    47  	ClusterMaterNodeNum int
    48  	ImageId             string
    49  	OsCustomizeType     string
    50  	ContainerRuntime    string
    51  	CreatedTime         string
    52  	DeletionProtection  bool
    53  	EnableExternalNode  bool
    54  }
    55  
    56  func (self *SKubeCluster) GetId() string {
    57  	return self.ClusterId
    58  }
    59  
    60  func (self *SKubeCluster) GetGlobalId() string {
    61  	return self.ClusterId
    62  }
    63  
    64  func (self *SKubeCluster) GetName() string {
    65  	return self.ClusterName
    66  }
    67  
    68  func (self *SKubeCluster) GetStatus() string {
    69  	return strings.ToLower(self.ClusterStatus)
    70  }
    71  
    72  func (self *SKubeCluster) GetEnabled() bool {
    73  	return true
    74  }
    75  
    76  func (self *SKubeCluster) Refresh() error {
    77  	cluster, err := self.region.GetKubeCluster(self.ClusterId)
    78  	if err != nil {
    79  		return errors.Wrapf(err, "GetKubeCluster(%s)", self.ClusterId)
    80  	}
    81  	return jsonutils.Update(self, cluster)
    82  }
    83  
    84  func (self *SKubeCluster) GetKubeConfig(private bool, expireMinutes int) (*cloudprovider.SKubeconfig, error) {
    85  	return self.region.GetKubeConfig(self.ClusterId, private)
    86  }
    87  
    88  func (self *SKubeCluster) Delete(isRetain bool) error {
    89  	return self.region.DeleteKubeCluster(self.ClusterId, isRetain)
    90  }
    91  
    92  func (self *SRegion) GetKubeConfig(clusterId string, private bool) (*cloudprovider.SKubeconfig, error) {
    93  	params := map[string]string{
    94  		"ClusterId":  clusterId,
    95  		"IsExtranet": fmt.Sprintf("%v", !private),
    96  	}
    97  	resp, err := self.tkeRequest("DescribeClusterKubeconfig", params)
    98  	if err != nil {
    99  		return nil, errors.Wrapf(err, "DescribeClusterKubeconfig")
   100  	}
   101  	config, err := resp.GetString("Kubeconfig")
   102  	if err != nil {
   103  		return nil, errors.Wrapf(err, "resp.GetKubeconfig")
   104  	}
   105  	result := &cloudprovider.SKubeconfig{}
   106  	result.Config = config
   107  	return result, nil
   108  }
   109  
   110  func (self *SRegion) GetICloudKubeClusters() ([]cloudprovider.ICloudKubeCluster, error) {
   111  	clusters := []SKubeCluster{}
   112  	for {
   113  		part, total, err := self.GetKubeClusters(nil, 100, len(clusters)/100)
   114  		if err != nil {
   115  			return nil, errors.Wrapf(err, "GetKubeClusters")
   116  		}
   117  		clusters = append(clusters, part...)
   118  		if len(clusters) >= total || len(part) == 0 {
   119  			break
   120  		}
   121  	}
   122  	ret := []cloudprovider.ICloudKubeCluster{}
   123  	for i := range clusters {
   124  		clusters[i].region = self
   125  		ret = append(ret, &clusters[i])
   126  	}
   127  	return ret, nil
   128  }
   129  
   130  func (self *SRegion) GetKubeClusters(ids []string, limit, offset int) ([]SKubeCluster, int, error) {
   131  	if limit < 1 || limit > 100 {
   132  		limit = 100
   133  	}
   134  	params := map[string]string{
   135  		"Limit":  fmt.Sprintf("%d", limit),
   136  		"Offset": fmt.Sprintf("%d", offset),
   137  	}
   138  	for i, id := range ids {
   139  		params[fmt.Sprintf("ClusterIds.%d", i)] = id
   140  	}
   141  	resp, err := self.tkeRequest("DescribeClusters", params)
   142  	if err != nil {
   143  		return nil, 0, errors.Wrapf(err, "DescribeClusters")
   144  	}
   145  	clusters := []SKubeCluster{}
   146  	err = resp.Unmarshal(&clusters, "Clusters")
   147  	if err != nil {
   148  		return nil, 0, errors.Wrapf(err, "resp.Unmarshal")
   149  	}
   150  	totalCount, _ := resp.Float("TotalCount")
   151  	return clusters, int(totalCount), nil
   152  }
   153  
   154  func (self *SRegion) GetICloudKubeClusterById(id string) (cloudprovider.ICloudKubeCluster, error) {
   155  	cluster, err := self.GetKubeCluster(id)
   156  	if err != nil {
   157  		return nil, errors.Wrapf(err, "GetKubeCluster(%s)", id)
   158  	}
   159  	return cluster, nil
   160  }
   161  
   162  func (self *SRegion) GetKubeCluster(id string) (*SKubeCluster, error) {
   163  	clusters, total, err := self.GetKubeClusters([]string{id}, 1, 0)
   164  	if err != nil {
   165  		return nil, errors.Wrapf(err, "GetKubeCluster(%s)", id)
   166  	}
   167  	if total == 1 {
   168  		clusters[0].region = self
   169  		return &clusters[0], nil
   170  	}
   171  	if total == 0 {
   172  		return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
   173  	}
   174  	return nil, errors.Wrapf(cloudprovider.ErrDuplicateId, id)
   175  }
   176  
   177  func (self *SRegion) DeleteKubeCluster(id string, isRetain bool) error {
   178  	params := map[string]string{
   179  		"ClusterId":          id,
   180  		"InstanceDeleteMode": "retain",
   181  	}
   182  	if !isRetain {
   183  		params["InstanceDeleteMode"] = "terminate"
   184  		params["ResourceDeleteOptions.0.ResourceType"] = "CBS"
   185  		params["ResourceDeleteOptions.0.DeleteMode"] = "terminate"
   186  	}
   187  	_, err := self.tkeRequest("DeleteCluster", params)
   188  	return errors.Wrapf(err, "DeleteCluster")
   189  }