diff --git a/extensions/cloudcredentials/ecs/create.go b/extensions/cloudcredentials/ecs/create.go index 961076d2..74a08562 100644 --- a/extensions/cloudcredentials/ecs/create.go +++ b/extensions/cloudcredentials/ecs/create.go @@ -2,26 +2,41 @@ package ecs import ( "github.com/rancher/shepherd/clients/rancher" - management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" + v1 "github.com/rancher/shepherd/clients/rancher/v1" "github.com/rancher/shepherd/extensions/cloudcredentials" - "github.com/rancher/shepherd/pkg/config" + "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/namespaces" + "github.com/rancher/shepherd/extensions/defaults/providers" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/steve" + "github.com/rancher/shepherd/pkg/namegenerator" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -const ecsCloudCredNameBase = "ecsCloudCredential" - -func CreateECSCloudCredentials(rancherClient *rancher.Client) (*cloudcredentials.CloudCredential, error) { - var aliyunECSCredentialConfig cloudcredentials.AliyunECSCredentialConfig - config.LoadConfig(cloudcredentials.AliyunECSCredentialConfigurationFileKey, &aliyunECSCredentialConfig) - - cloudCredential := cloudcredentials.CloudCredential{ - Name: ecsCloudCredNameBase, - AliyunECSCredentialConfig: &aliyunECSCredentialConfig, +func CreateECSCloudCredentials(client *rancher.Client, credentials cloudcredentials.CloudCredential) (*v1.SteveAPIObject, error) { + secretName := namegenerator.AppendRandomString(providers.Aliyun) + spec := corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + GenerateName: cloudcredentials.GeneratedName, + Namespace: namespaces.CattleData, + Annotations: map[string]string{ + "provisioning.cattle.io/driver": providers.Aliyun, + "field.cattle.io/name": secretName, + "field.cattle.io/creatorId": client.UserID, + }, + }, + Data: map[string][]byte{ + "aliyunecscredentialConfig-accessKeyId": []byte(credentials.AliyunECSCredentialConfig.AccessKeyID), + "aliyunecscredentialConfig-accessKeySecret": []byte(credentials.AliyunECSCredentialConfig.AccessKeySecret), + }, + Type: corev1.SecretTypeOpaque, } - resp := &cloudcredentials.CloudCredential{} - err := rancherClient.Management.APIBaseClient.Ops.DoCreate(management.CloudCredentialType, cloudCredential, resp) + ecsCloudCredentials, err := steve.CreateAndWaitForResource(client, stevetypes.Secret, spec, true, defaults.FiveSecondTimeout, defaults.FiveMinuteTimeout) if err != nil { return nil, err } - return resp, nil + + return ecsCloudCredentials, nil } diff --git a/extensions/cloudcredentials/huawei/create.go b/extensions/cloudcredentials/huawei/create.go index fc8e5de0..b5034b18 100644 --- a/extensions/cloudcredentials/huawei/create.go +++ b/extensions/cloudcredentials/huawei/create.go @@ -2,28 +2,45 @@ package huawei import ( "github.com/rancher/shepherd/clients/rancher" - management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" + v1 "github.com/rancher/shepherd/clients/rancher/v1" "github.com/rancher/shepherd/extensions/cloudcredentials" - "github.com/rancher/shepherd/pkg/config" + "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/namespaces" + "github.com/rancher/shepherd/extensions/defaults/providers" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/steve" + "github.com/rancher/shepherd/pkg/namegenerator" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -const huaweiCloudCredNameBase = "huaweiCloudCredential" - // CreateHuaweiCloudCredentials is a helper function that takes the rancher Client as a parameter and creates // an Huawei cloud credential, and returns the CloudCredential response -func CreateHuaweiCloudCredentials(rancherClient *rancher.Client) (*cloudcredentials.CloudCredential, error) { - var huaweiCredentialConfig cloudcredentials.HuaweiCredentialConfig - config.LoadConfig(cloudcredentials.HuaweiCredentialConfigurationFileKey, &huaweiCredentialConfig) - - cloudCredential := cloudcredentials.CloudCredential{ - Name: huaweiCloudCredNameBase, - HuaweiCredentialConfig: &huaweiCredentialConfig, +func CreateHuaweiCloudCredentials(client *rancher.Client, credentials cloudcredentials.CloudCredential) (*v1.SteveAPIObject, error) { + secretName := namegenerator.AppendRandomString(providers.Huawei) + spec := corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + GenerateName: cloudcredentials.GeneratedName, + Namespace: namespaces.CattleData, + Annotations: map[string]string{ + "provisioning.cattle.io/driver": providers.Huawei, + "field.cattle.io/name": secretName, + "field.cattle.io/creatorId": client.UserID, + }, + }, + Data: map[string][]byte{ + "huaweicredentialConfig-accessKey": []byte(credentials.HuaweiCredentialConfig.AccessKey), + "huaweicredentialConfig-secretKey": []byte(credentials.HuaweiCredentialConfig.SecretKey), + "huaweicredentialConfig-regionID": []byte(credentials.HuaweiCredentialConfig.RegionID), + "huaweicredentialConfig-projectID": []byte(credentials.HuaweiCredentialConfig.ProjectID), + }, + Type: corev1.SecretTypeOpaque, } - resp := &cloudcredentials.CloudCredential{} - err := rancherClient.Management.APIBaseClient.Ops.DoCreate(management.CloudCredentialType, cloudCredential, resp) + huaweiCloudCredentials, err := steve.CreateAndWaitForResource(client, stevetypes.Secret, spec, true, defaults.FiveSecondTimeout, defaults.FiveMinuteTimeout) if err != nil { return nil, err } - return resp, nil + + return huaweiCloudCredentials, nil } diff --git a/extensions/cloudcredentials/tencent/create.go b/extensions/cloudcredentials/tencent/create.go index f7d7d94c..d271b265 100644 --- a/extensions/cloudcredentials/tencent/create.go +++ b/extensions/cloudcredentials/tencent/create.go @@ -2,28 +2,43 @@ package tencent import ( "github.com/rancher/shepherd/clients/rancher" - management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" + v1 "github.com/rancher/shepherd/clients/rancher/v1" "github.com/rancher/shepherd/extensions/cloudcredentials" - "github.com/rancher/shepherd/pkg/config" + "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/namespaces" + "github.com/rancher/shepherd/extensions/defaults/providers" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/steve" + "github.com/rancher/shepherd/pkg/namegenerator" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -const tencentCloudCredNameBase = "tencentCloudCredential" - // CreateTencentCloudCredentials is a helper function that takes the rancher Client as a parameter and creates // an Tencent cloud credential, and returns the CloudCredential response -func CreateTencentCloudCredentials(rancherClient *rancher.Client) (*cloudcredentials.CloudCredential, error) { - var tencentCredentialConfig cloudcredentials.TencentCredentialConfig - config.LoadConfig(cloudcredentials.TencentCredentialConfigurationFileKey, &tencentCredentialConfig) - - cloudCredential := cloudcredentials.CloudCredential{ - Name: tencentCloudCredNameBase, - TencentCredentialConfig: &tencentCredentialConfig, +func CreateTencentCloudCredentials(client *rancher.Client, credentials cloudcredentials.CloudCredential) (*v1.SteveAPIObject, error) { + secretName := namegenerator.AppendRandomString(providers.Tencent) + spec := corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + GenerateName: cloudcredentials.GeneratedName, + Namespace: namespaces.CattleData, + Annotations: map[string]string{ + "provisioning.cattle.io/driver": providers.Tencent, + "field.cattle.io/name": secretName, + "field.cattle.io/creatorId": client.UserID, + }, + }, + Data: map[string][]byte{ + "tkecredentialConfig-accessKeyId": []byte(credentials.TencentCredentialConfig.AccessKeyID), + "tkecredentialConfig-accessKeySecret": []byte(credentials.TencentCredentialConfig.AccessKeySecret), + }, + Type: corev1.SecretTypeOpaque, } - resp := &cloudcredentials.CloudCredential{} - err := rancherClient.Management.APIBaseClient.Ops.DoCreate(management.CloudCredentialType, cloudCredential, resp) + tkeCloudCredentials, err := steve.CreateAndWaitForResource(client, stevetypes.Secret, spec, true, defaults.FiveSecondTimeout, defaults.FiveMinuteTimeout) if err != nil { return nil, err } - return resp, nil + + return tkeCloudCredentials, nil } diff --git a/extensions/defaults/providers/providers.go b/extensions/defaults/providers/providers.go index 70d9e4f8..198162c5 100644 --- a/extensions/defaults/providers/providers.go +++ b/extensions/defaults/providers/providers.go @@ -8,4 +8,7 @@ const ( Linode = "linode" Google = "google" Vsphere = "vsphere" + Aliyun = "aliyun" + Huawei = "huawei" + Tencent = "tke" ) diff --git a/extensions/machinepools/aliyunecs_machine_config.go b/extensions/machinepools/aliyunecs_machine_config.go deleted file mode 100644 index 295fdfcd..00000000 --- a/extensions/machinepools/aliyunecs_machine_config.go +++ /dev/null @@ -1,86 +0,0 @@ -package machinepools - -import ( - "github.com/rancher/shepherd/pkg/config" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" -) - -const ( - ECSKind = "aliyunecsConfig" - ECSPoolType = "rke-machine-config.cattle.io.aliyunecsconfig" - ECSResourceConfig = "aliyunecsconfigs" - ECSMachingConfigConfigurationFileKey = "ecsMachineConfigs" -) - -type ECSMachineConfigs struct { - ECSMachineConfig []ECSMachineConfig `json:"ecsMachineConfig" yaml:"ecsMachineConfig"` - Region string `json:"region" yaml:"region"` -} - -type ECSMachineConfig struct { - Roles - Region string `json:"region" yaml:"region"` - Zone string `json:"zone" yaml:"zone"` - ImageID string `json:"imageID" yaml:"imageID"` - InstanceType string `json:"instanceType" yaml:"instanceType"` - DiskFS string `json:"diskFs" yaml:"diskFs"` - DiskSize string `json:"diskSize" yaml:"diskSize"` - DiskCategory string `json:"diskCategory" yaml:"diskCategory"` - InternetChargeType string `json:"internetChargeType" yaml:"internetChargeType"` - InternetMaxBandwidth string `json:"internetMaxBandwidth" yaml:"internetMaxBandwidth"` - IoOptimized string `json:"ioOptimized" yaml:"ioOptimized"` - SecurityGroup string `json:"securityGroup" yaml:"securityGroup"` - SystemDiskCategory string `json:"systemDiskCategory" yaml:"systemDiskCategory"` - SystemDiskSize string `json:"systemDiskSize" yaml:"systemDiskSize"` - UpgradeKernel bool `json:"upgradeKernel" yaml:"upgradeKernel"` - VpcID string `json:"vpcId" yaml:"vpcId"` - VswitchID string `json:"vswitchId" yaml:"vswitchId"` -} - -func NewECSMachineConfig(generatedPoolName, namespace string) []unstructured.Unstructured { - var ecsMachineConfigs ECSMachineConfigs - config.LoadConfig(ECSMachingConfigConfigurationFileKey, &ecsMachineConfigs) - var multiConfig []unstructured.Unstructured - - for _, ecsMachineConfig := range ecsMachineConfigs.ECSMachineConfig { - machineConfig := unstructured.Unstructured{} - machineConfig.SetAPIVersion("rke-machine-config.cattle.io/v1") - machineConfig.SetKind(ECSKind) - machineConfig.SetGenerateName(generatedPoolName) - machineConfig.SetNamespace(namespace) - - machineConfig.Object["region"] = ecsMachineConfig.Region - machineConfig.Object["zone"] = ecsMachineConfig.Zone - machineConfig.Object["type"] = ECSPoolType - machineConfig.Object["imageId"] = ecsMachineConfig.ImageID - machineConfig.Object["instanceType"] = ecsMachineConfig.InstanceType - machineConfig.Object["diskFS"] = ecsMachineConfig.DiskFS - machineConfig.Object["diskSize"] = ecsMachineConfig.DiskSize - machineConfig.Object["diskCategory"] = ecsMachineConfig.DiskCategory - machineConfig.Object["systemDiskCategory"] = ecsMachineConfig.SystemDiskCategory - machineConfig.Object["systemDiskSize"] = ecsMachineConfig.SystemDiskSize - machineConfig.Object["internetChargeType"] = ecsMachineConfig.InternetChargeType - machineConfig.Object["internetMaxBandwidth"] = ecsMachineConfig.InternetMaxBandwidth - machineConfig.Object["ioOptimized"] = ecsMachineConfig.IoOptimized - machineConfig.Object["securityGroup"] = ecsMachineConfig.SecurityGroup - machineConfig.Object["upgradeKernel"] = ecsMachineConfig.UpgradeKernel - machineConfig.Object["vpcId"] = ecsMachineConfig.VpcID - machineConfig.Object["vswitchId"] = ecsMachineConfig.VswitchID - - multiConfig = append(multiConfig, machineConfig) - } - - return multiConfig -} - -func GetECSMachineRoles() []Roles { - var ecsMachineConfigs ECSMachineConfigs - config.LoadConfig(ECSMachingConfigConfigurationFileKey, &ecsMachineConfigs) - var allRoles []Roles - - for _, ecsMachineConfig := range ecsMachineConfigs.ECSMachineConfig { - allRoles = append(allRoles, ecsMachineConfig.Roles) - } - - return allRoles -} diff --git a/extensions/nodes/cvm/cvm.go b/extensions/nodes/cvm/cvm.go deleted file mode 100644 index c4f9cb10..00000000 --- a/extensions/nodes/cvm/cvm.go +++ /dev/null @@ -1,182 +0,0 @@ -package cvm - -import ( - "errors" - "strings" - "time" - - rancherCvm "github.com/rancher/shepherd/clients/cvm" - "github.com/rancher/shepherd/clients/rancher" - "github.com/rancher/shepherd/extensions/provisioninginput" - "github.com/rancher/shepherd/pkg/config" - "github.com/rancher/shepherd/pkg/nodes" - "github.com/sirupsen/logrus" - "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" - cvmapi "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312" - "k8s.io/apimachinery/pkg/util/wait" -) - -const ( - nodeBaseName = "rancher-automation" -) - -var backoff = wait.Backoff{ - Duration: 30 * time.Second, - Steps: 12, -} - -// CreateNodes creates `quantityPerPool[n]` number of cvm instances -func CreateNodes(client *rancher.Client, rolesPerPool []string, quantityPerPool []int32) (cvmNodes []*nodes.Node, err error) { - cvmClient, err := client.GetCVMClient() - if err != nil { - return nil, err - } - - runningReservations := []*cvmapi.RunInstancesResponse{} - reservationConfigs := []*rancherCvm.TencentCVMConfig{} - for i := len(quantityPerPool) - 1; i >= 0; i-- { - config := MatchRoleToConfig(rolesPerPool[i], cvmClient.ClientConfig.TencentCVMConfig) - if config == nil { - return nil, errors.New("No matching nodesAndRole for TencentCVMConfig with role:" + rolesPerPool[i]) - } - - runInstancesRequest := cvmapi.NewRunInstancesRequest() - runInstancesRequest.Placement = &cvmapi.Placement{ - Zone: common.StringPtr(config.Zone), - } - runInstancesRequest.InstanceType = common.StringPtr(config.InstanceType) - runInstancesRequest.InternetAccessible = &cvmapi.InternetAccessible{ - InternetChargeType: common.StringPtr(config.InternetChargeType), - PublicIpAssigned: common.BoolPtr(config.PublicIPAssigned), - InternetMaxBandwidthOut: common.Int64Ptr(int64(config.InternetMaxBandwidthOut)), - } - runInstancesRequest.VirtualPrivateCloud = &cvmapi.VirtualPrivateCloud{ - VpcId: common.StringPtr(config.VpcID), - SubnetId: common.StringPtr(config.SubnetID), - } - runInstancesRequest.InstanceName = common.StringPtr(config.InstanceName) - runInstancesRequest.ImageId = common.StringPtr(config.ImageID) - runInstancesRequest.SecurityGroupIds = common.StringPtrs(config.SecurityGroupIDs) - runInstancesRequest.LoginSettings = &cvmapi.LoginSettings{ - KeyIds: common.StringPtrs(config.KeyIDs), - } - runInstancesRequest.InstanceCount = common.Int64Ptr(int64(quantityPerPool[i])) - runInstancesRequest.UserData = common.StringPtr(config.UserData) - runInstancesRequest.TagSpecification = []*cvmapi.TagSpecification{ - { - ResourceType: common.StringPtr("instance"), - Tags: []*cvmapi.Tag{ - { - Key: common.StringPtr("Name"), - Value: common.StringPtr(nodeBaseName), - }, - }, - }, - } - - reservation, err := cvmClient.Client.RunInstances(runInstancesRequest) - if err != nil { - return nil, err - } - - runningReservations = append(runningReservations, reservation) - reservationConfigs = append(reservationConfigs, config) - } - - for i := 0; i < len(quantityPerPool); i++ { - listOfInstanceIds := runningReservations[i].Response.InstanceIdSet - if err = wait.ExponentialBackoff(backoff, func() (bool, error) { - getInstancesStatusRequest := cvmapi.NewDescribeInstancesStatusRequest() - getInstancesStatusRequest.InstanceIds = listOfInstanceIds - instancesStatus, err := cvmClient.Client.DescribeInstancesStatus(getInstancesStatusRequest) - if err != nil { - return false, err - } - - for _, instance := range instancesStatus.Response.InstanceStatusSet { - if *instance.InstanceState != "RUNNING" { - return false, nil - } - logrus.Infof("instance %s is running", *instance.InstanceId) - } - return true, nil - }); err != nil { - return nil, err - } - - getInstancesRequest := cvmapi.NewDescribeInstancesRequest() - getInstancesRequest.InstanceIds = listOfInstanceIds - instances, err := cvmClient.Client.DescribeInstances(getInstancesRequest) - if err != nil { - return nil, err - } - readyInstances := instances.Response.InstanceSet - - sshKey := []byte(reservationConfigs[i].TencentSSHKey) - for _, readyInstance := range readyInstances { - cvmNode := &nodes.Node{ - NodeID: *readyInstance.InstanceId, - PublicIPAddress: *readyInstance.PublicIpAddresses[0], - PrivateIPAddress: *readyInstance.PrivateIpAddresses[0], - SSHUser: reservationConfigs[i].TencentUser, - SSHKey: sshKey, - } - - provisioningInputConfig := new(provisioninginput.Config) - config.LoadConfig(provisioninginput.ConfigurationFileKey, provisioningInputConfig) - if len(provisioningInputConfig.RKE1KubernetesVersions) > 0 { - // wait for docker installed by cloud-init - if err = wait.ExponentialBackoff(backoff, func() (bool, error) { - _, err := cvmNode.ExecuteCommand("docker ps") - if err != nil { - logrus.Infof("wait for node [%v] install docker", cvmNode.NodeID) - return false, nil - } - return true, nil - }); err != nil { - return nil, err - } - } - - // re-reverse the list so that the order is corrected - cvmNodes = append([]*nodes.Node{cvmNode}, cvmNodes...) - } - } - time.Sleep(20 * time.Second) - - client.Session.RegisterCleanupFunc(func() error { - return DeleteNodes(client, cvmNodes) - }) - - return cvmNodes, nil -} - -// DeleteNodes terminates cvm instances that have been created. -func DeleteNodes(client *rancher.Client, nodes []*nodes.Node) error { - cvmClient, err := client.GetCVMClient() - if err != nil { - return err - } - - var instanceIDs []*string - for _, node := range nodes { - instanceIDs = append(instanceIDs, common.StringPtr(node.NodeID)) - } - - terminateInstancesRequest := cvmapi.NewTerminateInstancesRequest() - terminateInstancesRequest.InstanceIds = instanceIDs - _, err = cvmClient.Client.TerminateInstances(terminateInstancesRequest) - - return err -} - -// MatchRoleToConfig matches the role of nodesAndRoles to the cvmConfig that allows this role. -func MatchRoleToConfig(poolRole string, cvmConfigs []rancherCvm.TencentCVMConfig) *rancherCvm.TencentCVMConfig { - for _, config := range cvmConfigs { - configRoles := " --" + strings.Join(config.Roles, " --") - if strings.Contains(configRoles, poolRole) { - return &config - } - } - return nil -} diff --git a/extensions/nodes/huawei/huawei.go b/extensions/nodes/huawei/huawei.go deleted file mode 100644 index f485f1a6..00000000 --- a/extensions/nodes/huawei/huawei.go +++ /dev/null @@ -1,266 +0,0 @@ -package huawei - -import ( - "errors" - "strings" - "time" - - "github.com/cnrancher/cce-operator/pkg/utils" - ecs_model "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2/model" - rancherHuawei "github.com/rancher/shepherd/clients/huawei" - "github.com/rancher/shepherd/clients/rancher" - namegen "github.com/rancher/shepherd/pkg/namegenerator" - "github.com/rancher/shepherd/pkg/nodes" - "github.com/sirupsen/logrus" - "k8s.io/apimachinery/pkg/util/wait" -) - -const ( - nodeBaseName = "rancher-automation" -) - -var backoff = wait.Backoff{ - Duration: 30 * time.Second, - Steps: 20, // 10min -} - -// CreateNodes creates `quantityPerPool[n]` number of huawei ecs instances -func CreateNodes(client *rancher.Client, rolesPerPool []string, quantityPerPool []int32) ([]*nodes.Node, error) { - hwClient, err := client.GetHuaweiClient() - if err != nil { - return nil, err - } - - runningReservations := []*ecs_model.CreateServersResponse{} - reservationConfigs := []*rancherHuawei.ECSConfig{} - for i := len(quantityPerPool) - 1; i >= 0; i-- { - config := MatchRoleToConfig(rolesPerPool[i], hwClient.ClientConfig.HuaweiConfig) - if config == nil { - return nil, errors.New("No matching nodesAndRole for HuaweiConfig with role:" + rolesPerPool[i]) - } - - request := &ecs_model.CreateServersRequest{ - Body: &ecs_model.CreateServersRequestBody{ - DryRun: utils.Pointer(false), - Server: &ecs_model.PrePaidServer{ - ImageRef: config.ImageRef, - FlavorRef: config.FlavorRef, - Name: namegen.AppendRandomString(nodeBaseName), - KeyName: &config.KeyName, - Vpcid: config.VpcID, - Nics: []ecs_model.PrePaidServerNic{ - { - SubnetId: config.SubnetID, - }, - }, - Publicip: &ecs_model.PrePaidServerPublicip{ - Eip: &ecs_model.PrePaidServerEip{ - Iptype: config.EIPType, - Bandwidth: &ecs_model.PrePaidServerEipBandwidth{ - Size: &config.BandwidthSize, - Chargemode: &config.BandwidthChargeMode, - // Sharetype, - }, - }, - }, - RootVolume: &ecs_model.PrePaidServerRootVolume{ - // Volumetype, - Size: &config.RootVolumeSize, - }, - DataVolumes: nil, - SecurityGroups: nil, - UserData: &config.UserData, - }, - }, - } - switch config.BandwidthShareType { - case "WHOLE": - request.Body.Server.Publicip.Eip.Bandwidth.Sharetype = ecs_model.GetPrePaidServerEipBandwidthSharetypeEnum().WHOLE - default: - request.Body.Server.Publicip.Eip.Bandwidth.Sharetype = ecs_model.GetPrePaidServerEipBandwidthSharetypeEnum().PER - } - switch config.RootVolumeType { - case "SATA": - request.Body.Server.RootVolume.Volumetype = ecs_model.GetPrePaidServerRootVolumeVolumetypeEnum().SATA - case "SAS": - request.Body.Server.RootVolume.Volumetype = ecs_model.GetPrePaidServerRootVolumeVolumetypeEnum().SAS - case "GPSSD": - request.Body.Server.RootVolume.Volumetype = ecs_model.GetPrePaidServerRootVolumeVolumetypeEnum().GPSSD - case "ESSD": - request.Body.Server.RootVolume.Volumetype = ecs_model.GetPrePaidServerRootVolumeVolumetypeEnum().ESSD - default: - request.Body.Server.RootVolume.Volumetype = ecs_model.GetPrePaidServerRootVolumeVolumetypeEnum().SSD - } - dataVolume := []ecs_model.PrePaidServerDataVolume{ - { - Size: config.DataVolumeSize, - }, - } - switch config.DataVolumeType { - case "SATA": - dataVolume[0].Volumetype = ecs_model.GetPrePaidServerDataVolumeVolumetypeEnum().SATA - case "SAS": - dataVolume[0].Volumetype = ecs_model.GetPrePaidServerDataVolumeVolumetypeEnum().SAS - case "GPSSD": - dataVolume[0].Volumetype = ecs_model.GetPrePaidServerDataVolumeVolumetypeEnum().GPSSD - case "ESSD": - dataVolume[0].Volumetype = ecs_model.GetPrePaidServerDataVolumeVolumetypeEnum().ESSD - default: - dataVolume[0].Volumetype = ecs_model.GetPrePaidServerDataVolumeVolumetypeEnum().SSD - } - request.Body.Server.DataVolumes = &dataVolume - if config.SecurityGroups != nil { - sgs := []ecs_model.PrePaidServerSecurityGroup{} - for _, sg := range config.SecurityGroups { - sgs = append(sgs, ecs_model.PrePaidServerSecurityGroup{ - Id: utils.Pointer(sg), - }) - } - request.Body.Server.SecurityGroups = &sgs - } - reservation, err := hwClient.Client.CreateServers(request) - if err != nil { - return nil, err - } - if reservation == nil || reservation.ServerIds == nil { - logrus.Warnf("skip reservation: %+v, CreateServers returns invalid value", reservation) - } - logrus.Infof("create huawei ecs instance [%v] %v", - request.Body.Server.Name, *reservation.ServerIds) - runningReservations = append(runningReservations, reservation) - reservationConfigs = append(reservationConfigs, config) - } - - // wait for create instance jobs started - time.Sleep(time.Second * 10) - - hwNodes := []*nodes.Node{} - for i := 0; i < len(quantityPerPool); i++ { - serverIds := runningReservations[i].ServerIds - for _, id := range *serverIds { - var ( - publicIP string - privateIP string - server *ecs_model.ShowServerResponse - ) - - // wait for server initialized - if err = wait.ExponentialBackoff(backoff, func() (bool, error) { - var err error - server, err = hwClient.Client.ShowServer(&ecs_model.ShowServerRequest{ - ServerId: id, - }) - if err != nil { - return false, err - } - if server == nil || server.Server == nil { - logrus.Warnf("ShowServer returns invalid data, retry") - return false, nil - } - if server.Server.Status != "ACTIVE" { - logrus.Infof("wait for instance [%s] status [%s]", - server.Server.Name, server.Server.Status) - return false, nil - } - for _, addrs := range server.Server.Addresses { - for _, addr := range addrs { - if addr.OSEXTIPStype == nil { - logrus.Warnf("Skip %v: OSEXTIPStype is nil", addr.Addr) - continue - } - switch addr.OSEXTIPStype.Value() { - case "floating": - publicIP = addr.Addr - case "fixed": - privateIP = addr.Addr - } - } - } - if publicIP == "" || privateIP == "" { - logrus.Infof("wait for instance [%s] create public IP", - server.Server.Name) - return false, nil - } - logrus.Infof("instance [%s] [%s] is running", - server.Server.Name, id) - return true, nil - }); err != nil { - return nil, err - } - - logrus.Infof("public IP of node [%v]: %v", server.Server.Name, publicIP) - sshKey, err := nodes.GetSSHKey(reservationConfigs[i].KeyName) - if err != nil { - return nil, err - } - node := &nodes.Node{ - NodeID: id, - PublicIPAddress: publicIP, - PrivateIPAddress: privateIP, - SSHUser: reservationConfigs[i].SSHUser, - SSHKey: sshKey, - } - - // wait for docker installed by cloud-init - if err = wait.ExponentialBackoff(backoff, func() (bool, error) { - _, err := node.ExecuteCommand("docker ps") - if err != nil { - logrus.Infof("wait for server [%v] install docker", server.Server.Name) - return false, nil - } - return true, nil - }); err != nil { - return nil, err - } - // re-reverse the list so that the order is corrected - hwNodes = append([]*nodes.Node{node}, hwNodes...) - } - } - - client.Session.RegisterCleanupFunc(func() error { - return DeleteNodes(client, hwNodes) - }) - - return hwNodes, nil -} - -// MatchRoleToConfig matches the role of nodesAndRoles to the ECSConfig that allows this role. -func MatchRoleToConfig(poolRole string, huaweiConfigs []rancherHuawei.ECSConfig) *rancherHuawei.ECSConfig { - for _, config := range huaweiConfigs { - hasMatch := false - for _, configRole := range config.Roles { - if strings.Contains(poolRole, configRole) { - hasMatch = true - } - } - if hasMatch { - return &config - } - } - return nil -} - -// DeleteNodes terminates huawei ecs instances that have been created. -func DeleteNodes(client *rancher.Client, nodes []*nodes.Node) error { - hwClient, err := client.GetHuaweiClient() - if err != nil { - return err - } - - request := &ecs_model.DeleteServersRequest{ - Body: &ecs_model.DeleteServersRequestBody{ - DeletePublicip: utils.Pointer(true), - DeleteVolume: utils.Pointer(true), - Servers: nil, - }, - } - for _, node := range nodes { - request.Body.Servers = append(request.Body.Servers, ecs_model.ServerId{ - Id: node.NodeID, - }) - logrus.Infof("request to delete huawei ecs instance [%v]", node.NodeID) - } - - _, err = hwClient.Client.DeleteServers(request) - return err -} diff --git a/extensions/provisioning/pandaria_creates.go b/extensions/provisioning/pandaria_creates.go deleted file mode 100644 index 74d05834..00000000 --- a/extensions/provisioning/pandaria_creates.go +++ /dev/null @@ -1,79 +0,0 @@ -package provisioning - -import ( - "time" - - "github.com/rancher/shepherd/clients/rancher" - management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" - "github.com/rancher/shepherd/extensions/cloudcredentials/huawei" - "github.com/rancher/shepherd/extensions/cloudcredentials/tencent" - "github.com/rancher/shepherd/extensions/clusters/cce" - "github.com/rancher/shepherd/extensions/clusters/tke" - "github.com/rancher/shepherd/extensions/defaults" - "github.com/rancher/shepherd/extensions/pipeline" - "github.com/rancher/shepherd/pkg/environmentflag" - namegen "github.com/rancher/shepherd/pkg/namegenerator" - "github.com/sirupsen/logrus" - kwait "k8s.io/apimachinery/pkg/util/wait" -) - -// CreateProvisioningCCEHostedCluster provisions an CCE cluster, then runs verify checks -func CreateProvisioningCCEHostedCluster(client *rancher.Client) (*management.Cluster, error) { - cloudCredential, err := huawei.CreateHuaweiCloudCredentials(client) - if err != nil { - return nil, err - } - - clusterName := namegen.AppendRandomString("ccehostcluster") - clusterResp, err := cce.CreateCCEHostedCluster(client, clusterName, cloudCredential.ID, false, false, false, false, map[string]string{}) - if err != nil { - return nil, err - } - - if client.Flags.GetValue(environmentflag.UpdateClusterName) { - pipeline.UpdateConfigClusterName(clusterName) - } - - err = kwait.Poll(10*time.Second, defaults.ThirtyMinuteTimeout, func() (bool, error) { - done, err := cce.UpdateNodePublicIP(client, clusterResp.ID, cloudCredential) - if err != nil { - logrus.Errorf("UpdateNodePublicIP failed: %v", err) - } - return done, err - }) - if err != nil { - return nil, err - } - - client, err = client.ReLogin() - if err != nil { - return nil, err - } - - return client.Management.Cluster.ByID(clusterResp.ID) -} - -// CreateProvisioningTKEHostedCluster provisions an TKE cluster, then runs verify checks -func CreateProvisioningTKEHostedCluster(client *rancher.Client) (*management.Cluster, error) { - cloudCredential, err := tencent.CreateTencentCloudCredentials(client) - if err != nil { - return nil, err - } - - clusterName := namegen.AppendRandomString("tkehostcluster") - clusterResp, err := tke.CreateTKEHostedCluster(client, clusterName, cloudCredential.ID, false, false, false, false, nil) - if err != nil { - return nil, err - } - - if client.Flags.GetValue(environmentflag.UpdateClusterName) { - pipeline.UpdateConfigClusterName(clusterName) - } - - client, err = client.ReLogin() - if err != nil { - return nil, err - } - - return client.Management.Cluster.ByID(clusterResp.ID) -} diff --git a/extensions/rke1/nodetemplates/aliyun/create.go b/extensions/rke1/nodetemplates/aliyun/create.go deleted file mode 100644 index a5da359f..00000000 --- a/extensions/rke1/nodetemplates/aliyun/create.go +++ /dev/null @@ -1,28 +0,0 @@ -package nodetemplates - -import ( - "github.com/rancher/shepherd/clients/rancher" - management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" - "github.com/rancher/shepherd/extensions/rke1/nodetemplates" - "github.com/rancher/shepherd/pkg/config" -) - -const aliyunECSTemplateNameBase = "aliyunecsConfig" - -// CreateAliyunECSNodeTemplate is a helper function that takes the rancher Client as a parameter and creates -// an Aliyun ECS node template and returns the NodeTemplate response -func CreateAliyunECSNodeTemplate(rancherClient *rancher.Client) (*nodetemplates.NodeTemplate, error) { - var aliyunECSNodeNodeTemplateConfig nodetemplates.AliyunECSNodeTemplateConfig - config.LoadConfig(nodetemplates.AliyunECSNodeTemplateConfigurationFileKey, &aliyunECSNodeNodeTemplateConfig) - nodeTemplate := nodetemplates.NodeTemplate{ - EngineInstallURL: "https://raw.githubusercontent.com/cnrancher/autok3s/master/scripts/docker/24.0.sh", - Name: aliyunECSTemplateNameBase, - AliyunECSNodeTemplateConfig: &aliyunECSNodeNodeTemplateConfig, - } - resp := &nodetemplates.NodeTemplate{} - err := rancherClient.Management.APIBaseClient.Ops.DoCreate(management.NodeTemplateType, nodeTemplate, resp) - if err != nil { - return nil, err - } - return resp, nil -} diff --git a/extensions/rke1/nodetemplates/aliyun_config.go b/extensions/rke1/nodetemplates/aliyun_config.go deleted file mode 100644 index 4e9a1e28..00000000 --- a/extensions/rke1/nodetemplates/aliyun_config.go +++ /dev/null @@ -1,41 +0,0 @@ -package nodetemplates - -// The json/yaml config key for the Aliyun node template config -const AliyunECSNodeTemplateConfigurationFileKey = "aliyunNodeTemplate" - -// AliyunECSNodeTemplateConfig is configuration need to create a Aliyun node template -type AliyunECSNodeTemplateConfig struct { - AccessKey string `json:"accessKeyId" yaml:"accessKeyId"` - SecretKey string `json:"accessKeySecret" yaml:"accessKeySecret"` - Region string `json:"region" yaml:"region"` - ImageID string `json:"imageId" yaml:"imageId"` - SSHPassword string `json:"sshPassword" yaml:"sshPassword"` - SSHKeyPairName string `json:"sshKeypair" yaml:"sshKeypair"` - SSHPrivateKeyPath string `json:"sshPrivateKeyPath" yaml:"sshPrivateKeyPath"` - InstanceType string `json:"instanceType" yaml:"instanceType"` - SecurityGroupID string `json:"securityGroupId" yaml:"securityGroupId"` - SecurityGroupName string `json:"securityGroup" yaml:"securityGroup"` - VpcID string `json:"vpcId" yaml:"vpcId"` - VSwitchID string `json:"vswitchId" yaml:"vswitchId"` - Zone string `json:"zone" yaml:"zone"` - PrivateIPOnly bool `json:"privateAddressOnly" yaml:"privateAddressOnly"` - InternetMaxBandwidthOut string `json:"internetMaxBandwidth" yaml:"internetMaxBandwidth"` - InternetChargeType string `json:"internetChargeType" yaml:"internetChargeType"` - RouteCIDR string `json:"routeCidr" yaml:"routeCidr"` - SLBID string `json:"slbId" yaml:"slbId"` - DiskSize string `json:"diskSize" yaml:"diskSize"` - DiskFS string `json:"diskFs" yaml:"diskFs"` - DiskCategory string `json:"diskCategory" yaml:"diskCategory"` - Description string `json:"description" yaml:"description"` - APIEndpoint string `json:"apiEndpoint" yaml:"apiEndpoint"` - SystemDiskCategory string `json:"systemDiskCategory" yaml:"systemDiskCategory"` - SystemDiskSize string `json:"SystemDiskSize" yaml:"SystemDiskSize"` - ResourceGroupID string `json:"resourceGroupId" yaml:"resourceGroupId"` - InstanceChargeType string `json:"instanceChargeType" yaml:"instanceChargeType"` - Period string `json:"period" yaml:"period"` - PeriodUnit string `json:"periodUnit" yaml:"periodUnit"` - SpotStrategy string `json:"spotStrategy" yaml:"spotStrategy"` - SpotPriceLimit string `json:"spotPriceLimit" yaml:"spotPriceLimit"` - SpotDuration string `json:"spotDuration" yaml:"spotDuration"` - OpenPorts []string `json:"openPort" yaml:"openPort"` -} diff --git a/go.mod b/go.mod index 5fb470e0..4147dcb3 100644 --- a/go.mod +++ b/go.mod @@ -43,7 +43,6 @@ require ( github.com/rancher/wrangler/v3 v3.0.0 github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.8.0 - github.com/stretchr/testify v1.9.0 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.715 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.715 golang.org/x/crypto v0.25.0 @@ -152,8 +151,8 @@ require ( github.com/shopspring/decimal v1.3.1 // indirect github.com/spf13/cast v1.6.0 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/tjfoc/gmsm v1.4.1 // indirect github.com/stretchr/objx v0.5.2 // indirect + github.com/tjfoc/gmsm v1.4.1 // indirect github.com/xlab/treeprint v1.2.0 // indirect github.com/zclconf/go-cty v1.14.1 // indirect go.etcd.io/etcd/api/v3 v3.5.13 // indirect