github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/collector/host_region.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package collector
    21  
    22  import (
    23  	"bytes"
    24  	"encoding/json"
    25  	"strings"
    26  
    27  	"github.com/pkg/errors"
    28  	pkgcollector "github.com/replicatedhq/troubleshoot/pkg/collect"
    29  	"k8s.io/client-go/rest"
    30  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    31  
    32  	preflightv1beta2 "github.com/1aal/kubeblocks/externalapis/preflight/v1beta2"
    33  	"github.com/1aal/kubeblocks/pkg/cli/preflight/util"
    34  	cliutil "github.com/1aal/kubeblocks/pkg/cli/util"
    35  )
    36  
    37  const (
    38  	ClusterRegionTitle = "Cluster Region"
    39  	ClusterRegionPath  = "host-collectors/extend/region_name.json"
    40  )
    41  
    42  var RestConfigFn = func() (*rest.Config, error) {
    43  	// create rest config of target k8s cluster
    44  	return cmdutil.NewFactory(cmdutil.NewMatchVersionFlags(cliutil.NewConfigFlagNoWarnings())).ToRESTConfig()
    45  }
    46  
    47  type ClusterRegionInfo struct {
    48  	RegionName string `json:"regionName"`
    49  }
    50  
    51  type CollectClusterRegion struct {
    52  	HostCollector *preflightv1beta2.ClusterRegion
    53  	BundlePath    string
    54  }
    55  
    56  func (c *CollectClusterRegion) Title() string {
    57  	return util.TitleOrDefault(c.HostCollector.HostCollectorMeta, ClusterRegionTitle)
    58  }
    59  
    60  func (c *CollectClusterRegion) IsExcluded() (bool, error) {
    61  	return util.IsExcluded(c.HostCollector.Exclude)
    62  }
    63  
    64  func (c *CollectClusterRegion) Collect(progressChan chan<- interface{}) (map[string][]byte, error) {
    65  	b, err := doCollect(RestConfigFn, c.HostCollector.ProviderName)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	output := pkgcollector.NewResult()
    70  	_ = output.SaveResult(c.BundlePath, ClusterRegionPath, bytes.NewBuffer(b))
    71  	return output, nil
    72  }
    73  
    74  func doCollect(fn func() (*rest.Config, error), providerName string) ([]byte, error) {
    75  	kubeConfig, err := fn()
    76  	if err != nil {
    77  		return nil, errors.Wrap(err, "failed to get kube config")
    78  	}
    79  	regionName := ResolveRegionNameByEndPoint(providerName, kubeConfig.Host)
    80  	info := ClusterRegionInfo{regionName}
    81  	b, err := json.Marshal(info)
    82  	if err != nil {
    83  		return nil, errors.Wrap(err, "failed to marshal cluster region info")
    84  	}
    85  	return b, nil
    86  }
    87  
    88  func ResolveRegionNameByEndPoint(providerName, endPoint string) string {
    89  	var regionName string
    90  	switch {
    91  	case strings.EqualFold(providerName, "eks"):
    92  		if len(endPoint) > 0 {
    93  			// eks endpoint format: https://xxx.yl4.cn-northwest-1.eks.amazonaws.com.cn
    94  			strList := strings.Split(endPoint, ".")
    95  			if len(strList) == 7 {
    96  				regionName = strList[2]
    97  			}
    98  		}
    99  	default:
   100  	}
   101  	return regionName
   102  }