Skip to content

Commit 244f5cf

Browse files
committed
Added network tagging to controller
1 parent a0f04a8 commit 244f5cf

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

pkg/cloud/network.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,18 @@ func (c *client) ResolveNetwork(csCluster *infrav1.CloudStackCluster) (retErr er
5959
}
6060

6161
func (c *client) GetOrCreateNetwork(csCluster *infrav1.CloudStackCluster) (retErr error) {
62+
// Tag the network so we can clean it up later
63+
tagNetwork := func(csCluster *infrav1.CloudStackCluster, createdByCapc bool) error {
64+
clusterTag := "CAPC_cluster_" + string(csCluster.UID)
65+
tags := map[string]string{clusterTag: "1"}
66+
if createdByCapc {
67+
tags["created_by_CAPC"] = "1"
68+
}
69+
return c.AddNetworkTags(csCluster.Status.NetworkID, tags)
70+
}
71+
6272
if retErr = c.ResolveNetwork(csCluster); retErr == nil { // Found network.
63-
return nil
73+
return tagNetwork(csCluster, false)
6474
} else if !strings.Contains(retErr.Error(), "No match found") { // Some other error.
6575
return retErr
6676
} // Network not found.
@@ -86,7 +96,7 @@ func (c *client) GetOrCreateNetwork(csCluster *infrav1.CloudStackCluster) (retEr
8696
csCluster.Status.NetworkID = resp.Id
8797
csCluster.Status.NetworkType = resp.Type
8898

89-
return nil
99+
return tagNetwork(csCluster, true)
90100
}
91101

92102
func (c *client) ResolvePublicIPDetails(csCluster *infrav1.CloudStackCluster) (*cloudstack.PublicIpAddress, error) {

pkg/cloud/tags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ limitations under the License.
1717
package cloud
1818

1919
type TagIFace interface {
20-
TagNetwork(string, map[string]string) error
20+
AddNetworkTags(string, map[string]string) error
2121
GetNetworkTags(string) (map[string]string, error)
2222
DeleteNetworkTags(string, map[string]string) error
2323
}
2424

2525
const resourceTypeNetwork = "network"
2626

2727
// TagNetwork adds tags to a network by network id.
28-
func (c *client) TagNetwork(networkId string, tags map[string]string) error {
28+
func (c *client) AddNetworkTags(networkId string, tags map[string]string) error {
2929
// https://cloudstack.apache.org/api/apidocs-4.16/apis/createTags.html
3030
p := c.cs.Resourcetags.NewCreateTagsParams([]string{networkId}, resourceTypeNetwork, tags)
3131
_, err := c.cs.Resourcetags.CreateTags(p)

pkg/cloud/tags_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ var _ = Describe("Tag Unit Tests", func() {
3737
client, connectionErr := cloud.NewClient("../../cloud-config")
3838

3939
const (
40-
tagKey = "TestTag"
41-
tagValue = "ArbitraryValue"
40+
tagKey = "test_tag"
41+
tagValue = "arbitrary_value"
4242
)
4343

4444
var (
4545
networkId string
46+
testTags map[string]string
4647
)
4748

4849
BeforeEach(func() {
@@ -55,13 +56,13 @@ var _ = Describe("Tag Unit Tests", func() {
5556
}
5657

5758
networkId = cluster.Status.NetworkID
59+
testTags = map[string]string{tagKey: tagValue}
5860
})
5961

6062
It("Tags a network with an arbitrary tag.", func() {
61-
tags := map[string]string{tagKey: tagValue}
6263
// Delete the tag if it already exists from a prior test run, otherwise the test will fail.
63-
_ = client.DeleteNetworkTags(networkId, tags)
64-
Ω(client.TagNetwork(networkId, tags)).Should(Succeed())
64+
_ = client.DeleteNetworkTags(networkId, testTags)
65+
Ω(client.AddNetworkTags(networkId, testTags)).Should(Succeed())
6566
})
6667

6768
It("Fetches said tag.", func() {
@@ -71,7 +72,7 @@ var _ = Describe("Tag Unit Tests", func() {
7172
})
7273

7374
It("Deletes said tag.", func() {
74-
Ω(client.DeleteNetworkTags(networkId, map[string]string{tagKey: tagValue})).Should(Succeed())
75+
Ω(client.DeleteNetworkTags(networkId, testTags)).Should(Succeed())
7576
remainingTags, err := client.GetNetworkTags(networkId)
7677
Ω(err).Should(BeNil())
7778
Ω(remainingTags[tagKey]).Should(Equal(""))

0 commit comments

Comments
 (0)