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
32 changes: 23 additions & 9 deletions cloud/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,21 @@ func (c *Client) CreateServer(_ context.Context, input cloud.CreateServerInput)
if zone == "" {
zone = "eu01-1"
}
addresses := []cloud.Address{
{Type: cloud.AddressTypeInternalIP, Address: "10.0.0.10"},
}
if input.Name != "" {
addresses = append(addresses,
cloud.Address{Type: cloud.AddressTypeHostname, Address: input.Name},
cloud.Address{Type: cloud.AddressTypeInternalDNS, Address: input.Name},
)
}
server := &cloud.Server{
ID: id,
Name: input.Name,
State: "ACTIVE",
AvailabilityZone: zone,
Addresses: []cloud.Address{
{Type: "InternalIP", Address: "10.0.0.10"},
},
Addresses: addresses,
}
c.servers[id] = &serverEntry{
server: server,
Expand Down Expand Up @@ -389,13 +396,20 @@ func (c *Client) EnsureBastion(_ context.Context, input cloud.BastionInput) (*cl
}

serverID := c.genID()
addresses := []cloud.Address{
{Type: cloud.AddressTypeInternalIP, Address: "10.0.0.20"},
}
if input.Name != "" {
addresses = append(addresses,
cloud.Address{Type: cloud.AddressTypeHostname, Address: input.Name},
cloud.Address{Type: cloud.AddressTypeInternalDNS, Address: input.Name},
)
}
server := &cloud.Server{
ID: serverID,
Name: input.Name,
State: "ACTIVE",
Addresses: []cloud.Address{
{Type: "InternalIP", Address: "10.0.0.20"},
},
ID: serverID,
Name: input.Name,
State: "ACTIVE",
Addresses: addresses,
}
c.servers[serverID] = &serverEntry{
server: server,
Expand Down
10 changes: 8 additions & 2 deletions cloud/sdk_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1057,13 +1057,19 @@ func (c *SDKClient) serverFromSDK(ctx context.Context, server *iaas.Server) *Ser
if err == nil {
for _, nic := range nics.GetItems() {
if ipv4 := nic.GetIpv4(); ipv4 != "" {
out.Addresses = append(out.Addresses, Address{Type: "InternalIP", Address: ipv4})
out.Addresses = append(out.Addresses, Address{Type: AddressTypeInternalIP, Address: ipv4})
}
if ipv6 := nic.GetIpv6(); ipv6 != "" {
out.Addresses = append(out.Addresses, Address{Type: "InternalIP", Address: ipv6})
out.Addresses = append(out.Addresses, Address{Type: AddressTypeInternalIP, Address: ipv6})
}
}
}
if out.Name != "" {
out.Addresses = append(out.Addresses,
Address{Type: AddressTypeHostname, Address: out.Name},
Address{Type: AddressTypeInternalDNS, Address: out.Name},
)
}
return out
}

Expand Down
56 changes: 54 additions & 2 deletions cloud/sdk_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -85,8 +86,14 @@ func TestSDKClientCreateServerUsesExpectedPayload(t *testing.T) {
if created.ID != testSDKServerID || created.Name != "node-0" || created.State != "ACTIVE" {
t.Fatalf("CreateServer() = %#v", created)
}
if len(created.Addresses) != 2 {
t.Fatalf("CreateServer() addresses = %#v, want two internal addresses", created.Addresses)
wantAddresses := []Address{
{Type: AddressTypeInternalIP, Address: "10.0.0.10"},
{Type: AddressTypeInternalIP, Address: "fd00::10"},
{Type: AddressTypeHostname, Address: "node-0"},
{Type: AddressTypeInternalDNS, Address: "node-0"},
}
if !reflect.DeepEqual(created.Addresses, wantAddresses) {
t.Fatalf("CreateServer() addresses = %#v, want %#v", created.Addresses, wantAddresses)
}

assertStringField(t, createPayload, "name", "node-0")
Expand All @@ -104,6 +111,51 @@ func TestSDKClientCreateServerUsesExpectedPayload(t *testing.T) {
assertNestedNumberField(t, createPayload, []string{"bootVolume", "size"}, 50)
}

func TestSDKClientCreateServerWithEmptyNameDoesNotAddHostnameOrInternalDNS(t *testing.T) {
server := newSDKTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers"):
writeJSON(t, w, map[string]any{"items": []any{}})
case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/servers"):
writeJSON(t, w, map[string]any{
"id": testSDKServerID,
"machineType": "c2i.2",
"name": "",
"status": "ACTIVE",
})
case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers/"+testSDKServerID+"/nics"):
writeJSON(t, w, map[string]any{
"items": []any{
map[string]any{"ipv4": "10.0.0.10"},
},
})
default:
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String())
}
}))

client := newTestSDKClient(t, server.URL)
created, err := client.CreateServer(context.Background(), CreateServerInput{
Name: "",
MachineType: "c2i.2",
ImageID: testSDKImageID,
AvailabilityZone: "eu01-1",
SSHKeyName: "default",
NetworkID: testSDKNetworkID,
SecurityGroups: []string{testSDKSecurityGroup},
Tags: map[string]string{"cluster": "test"},
})
if err != nil {
t.Fatalf("CreateServer() error = %v", err)
}
wantAddresses := []Address{
{Type: AddressTypeInternalIP, Address: "10.0.0.10"},
}
if !reflect.DeepEqual(created.Addresses, wantAddresses) {
t.Fatalf("CreateServer() addresses = %#v, want %#v", created.Addresses, wantAddresses)
}
}

func assertFieldAbsent(t *testing.T, payload map[string]any, key string) {
t.Helper()
if _, ok := payload[key]; ok {
Expand Down
7 changes: 7 additions & 0 deletions cloud/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ type Address struct {
Address string
}

// Well-known server address types matching Cluster API conventions.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

const (
AddressTypeInternalIP = "InternalIP"
AddressTypeHostname = "Hostname"
AddressTypeInternalDNS = "InternalDNS"
)

// Network is an existing STACKIT virtual network referenced by the provider.
type Network struct {
ID string
Expand Down
6 changes: 5 additions & 1 deletion controller/stackitmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ var _ = Describe("StackitMachine Controller", func() {
Expect(got.Status.ProviderID).To(Equal("stackit://" + got.Status.InstanceID))
Expect(got.Spec.ProviderID).NotTo(BeNil())
Expect(*got.Spec.ProviderID).To(Equal(got.Status.ProviderID))
Expect(got.Status.Addresses).To(HaveLen(1))
Expect(got.Status.Addresses).To(ConsistOf(
clusterv1.MachineAddress{Type: clusterv1.MachineInternalIP, Address: "10.0.0.10"},
clusterv1.MachineAddress{Type: clusterv1.MachineHostName, Address: stackitName},
clusterv1.MachineAddress{Type: clusterv1.MachineInternalDNS, Address: stackitName},
))
expectCondition(got.Status.Conditions, infrav1.MachineReadyCondition, metav1.ConditionTrue, "Available")
expectCondition(got.Status.Conditions, infrav1.MachineBootstrapReadyCondition, metav1.ConditionTrue, "Available")
expectCondition(got.Status.Conditions, infrav1.MachineInstanceReadyCondition, metav1.ConditionTrue, "Available")
Expand Down
Loading