github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/cce/helpers.go (about)

     1  package cce
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/opentelekomcloud/gophertelekomcloud"
     8  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
     9  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
    10  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/clusters"
    11  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/compute/v2/extensions/keypairs"
    12  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    13  )
    14  
    15  func CreateCluster(t *testing.T, vpcID, subnetID string) string {
    16  	client, err := clients.NewCceV3Client()
    17  	th.AssertNoErr(t, err)
    18  
    19  	cluster, err := clusters.Create(client, clusters.CreateOpts{
    20  		Kind:       "Cluster",
    21  		ApiVersion: "v3",
    22  		Metadata: clusters.CreateMetaData{
    23  			Name: strings.ToLower(tools.RandomString("cce-gopher-", 4)),
    24  		},
    25  		Spec: clusters.Spec{
    26  			Type:   "VirtualMachine",
    27  			Flavor: "cce.s1.small",
    28  			HostNetwork: clusters.HostNetworkSpec{
    29  				VpcId:    vpcID,
    30  				SubnetId: subnetID,
    31  			},
    32  			Version: "v1.25",
    33  			ContainerNetwork: clusters.ContainerNetworkSpec{
    34  				Mode: "overlay_l2",
    35  			},
    36  			Authentication: clusters.AuthenticationSpec{
    37  				Mode:                "rbac",
    38  				AuthenticatingProxy: make(map[string]string),
    39  			},
    40  			KubernetesSvcIpRange: "10.247.0.0/16",
    41  		},
    42  	}).Extract()
    43  	th.AssertNoErr(t, err)
    44  
    45  	th.AssertNoErr(t, waitForClusterToActivate(client, cluster.Metadata.Id, 30*60))
    46  	return cluster.Metadata.Id
    47  }
    48  
    49  func CreateTurboCluster(t *testing.T, vpcID, subnetID string, eniSubnetID string, eniCidr string) string {
    50  	client, err := clients.NewCceV3Client()
    51  	th.AssertNoErr(t, err)
    52  
    53  	cluster, err := clusters.Create(client, clusters.CreateOpts{
    54  		Kind:       "Cluster",
    55  		ApiVersion: "v3",
    56  		Metadata: clusters.CreateMetaData{
    57  			Name: strings.ToLower(tools.RandomString("cce-gopher-turbo-", 4)),
    58  		},
    59  		Spec: clusters.Spec{
    60  			Category: "Turbo",
    61  			Type:     "VirtualMachine",
    62  			Flavor:   "cce.s1.small",
    63  			HostNetwork: clusters.HostNetworkSpec{
    64  				VpcId:    vpcID,
    65  				SubnetId: subnetID,
    66  			},
    67  			ContainerNetwork: clusters.ContainerNetworkSpec{
    68  				Mode: "eni",
    69  			},
    70  			EniNetwork: &clusters.EniNetworkSpec{
    71  				SubnetId: eniSubnetID,
    72  				Cidr:     eniCidr,
    73  			},
    74  			Authentication: clusters.AuthenticationSpec{
    75  				Mode:                "rbac",
    76  				AuthenticatingProxy: make(map[string]string),
    77  			},
    78  			KubernetesSvcIpRange: "10.247.0.0/16",
    79  		},
    80  	}).Extract()
    81  	th.AssertNoErr(t, err)
    82  
    83  	th.AssertNoErr(t, waitForClusterToActivate(client, cluster.Metadata.Id, 30*60))
    84  	return cluster.Metadata.Id
    85  }
    86  
    87  func DeleteCluster(t *testing.T, clusterID string) {
    88  	client, err := clients.NewCceV3Client()
    89  	th.AssertNoErr(t, err)
    90  	err = clusters.DeleteWithOpts(client, clusterID, clusters.DeleteOpts{
    91  		DeleteEfs: "true",
    92  		DeleteObs: "true",
    93  		DeleteSfs: "true",
    94  	})
    95  	th.AssertNoErr(t, err)
    96  	th.AssertNoErr(t, waitForClusterToDelete(client, clusterID, 20*60))
    97  }
    98  
    99  func waitForClusterToActivate(client *golangsdk.ServiceClient, id string, secs int) error {
   100  	return golangsdk.WaitFor(secs, func() (bool, error) {
   101  		cluster, err := clusters.Get(client, id).Extract()
   102  		if err != nil {
   103  			return false, err
   104  		}
   105  		if cluster == nil {
   106  			return false, nil
   107  		}
   108  		if cluster.Status.Phase == "Available" {
   109  			return true, nil
   110  		}
   111  		return false, nil
   112  	})
   113  }
   114  
   115  func waitForClusterToDelete(client *golangsdk.ServiceClient, id string, secs int) error {
   116  	return golangsdk.WaitFor(secs, func() (bool, error) {
   117  		_, err := clusters.Get(client, id).Extract()
   118  		if err != nil {
   119  			if _, ok := err.(golangsdk.ErrDefault404); ok {
   120  				return true, nil
   121  			}
   122  			return false, err
   123  		}
   124  		return false, nil
   125  	})
   126  }
   127  
   128  func CreateKeypair(t *testing.T) string {
   129  	client, err := clients.NewComputeV2Client()
   130  	th.AssertNoErr(t, err)
   131  
   132  	opts := keypairs.CreateOpts{
   133  		Name: tools.RandomString("cce-nodes-", 4),
   134  	}
   135  	_, err = keypairs.Create(client, opts).Extract()
   136  	th.AssertNoErr(t, err)
   137  	return opts.Name
   138  }
   139  
   140  func DeleteKeypair(t *testing.T, kp string) {
   141  	client, err := clients.NewComputeV2Client()
   142  	th.AssertNoErr(t, err)
   143  
   144  	th.AssertNoErr(t, keypairs.Delete(client, kp).ExtractErr())
   145  }