Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aws cloud import cluster #3226

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
proto "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/api/clustermanager"
"github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/cloudprovider"
"github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/cloudprovider/utils"
"github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/remote/encrypt"
"github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/types"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -188,7 +189,7 @@ func GetClusterKubeConfig(opt *cloudprovider.CommonOption, cluster *eks.Cluster)
return "", fmt.Errorf("GetClusterKubeConfig marsh kubeconfig failed, %v", err)
}

return base64.StdEncoding.EncodeToString(configByte), nil
return encrypt.Encrypt(nil, string(configByte))
}

// MapToTaints converts a map of string-string to a slice of Taint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (c *CloudInfoManager) SyncClusterCloudInfo(cls *cmproto.Cluster,
return err
}

cluster, err := client.GetEksCluster(cls.ClusterName)
cluster, err := client.GetEksCluster(opt.ImportMode.CloudID)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
proto "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/api/clustermanager"
"github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/cloudprovider"
"github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/cloudprovider/aws/api"
"github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/remote/encrypt"
)

func init() {
Expand Down Expand Up @@ -112,12 +113,19 @@ func (c *Cluster) ListCluster(opt *cloudprovider.ListClusterOption) ([]*proto.Cl

cloudClusterList := make([]*proto.CloudClusterInfo, 0)
for _, v := range clusters {
cluster, err := cli.GetEksCluster(*v)
if err != nil {
return nil, err
}

cloudClusterList = append(cloudClusterList, &proto.CloudClusterInfo{
ClusterID: *v,
ClusterName: *v,
ClusterID: *v,
ClusterName: *v,
ClusterStatus: *cluster.Status,
ClusterVersion: *cluster.Version,
Location: opt.CommonOption.Region,
})
}

return cloudClusterList, nil
}

Expand Down Expand Up @@ -166,7 +174,36 @@ func (c *Cluster) ListProjects(opt *cloudprovider.CommonOption) ([]*proto.CloudP
// CheckClusterEndpointStatus check cluster endpoint status
func (c *Cluster) CheckClusterEndpointStatus(clusterID string, isExtranet bool,
opt *cloudprovider.CheckEndpointStatusOption) (bool, error) {
return false, cloudprovider.ErrCloudNotImplemented
if opt == nil || len(opt.Account.SecretID) == 0 || len(opt.Account.SecretKey) == 0 || len(opt.Region) == 0 {
return false, fmt.Errorf("cloud CheckClusterEndpointStatus lost authoration")
}

client, err := api.NewEksClient(&opt.CommonOption)
if err != nil {
return false, fmt.Errorf("CheckClusterEndpointStatus get eks client failed, %v", err)
}

cluster, err := client.GetEksCluster(clusterID)
if err != nil {
return false, fmt.Errorf("CheckClusterEndpointStatus get cluster failed, %v", err)
}

kubeConfig, err := api.GetClusterKubeConfig(&opt.CommonOption, cluster)
if err != nil {
return false, fmt.Errorf("CheckClusterEndpointStatus get kubeConfig failed, %v", err)
}

data, err := encrypt.Decrypt(nil, kubeConfig)
if err != nil {
return false, fmt.Errorf("decode kube config failed: %v", err)
}

_, err = cloudprovider.GetCRDByKubeConfig(data)
if err != nil {
return false, fmt.Errorf("CheckClusterEndpointStatus get CRDB failed, %v", err)
}

return true, nil
}

// AddSubnetsToCluster add subnets to cluster
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ package tasks

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"time"
Expand All @@ -28,6 +27,7 @@ import (
proto "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/api/clustermanager"
"github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/cloudprovider"
"github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/common"
"github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/remote/encrypt"
"github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/types"
"github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/utils"
)
Expand Down Expand Up @@ -79,12 +79,12 @@ func RegisterClusterKubeConfigTask(taskID string, stepName string) error {
}

func importClusterCredential(ctx context.Context, data *cloudprovider.CloudDependBasicInfo) error { // nolint
configByte, err := base64.StdEncoding.DecodeString(data.Cluster.KubeConfig)
configByte, err := encrypt.Decrypt(nil, data.Cluster.KubeConfig)
if err != nil {
return fmt.Errorf("failed to decode kubeconfig, %v", err)
}
typesConfig := &types.Config{}
err = json.Unmarshal(configByte, typesConfig)
err = json.Unmarshal([]byte(configByte), typesConfig)
if err != nil {
return fmt.Errorf("failed to unmarshal kubeconfig, %v", err)
}
Expand Down Expand Up @@ -149,10 +149,11 @@ func ImportClusterNodesTask(taskID string, stepName string) error {
}

func importClusterInstances(data *cloudprovider.CloudDependBasicInfo) error {
kubeConfigByte, err := base64.StdEncoding.DecodeString(data.Cluster.KubeConfig)
kubeConfig, err := encrypt.Decrypt(nil, data.Cluster.KubeConfig)
if err != nil {
return fmt.Errorf("decode kube config failed: %v", err)
}
kubeConfigByte := []byte(kubeConfig)

config, err := clientcmd.RESTConfigFromKubeConfig(kubeConfigByte)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
"fmt"
"sync"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"

"github.com/Tencent/bk-bcs/bcs-common/common/blog"

proto "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/api/clustermanager"
Expand All @@ -28,6 +31,10 @@ import (

var validateMgr sync.Once

const (
defaultRegion = "ap-southeast-1"
)

func init() {
validateMgr.Do(func() {
// init Cluster
Expand Down Expand Up @@ -263,7 +270,30 @@ func (c *CloudValidate) DeleteNodesFromClusterValidate(req *proto.DeleteNodesReq

// CreateCloudAccountValidate create cloud account validate
func (c *CloudValidate) CreateCloudAccountValidate(account *proto.Account) error {
return cloudprovider.ErrCloudNotImplemented
// call cloud interface to check accout
if c == nil || account == nil {
return fmt.Errorf("%s CreateCloudAccountValidate request is empty", cloudName)
}

if len(account.SecretID) == 0 || len(account.SecretKey) == 0 {
return fmt.Errorf("%s CreateCloudAccountValidate request lost valid crendential info", cloudName)
}

client, err := api.GetEc2Client(&cloudprovider.CommonOption{Account: account, Region: defaultRegion})
if err != nil {
return err
}

input := &ec2.DescribeRegionsInput{
AllRegions: aws.Bool(true),
}

_, err = client.DescribeRegions(input)
if err != nil {
return err
}

return nil
}

// ListCloudVpcsValidate list cloudAccount validate
Expand Down
Loading