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

Commit d73c77b

Browse files
committed
Save updated cluster status
1 parent ba7a177 commit d73c77b

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package cluster
1919
import (
2020
"fmt"
2121
"log"
22+
"reflect"
2223

2324
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/ca"
2425
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/deployer"
@@ -52,6 +53,8 @@ func NewActuator(params ActuatorParams) (*Actuator, error) {
5253
// Reconcile reconciles a cluster and is invoked by the Cluster Controller
5354
func (a *Actuator) Reconcile(cluster *clusterv1.Cluster) error {
5455
log.Printf("Reconciling cluster %v.", cluster.Name)
56+
// save the original status
57+
clusterCopy := cluster.DeepCopy()
5558
// ensure that we have a CA cert/key and save it
5659
if cert, _ := a.deployer.GetCA(cluster.Name); cert == nil {
5760
caCertAndKey, err := ca.GenerateCACertAndKey(cluster.Name, "")
@@ -63,6 +66,39 @@ func (a *Actuator) Reconcile(cluster *clusterv1.Cluster) error {
6366
return fmt.Errorf("unable to save CA cert and key: %v", err)
6467
}
6568
}
69+
// ensure that we save the correct IP address for the cluster
70+
address, err := a.deployer.GetIP(cluster, nil)
71+
_, isNoMachine := err.(*deployer.MachineNotFound)
72+
_, isNoIP := err.(*deployer.MachineNoIP)
73+
switch {
74+
case err != nil && isNoMachine:
75+
return nil
76+
case err != nil && isNoIP:
77+
return nil
78+
case err != nil:
79+
return err
80+
case err == nil:
81+
cluster.Status.APIEndpoints = []clusterv1.APIEndpoint{
82+
{
83+
Host: address,
84+
Port: a.deployer.ControlPort,
85+
},
86+
}
87+
}
88+
89+
var clusterClient client.ClusterInterface
90+
if a.clustersGetter != nil {
91+
clusterClient = a.clustersGetter.Clusters(cluster.Namespace)
92+
}
93+
if !reflect.DeepEqual(cluster.Status, clusterCopy.Status) {
94+
log.Printf("saving updated cluster status %s", cluster.Name)
95+
if _, err := clusterClient.UpdateStatus(cluster); err != nil {
96+
msg := fmt.Sprintf("failed to save updated cluster status %s: %v", cluster.Name, err)
97+
log.Printf(msg)
98+
return fmt.Errorf(msg)
99+
}
100+
}
101+
66102
return nil
67103
}
68104

pkg/cloud/packet/deployer/deploy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ func (d *Deployer) GetIP(cluster *clusterv1.Cluster, machine *clusterv1.Machine)
105105
return "", fmt.Errorf("error retrieving machine status %s: %v", machineRef, err)
106106
}
107107
if device == nil {
108-
return "", fmt.Errorf("machine does not exist: %s", machineRef)
108+
return "", &MachineNotFound{err: fmt.Sprintf("machine does not exist: %s", machineRef)}
109109
}
110110
if device.Network == nil || len(device.Network) == 0 || device.Network[0].Address == "" {
111-
return "", fmt.Errorf("machine does not yet have an IP address: %s", machineRef)
111+
return "", &MachineNoIP{err: fmt.Sprintf("machine does not yet have an IP address: %s", machineRef)}
112112
}
113113
// TODO: validate that this address exists, so we don't hit nil pointer
114114
// TODO: check which address to return

pkg/cloud/packet/deployer/error.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package deployer
2+
3+
// MachineNotFound error representing that the requested device was not yet found
4+
type MachineNotFound struct {
5+
err string
6+
}
7+
8+
func (e *MachineNotFound) Error() string {
9+
return e.err
10+
}
11+
12+
// MachineNoIP error representing that the requested device does not have an IP yet assigned
13+
type MachineNoIP struct {
14+
err string
15+
}
16+
17+
func (e *MachineNoIP) Error() string {
18+
return e.err
19+
}

0 commit comments

Comments
 (0)