Skip to content

Commit

Permalink
remove context timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterp committed Mar 8, 2024
1 parent 0cf9178 commit 2f30dfa
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 87 deletions.
3 changes: 1 addition & 2 deletions internal/integ/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ func Get() *Cluster {

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

Name string
ID exov3.UUID
Expand Down
56 changes: 30 additions & 26 deletions internal/integ/cluster/setup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cluster

import (
"context"
"flag"
"fmt"
"log/slog"
Expand All @@ -11,8 +12,8 @@ import (
exov3 "github.com/exoscale/egoscale/v3"
)

func (c *Cluster) getLatestSKSVersion() (string, error) {
versions, err := c.Ego.ListSKSClusterVersions(c.context)
func (c *Cluster) getLatestSKSVersion(ctx context.Context) (string, error) {
versions, err := c.Ego.ListSKSClusterVersions(ctx)
if err != nil {
return "", fmt.Errorf("error retrieving SKS versions: %w", err)
}
Expand All @@ -24,40 +25,40 @@ func (c *Cluster) getLatestSKSVersion() (string, error) {
return versions.SKSClusterVersions[0], nil
}

func (c *Cluster) getInstanceType(family, size string) (*exov3.InstanceType, error) {
instanceTypes, err := c.Ego.ListInstanceTypes(c.context)
func (c *Cluster) getInstanceType(ctx context.Context, family, size string) (*exov3.InstanceType, error) {
instanceTypes, err := c.Ego.ListInstanceTypes(ctx)
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 c.Ego.GetInstanceType(ctx, instanceType.ID)
}
}

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

func (c *Cluster) provisionSKSCluster(zone string) error {
func (c *Cluster) provisionSKSCluster(ctx context.Context, zone string) error {
// do nothing if cluster exists
_, err := c.getCluster()
_, err := c.getCluster(ctx)
if err == nil {
return nil
}

latestSKSVersion, err := c.getLatestSKSVersion()
latestSKSVersion, err := c.getLatestSKSVersion(ctx)
if err != nil {
return err
}

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

newClusterID, err := c.awaitID(c.Ego.CreateSKSCluster(c.context, exov3.CreateSKSClusterRequest{
op, err := c.Ego.CreateSKSCluster(ctx, exov3.CreateSKSClusterRequest{
Addons: []string{
"exoscale-cloud-controller",
},
Expand All @@ -66,20 +67,22 @@ func (c *Cluster) provisionSKSCluster(zone string) error {
Name: c.Name,
Level: exov3.CreateSKSClusterRequestLevelPro,
Version: latestSKSVersion,
}))
})
newClusterID, err := c.awaitID(ctx, op, err)
if err != nil {
return err
}

c.ID = newClusterID

if err = c.awaitSuccess(c.Ego.CreateSKSNodepool(c.context, newClusterID, exov3.CreateSKSNodepoolRequest{
op, err = c.Ego.CreateSKSNodepool(ctx, newClusterID, exov3.CreateSKSNodepoolRequest{
Name: c.Name + "-nodepool",
DiskSize: int64(20),
Size: int64(2),
InstancePrefix: "pool",
InstanceType: instanceType,
})); err != nil {
})
if err = c.awaitSuccess(ctx, op, err); 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)
Expand All @@ -97,27 +100,26 @@ func exitApplication(msg string, err error) {
os.Exit(1)
}

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

cluster := &Cluster{
Ego: v3Client,
Name: name,
context: ctx,
cancelContext: ctxCancel,
Ego: client,
Name: name,
context: context.Background(),
}

if createCluster {
err = cluster.provisionSKSCluster(zone)
err = cluster.provisionSKSCluster(ctx, zone)
if err != nil {
return nil, fmt.Errorf("error creating SKS cluster: %w", err)
}
}

id, err := cluster.getClusterID()
id, err := cluster.getClusterID(ctx)
if err != nil {
return nil, fmt.Errorf("error getting cluster ID: %w", err)
}
Expand All @@ -126,7 +128,7 @@ func ConfigureCluster(createCluster bool, name, zone string) (*Cluster, error) {
cluster.APIKeyName = apiKeyPrefix + cluster.Name
cluster.APIRoleName = cluster.APIKeyName + "-role"

k, err := cluster.getK8sClients()
k, err := cluster.getK8sClients(ctx)
if err != nil {
return nil, fmt.Errorf("error initializing k8s clients: %w", err)
}
Expand All @@ -137,30 +139,32 @@ func ConfigureCluster(createCluster bool, name, zone string) (*Cluster, error) {
}

func Setup() error {
ctx := context.Background()

if err := flags.ValidateFlags(); err != nil {
exitApplication("invalid flags", err)

return err
}

var err error
testCluster, err = ConfigureCluster(*flags.CreateCluster, *flags.ClusterName, *flags.Zone)
testCluster, err = ConfigureCluster(ctx, *flags.CreateCluster, *flags.ClusterName, *flags.Zone)
if err != nil {
return err
}

calicoControllerName := "calico-kube-controllers"
if err := testCluster.awaitDeploymentReadiness(calicoControllerName); err != nil {
if err := testCluster.awaitDeploymentReadiness(ctx, calicoControllerName); err != nil {
slog.Warn("error while awaiting", "deployment", calicoControllerName, "error", err)
}

calicoNodeName := "calico-node"
if err := testCluster.awaitDaemonSetReadiness(calicoNodeName); err != nil {
if err := testCluster.awaitDaemonSetReadiness(ctx, calicoNodeName); err != nil {
slog.Warn("error while awaiting", "DaemonSet", calicoNodeName, "error", err)
}

if !*flags.DontApplyCSI {
if err := testCluster.applyCSI(); err != nil {
if err := testCluster.applyCSI(ctx); err != nil {
return fmt.Errorf("error applying CSI: %w", err)
}
}
Expand Down
33 changes: 17 additions & 16 deletions internal/integ/cluster/teardown.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@ import (
exov3 "github.com/exoscale/egoscale/v3"
)

func (c *Cluster) tearDownCluster() error {
id, err := c.getClusterID()
func (c *Cluster) tearDownCluster(ctx context.Context) error {
id, err := c.getClusterID(ctx)
if err != nil {
return fmt.Errorf("error getting cluster ID: %w", err)
}

cluster, err := c.Ego.GetSKSCluster(c.context, id)
cluster, err := c.Ego.GetSKSCluster(ctx, id)
if err != nil {
return err
}

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

return c.awaitSuccess(c.Ego.DeleteSKSCluster(c.context, cluster.ID))
op, err := c.Ego.DeleteSKSCluster(ctx, cluster.ID)
return c.awaitSuccess(ctx, op, err)
}

func (c Cluster) Wait(ctx context.Context, op *exov3.Operation, states ...exov3.OperationState) (*exov3.Operation, error) {
Expand Down Expand Up @@ -88,12 +90,12 @@ polling:
)
}

func (c *Cluster) awaitID(op *exov3.Operation, err error) (exov3.UUID, error) {
func (c *Cluster) awaitID(ctx context.Context, op *exov3.Operation, err error) (exov3.UUID, error) {
if err != nil {
return "", err
}

finishedOP, err := c.Wait(c.context, op, exov3.OperationStateSuccess)
finishedOP, err := c.Wait(ctx, op, exov3.OperationStateSuccess)
if err != nil {
return "", err
}
Expand All @@ -105,42 +107,41 @@ func (c *Cluster) awaitID(op *exov3.Operation, err error) (exov3.UUID, error) {
return "", nil
}

func (c *Cluster) awaitSuccess(op *exov3.Operation, err error) error {
_, err = c.awaitID(op, err)
func (c *Cluster) awaitSuccess(ctx context.Context, op *exov3.Operation, err error) error {
_, err = c.awaitID(ctx, op, err)
return err
}

func (c *Cluster) TearDown() error {
ctx := context.Background()
if *flags.TearDownCSI {
if err := c.tearDownCSI(); err != nil {
if err := c.tearDownCSI(ctx); err != nil {
return err
}
}

if *flags.TearDownCluster {
if err := c.tearDownCluster(); err != nil {
if err := c.tearDownCluster(ctx); err != nil {
return err
}
}

c.cancelContext()

return nil
}

func (c *Cluster) tearDownCSI() error {
func (c *Cluster) tearDownCSI(ctx context.Context) error {
var finalErr error = nil

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

finalErr = fmt.Errorf("errors while deleting manifests: %w", err)
}
}

err := c.deleteAPIKeyAndRole()
err := c.deleteAPIKeyAndRole(ctx)
if err != nil {
slog.Error("failed to clean up CSI API key and role", "err", err)

Expand Down
Loading

0 comments on commit 2f30dfa

Please sign in to comment.