Skip to content

Commit bc37725

Browse files
authored
🌱 Refactor statusFromHCloudServer (#1649)
1 parent 8632a84 commit bc37725

File tree

4 files changed

+58
-52
lines changed

4 files changed

+58
-52
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/go-logr/zapr v1.3.0
1010
github.com/guettli/check-conditions v0.0.9
1111
github.com/hetznercloud/hcloud-go/v2 v2.19.1
12+
github.com/mitchellh/copystructure v1.2.0
1213
github.com/onsi/ginkgo/v2 v2.23.0
1314
github.com/onsi/gomega v1.36.2
1415
github.com/prometheus/common v0.63.0
@@ -89,7 +90,6 @@ require (
8990
github.com/magiconair/properties v1.8.7 // indirect
9091
github.com/mailru/easyjson v0.7.7 // indirect
9192
github.com/mattn/go-isatty v0.0.20 // indirect
92-
github.com/mitchellh/copystructure v1.2.0 // indirect
9393
github.com/mitchellh/mapstructure v1.5.0 // indirect
9494
github.com/mitchellh/reflectwalk v1.0.2 // indirect
9595
github.com/moby/docker-image-spec v1.3.1 // indirect

pkg/services/hcloud/server/server.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24+
"net"
2425
"time"
2526

2627
"github.com/hetznercloud/hcloud-go/v2/hcloud"
2728
corev1 "k8s.io/api/core/v1"
29+
"k8s.io/utils/ptr"
2830
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2931
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
3032
capierrors "sigs.k8s.io/cluster-api/errors"
@@ -110,12 +112,10 @@ func (s *Service) Reconcile(ctx context.Context) (res reconcile.Result, err erro
110112
s.scope.SetProviderID(server.ID)
111113

112114
// update HCloudMachineStatus
113-
c := s.scope.HCloudMachine.Status.Conditions.DeepCopy()
114-
sshKeys := s.scope.HCloudMachine.Status.SSHKeys
115-
s.scope.HCloudMachine.Status = statusFromHCloudServer(server)
116-
s.scope.SetRegion(failureDomain)
117-
s.scope.HCloudMachine.Status.Conditions = c
118-
s.scope.HCloudMachine.Status.SSHKeys = sshKeys
115+
s.scope.HCloudMachine.Status.Addresses = statusAddresses(server)
116+
117+
// Copy value
118+
s.scope.HCloudMachine.Status.InstanceState = ptr.To(server.Status)
119119

120120
// validate labels
121121
if err := validateLabels(server, s.createLabels()); err != nil {
@@ -738,10 +738,7 @@ func validateLabels(server *hcloud.Server, labels map[string]string) error {
738738
return nil
739739
}
740740

741-
func statusFromHCloudServer(server *hcloud.Server) infrav1.HCloudMachineStatus {
742-
// set instance state
743-
instanceState := server.Status
744-
741+
func statusAddresses(server *hcloud.Server) []clusterv1.MachineAddress {
745742
// populate addresses
746743
addresses := []clusterv1.MachineAddress{}
747744

@@ -755,8 +752,15 @@ func statusFromHCloudServer(server *hcloud.Server) infrav1.HCloudMachineStatus {
755752
)
756753
}
757754

758-
if ip := server.PublicNet.IPv6.IP; ip.IsGlobalUnicast() {
755+
if unicastIP := server.PublicNet.IPv6.IP; unicastIP.IsGlobalUnicast() {
756+
// Create a copy. This is important, otherwise we modify the IP of `server`. This could lead
757+
// to unexpected behaviour.
758+
ip := append(net.IP(nil), unicastIP...)
759+
760+
// Hetzner returns the routed /64 base, increment last byte to obtain first usable address
761+
// The local value gets changed, not the IP of `server`.
759762
ip[15]++
763+
760764
addresses = append(
761765
addresses,
762766
clusterv1.MachineAddress{
@@ -776,10 +780,7 @@ func statusFromHCloudServer(server *hcloud.Server) infrav1.HCloudMachineStatus {
776780
)
777781
}
778782

779-
return infrav1.HCloudMachineStatus{
780-
InstanceState: &instanceState,
781-
Addresses: addresses,
782-
}
783+
return addresses
783784
}
784785

785786
func (s *Service) createLabels() map[string]string {

pkg/services/hcloud/server/server_suite_test.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
2626
. "github.com/onsi/ginkgo/v2"
2727
. "github.com/onsi/gomega"
28-
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2928

3029
infrav1 "github.com/syself/cluster-api-provider-hetzner/api/v1beta1"
3130
"github.com/syself/cluster-api-provider-hetzner/pkg/scope"
@@ -188,27 +187,25 @@ const serverJSON = `
188187
"volumes": []
189188
}`
190189

191-
var server *hcloud.Server
192-
193-
const instanceState = hcloud.ServerStatusRunning
194-
195-
var ips = []string{"1.2.3.4", "2001:db8::3", "10.0.0.2"}
196-
var addressTypes = []clusterv1.MachineAddressType{clusterv1.MachineExternalIP, clusterv1.MachineExternalIP, clusterv1.MachineInternalIP}
197-
198190
func TestServer(t *testing.T) {
199191
RegisterFailHandler(Fail)
200192
RunSpecs(t, "Server Suite")
201193
}
202194

203-
var _ = BeforeSuite(func() {
195+
func newTestServer() *hcloud.Server {
204196
var serverSchema schema.Server
205197
b := []byte(serverJSON)
206198
var buffer bytes.Buffer
207-
Expect(json.Compact(&buffer, b))
208-
Expect(json.Unmarshal(buffer.Bytes(), &serverSchema)).To(Succeed())
209-
210-
server = hcloud.ServerFromSchema(serverSchema)
211-
})
199+
err := json.Compact(&buffer, b)
200+
if err != nil {
201+
panic(err)
202+
}
203+
err = json.Unmarshal(buffer.Bytes(), &serverSchema)
204+
if err != nil {
205+
panic(err)
206+
}
207+
return hcloud.ServerFromSchema(serverSchema)
208+
}
212209

213210
func newTestService(hcloudMachine *infrav1.HCloudMachine, hcloudClient hcloudclient.Client) *Service {
214211
return &Service{

pkg/services/hcloud/server/server_test.go

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ package server
1919
import (
2020
"context"
2121
"fmt"
22+
"testing"
2223
"time"
2324

2425
"github.com/hetznercloud/hcloud-go/v2/hcloud"
26+
"github.com/mitchellh/copystructure"
2527
. "github.com/onsi/ginkgo/v2"
2628
. "github.com/onsi/gomega"
29+
"github.com/stretchr/testify/require"
2730
corev1 "k8s.io/api/core/v1"
2831
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2932
"k8s.io/utils/ptr"
@@ -36,28 +39,33 @@ import (
3639
fakeclient "github.com/syself/cluster-api-provider-hetzner/pkg/services/hcloud/client/fake"
3740
)
3841

39-
var _ = Describe("statusFromHCloudServer", func() {
40-
var sts infrav1.HCloudMachineStatus
41-
BeforeEach(func() {
42-
sts = statusFromHCloudServer(server)
43-
})
44-
It("should have the right instance state", func() {
45-
Expect(*sts.InstanceState).To(Equal(instanceState))
46-
})
47-
It("should have three addresses", func() {
48-
Expect(len(sts.Addresses)).To(Equal(3))
49-
})
50-
It("should have the right address IPs", func() {
51-
for i, addr := range sts.Addresses {
52-
Expect(addr.Address).To(Equal(ips[i]))
53-
}
54-
})
55-
It("should have the right address types", func() {
56-
for i, addr := range sts.Addresses {
57-
Expect(addr.Type).To(Equal(addressTypes[i]))
58-
}
59-
})
60-
})
42+
func Test_statusAddresses(t *testing.T) {
43+
server := newTestServer()
44+
45+
// Create deep copy.
46+
saved, err := copystructure.Copy(server)
47+
require.NoError(t, err)
48+
49+
addresses := statusAddresses(server)
50+
51+
// should have three addresses
52+
require.Equal(t, 3, len(addresses))
53+
54+
// should have the right address IPs
55+
ips := []string{"1.2.3.4", "2001:db8::1", "10.0.0.2"}
56+
for i, addr := range addresses {
57+
require.Equal(t, ips[i], addr.Address)
58+
}
59+
60+
// Check that input was not altered.
61+
require.Equal(t, saved, server)
62+
63+
// should have the right address types
64+
addressTypes := []clusterv1.MachineAddressType{clusterv1.MachineExternalIP, clusterv1.MachineExternalIP, clusterv1.MachineInternalIP}
65+
for i, addr := range addresses {
66+
require.Equal(t, addressTypes[i], addr.Type)
67+
}
68+
}
6169

6270
type testCaseStatusFromHCloudServer struct {
6371
isControlPlane bool

0 commit comments

Comments
 (0)