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

Commit e0f508f

Browse files
committed
Prioritize metro over facility and machine settings over cluster
Signed-off-by: Chris Privitere <[email protected]>
1 parent ee680d2 commit e0f508f

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

pkg/cloud/packet/client.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ type CreateDeviceRequest struct {
9292
}
9393

9494
func (p *Client) NewDevice(ctx context.Context, req CreateDeviceRequest) (*packngo.Device, error) {
95-
if req.MachineScope.PacketMachine.Spec.IPXEUrl != "" {
95+
packetMachineSpec := req.MachineScope.PacketMachine.Spec
96+
packetClusterSpec := req.MachineScope.PacketCluster.Spec
97+
if packetMachineSpec.IPXEUrl != "" {
9698
// Error if pxe url and OS conflict
97-
if req.MachineScope.PacketMachine.Spec.OS != ipxeOS {
99+
if packetMachineSpec.OS != ipxeOS {
98100
return nil, fmt.Errorf("os should be set to custom_pxe when using pxe urls: %w", ErrInvalidRequest)
99101
}
100102
}
@@ -110,8 +112,8 @@ func (p *Client) NewDevice(ctx context.Context, req CreateDeviceRequest) (*packn
110112
"kubernetesVersion": pointer.StringPtrDerefOr(req.MachineScope.Machine.Spec.Version, ""),
111113
}
112114

113-
tags := make([]string, 0, len(req.MachineScope.PacketMachine.Spec.Tags)+len(req.ExtraTags))
114-
copy(tags, req.MachineScope.PacketMachine.Spec.Tags)
115+
tags := make([]string, 0, len(packetMachineSpec.Tags)+len(req.ExtraTags))
116+
copy(tags, packetMachineSpec.Tags)
115117
tags = append(tags, req.ExtraTags...)
116118

117119
tmpl, err := template.New("user-data").Parse(userData)
@@ -138,32 +140,42 @@ func (p *Client) NewDevice(ctx context.Context, req CreateDeviceRequest) (*packn
138140

139141
userData = stringWriter.String()
140142

141-
// Allow to override the facility for each PacketMachineTemplate
142-
facility := req.MachineScope.PacketCluster.Spec.Facility
143-
if req.MachineScope.PacketMachine.Spec.Facility != "" {
144-
facility = req.MachineScope.PacketMachine.Spec.Facility
145-
}
143+
// If Metro or Facility are specified at the Machine level, we ignore the
144+
// values set at the Cluster level
145+
var metro, facility string
146146

147-
// Allow to override the metro for each PacketMachineTemplate
148-
metro := req.MachineScope.PacketCluster.Spec.Metro
149-
if req.MachineScope.PacketMachine.Spec.Metro != "" {
150-
metro = req.MachineScope.PacketMachine.Spec.Metro
147+
if packetMachineSpec.Facility != "" || packetMachineSpec.Metro != "" {
148+
metro = packetMachineSpec.Metro
149+
facility = packetMachineSpec.Facility
150+
// If both specified, metro takes precedence over facility
151+
if metro != "" {
152+
facility = ""
153+
}
154+
} else {
155+
// Machine level is empty so set facility and metro to cluster level values
156+
facility = packetClusterSpec.Facility
157+
metro = packetClusterSpec.Metro
158+
159+
// If both specified, metro takes precedence over facility
160+
if metro != "" {
161+
facility = ""
162+
}
151163
}
152164

153165
serverCreateOpts := &packngo.DeviceCreateRequest{
154166
Hostname: req.MachineScope.Name(),
155-
ProjectID: req.MachineScope.PacketCluster.Spec.ProjectID,
167+
ProjectID: packetClusterSpec.ProjectID,
156168
Facility: []string{facility},
157169
Metro: metro,
158-
BillingCycle: req.MachineScope.PacketMachine.Spec.BillingCycle,
159-
Plan: req.MachineScope.PacketMachine.Spec.MachineType,
160-
OS: req.MachineScope.PacketMachine.Spec.OS,
161-
IPXEScriptURL: req.MachineScope.PacketMachine.Spec.IPXEUrl,
170+
BillingCycle: packetMachineSpec.BillingCycle,
171+
Plan: packetMachineSpec.MachineType,
172+
OS: packetMachineSpec.OS,
173+
IPXEScriptURL: packetMachineSpec.IPXEUrl,
162174
Tags: tags,
163175
UserData: userData,
164176
}
165177

166-
reservationIDs := strings.Split(req.MachineScope.PacketMachine.Spec.HardwareReservationID, ",")
178+
reservationIDs := strings.Split(packetMachineSpec.HardwareReservationID, ",")
167179

168180
// If there are no reservationIDs to process, go ahead and return early
169181
if len(reservationIDs) == 0 {

0 commit comments

Comments
 (0)