Skip to content

Commit 1048502

Browse files
committed
Tag PowerVS Cluster Resources
1 parent 126acb0 commit 1048502

File tree

3 files changed

+395
-27
lines changed

3 files changed

+395
-27
lines changed

cloud/scope/powervs_cluster.go

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
cosSession "github.com/IBM/ibm-cos-sdk-go/aws/session"
3535
"github.com/IBM/ibm-cos-sdk-go/service/s3"
3636
tgapiv1 "github.com/IBM/networking-go-sdk/transitgatewayapisv1"
37+
"github.com/IBM/platform-services-go-sdk/globaltaggingv1"
3738
"github.com/IBM/platform-services-go-sdk/resourcecontrollerv2"
3839
"github.com/IBM/platform-services-go-sdk/resourcemanagerv2"
3940
"github.com/IBM/vpc-go-sdk/vpcv1"
@@ -51,6 +52,7 @@ import (
5152
infrav1beta2 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
5253
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/authenticator"
5354
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/cos"
55+
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/globaltagging"
5456
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/powervs"
5557
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcecontroller"
5658
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcemanager"
@@ -80,6 +82,7 @@ const (
8082
// vpcSubnetIPAddressCount is the total IP Addresses for the subnet.
8183
// Support for custom address prefixes will be added at a later time. Currently, we use the ip count for subnet creation.
8284
vpcSubnetIPAddressCount int64 = 256
85+
tagKey = "powervs.cluster.x-k8s.io-resource-owner:"
8386
)
8487

8588
// PowerVSClusterScopeParams defines the input parameters used to create a new PowerVSClusterScope.
@@ -102,6 +105,7 @@ type ClientFactory struct {
102105
TransitGatewayFactory func() (transitgateway.TransitGateway, error)
103106
ResourceControllerFactory func() (resourcecontroller.ResourceController, error)
104107
ResourceManagerFactory func() (resourcemanager.ResourceManager, error)
108+
GlobalTaggingFactory func() (globaltagging.GlobalTagging, error)
105109
}
106110

107111
// PowerVSClusterScope defines a scope defined around a Power VS Cluster.
@@ -110,6 +114,7 @@ type PowerVSClusterScope struct {
110114
patchHelper *patch.Helper
111115

112116
IBMPowerVSClient powervs.PowerVS
117+
GlobalTaggingClient globaltagging.GlobalTagging
113118
IBMVPCClient vpc.Vpc
114119
TransitGatewayClient transitgateway.TransitGateway
115120
ResourceClient resourcecontroller.ResourceController
@@ -253,6 +258,18 @@ func NewPowerVSClusterScope(params PowerVSClusterScopeParams) (*PowerVSClusterSc
253258
Authenticator: auth,
254259
}
255260

261+
// Create Global Tagging client.
262+
gtOptions := globaltagging.ServiceOptions{
263+
GlobalTaggingV1Options: &globaltaggingv1.GlobalTaggingV1Options{
264+
Authenticator: auth,
265+
},
266+
}
267+
268+
globalTaggingClient, err := params.getGlobalTaggingClient(gtOptions)
269+
if err != nil {
270+
return nil, fmt.Errorf("failed to create global tagging client: %w", err)
271+
}
272+
256273
rmClient, err := params.getResourceManagerClient(rcManagerOptions)
257274
if err != nil {
258275
return nil, fmt.Errorf("failed to create resource manager client: %w", err)
@@ -267,6 +284,7 @@ func NewPowerVSClusterScope(params PowerVSClusterScopeParams) (*PowerVSClusterSc
267284
IBMPowerVSClient: powerVSClient,
268285
IBMVPCClient: vpcClient,
269286
TransitGatewayClient: tgClient,
287+
GlobalTaggingClient: globalTaggingClient,
270288
ResourceClient: resourceClient,
271289
ResourceManagerClient: rmClient,
272290
}
@@ -294,6 +312,18 @@ func (params PowerVSClusterScopeParams) getPowerVSClient(options powervs.Service
294312
return powervs.NewService(options)
295313
}
296314

315+
func (params PowerVSClusterScopeParams) getGlobalTaggingClient(gtOptions globaltagging.ServiceOptions) (globaltagging.GlobalTagging, error) {
316+
if params.GlobalTaggingFactory != nil {
317+
return params.GlobalTaggingFactory()
318+
}
319+
320+
if gtEndpoint := endpoints.FetchEndpoints(string(endpoints.GlobalTagging), params.ServiceEndpoint); gtEndpoint != "" {
321+
params.Logger.V(3).Info("Overriding the default global tagging endpoint", "GlobaTaggingEndpoint", gtEndpoint)
322+
gtOptions.URL = gtEndpoint
323+
}
324+
return globaltagging.NewService(gtOptions)
325+
}
326+
297327
func (params PowerVSClusterScopeParams) getVPCClient() (vpc.Vpc, error) {
298328
if params.Logger.V(DEBUGLEVEL).Enabled() {
299329
core.SetLoggingLevel(core.LevelDebug)
@@ -859,10 +889,13 @@ func (s *PowerVSClusterScope) createServiceInstance(ctx context.Context) (*resou
859889
if zone == nil {
860890
return nil, fmt.Errorf("PowerVS zone is not set")
861891
}
892+
893+
tag := tagKey + s.Name()
862894
serviceInstance, _, err := s.ResourceClient.CreateResourceInstance(&resourcecontrollerv2.CreateResourceInstanceOptions{
863895
Name: s.GetServiceName(infrav1beta2.ResourceTypeServiceInstance),
864896
Target: zone,
865897
ResourceGroup: &resourceGroupID,
898+
Tags: append(make([]string, 0), tag),
866899
ResourcePlanID: ptr.To(resourcecontroller.PowerVSResourcePlanID),
867900
})
868901
if err != nil {
@@ -1084,6 +1117,24 @@ func (s *PowerVSClusterScope) createDHCPServer(ctx context.Context) (*string, er
10841117
return dhcpServer.ID, nil
10851118
}
10861119

1120+
// TagResource will attach a user Tag to a resource.
1121+
func (s *PowerVSClusterScope) TagResource(tagName string, resourceCRN *string) error {
1122+
tagOptions := &globaltaggingv1.AttachTagOptions{}
1123+
tagOptions.SetResources([]globaltaggingv1.Resource{
1124+
{
1125+
ResourceID: resourceCRN,
1126+
},
1127+
})
1128+
1129+
tagOptions.SetTagName(tagName)
1130+
tagOptions.SetTagType(globaltaggingv1.AttachTagOptionsTagTypeUserConst)
1131+
if _, _, err := s.GlobalTaggingClient.AttachTag(tagOptions); err != nil {
1132+
return fmt.Errorf("failure tagging resource: %w", err)
1133+
}
1134+
1135+
return nil
1136+
}
1137+
10871138
// ReconcileVPC reconciles VPC.
10881139
func (s *PowerVSClusterScope) ReconcileVPC(ctx context.Context) (bool, error) {
10891140
log := ctrl.LoggerFrom(ctx)
@@ -1184,6 +1235,11 @@ func (s *PowerVSClusterScope) createVPC() (*string, error) {
11841235
return nil, err
11851236
}
11861237

1238+
tag := tagKey + s.Name()
1239+
if err = s.TagResource(tag, vpcDetails.CRN); err != nil {
1240+
s.Error(err, "failed to tag vpc")
1241+
}
1242+
11871243
// set security group for vpc
11881244
options := &vpcv1.CreateSecurityGroupRuleOptions{}
11891245
options.SetSecurityGroupID(*vpcDetails.DefaultSecurityGroup.ID)
@@ -1338,6 +1394,12 @@ func (s *PowerVSClusterScope) createVPCSubnet(subnet infrav1beta2.Subnet) (*stri
13381394
if subnetDetails == nil {
13391395
return nil, fmt.Errorf("created VPC subnet is nil")
13401396
}
1397+
1398+
tag := tagKey + s.Name()
1399+
err = s.TagResource(tag, subnetDetails.CRN)
1400+
if err != nil {
1401+
s.Error(err, "failed to tag subnet")
1402+
}
13411403
return subnetDetails.ID, nil
13421404
}
13431405

@@ -1560,7 +1622,12 @@ func (s *PowerVSClusterScope) createVPCSecurityGroup(ctx context.Context, spec i
15601622
if err != nil {
15611623
return nil, fmt.Errorf("error creating VPC security group: %w", err)
15621624
}
1563-
// To-Do: Add tags to VPC security group, need to implement the client for "github.com/IBM/platform-services-go-sdk/globaltaggingv1".
1625+
1626+
tag := tagKey + s.Name()
1627+
err = s.TagResource(tag, securityGroup.CRN)
1628+
if err != nil {
1629+
s.Error(err, "failed to tag security group")
1630+
}
15641631
return securityGroup.ID, nil
15651632
}
15661633

@@ -2023,6 +2090,12 @@ func (s *PowerVSClusterScope) createTransitGateway(ctx context.Context) error {
20232090
return err
20242091
}
20252092

2093+
tag := tagKey + s.Name()
2094+
err = s.TagResource(tag, tg.Crn)
2095+
if err != nil {
2096+
s.Error(err, "failed to tag transitGateway")
2097+
}
2098+
20262099
s.SetTransitGatewayStatus(tg.ID, ptr.To(true))
20272100

20282101
vpcCRN, err := s.fetchVPCCRN()
@@ -2243,6 +2316,12 @@ func (s *PowerVSClusterScope) createLoadBalancer(ctx context.Context, lb infrav1
22432316
if err != nil {
22442317
return nil, fmt.Errorf("failed to create load balancer: %w", err)
22452318
}
2319+
2320+
tag := tagKey + s.Name()
2321+
if err = s.TagResource(tag, loadBalancer.CRN); err != nil {
2322+
s.Error(err, "failed to tag load balancer")
2323+
}
2324+
22462325
lbState := infrav1beta2.VPCLoadBalancerState(*loadBalancer.ProvisioningStatus)
22472326
return &infrav1beta2.VPCLoadBalancerStatus{
22482327
ID: loadBalancer.ID,
@@ -2398,12 +2477,14 @@ func (s *PowerVSClusterScope) createCOSServiceInstance() (*resourcecontrollerv2.
23982477
return nil, fmt.Errorf("failed to fetch resource group ID for resource group %v, ID is empty", s.ResourceGroup())
23992478
}
24002479

2480+
tag := tagKey + s.Name()
24012481
target := "Global"
24022482
// create service instance
24032483
serviceInstance, _, err := s.ResourceClient.CreateResourceInstance(&resourcecontrollerv2.CreateResourceInstanceOptions{
24042484
Name: s.GetServiceName(infrav1beta2.ResourceTypeCOSInstance),
24052485
Target: &target,
24062486
ResourceGroup: &resourceGroupID,
2487+
Tags: append(make([]string, 0), tag),
24072488
ResourcePlanID: ptr.To(resourcecontroller.CosResourcePlanID),
24082489
})
24092490
if err != nil {

0 commit comments

Comments
 (0)