Skip to content

Commit

Permalink
wip: write tests with egoscale v3
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterp committed Mar 7, 2024
1 parent 96b5084 commit 991b1ff
Show file tree
Hide file tree
Showing 6 changed files with 354 additions and 111 deletions.
11 changes: 6 additions & 5 deletions internal/integ/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package cluster
import (
"context"

exov2 "github.com/exoscale/egoscale/v2"
exov3 "github.com/exoscale/egoscale/v3"
"github.com/exoscale/exoscale/csi-driver/internal/integ/k8s"
)

Expand All @@ -17,13 +17,14 @@ func Get() *Cluster {
}

type Cluster struct {
exoV2Context context.Context
exoV2ContextCancel context.CancelFunc
// TODO (sauterp) get rid of these
context context.Context
cancelContext context.CancelFunc

Name string
ID string
ID exov3.UUID
K8s *k8s.K8S
Ego *exov2.Client
Ego *exov3.Client
APIKeyName string
APIRoleName string
}
80 changes: 43 additions & 37 deletions internal/integ/cluster/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,35 @@ import (

"github.com/exoscale/exoscale/csi-driver/internal/integ/flags"

exov2 "github.com/exoscale/egoscale/v2"
exov3 "github.com/exoscale/egoscale/v3"
)

func (c *Cluster) getLatestSKSVersion() (string, error) {
versions, err := c.Ego.ListSKSClusterVersions(c.exoV2Context)
versions, err := c.Ego.ListSKSClusterVersions(c.context)
if err != nil {
return "", fmt.Errorf("error retrieving SKS versions: %w", err)
}

if len(versions) == 0 {
if len(versions.SKSClusterVersions) == 0 {
return "", fmt.Errorf("no SKS version returned by the API")
}

return versions[0], nil
return versions.SKSClusterVersions[0], nil
}

func (c *Cluster) getInstanceType(family, size string) (*exov3.InstanceType, error) {
instanceTypes, err := c.Ego.ListInstanceTypes(c.context)
if err != nil {
return nil, err
}

for _, instanceType := range instanceTypes.InstanceTypes {
if instanceType.Family == exov3.InstanceTypeFamilyStandard && instanceType.Size == exov3.InstanceTypeSizeMedium {
return c.Ego.GetInstanceType(c.context, instanceType.ID)
}
}

return nil, fmt.Errorf("unable to find instance type %s.%s", family, size)
}

func (c *Cluster) provisionSKSCluster(zone string) error {
Expand All @@ -37,49 +52,40 @@ func (c *Cluster) provisionSKSCluster(zone string) error {
}

// intance type must be at least standard.medium for block storage volume attachment to work
instanceType, err := c.Ego.FindInstanceType(c.exoV2Context, zone, "standard.medium")
instanceType, err := c.getInstanceType("standard", "medium")
if err != nil {
return err
}

nodepool := exov2.SKSNodepool{
Name: ptr(c.Name + "-nodepool"),
DiskSize: ptr(int64(20)),
Size: ptr(int64(2)),
InstancePrefix: ptr("pool"),
InstanceTypeID: instanceType.ID,
}

sksCluster := &exov2.SKSCluster{
AddOns: &[]string{
// TODO(sauterp) remove once the CCM is no longer necessary for the CSI.
newCluster, err := c.Ego.CreateSKSCluster(c.context, exov3.CreateSKSClusterRequest{
Addons: []string{
"exoscale-cloud-controller",
},
CNI: ptr("calico"),
Description: ptr("This cluster was created to test the exoscale CSI driver in SKS."),
Name: ptr(c.Name),
Nodepools: []*exov2.SKSNodepool{
ptr(nodepool),
},
ServiceLevel: ptr("pro"),
Version: ptr(latestSKSVersion),
Zone: ptr(zone),
}

newCluster, err := c.Ego.CreateSKSCluster(c.exoV2Context, zone, sksCluster)
Cni: "calico",
Description: "This cluster was created to test the exoscale CSI driver in SKS.",
Name: c.Name,
Level: exov3.CreateSKSClusterRequestLevelPro,
Version: latestSKSVersion,
})
if err != nil {
return err
}

c.ID = *newCluster.ID
slog.Info("successfully created cluster", "clusterID", c.ID)
c.ID = newCluster.ID

_, err = c.Ego.CreateSKSNodepool(c.exoV2Context, zone, newCluster, &nodepool)
_, err = c.Ego.CreateSKSNodepool(c.context, newCluster.ID, exov3.CreateSKSNodepoolRequest{
Name: c.Name + "-nodepool",
DiskSize: int64(20),
Size: int64(2),
InstancePrefix: "pool",
InstanceType: instanceType,
})
if err != nil {
// this can error even when the nodepool is successfully created
// it's probably a bug, so we're not returning the error
slog.Warn("error creating nodepool", "err", err)
}
slog.Info("successfully created cluster", "clusterID", c.ID)

return nil
}
Expand All @@ -93,16 +99,16 @@ func exitApplication(msg string, err error) {
}

func ConfigureCluster(createCluster bool, name, zone string) (*Cluster, error) {
v2Client, ctx, ctxCancel, err := createV2ClientAndContext()
v3Client, ctx, ctxCancel, err := createV3ClientAndContext()
if err != nil {
return nil, fmt.Errorf("error creating egoscale v2 client: %w", err)
return nil, fmt.Errorf("error creating egoscale v3 client: %w", err)
}

cluster := &Cluster{
Ego: v2Client,
Name: name,
exoV2Context: ctx,
exoV2ContextCancel: ctxCancel,
Ego: v3Client,
Name: name,
context: ctx,
cancelContext: ctxCancel,
}

if createCluster {
Expand Down
22 changes: 17 additions & 5 deletions internal/integ/cluster/teardown.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"log/slog"

"github.com/exoscale/exoscale/csi-driver/internal/integ/flags"

exov3 "github.com/exoscale/egoscale/v3"
)

func (c *Cluster) tearDownCluster() error {
Expand All @@ -13,18 +15,28 @@ func (c *Cluster) tearDownCluster() error {
return fmt.Errorf("error getting cluster ID: %w", err)
}

cluster, err := c.Ego.GetSKSCluster(c.exoV2Context, *flags.Zone, id)
cluster, err := c.Ego.GetSKSCluster(c.context, id)
if err != nil {
return err
}

if len(cluster.Nodepools) > 0 {
if err := c.Ego.DeleteSKSNodepool(c.exoV2Context, *flags.Zone, cluster, cluster.Nodepools[0]); err != nil {
if err := c.awaitSuccess(c.Ego.DeleteSKSNodepool(c.context, cluster.ID, cluster.Nodepools[0].ID)); err != nil {
return fmt.Errorf("error deleting nodepool: %w", err)
}
}

return c.Ego.DeleteSKSCluster(c.exoV2Context, *flags.Zone, cluster)
return c.awaitSuccess(c.Ego.DeleteSKSCluster(c.context, cluster.ID))
}

func (c *Cluster) awaitSuccess(op *exov3.Operation, err error) error {
if err != nil {
return err
}

_, err = c.Ego.Wait(c.context, op, exov3.OperationStateSuccess)

return err
}

func (c *Cluster) TearDown() error {
Expand All @@ -40,7 +52,7 @@ func (c *Cluster) TearDown() error {
}
}

c.exoV2ContextCancel()
c.cancelContext()

return nil
}
Expand All @@ -49,7 +61,7 @@ func (c *Cluster) tearDownCSI() error {
var finalErr error = nil

for _, manifestPath := range allManifests {
err := c.K8s.DeleteManifest(c.exoV2Context, manifestDir+manifestPath)
err := c.K8s.DeleteManifest(c.context, manifestDir+manifestPath)
if err != nil {
slog.Error("failed to delete manifest", "manifest", manifestPath, "err", err)

Expand Down
Loading

0 comments on commit 991b1ff

Please sign in to comment.