Skip to content

Commit 9133831

Browse files
author
Emilio Garcia
committed
Allow tagging of ports
The replaceAllAttributesTags function has been extended to support trunks and ports. GetOrReplacePorts was also updated to add tags to ports it creates or gets.
1 parent 650ce1c commit 9133831

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

pkg/cloud/services/networking/port.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,20 @@ 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-
164+
if len(tags) > 0 {
165+
if err = s.replaceAllAttributesTags(eventObject, portResource, port.ID, tags); err != nil {
166+
record.Warnf(eventObject, "FailedReplaceTags", "Failed to replace port tags %s: %v", portName, err)
167+
return nil, err
168+
}
169+
}
165170
record.Eventf(eventObject, "SuccessfulCreatePort", "Created port %s with id %s", port.Name, port.ID)
166171
if portOpts.Trunk != nil && *portOpts.Trunk {
167172
trunk, err := s.getOrCreateTrunk(eventObject, clusterName, port.Name, port.ID)
168173
if err != nil {
169174
record.Warnf(eventObject, "FailedCreateTrunk", "Failed to create trunk for port %s: %v", portName, err)
170175
return nil, err
171176
}
172-
if err = s.replaceAllAttributesTags(eventObject, trunk.ID, tags); err != nil {
177+
if err = s.replaceAllAttributesTags(eventObject, trunkResource, trunk.ID, tags); err != nil {
173178
record.Warnf(eventObject, "FailedReplaceTags", "Failed to replace trunk tags %s: %v", portName, err)
174179
return nil, err
175180
}

pkg/cloud/services/networking/service.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ import (
2222
"github.com/go-logr/logr"
2323
"github.com/gophercloud/gophercloud"
2424
"github.com/gophercloud/gophercloud/openstack"
25+
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/attributestags"
2526
"github.com/gophercloud/utils/openstack/clientconfig"
27+
"k8s.io/apimachinery/pkg/runtime"
2628

2729
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/provider"
30+
"sigs.k8s.io/cluster-api-provider-openstack/pkg/record"
2831
)
2932

3033
const (
3134
networkPrefix string = "k8s-clusterapi"
35+
trunkResource string = "trunks"
36+
portResource string = "ports"
3237
)
3338

3439
// Service interfaces with the OpenStack Networking API.
@@ -78,3 +83,23 @@ func NewTestService(projectID string, client NetworkClient, logger logr.Logger)
7883
logger: logger,
7984
}
8085
}
86+
87+
// replaceAllAttributesTags replaces all tags on a neworking resource.
88+
// the value of resourceType must match one of the allowed constants: trunkResource or portResource.
89+
func (s *Service) replaceAllAttributesTags(eventObject runtime.Object, resourceType string, resourceID string, tags []string) error {
90+
if resourceType != trunkResource && resourceType != portResource {
91+
record.Warnf(eventObject, "FailedReplaceAllAttributesTags", "Invalid resourceType argument in function call")
92+
panic(fmt.Errorf("invalid argument: resourceType, %s, does not match allowed arguments: %s or %s", resourceType, trunkResource, portResource))
93+
}
94+
95+
_, err := s.client.ReplaceAllAttributesTags(resourceType, resourceID, attributestags.ReplaceAllOpts{
96+
Tags: tags,
97+
})
98+
if err != nil {
99+
record.Warnf(eventObject, "FailedReplaceAllAttributesTags", "Failed to replace all attributestags, %s: %v", resourceID, err)
100+
return err
101+
}
102+
103+
record.Eventf(eventObject, "SuccessfulReplaceAllAttributeTags", "Replaced all attributestags for %s with tags %s", resourceID, tags)
104+
return nil
105+
}

pkg/cloud/services/networking/trunk.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121
"time"
2222

23-
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/attributestags"
2423
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/trunks"
2524
"k8s.io/apimachinery/pkg/runtime"
2625
"sigs.k8s.io/cluster-api/util"
@@ -78,19 +77,6 @@ func (s *Service) getOrCreateTrunk(eventObject runtime.Object, clusterName, trun
7877
return trunk, nil
7978
}
8079

