Skip to content
Prev Previous commit
Next Next commit
some refacto
Signed-off-by: Pierre-Emmanuel Jacquier <[email protected]>
  • Loading branch information
pierre-emmanuelJ committed Oct 30, 2025
commit 6ef9ea3eeb66ec3b00dcd96fae0ea5019dbdc63a
66 changes: 30 additions & 36 deletions drivers/exoscale/exoscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,12 @@ func (d *Driver) GetState() (state.State, error) {
}

func (d *Driver) createDefaultSecurityGroup(ctx context.Context, sgName string) (v3.UUID, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This SG is never needed to be cleaned-up, at some point?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

580c27d not urgent for now since the driver lived until now without.

cs, err := d.client(ctx)
client, err := d.client(ctx)
if err != nil {
return "", err
}

op, err := cs.CreateSecurityGroup(ctx,
op, err := client.CreateSecurityGroup(ctx,
v3.CreateSecurityGroupRequest{
Name: sgName,
Description: "created by docker-machine",
Expand All @@ -349,7 +349,7 @@ func (d *Driver) createDefaultSecurityGroup(ctx context.Context, sgName string)
return "", err
}

res, err := cs.Wait(ctx, op, v3.OperationStateSuccess)
res, err := client.Wait(ctx, op, v3.OperationStateSuccess)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -429,7 +429,7 @@ func (d *Driver) createDefaultSecurityGroup(ctx context.Context, sgName string)
for _, req := range requests {
req.FlowDirection = v3.AddRuleToSecurityGroupRequestFlowDirectionIngress
if req.Network != "" {
err := addRuleToSG(ctx, cs, sgID, req)
err := addRuleToSG(ctx, client, sgID, req)
if err != nil {
return "", err
}
Expand All @@ -438,13 +438,13 @@ func (d *Driver) createDefaultSecurityGroup(ctx context.Context, sgName string)
for _, cidr := range cidrList {
req.Network = cidr

err := addRuleToSG(ctx, cs, sgID, req)
err := addRuleToSG(ctx, client, sgID, req)
if err != nil {
return "", err
}
}
} else {
err := addRuleToSG(ctx, cs, sgID, req)
err := addRuleToSG(ctx, client, sgID, req)
if err != nil {
return "", err
}
Expand All @@ -462,28 +462,25 @@ func addRuleToSG(ctx context.Context, cs *v3.Client, sgID v3.UUID, req v3.AddRul
}

_, err = cs.Wait(ctx, op, v3.OperationStateSuccess)
if err != nil {
return err
}

return nil
return err
}

func (d *Driver) createDefaultAffinityGroup(ctx context.Context, agName string) (v3.UUID, error) {
cs, err := d.client(ctx)
client, err := d.client(ctx)
if err != nil {
return "", err
}

resp, err := cs.CreateAntiAffinityGroup(ctx, v3.CreateAntiAffinityGroupRequest{
resp, err := client.CreateAntiAffinityGroup(ctx, v3.CreateAntiAffinityGroupRequest{
Name: agName,
Description: "created by docker-machine",
})
if err != nil {
return "", err
}

op, err := cs.Wait(ctx, resp)
op, err := client.Wait(ctx, resp, v3.OperationStateSuccess)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -541,7 +538,7 @@ func (d *Driver) Create() error {
}
}
if template.ID == "" {
return fmt.Errorf("Unable to find image %v", d.Image)
return fmt.Errorf("unable to find image %v", d.Image)
}

// Reading the username from the template
Expand Down Expand Up @@ -575,15 +572,13 @@ func (d *Driver) Create() error {
return err
}

var found *v3.SecurityGroup
for _, elem := range sglist.SecurityGroups {
if string(elem.Name) == sgName {
found = &elem
}
sg, err := sglist.FindSecurityGroup(sgName)
if err != nil && !errors.Is(err, v3.ErrNotFound) {
return err
}

var sgID v3.UUID
if found == nil {
if errors.Is(err, v3.ErrNotFound) {
log.Infof("Security group %v does not exist. Creating it...", sgName)
newSGID, err := d.createDefaultSecurityGroup(ctx, sgName)
if err != nil {
Expand All @@ -592,7 +587,7 @@ func (d *Driver) Create() error {

sgID = newSGID
} else {
sgID = found.ID
sgID = sg.ID
}

log.Debugf("Security group %v = %s", sgName, sgID)
Expand All @@ -607,28 +602,27 @@ func (d *Driver) Create() error {
if group == "" {
continue
}
var agID v3.UUID

agList, err := client.ListAntiAffinityGroups(ctx)
if err != nil {
return err
}

var found *v3.AntiAffinityGroup
for _, elem := range agList.AntiAffinityGroups {
if string(elem.Name) == group {
found = &elem
}
ag, err := agList.FindAntiAffinityGroup(group)
if err != nil && !errors.Is(err, v3.ErrNotFound) {
return err
}

if found == nil {
var agID v3.UUID
if errors.Is(err, v3.ErrNotFound) {
log.Infof("Affinity Group %v does not exist, create it", group)
newAGID, err := d.createDefaultAffinityGroup(ctx, group)
if err != nil {
return err
}
agID = newAGID
} else {
agID = found.ID
agID = ag.ID
}

log.Debugf("Affinity group %v = %s", group, agID)
Expand Down Expand Up @@ -736,28 +730,28 @@ ssh_authorized_keys:
return err
}

vm, err := client.GetInstance(ctx, res.Reference.ID)
instance, err := client.GetInstance(ctx, res.Reference.ID)
if err != nil {
return err
}

IPAddress := vm.PublicIP.String()
IPAddress := instance.PublicIP.String()
if IPAddress != "<nil>" {
d.IPAddress = IPAddress
}
d.ID = vm.ID
d.ID = instance.ID
log.Infof("IP Address: %v, SSH User: %v", d.IPAddress, d.GetSSHUsername())

if vm.Template != nil && vm.Template.PasswordEnabled != nil && *vm.Template.PasswordEnabled {
res, err := client.RevealInstancePassword(ctx, vm.ID)
if instance.Template != nil && instance.Template.PasswordEnabled != nil && *instance.Template.PasswordEnabled {
res, err := client.RevealInstancePassword(ctx, instance.ID)
if err != nil {
return err
}

d.Password = res.Password
}

// Destroy the SSH key from CloudStack
// Destroy the SSH key
if d.KeyPair != "" {
if err := drivers.WaitForSSH(d); err != nil {
return err
Expand Down Expand Up @@ -847,7 +841,7 @@ func (d *Driver) Remove() error {
}
}

// Destroy the virtual machine
// Destroy the Instance
if d.ID != "" {
op, err := client.DeleteInstance(ctx, d.ID)
if err != nil {
Expand Down