@@ -92,9 +92,11 @@ type CreateDeviceRequest struct {
92
92
}
93
93
94
94
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 != "" {
96
98
// Error if pxe url and OS conflict
97
- if req . MachineScope . PacketMachine . Spec .OS != ipxeOS {
99
+ if packetMachineSpec .OS != ipxeOS {
98
100
return nil , fmt .Errorf ("os should be set to custom_pxe when using pxe urls: %w" , ErrInvalidRequest )
99
101
}
100
102
}
@@ -110,8 +112,8 @@ func (p *Client) NewDevice(ctx context.Context, req CreateDeviceRequest) (*packn
110
112
"kubernetesVersion" : pointer .StringPtrDerefOr (req .MachineScope .Machine .Spec .Version , "" ),
111
113
}
112
114
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 )
115
117
tags = append (tags , req .ExtraTags ... )
116
118
117
119
tmpl , err := template .New ("user-data" ).Parse (userData )
@@ -138,32 +140,42 @@ func (p *Client) NewDevice(ctx context.Context, req CreateDeviceRequest) (*packn
138
140
139
141
userData = stringWriter .String ()
140
142
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
146
146
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
+ }
151
163
}
152
164
153
165
serverCreateOpts := & packngo.DeviceCreateRequest {
154
166
Hostname : req .MachineScope .Name (),
155
- ProjectID : req . MachineScope . PacketCluster . Spec .ProjectID ,
167
+ ProjectID : packetClusterSpec .ProjectID ,
156
168
Facility : []string {facility },
157
169
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 ,
162
174
Tags : tags ,
163
175
UserData : userData ,
164
176
}
165
177
166
- reservationIDs := strings .Split (req . MachineScope . PacketMachine . Spec .HardwareReservationID , "," )
178
+ reservationIDs := strings .Split (packetMachineSpec .HardwareReservationID , "," )
167
179
168
180
// If there are no reservationIDs to process, go ahead and return early
169
181
if len (reservationIDs ) == 0 {
0 commit comments