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

[Refactor] Refactor CCE clusters v3 #761

Merged
merged 17 commits into from
Nov 29, 2024
Merged
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
16 changes: 8 additions & 8 deletions acceptance/openstack/cce/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strings"
"testing"

"github.com/opentelekomcloud/gophertelekomcloud"
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/clusters"
Expand Down Expand Up @@ -44,14 +44,14 @@ func CreateCluster(t *testing.T, vpcID, subnetID string) string {
"kubernetes.io/cpuManagerPolicy": "static",
},
},
}).Extract()
})
th.AssertNoErr(t, err)

th.AssertNoErr(t, waitForClusterToActivate(client, cluster.Metadata.Id, 30*60))
return cluster.Metadata.Id
}

func CreateTurboCluster(t *testing.T, vpcID, subnetID string, eniSubnetID string, eniCidr string) string {
func CreateTurboCluster(t *testing.T, vpcID, subnetID string, eniSubnetID string, eniCidr string) *clusters.Clusters {
client, err := clients.NewCceV3Client()
th.AssertNoErr(t, err)

Expand Down Expand Up @@ -82,17 +82,17 @@ func CreateTurboCluster(t *testing.T, vpcID, subnetID string, eniSubnetID string
},
KubernetesSvcIpRange: "10.247.0.0/16",
},
}).Extract()
})
th.AssertNoErr(t, err)

th.AssertNoErr(t, waitForClusterToActivate(client, cluster.Metadata.Id, 30*60))
return cluster.Metadata.Id
return cluster
}

