@@ -34,6 +34,7 @@ import (
34
34
cosSession "github.com/IBM/ibm-cos-sdk-go/aws/session"
35
35
"github.com/IBM/ibm-cos-sdk-go/service/s3"
36
36
tgapiv1 "github.com/IBM/networking-go-sdk/transitgatewayapisv1"
37
+ "github.com/IBM/platform-services-go-sdk/globaltaggingv1"
37
38
"github.com/IBM/platform-services-go-sdk/resourcecontrollerv2"
38
39
"github.com/IBM/platform-services-go-sdk/resourcemanagerv2"
39
40
"github.com/IBM/vpc-go-sdk/vpcv1"
@@ -51,6 +52,7 @@ import (
51
52
infrav1beta2 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
52
53
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/authenticator"
53
54
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/cos"
55
+ "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/globaltagging"
54
56
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/powervs"
55
57
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcecontroller"
56
58
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcemanager"
@@ -80,6 +82,7 @@ const (
80
82
// vpcSubnetIPAddressCount is the total IP Addresses for the subnet.
81
83
// Support for custom address prefixes will be added at a later time. Currently, we use the ip count for subnet creation.
82
84
vpcSubnetIPAddressCount int64 = 256
85
+ tagKey = "powervs.cluster.x-k8s.io-resource-owner:"
83
86
)
84
87
85
88
// PowerVSClusterScopeParams defines the input parameters used to create a new PowerVSClusterScope.
@@ -102,6 +105,7 @@ type ClientFactory struct {
102
105
TransitGatewayFactory func () (transitgateway.TransitGateway , error )
103
106
ResourceControllerFactory func () (resourcecontroller.ResourceController , error )
104
107
ResourceManagerFactory func () (resourcemanager.ResourceManager , error )
108
+ GlobalTaggingFactory func () (globaltagging.GlobalTagging , error )
105
109
}
106
110
107
111
// PowerVSClusterScope defines a scope defined around a Power VS Cluster.
@@ -110,6 +114,7 @@ type PowerVSClusterScope struct {
110
114
patchHelper * patch.Helper
111
115
112
116
IBMPowerVSClient powervs.PowerVS
117
+ GlobalTaggingClient globaltagging.GlobalTagging
113
118
IBMVPCClient vpc.Vpc
114
119
TransitGatewayClient transitgateway.TransitGateway
115
120
ResourceClient resourcecontroller.ResourceController
@@ -253,6 +258,18 @@ func NewPowerVSClusterScope(params PowerVSClusterScopeParams) (*PowerVSClusterSc
253
258
Authenticator : auth ,
254
259
}
255
260
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
+
256
273
rmClient , err := params .getResourceManagerClient (rcManagerOptions )
257
274
if err != nil {
258
275
return nil , fmt .Errorf ("failed to create resource manager client: %w" , err )
@@ -267,6 +284,7 @@ func NewPowerVSClusterScope(params PowerVSClusterScopeParams) (*PowerVSClusterSc
267
284
IBMPowerVSClient : powerVSClient ,
268
285
IBMVPCClient : vpcClient ,
269
286
TransitGatewayClient : tgClient ,
287
+ GlobalTaggingClient : globalTaggingClient ,
270
288
ResourceClient : resourceClient ,
271
289
ResourceManagerClient : rmClient ,
272
290
}
@@ -294,6 +312,18 @@ func (params PowerVSClusterScopeParams) getPowerVSClient(options powervs.Service
294
312
return powervs .NewService (options )
295
313
}
296
314
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
+
297
327
func (params PowerVSClusterScopeParams ) getVPCClient () (vpc.Vpc , error ) {
298
328
if params .Logger .V (DEBUGLEVEL ).Enabled () {
299
329
core .SetLoggingLevel (core .LevelDebug )
@@ -859,10 +889,13 @@ func (s *PowerVSClusterScope) createServiceInstance(ctx context.Context) (*resou
859
889
if zone == nil {
860
890
return nil , fmt .Errorf ("PowerVS zone is not set" )
861
891
}
892
+
893
+ tag := tagKey + s .Name ()
862
894
serviceInstance , _ , err := s .ResourceClient .CreateResourceInstance (& resourcecontrollerv2.CreateResourceInstanceOptions {
863
895
Name : s .GetServiceName (infrav1beta2 .ResourceTypeServiceInstance ),
864
896
Target : zone ,
865
897
ResourceGroup : & resourceGroupID ,
898
+ Tags : append (make ([]string , 0 ), tag ),
866
899
ResourcePlanID : ptr .To (resourcecontroller .PowerVSResourcePlanID ),
867
900
})
868
901
if err != nil {
@@ -1084,6 +1117,24 @@ func (s *PowerVSClusterScope) createDHCPServer(ctx context.Context) (*string, er
1084
1117
return dhcpServer .ID , nil
1085
1118
}
1086
1119
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
+
1087
1138
// ReconcileVPC reconciles VPC.
1088
1139
func (s * PowerVSClusterScope ) ReconcileVPC (ctx context.Context ) (bool , error ) {
1089
1140
log := ctrl .LoggerFrom (ctx )
@@ -1184,6 +1235,11 @@ func (s *PowerVSClusterScope) createVPC() (*string, error) {
1184
1235
return nil , err
1185
1236
}
1186
1237
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
+
1187
1243
// set security group for vpc
1188
1244
options := & vpcv1.CreateSecurityGroupRuleOptions {}
1189
1245
options .SetSecurityGroupID (* vpcDetails .DefaultSecurityGroup .ID )
@@ -1338,6 +1394,12 @@ func (s *PowerVSClusterScope) createVPCSubnet(subnet infrav1beta2.Subnet) (*stri
1338
1394
if subnetDetails == nil {
1339
1395
return nil , fmt .Errorf ("created VPC subnet is nil" )
1340
1396
}
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
+ }
1341
1403
return subnetDetails .ID , nil
1342
1404
}
1343
1405
@@ -1560,7 +1622,12 @@ func (s *PowerVSClusterScope) createVPCSecurityGroup(ctx context.Context, spec i
1560
1622
if err != nil {
1561
1623
return nil , fmt .Errorf ("error creating VPC security group: %w" , err )
1562
1624
}
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
+ }
1564
1631
return securityGroup .ID , nil
1565
1632
}
1566
1633
@@ -2023,6 +2090,12 @@ func (s *PowerVSClusterScope) createTransitGateway(ctx context.Context) error {
2023
2090
return err
2024
2091
}
2025
2092
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
+
2026
2099
s .SetTransitGatewayStatus (tg .ID , ptr .To (true ))
2027
2100
2028
2101
vpcCRN , err := s .fetchVPCCRN ()
@@ -2243,6 +2316,12 @@ func (s *PowerVSClusterScope) createLoadBalancer(ctx context.Context, lb infrav1
2243
2316
if err != nil {
2244
2317
return nil , fmt .Errorf ("failed to create load balancer: %w" , err )
2245
2318
}
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
+
2246
2325
lbState := infrav1beta2 .VPCLoadBalancerState (* loadBalancer .ProvisioningStatus )
2247
2326
return & infrav1beta2.VPCLoadBalancerStatus {
2248
2327
ID : loadBalancer .ID ,
@@ -2398,12 +2477,14 @@ func (s *PowerVSClusterScope) createCOSServiceInstance() (*resourcecontrollerv2.
2398
2477
return nil , fmt .Errorf ("failed to fetch resource group ID for resource group %v, ID is empty" , s .ResourceGroup ())
2399
2478
}
2400
2479
2480
+ tag := tagKey + s .Name ()
2401
2481
target := "Global"
2402
2482
// create service instance
2403
2483
serviceInstance , _ , err := s .ResourceClient .CreateResourceInstance (& resourcecontrollerv2.CreateResourceInstanceOptions {
2404
2484
Name : s .GetServiceName (infrav1beta2 .ResourceTypeCOSInstance ),
2405
2485
Target : & target ,
2406
2486
ResourceGroup : & resourceGroupID ,
2487
+ Tags : append (make ([]string , 0 ), tag ),
2407
2488
ResourcePlanID : ptr .To (resourcecontroller .CosResourcePlanID ),
2408
2489
})
2409
2490
if err != nil {
0 commit comments