Skip to content
Open
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
24 changes: 21 additions & 3 deletions cloudstack_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,21 @@ func (cs *CSCloud) InstanceExistsByProviderID(ctx context.Context, providerID st
return true, nil
}

// InstanceShutdownByProviderID returns true if the instance is in safe state to detach volumes
// InstanceShutdownByProviderID returns true if the instance is stopped.
func (cs *CSCloud) InstanceShutdownByProviderID(ctx context.Context, providerID string) (bool, error) {
return false, cloudprovider.NotImplemented
instance, count, err := cs.client.VirtualMachine.GetVirtualMachineByID(
cs.getInstanceIDFromProviderID(providerID),
cloudstack.WithProject(cs.projectID),
)
if err != nil {
if count == 0 {
return false, cloudprovider.InstanceNotFound
}
return false, fmt.Errorf("error retrieving instance state: %v", err)
}

// Stopping is not shut down: the guest may still be running and writing to its volumes.
return instance.State == "Stopped", nil
}

func (cs *CSCloud) InstanceExists(ctx context.Context, node *corev1.Node) (bool, error) {
Expand All @@ -180,8 +192,14 @@ func (cs *CSCloud) InstanceExists(ctx context.Context, node *corev1.Node) (bool,
return cs.InstanceExistsByProviderID(ctx, providerID)
}

// InstanceShutdown returns true if the node's instance is stopped.
func (cs *CSCloud) InstanceShutdown(ctx context.Context, node *corev1.Node) (bool, error) {
return false, cloudprovider.NotImplemented
providerID, err := cs.InstanceID(ctx, types.NodeName(node.Name))
if err != nil {
return false, err
}

return cs.InstanceShutdownByProviderID(ctx, providerID)
}

func (cs *CSCloud) InstanceMetadata(ctx context.Context, node *corev1.Node) (*cloudprovider.InstanceMetadata, error) {
Expand Down
101 changes: 94 additions & 7 deletions cloudstack_node_lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ var providerIDLookupOps = []instanceLookupOp{
notFound: false,
errPrefix: "error retrieving instance",
},
{
name: "InstanceShutdownByProviderID",
call: func(cs *CSCloud, key string) (any, error) {
return cs.InstanceShutdownByProviderID(context.TODO(), key)
},
want: false,
notFound: false,
notFoundErr: cloudprovider.InstanceNotFound,
errPrefix: "error retrieving instance state",
},
}

type lookupScenario struct {
Expand Down Expand Up @@ -349,6 +359,90 @@ func TestInstanceExists(t *testing.T) {
}
}

// TestInstanceShutdown checks that only a Stopped instance is reported as shut down, and that the
// node is resolved to its ID by name first, as in InstanceExists.
func TestInstanceShutdown(t *testing.T) {
states := map[string]bool{
"Running": false,
"Starting": false,
"Stopping": false,
"Stopped": true,
"Migrating": false,
}

for state, want := range states {
instance := testInstance()
instance.State = state

t.Run(state, func(t *testing.T) {
env := newVMLookupEnv(t, testProjectID)
gomock.InOrder(
env.expectByName(t, "node-1", testInstance(), 1, nil),
env.expectByID(t, "vm-1", instance, 1, nil),
)

got, err := env.cs.InstanceShutdown(context.TODO(), &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: "node-1"}})

if err != nil || got != want {
t.Errorf("InstanceShutdown() = %v, %v, want %v", got, err, want)
}
})
}

errTests := []struct {
name string
byName func(e *vmLookupEnv, t *testing.T)
byID func(e *vmLookupEnv, t *testing.T)
wantErr error
errContains string
}{
{
name: "node unknown to CloudStack",
byName: func(e *vmLookupEnv, t *testing.T) { e.expectByName(t, "node-1", nil, 0, errors.New("no match")) },
wantErr: cloudprovider.InstanceNotFound,
},
{
name: "api error on the name lookup",
byName: func(e *vmLookupEnv, t *testing.T) { e.expectByName(t, "node-1", nil, 1, errors.New("boom")) },
errContains: "error retrieving instance ID: boom",
},
{
name: "instance deleted after the name lookup",
byName: func(e *vmLookupEnv, t *testing.T) { e.expectByName(t, "node-1", testInstance(), 1, nil) },
byID: func(e *vmLookupEnv, t *testing.T) { e.expectByID(t, "vm-1", nil, 0, errors.New("no match")) },
wantErr: cloudprovider.InstanceNotFound,
},
{
name: "api error on the ID lookup",
byName: func(e *vmLookupEnv, t *testing.T) { e.expectByName(t, "node-1", testInstance(), 1, nil) },
byID: func(e *vmLookupEnv, t *testing.T) { e.expectByID(t, "vm-1", nil, 1, errors.New("boom")) },
errContains: "error retrieving instance state: boom",
},
}

for _, tt := range errTests {
t.Run(tt.name, func(t *testing.T) {
env := newVMLookupEnv(t, testProjectID)
tt.byName(env, t)
if tt.byID != nil {
tt.byID(env, t)
}

got, err := env.cs.InstanceShutdown(context.TODO(), &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: "node-1"}})

if got {
t.Errorf("InstanceShutdown() = true, want false")
}
if tt.wantErr != nil && err != tt.wantErr {
t.Errorf("InstanceShutdown() error = %v, want %v", err, tt.wantErr)
}
if tt.errContains != "" && (err == nil || !strings.Contains(err.Error(), tt.errContains)) {
t.Errorf("InstanceShutdown() error = %v, want it to contain %q", err, tt.errContains)
}
})
}
}

// TestInstanceMetadata checks the metadata the node controller sets on a new node and that a
// failure of any of the four lookups behind it is returned instead of a half-filled result.
func TestInstanceMetadata(t *testing.T) {
Expand Down Expand Up @@ -408,14 +502,7 @@ func TestInstanceMetadata(t *testing.T) {
// TestUnsupportedInstanceOperations checks the operations the provider leaves to Kubernetes.
func TestUnsupportedInstanceOperations(t *testing.T) {
cs := &CSCloud{}
node := &corev1.Node{}

if shutdown, err := cs.InstanceShutdownByProviderID(context.TODO(), "vm-1"); shutdown || err != cloudprovider.NotImplemented {
t.Errorf("InstanceShutdownByProviderID() = %v, %v, want false, NotImplemented", shutdown, err)
}
if shutdown, err := cs.InstanceShutdown(context.TODO(), node); shutdown || err != cloudprovider.NotImplemented {
t.Errorf("InstanceShutdown() = %v, %v, want false, NotImplemented", shutdown, err)
}
if err := cs.AddSSHKeyToAllInstances(context.TODO(), "user", nil); err != cloudprovider.NotImplemented {
t.Errorf("AddSSHKeyToAllInstances() = %v, want NotImplemented", err)
}
Expand Down