81-
func (s *Service) replaceAllAttributesTags(eventObject runtime.Object, trunkID string, tags []string) error {
82-
_, err := s.client.ReplaceAllAttributesTags("trunks", trunkID, attributestags.ReplaceAllOpts{
83-
Tags: tags,
84-
})
85-
if err != nil {
86-
record.Warnf(eventObject, "FailedReplaceAllAttributesTags", "Failed to replace all attributestags, trunk %s: %v", trunkID, err)
87-
return err
88-
}
89-
90-
record.Eventf(eventObject, "SuccessfulReplaceAllAttributeTags", "Replaced all attributestags %s with tags %s", trunkID, tags)
91-
return nil
92-
}
93-
9480
func (s *Service) DeleteTrunk(eventObject runtime.Object, portID string) error {
9581
listOpts := trunks.ListOpts{
9682
PortID: portID,

test/e2e/suites/e2e/e2e_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
corev1 "k8s.io/api/core/v1"
3737
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3838
apimachinerytypes "k8s.io/apimachinery/pkg/types"
39+
utilrand "k8s.io/apimachinery/pkg/util/rand"
3940
"k8s.io/utils/pointer"
4041
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
4142
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha4"
@@ -153,26 +154,30 @@ var _ = Describe("e2e tests", func() {
153154
{Description: "primary"},
154155
}
155156

157+
testTag := utilrand.String(6)
158+
machineTags := []string{testTag}
159+
156160
// Note that as the bootstrap config does not have cloud.conf, the node will not be added to the cluster.
157161
// We still expect the port for the machine to be created.
158162
framework.CreateMachineDeployment(ctx, framework.CreateMachineDeploymentInput{
159163
Creator: e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
160164
MachineDeployment: makeMachineDeployment(namespace.Name, md3Name, clusterName, "", 1),
161165
BootstrapConfigTemplate: makeJoinBootstrapConfigTemplate(namespace.Name, md3Name),
162-
InfraMachineTemplate: makeOpenStackMachineTemplateWithPortOptions(namespace.Name, clusterName, md3Name, customPortOptions),
166+
InfraMachineTemplate: makeOpenStackMachineTemplateWithPortOptions(namespace.Name, clusterName, md3Name, customPortOptions, machineTags),
163167
})
164168

165169
shared.Byf("Waiting for custom port to be created")
166170
var plist []ports.Port
167171
var err error
168172
Eventually(func() int {
169-
plist, err = shared.DumpOpenStackPorts(e2eCtx, ports.ListOpts{Description: "primary"})
173+
plist, err = shared.DumpOpenStackPorts(e2eCtx, ports.ListOpts{Description: "primary", Tags: testTag})
170174
Expect(err).To(BeNil())
171175
return len(plist)
172176
}, e2eCtx.E2EConfig.GetIntervals(specName, "wait-worker-nodes")...).Should(Equal(1))
173177

174178
port := plist[0]
175179
Expect(port.Description).To(Equal("primary"))
180+
Expect(port.Tags).To(ContainElement(testTag))
176181
})
177182
It("It should be creatable and deletable", func() {
178183
shared.Byf("Creating a cluster")
@@ -465,7 +470,7 @@ func makeOpenStackMachineTemplate(namespace, clusterName, name string, subnetID
465470
}
466471
}
467472

468-
func makeOpenStackMachineTemplateWithPortOptions(namespace, clusterName, name string, portOpts *[]infrav1.PortOpts) *infrav1.OpenStackMachineTemplate {
473+
func makeOpenStackMachineTemplateWithPortOptions(namespace, clusterName, name string, portOpts *[]infrav1.PortOpts, machineTags []string) *infrav1.OpenStackMachineTemplate {
469474
return &infrav1.OpenStackMachineTemplate{
470475
ObjectMeta: metav1.ObjectMeta{
471476
Name: name,
@@ -483,6 +488,7 @@ func makeOpenStackMachineTemplateWithPortOptions(namespace, clusterName, name st
483488
Name: fmt.Sprintf("%s-cloud-config", clusterName),
484489
},
485490
Ports: *portOpts,
491+
Tags: machineTags,
486492
},
487493
},
488494
},

0 commit comments

Comments
 (0)