@@ -70,19 +70,9 @@ func (s *Service) createInstanceImpl(openStackCluster *infrav1.OpenStackCluster,
70
70
RootVolume : openStackMachine .Spec .RootVolume ,
71
71
Subnet : openStackMachine .Spec .Subnet ,
72
72
ServerGroupID : openStackMachine .Spec .ServerGroupID ,
73
+ Trunk : openStackMachine .Spec .Trunk ,
73
74
}
74
75
75
- // verify that trunk is supported if set at instance level.
76
- if openStackMachine .Spec .Trunk {
77
- trunkSupported , err := s .isTrunkExtSupported ()
78
- if err != nil {
79
- return nil , err
80
- }
81
- if ! trunkSupported {
82
- return nil , fmt .Errorf ("there is no trunk support. please ensure that the trunk extension is enabled in your OpenStack deployment" )
83
- }
84
- instanceSpec .Trunk = true
85
- }
86
76
machineTags := []string {}
87
77
88
78
// Append machine specific tags
@@ -96,56 +86,44 @@ func (s *Service) createInstanceImpl(openStackCluster *infrav1.OpenStackCluster,
96
86
97
87
instanceSpec .Tags = machineTags
98
88
99
- // Get security groups
100
- securityGroups , err := s .networkingService .GetSecurityGroups (openStackMachine .Spec .SecurityGroups )
101
- if err != nil {
102
- return nil , err
103
- }
89
+ instanceSpec .SecurityGroups = openStackMachine .Spec .SecurityGroups
104
90
if openStackCluster .Spec .ManagedSecurityGroups {
91
+ var managedSecurityGroup string
105
92
if util .IsControlPlaneMachine (machine ) {
106
- securityGroups = append ( securityGroups , openStackCluster .Status .ControlPlaneSecurityGroup .ID )
93
+ managedSecurityGroup = openStackCluster .Status .ControlPlaneSecurityGroup .ID
107
94
} else {
108
- securityGroups = append ( securityGroups , openStackCluster .Status .WorkerSecurityGroup .ID )
95
+ managedSecurityGroup = openStackCluster .Status .WorkerSecurityGroup .ID
109
96
}
110
- }
111
- instanceSpec .SecurityGroups = securityGroups
112
97
113
- nets , err := s . constructNetworks ( openStackCluster , openStackMachine )
114
- if err != nil {
115
- return nil , err
98
+ instanceSpec . SecurityGroups = append ( instanceSpec . SecurityGroups , infrav1. SecurityGroupParam {
99
+ UUID : managedSecurityGroup ,
100
+ })
116
101
}
117
102
118
- trunkConfigured := s .isTrunkConfigured (nets , instanceSpec .Trunk )
119
- if trunkConfigured {
120
- trunkSupported , err := s .isTrunkExtSupported ()
121
- if err != nil {
122
- return nil , err
123
- }
124
- if ! trunkSupported {
125
- return nil , fmt .Errorf ("there is no trunk support. please ensure that the trunk extension is enabled in your OpenStack deployment" )
126
- }
127
- }
128
- instanceSpec .Networks = nets
103
+ instanceSpec .Networks = openStackMachine .Spec .Networks
104
+ instanceSpec .Ports = openStackMachine .Spec .Ports
129
105
130
- return s .createInstance (openStackMachine , clusterName , & instanceSpec , retryInterval )
106
+ return s .createInstance (openStackMachine , openStackCluster , clusterName , & instanceSpec , retryInterval )
131
107
}
132
108
133
- // constructNetworks builds an array of networks from the network, subnet and ports items in the machine spec.
109
+ // constructNetworks builds an array of networks from the network, subnet and ports items in the instance spec.
134
110
// If no networks or ports are in the spec, returns a single network item for a network connection to the default cluster network.
135
- func (s * Service ) constructNetworks (openStackCluster * infrav1.OpenStackCluster , openStackMachine * infrav1.OpenStackMachine ) ([]infrav1.Network , error ) {
136
- var nets []infrav1.Network
137
- if len (openStackMachine .Spec .Networks ) > 0 {
138
- var err error
139
- nets , err = s .getServerNetworks (openStackMachine .Spec .Networks )
140
- if err != nil {
141
- return nil , err
142
- }
111
+ func (s * Service ) constructNetworks (openStackCluster * infrav1.OpenStackCluster , instanceSpec * InstanceSpec ) ([]infrav1.Network , error ) {
112
+ trunkRequired := false
113
+
114
+ nets , err := s .getServerNetworks (instanceSpec .Networks )
115
+ if err != nil {
116
+ return nil , err
143
117
}
144
- for i , port := range openStackMachine .Spec .Ports {
145
- pOpts := & openStackMachine .Spec .Ports [i ]
118
+
119
+ for i := range instanceSpec .Ports {
120
+ port := & instanceSpec .Ports [i ]
146
121
// No Trunk field specified for the port, inherit openStackMachine.Spec.Trunk.
147
- if pOpts .Trunk == nil {
148
- pOpts .Trunk = & openStackMachine .Spec .Trunk
122
+ if port .Trunk == nil {
123
+ port .Trunk = & instanceSpec .Trunk
124
+ }
125
+ if * port .Trunk {
126
+ trunkRequired = true
149
127
}
150
128
if port .Network != nil {
151
129
netID := port .Network .ID
@@ -164,18 +142,19 @@ func (s *Service) constructNetworks(openStackCluster *infrav1.OpenStackCluster,
164
142
nets = append (nets , infrav1.Network {
165
143
ID : netID ,
166
144
Subnet : & infrav1.Subnet {},
167
- PortOpts : pOpts ,
145
+ PortOpts : port ,
168
146
})
169
147
} else {
170
148
nets = append (nets , infrav1.Network {
171
149
ID : openStackCluster .Status .Network .ID ,
172
150
Subnet : & infrav1.Subnet {
173
151
ID : openStackCluster .Status .Network .Subnet .ID ,
174
152
},
175
- PortOpts : pOpts ,
153
+ PortOpts : port ,
176
154
})
177
155
}
178
156
}
157
+
179
158
// no networks or ports found in the spec, so create a port on the cluster network
180
159
if len (nets ) == 0 {
181
160
nets = []infrav1.Network {{
@@ -184,14 +163,26 @@ func (s *Service) constructNetworks(openStackCluster *infrav1.OpenStackCluster,
184
163
ID : openStackCluster .Status .Network .Subnet .ID ,
185
164
},
186
165
PortOpts : & infrav1.PortOpts {
187
- Trunk : & openStackMachine . Spec .Trunk ,
166
+ Trunk : & instanceSpec .Trunk ,
188
167
},
189
168
}}
169
+ trunkRequired = instanceSpec .Trunk
170
+ }
171
+
172
+ if trunkRequired {
173
+ trunkSupported , err := s .isTrunkExtSupported ()
174
+ if err != nil {
175
+ return nil , err
176
+ }
177
+ if ! trunkSupported {
178
+ return nil , fmt .Errorf ("there is no trunk support. please ensure that the trunk extension is enabled in your OpenStack deployment" )
179
+ }
190
180
}
181
+
191
182
return nets , nil
192
183
}
193
184
194
- func (s * Service ) createInstance (eventObject runtime.Object , clusterName string , instanceSpec * InstanceSpec , retryInterval time.Duration ) (* InstanceStatus , error ) {
185
+ func (s * Service ) createInstance (eventObject runtime.Object , openStackCluster * infrav1. OpenStackCluster , clusterName string , instanceSpec * InstanceSpec , retryInterval time.Duration ) (* InstanceStatus , error ) {
195
186
var server * ServerExt
196
187
accessIPv4 := ""
197
188
portList := []servers.Network {}
@@ -221,7 +212,17 @@ func (s *Service) createInstance(eventObject runtime.Object, clusterName string,
221
212
}
222
213
}()
223
214
224
- for i , network := range instanceSpec .Networks {
215
+ nets , err := s .constructNetworks (openStackCluster , instanceSpec )
216
+ if err != nil {
217
+ return nil , err
218
+ }
219
+
220
+ securityGroups , err := s .networkingService .GetSecurityGroups (instanceSpec .SecurityGroups )
221
+ if err != nil {
222
+ return nil , fmt .Errorf ("error getting security groups: %v" , err )
223
+ }
224
+
225
+ for i , network := range nets {
225
226
if network .ID == "" {
226
227
return nil , fmt .Errorf ("no network was found or provided. Please check your machine configuration and try again" )
227
228
}
@@ -230,7 +231,7 @@ func (s *Service) createInstance(eventObject runtime.Object, clusterName string,
230
231
iTags = instanceSpec .Tags
231
232
}
232
233
portName := getPortName (instanceSpec .Name , network .PortOpts , i )
233
- port , err := s .networkingService .GetOrCreatePort (eventObject , clusterName , portName , network , & instanceSpec . SecurityGroups , iTags )
234
+ port , err := s .networkingService .GetOrCreatePort (eventObject , clusterName , portName , network , & securityGroups , iTags )
234
235
if err != nil {
235
236
return nil , err
236
237
}
@@ -253,7 +254,7 @@ func (s *Service) createInstance(eventObject runtime.Object, clusterName string,
253
254
AvailabilityZone : instanceSpec .FailureDomain ,
254
255
Networks : portList ,
255
256
UserData : []byte (instanceSpec .UserData ),
256
- SecurityGroups : instanceSpec . SecurityGroups ,
257
+ SecurityGroups : securityGroups ,
257
258
Tags : instanceSpec .Tags ,
258
259
Metadata : instanceSpec .Metadata ,
259
260
ConfigDrive : & instanceSpec .ConfigDrive ,
@@ -791,19 +792,3 @@ func (s *Service) isTrunkExtSupported() (trunknSupported bool, err error) {
791
792
}
792
793
return true , nil
793
794
}
794
-
795
- // isTrunkConfigured verifies trunk configuration at instance and port levels, useful for avoiding multple api calls to verify trunk support.
796
- func (s * Service ) isTrunkConfigured (nets []infrav1.Network , instanceLevelTrunk bool ) bool {
797
- if instanceLevelTrunk {
798
- return true
799
- }
800
- for _ , net := range nets {
801
- port := net .PortOpts
802
- if port != nil {
803
- if port .Trunk != nil && * port .Trunk {
804
- return true
805
- }
806
- }
807
- }
808
- return false
809
- }
0 commit comments