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

Commit 70ecde7

Browse files
committed
Create custom UID annotation for Machine so it will survive the pivot
1 parent 74a4bcb commit 70ecde7

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

Gopkg.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cloud/packet/actuators/machine/actuator.go

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

26+
"github.com/google/uuid"
2627
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet"
2728
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/actuators/machine/machineconfig"
2829
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/deployer"
@@ -90,8 +91,11 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
9091
return fmt.Errorf("unable to unpack cluster provider: %v", err)
9192
}
9293

94+
// generate a unique UID that will survive pivot, i.e. is not tied to the cluster itself
95+
mUID := uuid.New().String()
96+
9397
tags := []string{
94-
util.GenerateMachineTag(string(machine.UID)),
98+
util.GenerateMachineTag(mUID),
9599
util.GenerateClusterTag(string(cluster.Name)),
96100
}
97101
// generate userdata from the template
@@ -158,6 +162,7 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
158162

159163
machine.Annotations["cluster-api-provider-packet"] = "true"
160164
machine.Annotations["cluster.k8s.io/machine"] = cluster.Name
165+
machine.Annotations[util.AnnotationUID] = mUID
161166

162167
if _, err = a.updateMachine(cluster, machine); err != nil {
163168
return fmt.Errorf("error updating Machine object with annotations: %v", err)
@@ -177,10 +182,10 @@ func (a *Actuator) Delete(ctx context.Context, cluster *clusterv1.Cluster, machi
177182
log.Printf("Deleting machine %v for cluster %v.", machine.Name, cluster.Name)
178183
device, err := a.packetClient.GetDevice(machine)
179184
if err != nil {
180-
return fmt.Errorf("error retrieving machine status %s: %v", machine.UID, err)
185+
return fmt.Errorf("error retrieving machine status %s: %v", machine.Name, err)
181186
}
182187
if device == nil {
183-
return fmt.Errorf("machine does not exist: %s", machine.UID)
188+
return fmt.Errorf("machine does not exist: %s", machine.Name)
184189
}
185190

186191
_, err = a.packetClient.Devices.Delete(device.ID)
@@ -211,7 +216,7 @@ func (a *Actuator) Update(ctx context.Context, cluster *clusterv1.Cluster, machi
211216
// - check if anything mutable has changed, in which case we update the instance
212217
device, err := a.packetClient.GetDevice(machine)
213218
if err != nil {
214-
return fmt.Errorf("error retrieving machine status %s: %v", machine.UID, err)
219+
return fmt.Errorf("error retrieving machine status %s: %v", machine.Name, err)
215220
}
216221
if device == nil {
217222
return fmt.Errorf("received nil machine")
@@ -246,7 +251,7 @@ func (a *Actuator) Exists(ctx context.Context, cluster *clusterv1.Cluster, machi
246251
log.Printf("Checking if machine %v for cluster %v exists.", machine.Name, cluster.Name)
247252
device, err := a.packetClient.GetDevice(machine)
248253
if err != nil {
249-
return false, fmt.Errorf("error retrieving machine status %s: %v", machine.UID, err)
254+
return false, fmt.Errorf("error retrieving machine status %s: %v", machine.Name, err)
250255
}
251256
if device == nil {
252257
return false, nil
@@ -264,7 +269,7 @@ func (a *Actuator) get(machine *clusterv1.Machine) (*packngo.Device, error) {
264269
return device, nil
265270
}
266271

267-
return nil, fmt.Errorf("Device %s not found", machine.UID)
272+
return nil, fmt.Errorf("Device %s not found", machine.Name)
268273
}
269274

270275
func (a *Actuator) updateMachine(cluster *clusterv1.Cluster, machine *clusterv1.Machine) (*clusterv1.Machine, error) {

pkg/cloud/packet/client.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,20 @@ func (p *PacketClient) GetDevice(machine *clusterv1.Machine) (*packngo.Device, e
4141
if err != nil {
4242
return nil, fmt.Errorf("Failed to process config for providerSpec: %v", err)
4343
}
44-
tag := util.GenerateMachineTag(string(machine.UID))
44+
// if there are no annotations, or the annotation we want does not exist, nothing to do
45+
if machine.Annotations == nil {
46+
fmt.Printf("No annotations with machine UID for %s, machine does not exist", machine.Name)
47+
return nil, nil
48+
}
49+
var (
50+
mUID string
51+
ok bool
52+
)
53+
if mUID, ok = machine.Annotations[util.AnnotationUID]; !ok {
54+
fmt.Printf("No UID annotation %s with machine UID for %s, machine does not exist", util.AnnotationUID, machine.Name)
55+
return nil, nil
56+
}
57+
tag := util.GenerateMachineTag(mUID)
4558
return p.GetDeviceByTags(c.ProjectID, []string{tag})
4659
}
4760
func (p *PacketClient) GetDeviceByTags(project string, tags []string) (*packngo.Device, error) {

pkg/cloud/packet/deployer/deploy.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,18 @@ func (d *Deployer) GetIP(cluster *clusterv1.Cluster, machine *clusterv1.Machine)
7272
)
7373
if machine != nil {
7474
log.Printf("Getting IP of machine %v for cluster %v.", machine.Name, cluster.Name)
75-
machineRef = string(machine.UID)
75+
// if there are no annotations, or the annotation we want does not exist, nothing to do
76+
if machine.Annotations == nil {
77+
return "", fmt.Errorf("No annotations with machine UID for %s", machine.Name)
78+
}
79+
var (
80+
mUID string
81+
ok bool
82+
)
83+
if mUID, ok = machine.Annotations[util.AnnotationUID]; !ok {
84+
return "", fmt.Errorf("No UID annotation %s on machine %s", util.AnnotationUID, machine.Name)
85+
}
86+
machineRef = mUID
7687
device, err = d.client.GetDevice(machine)
7788
} else {
7889
log.Printf("Getting IP of any master machine for cluster %v.", cluster.Name)

pkg/cloud/packet/util/util.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
MasterTag = "kubernetes.io/role:master"
1717
WorkerTag = "kubernetes.io/role:node"
1818
ControlPort = 6443
19+
AnnotationUID = "cluster.k8s.io/machine-uid"
1920
)
2021

2122
func MachineProviderFromProviderConfig(providerConfig clusterv1.ProviderSpec) (*packetconfigv1.PacketMachineProviderConfig, error) {

0 commit comments

Comments
 (0)