Skip to content
Merged
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
2 changes: 1 addition & 1 deletion cloud/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (c *Client) createServerEntry(input server.ServerSpec) *serverEntry {
State: "ACTIVE",
AvailabilityZone: zone,
Addresses: []server.Address{
{Type: "InternalIP", Address: "10.0.0.10"},
{Type: server.AddressTypeInternalIP, Address: "10.0.0.10"},
},
}
entry := &serverEntry{
Expand Down
10 changes: 8 additions & 2 deletions cloud/services/server/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,18 @@ func (s *Service) serverFromSDK(ctx context.Context, srv *iaas.Server) *Server {
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
}
56 changes: 54 additions & 2 deletions cloud/services/server/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"errors"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -233,8 +234,14 @@ func TestServiceReconcile(t *testing.T) {
if recordedServer == nil || recordedServer.ID != testServerID || recordedServer.Name != "node-0" || recordedServer.State != "ACTIVE" {
t.Fatalf("recordedServer = %#v", recordedServer)
}
if len(recordedServer.Addresses) != 2 {
t.Fatalf("recordedServer.Addresses = %#v, want 2 addresses", recordedServer.Addresses)
wantAddresses := []server.Address{
{Type: server.AddressTypeInternalIP, Address: "10.0.0.10"},
{Type: server.AddressTypeInternalIP, Address: "fd00::10"},
{Type: server.AddressTypeHostname, Address: "node-0"},
{Type: server.AddressTypeInternalDNS, Address: "node-0"},
}
if !reflect.DeepEqual(recordedServer.Addresses, wantAddresses) {
t.Fatalf("recordedServer.Addresses = %#v, want %#v", recordedServer.Addresses, wantAddresses)
}

assertStringField(t, createPayload, "name", "node-0")
Expand Down Expand Up @@ -287,6 +294,51 @@ func TestServiceReconcile(t *testing.T) {
assertStringField(t, createPayload, "imageId", testImageID)
assertFieldAbsent(t, createPayload, "bootVolume")
})

t.Run("does not add Hostname or InternalDNS when server name is empty", func(t *testing.T) {
var recordedServer *server.Server
spec := server.ServerSpec{
ID: testServerID,
Name: "node-0",
MachineType: "c2i.2",
ImageID: testImageID,
NetworkID: testNetworkID,
Tags: map[string]string{"cluster": "test"},
SetServer: func(srv *server.Server, _ bool) {
recordedServer = srv
},
}

svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID):
writeJSON(t, w, map[string]any{
"id": testServerID,
"machineType": "c2i.2",
"name": "",
"status": "ACTIVE",
})
case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/servers/"+testServerID+"/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())
}
}))

if err := svc.Reconcile(context.Background()); err != nil {
t.Fatalf("Reconcile() error = %v", err)
}
wantAddresses := []server.Address{
{Type: server.AddressTypeInternalIP, Address: "10.0.0.10"},
}
if !reflect.DeepEqual(recordedServer.Addresses, wantAddresses) {
t.Fatalf("recordedServer.Addresses = %#v, want %#v", recordedServer.Addresses, wantAddresses)
}
})
}

func TestServiceReconcileReuseAndErrors(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions cloud/services/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ type Address struct {
Address string
}

// Well-known server address types matching Cluster API MachineAddress conventions.
// https://cluster-api.sigs.k8s.io/reference/api/crd-api-reference.html?highlight=InternalIP#machineaddress
const (
AddressTypeInternalIP = "InternalIP"
AddressTypeHostname = "Hostname"
AddressTypeInternalDNS = "InternalDNS"
)

// RootVolume describes the root disk of a VM.
type RootVolume struct {
SizeGiB int
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 @@ -134,7 +134,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