@@ -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"
@@ -50,6 +51,7 @@ import (
50
51
infrav1beta2 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
51
52
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/authenticator"
52
53
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/cos"
54
+ "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/globaltagging"
53
55
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/powervs"
54
56
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcecontroller"
55
57
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcemanager"
@@ -79,6 +81,7 @@ const (
79
81
// vpcSubnetIPAddressCount is the total IP Addresses for the subnet.
80
82
// Support for custom address prefixes will be added at a later time. Currently, we use the ip count for subnet creation.
81
83
vpcSubnetIPAddressCount int64 = 256
84
+ tagKey = "powervs.cluster.x-k8s.io-resource-owner:"
82
85
)
83
86
84
87
// PowerVSClusterScopeParams defines the input parameters used to create a new PowerVSClusterScope.
@@ -101,6 +104,7 @@ type ClientFactory struct {
101
104
TransitGatewayFactory func () (transitgateway.TransitGateway , error )
102
105
ResourceControllerFactory func () (resourcecontroller.ResourceController , error )
103
106
ResourceManagerFactory func () (resourcemanager.ResourceManager , error )
107
+ GlobalTaggingFactory func () (globaltagging.GlobalTagging , error )
104
108
}
105
109
106
110
// 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
@@ -254,6 +259,18 @@ func NewPowerVSClusterScope(params PowerVSClusterScopeParams) (*PowerVSClusterSc
254
259
Authenticator : auth ,
255
260
}
256
261
262
+ // Create Global Tagging client.
263
+ gtOptions := globaltagging.ServiceOptions {
264
+ GlobalTaggingV1Options : & globaltaggingv1.GlobalTaggingV1Options {
265
+ Authenticator : auth ,
266
+ },
267
+ }
268
+
269
+ globalTaggingClient , err := params .getGlobalTaggingClient (gtOptions )
270
+ if err != nil {
271
+ return nil , fmt .Errorf ("failed to create global tagging client: %w" , err )
272
+ }
273
+
257
274
rmClient , err := params .getResourceManagerClient (rcManagerOptions )
258
275
if err != nil {
259
276
return nil , fmt .Errorf ("failed to create resource manager client: %w" , err )
@@ -269,6 +286,7 @@ func NewPowerVSClusterScope(params PowerVSClusterScopeParams) (*PowerVSClusterSc
269
286
IBMPowerVSClient : powerVSClient ,
270
287
IBMVPCClient : vpcClient ,
271
288
TransitGatewayClient : tgClient ,
289
+ GlobalTaggingClient : globalTaggingClient ,
272
290
ResourceClient : resourceClient ,
273
291
ResourceManagerClient : rmClient ,
274
292
}
@@ -296,6 +314,18 @@ func (params PowerVSClusterScopeParams) getPowerVSClient(options powervs.Service
296
314
return powervs .NewService (options )
297
315
}
298
316
317
+ func (params PowerVSClusterScopeParams ) getGlobalTaggingClient (gtOptions globaltagging.ServiceOptions ) (globaltagging.GlobalTagging , error ) {
318
+ if params .GlobalTaggingFactory != nil {
319
+ return params .GlobalTaggingFactory ()
320
+ }
321
+
322
+ if gtEndpoint := endpoints .FetchEndpoints (string (endpoints .GlobalTagging ), params .ServiceEndpoint ); gtEndpoint != "" {
323
+ params .Logger .V (3 ).Info ("Overriding the default global tagging endpoint" , "GlobaTaggingEndpoint" , gtEndpoint )
324
+ gtOptions .URL = gtEndpoint
325
+ }
326
+ return globaltagging .NewService (gtOptions )
327
+ }
328
+
299
329
func (params PowerVSClusterScopeParams ) getVPCClient () (vpc.Vpc , error ) {
300
330
if params .Logger .V (DEBUGLEVEL ).Enabled () {
301
331
core .SetLoggingLevel (core .LevelDebug )
@@ -852,10 +882,13 @@ func (s *PowerVSClusterScope) createServiceInstance() (*resourcecontrollerv2.Res
852
882
if zone == nil {
853
883
return nil , fmt .Errorf ("PowerVS zone is not set" )
854
884
}
885
+
886
+ tag := tagKey + s .Name ()
855
887
serviceInstance , _ , err := s .ResourceClient .CreateResourceInstance (& resourcecontrollerv2.CreateResourceInstanceOptions {
856
888
Name : s .GetServiceName (infrav1beta2 .ResourceTypeServiceInstance ),
857
889
Target : zone ,
858
890
ResourceGroup : & resourceGroupID ,
891
+ Tags : append (make ([]string , 0 ), tag ),
859
892
ResourcePlanID : ptr .To (resourcecontroller .PowerVSResourcePlanID ),
860
893
})
861
894
if err != nil {
@@ -1072,6 +1105,24 @@ func (s *PowerVSClusterScope) createDHCPServer() (*string, error) {
1072
1105
return dhcpServer .ID , nil
1073
1106
}
1074
1107
1108
+ // TagResource will attach a user Tag to a resource.
1109
+ func (s * PowerVSClusterScope ) TagResource (tagName string , resourceCRN * string ) error {
1110
+ tagOptions := & globaltaggingv1.AttachTagOptions {}
1111
+ tagOptions .SetResources ([]globaltaggingv1.Resource {
1112
+ {
1113
+ ResourceID : resourceCRN ,
1114
+ },
1115
+ })
1116
+
1117
+ tagOptions .SetTagName (tagName )
1118
+ tagOptions .SetTagType (globaltaggingv1 .AttachTagOptionsTagTypeUserConst )
1119
+ if _ , _ , err := s .GlobalTaggingClient .AttachTag (tagOptions ); err != nil {
1120
+ return fmt .Errorf ("failure tagging resource: %w" , err )
1121
+ }
1122
+
1123
+ return nil
1124
+ }
1125
+
1075
1126
// ReconcileVPC reconciles VPC.
1076
1127
func (s * PowerVSClusterScope ) ReconcileVPC () (bool , error ) {
1077
1128
// if VPC server id is set means the VPC is already created
@@ -1172,6 +1223,11 @@ func (s *PowerVSClusterScope) createVPC() (*string, error) {
1172
1223
return nil , err
1173
1224
}
1174
1225
1226
+ tag := tagKey + s .Name ()
1227
+ if err = s .TagResource (tag , vpcDetails .CRN ); err != nil {
1228
+ s .Error (err , "failed to tag vpc" )
1229
+ }
1230
+
1175
1231
// set security group for vpc
1176
1232
options := & vpcv1.CreateSecurityGroupRuleOptions {}
1177
1233
options .SetSecurityGroupID (* vpcDetails .DefaultSecurityGroup .ID )
@@ -1325,6 +1381,12 @@ func (s *PowerVSClusterScope) createVPCSubnet(subnet infrav1beta2.Subnet) (*stri
1325
1381
if subnetDetails == nil {
1326
1382
return nil , fmt .Errorf ("create VPC subnet is nil" )
1327
1383
}
1384
+
1385
+ tag := tagKey + s .Name ()
1386
+ err = s .TagResource (tag , subnetDetails .CRN )
1387
+ if err != nil {
1388
+ s .Error (err , "failed to tag subnet" )
1389
+ }
1328
1390
return subnetDetails .ID , nil
1329
1391
}
1330
1392
@@ -1542,7 +1604,12 @@ func (s *PowerVSClusterScope) createVPCSecurityGroup(spec infrav1beta2.VPCSecuri
1542
1604
if err != nil {
1543
1605
return nil , err
1544
1606
}
1545
- // To-Do: Add tags to VPC security group, need to implement the client for "github.com/IBM/platform-services-go-sdk/globaltaggingv1".
1607
+
1608
+ tag := tagKey + s .Name ()
1609
+ err = s .TagResource (tag , securityGroup .CRN )
1610
+ if err != nil {
1611
+ s .Error (err , "failed to tag security group" )
1612
+ }
1546
1613
return securityGroup .ID , nil
1547
1614
}
1548
1615
@@ -2002,6 +2069,12 @@ func (s *PowerVSClusterScope) createTransitGateway() error {
2002
2069
return err
2003
2070
}
2004
2071
2072
+ tag := tagKey + s .Name ()
2073
+ err = s .TagResource (tag , tg .Crn )
2074
+ if err != nil {
2075
+ s .Error (err , "failed to tag transitGateway" )
2076
+ }
2077
+
2005
2078
s .SetTransitGatewayStatus (tg .ID , ptr .To (true ))
2006
2079
2007
2080
vpcCRN , err := s .fetchVPCCRN ()
@@ -2217,6 +2290,12 @@ func (s *PowerVSClusterScope) createLoadBalancer(lb infrav1beta2.VPCLoadBalancer
2217
2290
if err != nil {
2218
2291
return nil , err
2219
2292
}
2293
+
2294
+ tag := tagKey + s .Name ()
2295
+ if err = s .TagResource (tag , loadBalancer .CRN ); err != nil {
2296
+ s .Error (err , "failed to tag load balancer" )
2297
+ }
2298
+
2220
2299
lbState := infrav1beta2 .VPCLoadBalancerState (* loadBalancer .ProvisioningStatus )
2221
2300
return & infrav1beta2.VPCLoadBalancerStatus {
2222
2301
ID : loadBalancer .ID ,
@@ -2373,12 +2452,14 @@ func (s *PowerVSClusterScope) createCOSServiceInstance() (*resourcecontrollerv2.
2373
2452
return nil , fmt .Errorf ("failed to fetch resource group ID for resource group %v, ID is empty" , s .ResourceGroup ())
2374
2453
}
2375
2454
2455
+ tag := tagKey + s .Name ()
2376
2456
target := "Global"
2377
2457
// create service instance
2378
2458
serviceInstance , _ , err := s .ResourceClient .CreateResourceInstance (& resourcecontrollerv2.CreateResourceInstanceOptions {
2379
2459
Name : s .GetServiceName (infrav1beta2 .ResourceTypeCOSInstance ),
2380
2460
Target : & target ,
2381
2461
ResourceGroup : & resourceGroupID ,
2462
+ Tags : append (make ([]string , 0 ), tag ),
2382
2463
ResourcePlanID : ptr .To (resourcecontroller .CosResourcePlanID ),
2383
2464
})
2384
2465
if err != nil {
0 commit comments