Skip to content

Commit a88a8b3

Browse files
committed
Add tags to portOpts
Add a 'Tags' field to portOpts which allows setting the tags on the port. (The same tags will also be applied to the trunk port, if a trunk port is created.) Until now, the tags were inherited from the instance tags. Now it's possible to have different instance and port tags. The port tags are appended to any instance tags.
1 parent debe6c0 commit a88a8b3

8 files changed

+106
-4
lines changed

api/v1alpha4/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ type PortOpts struct {
151151
// DisablePortSecurity enables or disables the port security when set.
152152
// When not set, it takes the value of the corresponding field at the network level.
153153
DisablePortSecurity *bool `json:"disablePortSecurity,omitempty"`
154+
155+
// Tags applied to the port (and corresponding trunk, if a trunk is configured.)
156+
// These tags are applied in addition to the instance's tags, which will also be applied to the port.
157+
Tags []string `json:"tags,omitempty"`
154158
}
155159

156160
type FixedIP struct {

api/v1alpha4/zz_generated.deepcopy.go

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

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclusters.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,14 @@ spec:
13411341
items:
13421342
type: string
13431343
type: array
1344+
tags:
1345+
description: Tags applied to the port (and corresponding
1346+
trunk, if a trunk is configured.) These tags are applied
1347+
in addition to the instance's tags, which will also
1348+
be applied to the port.
1349+
items:
1350+
type: string
1351+
type: array
13441352
tenantId:
13451353
type: string
13461354
trunk:
@@ -1810,6 +1818,14 @@ spec:
18101818
items:
18111819
type: string
18121820
type: array
1821+
tags:
1822+
description: Tags applied to the port (and corresponding
1823+
trunk, if a trunk is configured.) These tags are applied
1824+
in addition to the instance's tags, which will also
1825+
be applied to the port.
1826+
items:
1827+
type: string
1828+
type: array
18131829
tenantId:
18141830
type: string
18151831
trunk:
@@ -2090,6 +2106,14 @@ spec:
20902106
items:
20912107
type: string
20922108
type: array
2109+
tags:
2110+
description: Tags applied to the port (and corresponding trunk,
2111+
if a trunk is configured.) These tags are applied in addition
2112+
to the instance's tags, which will also be applied to the
2113+
port.
2114+
items:
2115+
type: string
2116+
type: array
20932117
tenantId:
20942118
type: string
20952119
trunk:
@@ -2282,6 +2306,14 @@ spec:
22822306
items:
22832307
type: string
22842308
type: array
2309+
tags:
2310+
description: Tags applied to the port (and corresponding trunk,
2311+
if a trunk is configured.) These tags are applied in addition
2312+
to the instance's tags, which will also be applied to the
2313+
port.
2314+
items:
2315+
type: string
2316+
type: array
22852317
tenantId:
22862318
type: string
22872319
trunk:

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclustertemplates.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,14 @@ spec:
328328
items:
329329
type: string
330330
type: array
331+
tags:
332+
description: Tags applied to the port (and corresponding
333+
trunk, if a trunk is configured.) These tags
334+
are applied in addition to the instance's
335+
tags, which will also be applied to the port.
336+
items:
337+
type: string
338+
type: array
331339
tenantId:
332340
type: string
333341
trunk:

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachines.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,14 @@ spec:
612612
items:
613613
type: string
614614
type: array
615+
tags:
616+
description: Tags applied to the port (and corresponding trunk,
617+
if a trunk is configured.) These tags are applied in addition
618+
to the instance's tags, which will also be applied to the
619+
port.
620+
items:
621+
type: string
622+
type: array
615623
tenantId:
616624
type: string
617625
trunk:

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachinetemplates.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,14 @@ spec:
562562
items:
563563
type: string
564564
type: array
565+
tags:
566+
description: Tags applied to the port (and corresponding
567+
trunk, if a trunk is configured.) These tags are applied
568+
in addition to the instance's tags, which will also
569+
be applied to the port.
570+
items:
571+
type: string
572+
type: array
565573
tenantId:
566574
type: string
567575
trunk:

pkg/cloud/services/networking/port.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (s *Service) GetPortFromInstanceIP(instanceID string, ip string) ([]ports.P
6666
return s.client.ListPort(portOpts)
6767
}
6868

69-
func (s *Service) GetOrCreatePort(eventObject runtime.Object, clusterName string, portName string, net infrav1.Network, instanceSecurityGroups *[]string, tags []string) (*ports.Port, error) {
69+
func (s *Service) GetOrCreatePort(eventObject runtime.Object, clusterName string, portName string, net infrav1.Network, instanceSecurityGroups *[]string, instanceTags []string) (*ports.Port, error) {
7070
existingPorts, err := s.client.ListPort(ports.ListOpts{
7171
Name: portName,
7272
NetworkID: net.ID,
@@ -161,6 +161,10 @@ func (s *Service) GetOrCreatePort(eventObject runtime.Object, clusterName string
161161
record.Warnf(eventObject, "FailedCreatePort", "Failed to create port %s: %v", portName, err)
162162
return nil, err
163163
}
164+
165+
var tags []string
166+
tags = append(tags, instanceTags...)
167+
tags = append(tags, portOpts.Tags...)
164168
if len(tags) > 0 {
165169
if err = s.replaceAllAttributesTags(eventObject, portResource, port.ID, tags); err != nil {
166170
record.Warnf(eventObject, "FailedReplaceTags", "Failed to replace port tags %s: %v", portName, err)

pkg/cloud/services/networking/port_test.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ func Test_GetOrCreatePort(t *testing.T) {
174174
VNICType: "direct",
175175
Profile: map[string]string{"interface_name": "eno1"},
176176
DisablePortSecurity: pointerToFalse,
177+
Tags: []string{"my-port-tag"},
177178
},
178179
},
179180
nil,
@@ -223,6 +224,7 @@ func Test_GetOrCreatePort(t *testing.T) {
223224
Return(&ports.Port{
224225
ID: portID1,
225226
}, nil)
227+
m.ReplaceAllAttributesTags("ports", portID1, attributestags.ReplaceAllOpts{Tags: []string{"my-port-tag"}}).Return([]string{"my-port-tag"}, nil)
226228
},
227229
&ports.Port{
228230
ID: portID1,
@@ -263,14 +265,14 @@ func Test_GetOrCreatePort(t *testing.T) {
263265
false,
264266
},
265267
{
266-
"creates port with tags passed to function",
268+
"creates port with instance tags when port tags aren't specified",
267269
"foo-port-1",
268270
infrav1.Network{
269271
ID: netID,
270272
PortOpts: &infrav1.PortOpts{},
271273
},
272274
nil,
273-
[]string{"my-tag"},
275+
[]string{"my-instance-tag"},
274276
func(m *mock_networking.MockNetworkClientMockRecorder) {
275277
// No ports found
276278
m.
@@ -286,7 +288,38 @@ func Test_GetOrCreatePort(t *testing.T) {
286288
AllowedAddressPairs: []ports.AddressPair{},
287289
},
288290
}).Return(&ports.Port{ID: portID1}, nil)
289-
m.ReplaceAllAttributesTags("ports", portID1, attributestags.ReplaceAllOpts{Tags: []string{"my-tag"}}).Return([]string{"my-tag"}, nil)
291+
m.ReplaceAllAttributesTags("ports", portID1, attributestags.ReplaceAllOpts{Tags: []string{"my-instance-tag"}}).Return([]string{"my-instance-tag"}, nil)
292+
},
293+
&ports.Port{ID: portID1},
294+
false,
295+
},
296+
{
297+
"creates port with port specific tags appending to instance tags",
298+
"foo-port-1",
299+
infrav1.Network{
300+
ID: netID,
301+
PortOpts: &infrav1.PortOpts{Tags: []string{"my-port-tag"}},
302+
},
303+
nil,
304+
[]string{"my-instance-tag"},
305+
func(m *mock_networking.MockNetworkClientMockRecorder) {
306+
// No ports found
307+
m.
308+
ListPort(ports.ListOpts{
309+
Name: "foo-port-1",
310+
NetworkID: netID,
311+
}).Return([]ports.Port{}, nil)
312+
m.CreatePort(portsbinding.CreateOptsExt{
313+
CreateOptsBuilder: ports.CreateOpts{
314+
Name: "foo-port-1",
315+
Description: "Created by cluster-api-provider-openstack cluster test-cluster",
316+
NetworkID: netID,
317+
AllowedAddressPairs: []ports.AddressPair{},
318+
},
319+
}).Return(&ports.Port{ID: portID1}, nil)
320+
m.
321+
ReplaceAllAttributesTags("ports", portID1, attributestags.ReplaceAllOpts{Tags: []string{"my-instance-tag", "my-port-tag"}}).
322+
Return([]string{"my-instance-tag", "my-port-tag"}, nil)
290323
},
291324
&ports.Port{ID: portID1},
292325
false,

0 commit comments

Comments
 (0)