Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit a0161cb

Browse files
committed
Make PacketMachine controller re-entrant when crashes happen during creation process
1 parent 34f9668 commit a0161cb

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

controllers/packetmachine_controller.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ import (
2323
"strings"
2424
"time"
2525

26-
corev1 "k8s.io/api/core/v1"
27-
2826
"github.com/go-logr/logr"
29-
"github.com/google/uuid"
3027
"github.com/packethost/packngo"
3128
"github.com/pkg/errors"
32-
29+
corev1 "k8s.io/api/core/v1"
3330
apierrors "k8s.io/apimachinery/pkg/api/errors"
3431
"k8s.io/apimachinery/pkg/runtime"
3532
"k8s.io/client-go/tools/record"
33+
infrastructurev1alpha3 "sigs.k8s.io/cluster-api-provider-packet/api/v1alpha3"
34+
packet "sigs.k8s.io/cluster-api-provider-packet/pkg/cloud/packet"
35+
"sigs.k8s.io/cluster-api-provider-packet/pkg/cloud/packet/scope"
3636
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
3737
capierrors "sigs.k8s.io/cluster-api/errors"
3838
"sigs.k8s.io/cluster-api/util"
@@ -42,11 +42,6 @@ import (
4242
"sigs.k8s.io/controller-runtime/pkg/handler"
4343
"sigs.k8s.io/controller-runtime/pkg/reconcile"
4444
"sigs.k8s.io/controller-runtime/pkg/source"
45-
46-
packet "sigs.k8s.io/cluster-api-provider-packet/pkg/cloud/packet"
47-
"sigs.k8s.io/cluster-api-provider-packet/pkg/cloud/packet/scope"
48-
49-
infrastructurev1alpha3 "sigs.k8s.io/cluster-api-provider-packet/api/v1alpha3"
5045
)
5146

5247
const (
@@ -195,6 +190,13 @@ func (r *PacketMachineReconciler) reconcile(ctx context.Context, machineScope *s
195190
}
196191

197192
providerID := machineScope.GetInstanceID()
193+
194+
defaultTags := []string{
195+
packet.GenerateClusterTag(clusterScope.Name()),
196+
packet.GenerateMachineNameTag(machineScope.Name()),
197+
packet.GenerateNamespaceTag(machineScope.Namespace()),
198+
}
199+
198200
var (
199201
dev *packngo.Device
200202
addrs []corev1.NodeAddress
@@ -208,15 +210,22 @@ func (r *PacketMachineReconciler) reconcile(ctx context.Context, machineScope *s
208210
return ctrl.Result{}, err
209211
}
210212
}
213+
214+
if dev == nil {
215+
dev, err = r.PacketClient.GetDeviceByTags(
216+
machineScope.PacketCluster.Spec.ProjectID,
217+
defaultTags,
218+
)
219+
if err != nil {
220+
return ctrl.Result{}, err
221+
}
222+
}
223+
211224
if dev == nil {
212225
createDeviceReq := packet.CreateDeviceRequest{
226+
ExtraTags: defaultTags,
213227
MachineScope: machineScope,
214228
}
215-
mUID := uuid.New().String()
216-
tags := []string{
217-
packet.GenerateMachineTag(mUID),
218-
packet.GenerateClusterTag(clusterScope.Name()),
219-
}
220229

221230
// when the node is a control plan we should check if the elastic ip
222231
// for this cluster is not assigned. If it is free we can prepare the
@@ -236,8 +245,6 @@ func (r *PacketMachineReconciler) reconcile(ctx context.Context, machineScope *s
236245
createDeviceReq.ControlPlaneEndpoint = controlPlaneEndpoint.Address
237246
}
238247

239-
createDeviceReq.ExtraTags = tags
240-
241248
dev, err = r.PacketClient.NewDevice(createDeviceReq)
242249

243250
switch {

pkg/cloud/packet/util.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,26 @@ import (
2121
)
2222

2323
const (
24-
MachineUIDTag = "cluster-api-provider-packet:machine-uid"
25-
clusterIDTag = "cluster-api-provider-packet:cluster-id"
26-
AnnotationUID = "cluster.k8s.io/machine-uid"
24+
MachineUIDTag = "cluster-api-provider-packet:machine-uid"
25+
machineNameTag = "cluster-api-provider-packet:machine-name"
26+
clusterIDTag = "cluster-api-provider-packet:cluster-id"
27+
namespaceTag = "cluster-api-provider-packet:namespace"
2728
)
2829

29-
func GenerateMachineTag(ID string) string {
30-
return fmt.Sprintf("%s:%s", MachineUIDTag, ID)
30+
func GenerateMachineTag(id string) string {
31+
return fmt.Sprintf("%s:%s", MachineUIDTag, id)
3132
}
32-
func GenerateClusterTag(ID string) string {
33-
return fmt.Sprintf("%s:%s", clusterIDTag, ID)
33+
34+
func GenerateMachineNameTag(name string) string {
35+
return fmt.Sprintf("%s:%s", machineNameTag, name)
36+
}
37+
38+
func GenerateClusterTag(clusterName string) string {
39+
return fmt.Sprintf("%s:%s", clusterIDTag, clusterName)
40+
}
41+
42+
func GenerateNamespaceTag(namespace string) string {
43+
return fmt.Sprintf("%s:%s", namespaceTag, namespace)
3444
}
3545

3646
// ItemsInList checks if all items are in the list

0 commit comments

Comments
 (0)