func DeleteCluster(t *testing.T, clusterID string) {
client, err := clients.NewCceV3Client()
th.AssertNoErr(t, err)
err = clusters.DeleteWithOpts(client, clusterID, clusters.DeleteOpts{
err = clusters.Delete(client, clusterID, clusters.DeleteQueryParams{
DeleteEfs: "true",
DeleteObs: "true",
DeleteSfs: "true",
Expand All @@ -103,7 +103,7 @@ func DeleteCluster(t *testing.T, clusterID string) {

func waitForClusterToActivate(client *golangsdk.ServiceClient, id string, secs int) error {
return golangsdk.WaitFor(secs, func() (bool, error) {
cluster, err := clusters.Get(client, id).Extract()
cluster, err := clusters.Get(client, id)
if err != nil {
return false, err
}
Expand All @@ -119,7 +119,7 @@ func waitForClusterToActivate(client *golangsdk.ServiceClient, id string, secs i

func waitForClusterToDelete(client *golangsdk.ServiceClient, id string, secs int) error {
return golangsdk.WaitFor(secs, func() (bool, error) {
_, err := clusters.Get(client, id).Extract()
_, err := clusters.Get(client, id)
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
return true, nil
Expand Down
61 changes: 36 additions & 25 deletions acceptance/openstack/cce/v3/clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,50 @@ import (

"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/openstack/cce"
"github.com/stretchr/testify/suite"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/clusters"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v1/subnets"
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
)

type testCluster struct {
suite.Suite
func TestListCluster(t *testing.T) {
client, err := clients.NewCceV3Client()
th.AssertNoErr(t, err)

vpcID string
subnetID string
clusterID string
eniSubnetID string
eniCidr string
_, err = clusters.List(client, clusters.ListOpts{})
th.AssertNoErr(t, err)
}

func TestCluster(t *testing.T) {
suite.Run(t, new(testCluster))
}
vpcID := clients.EnvOS.GetEnv("VPC_ID")
if vpcID == "" {
t.Skip("OS_VPC_ID is required for this test")
}

clientNet, err := clients.NewNetworkV1Client()
th.AssertNoErr(t, err)

func (s *testCluster) SetupSuite() {
t := s.T()
s.vpcID = clients.EnvOS.GetEnv("VPC_ID")
s.subnetID = clients.EnvOS.GetEnv("NETWORK_ID")
s.eniSubnetID = clients.EnvOS.GetEnv("ENI_SUBNET_ID")
s.eniCidr = "10.0.0.0/14"
if s.vpcID == "" || s.subnetID == "" || s.eniSubnetID == "" {
t.Skip("OS_VPC_ID, OS_NETWORK_ID and OS_ENI_SUBNET_ID are required for this test")
listOpts := subnets.ListOpts{
VpcID: vpcID,
}
s.clusterID = cce.CreateTurboCluster(t, s.vpcID, s.subnetID, s.eniSubnetID, s.eniCidr)
}
subnetsList, err := subnets.List(clientNet, listOpts)
th.AssertNoErr(t, err)

if len(subnetsList) < 1 {
t.Skip("no subnets found in selected VPC")
}

client, err := clients.NewCceV3Client()
th.AssertNoErr(t, err)

cluster := cce.CreateTurboCluster(t, vpcID, subnetsList[0].NetworkID, subnetsList[0].SubnetID, subnetsList[0].CIDR)

clusterID := cluster.Metadata.Id

clusterGet, err := clusters.Get(client, clusterID)
th.AssertNoErr(t, err)
th.AssertEquals(t, cluster.Metadata.Name, clusterGet.Metadata.Name)

func (s *testCluster) TearDownSuite() {
t := s.T()
if s.clusterID != "" {
cce.DeleteCluster(t, s.clusterID)
s.clusterID = ""
if clusterID != "" {
cce.DeleteCluster(t, clusterID)
}
}
45 changes: 11 additions & 34 deletions acceptance/openstack/cce/v3/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,29 @@ import (
"github.com/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/clusters"
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

type testKubeConfig struct {
suite.Suite

routerID string
subnetID string
clusterID string
}

func TestKubeConfig(t *testing.T) {
suite.Run(t, new(testKubeConfig))
}

func (s *testKubeConfig) SetupSuite() {
t := s.T()
s.routerID = clients.EnvOS.GetEnv("VPC_ID", "ROUTER_ID")
s.subnetID = clients.EnvOS.GetEnv("NETWORK_ID")
if s.routerID == "" || s.subnetID == "" {
routerID := clients.EnvOS.GetEnv("VPC_ID", "ROUTER_ID")
subnetID := clients.EnvOS.GetEnv("NETWORK_ID")
if routerID == "" || subnetID == "" {
t.Skip("OS_ROUTER_ID and OS_NETWORK_ID are required for this test")
}
s.clusterID = cce.CreateCluster(t, s.routerID, s.subnetID)
}

func (s *testKubeConfig) TearDownSuite() {
t := s.T()
if s.clusterID != "" {
cce.DeleteCluster(t, s.clusterID)
s.clusterID = ""
}
}

func (s *testKubeConfig) TestKubeConfigReading() {
t := s.T()

client, err := clients.NewCceV3Client()
th.AssertNoErr(t, err)

kubeConfig, err := clusters.GetCert(client, s.clusterID).ExtractMap()
clusterID := cce.CreateCluster(t, routerID, subnetID)
t.Cleanup(func() {
cce.DeleteCluster(t, clusterID)
})

kubeConfig, err := clusters.GetCert(client, clusterID)
th.AssertNoErr(t, err)
require.NotEmpty(t, kubeConfig)

kubeConfigExp, err := clusters.GetCertWithExpiration(client, s.clusterID, clusters.ExpirationOpts{
kubeConfigExp, err := clusters.GetCertWithExpiration(client, clusterID, clusters.ExpirationOpts{
Duration: 5,
}).ExtractMap()
})
th.AssertNoErr(t, err)
require.NotEmpty(t, kubeConfigExp)
}
49 changes: 49 additions & 0 deletions openstack/cce/v3/clusters/Create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package clusters

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

// CreateOpts contains all the values needed to create a new cluster
type CreateOpts struct {
// API type, fixed value Cluster
Kind string `json:"kind" required:"true"`
// API version, fixed value v3
ApiVersion string `json:"apiversion" required:"true"`
// Metadata required to create a cluster
Metadata CreateMetaData `json:"metadata" required:"true"`
// specifications to create a cluster
Spec Spec `json:"spec" required:"true"`
}

// Metadata required to create a cluster
type CreateMetaData struct {
// Cluster unique name
Name string `json:"name" required:"true"`
// Cluster tag, key/value pair format
Labels map[string]string `json:"labels,omitempty"`
// Cluster annotation, key/value pair format
Annotations map[string]string `json:"annotations,omitempty"`
}

// Create accepts a CreateOpts struct and uses the values to create a new
// logical cluster.
func Create(client *golangsdk.ServiceClient, opts CreateOpts) (*Clusters, error) {
b, err := build.RequestBody(opts, "")
if err != nil {
return nil, err
}

// POST /api/v3/projects/{project_id}/clusters
raw, err := client.Post(client.ServiceURL("clusters"), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{201},
})
if err != nil {
return nil, err
}

var res Clusters
return &res, extract.Into(raw.Body, &res)
}
34 changes: 34 additions & 0 deletions openstack/cce/v3/clusters/Delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package clusters

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
)

type DeleteQueryParams struct {
ErrorStatus string `q:"errorStatus,omitempty"`
DeleteEfs string `q:"delete_efs,omitempty"`
DeleteENI string `q:"delete_eni,omitempty"`
DeleteEvs string `q:"delete_evs,omitempty"`
DeleteNet string `q:"delete_net,omitempty"`
DeleteObs string `q:"delete_obs,omitempty"`
DeleteSfs string `q:"delete_sfs,omitempty"`
}

// Delete will permanently delete a particular cluster based on its unique ID.
func Delete(client *golangsdk.ServiceClient, clusterId string, opts DeleteQueryParams) error {

url, err := golangsdk.NewURLBuilder().WithEndpoints("clusters", clusterId).WithQueryParams(opts).Build()
if err != nil {
return err
}
_, err = client.Delete(client.ServiceURL(url.String()), &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: map[string]string{"Content-Type": "application/json"},
JSONBody: nil,
})

if err != nil {
return err
}
return nil
}
22 changes: 22 additions & 0 deletions openstack/cce/v3/clusters/Get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package clusters

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

// Get retrieves a particular cluster based on its unique ID.
func Get(client *golangsdk.ServiceClient, clusterId string) (*Clusters, error) {
// GET /api/v3/projects/{project_id}/clusters/{cluster_id}
raw, err := client.Get(client.ServiceURL("clusters", clusterId), nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: map[string]string{"Content-Type": "application/json"},
JSONBody: nil,
})
if err != nil {
return nil, err
}

var res Clusters
return &res, extract.Into(raw.Body, &res)
}
21 changes: 21 additions & 0 deletions openstack/cce/v3/clusters/GetCert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package clusters

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

// GetCert retrieves a particular cluster certificate based on its unique ID. (Depreciated)
func GetCert(client *golangsdk.ServiceClient, clusterId string) (*Certificate, error) {
// GET /api/v3/projects/{project_id}/clusters/{cluster_id}/clustercert
raw, err := client.Get(client.ServiceURL("clusters", clusterId, "clustercert"), nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: map[string]string{"Content-Type": "application/json"},
})
if err != nil {
return nil, err
}

var res Certificate
return &res, extract.Into(raw.Body, &res)
}
31 changes: 31 additions & 0 deletions openstack/cce/v3/clusters/GetCertWithExpiration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package clusters

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

type ExpirationOpts struct {
Duration int `json:"duration" required:"true"`
}

// GetCertWithExpiration retrieves a particular cluster certificate based on its unique ID.
func GetCertWithExpiration(client *golangsdk.ServiceClient, clusterId string, opts ExpirationOpts) (*Certificate, error) {
b, err := build.RequestBody(opts, "")
if err != nil {
return nil, err
}

// POST /api/v3/projects/{project_id}/clusters/{cluster_id}/clustercert
raw, err := client.Post(client.ServiceURL("clusters", clusterId, "clustercert"), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: map[string]string{"Content-Type": "application/json"},
})
if err != nil {
return nil, err
}

var res Certificate
return &res, extract.Into(raw.Body, &res)
}
Loading