Skip to content

Commit e58c1f0

Browse files
committed
Tag PowerVS Cluster Resources
1 parent ff2e9fa commit e58c1f0

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"
@@ -50,6 +51,7 @@ import (
5051
infrav1beta2 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
5152
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/authenticator"
5253
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/cos"
54+
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/globaltagging"
5355
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/powervs"
5456
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcecontroller"
5557
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcemanager"
@@ -79,6 +81,7 @@ const (
7981
// vpcSubnetIPAddressCount is the total IP Addresses for the subnet.
8082
// Support for custom address prefixes will be added at a later time. Currently, we use the ip count for subnet creation.
8183
vpcSubnetIPAddressCount int64 = 256
84+
tagKey = "powervs.cluster.x-k8s.io-resource-owner:"
8285
)
8386

8487
// PowerVSClusterScopeParams defines the input parameters used to create a new PowerVSClusterScope.
@@ -101,6 +104,7 @@ type ClientFactory struct {
101104
TransitGatewayFactory func() (transitgateway.TransitGateway, error)
102105
ResourceControllerFactory func() (resourcecontroller.ResourceController, error)
103106
ResourceManagerFactory func() (resourcemanager.ResourceManager, error)
107+
GlobalTaggingFactory func() (globaltagging.GlobalTagging, error)
104108
}
105109

106110
// 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
@@ -254,6 +259,18 @@ func NewPowerVSClusterScope(params PowerVSClusterScopeParams) (*PowerVSClusterSc
254259
Authenticator: auth,
255260
}
256261

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+
257274
rmClient, err := params.getResourceManagerClient(rcManagerOptions)
258275
if err != nil {
259276
return nil, fmt.Errorf("failed to create resource manager client: %w", err)
@@ -269,6 +286,7 @@ func NewPowerVSClusterScope(params PowerVSClusterScopeParams) (*PowerVSClusterSc
269286
IBMPowerVSClient: powerVSClient,
270287
IBMVPCClient: vpcClient,
271288
TransitGatewayClient: tgClient,
289+
GlobalTaggingClient: globalTaggingClient,
272290
ResourceClient: resourceClient,
273291
ResourceManagerClient: rmClient,
274292
}
@@ -296,6 +314,18 @@ func (params PowerVSClusterScopeParams) getPowerVSClient(options powervs.Service
296314
return powervs.NewService(options)
297315
}
298316

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+
299329
func (params PowerVSClusterScopeParams) getVPCClient() (vpc.Vpc, error) {
300330
if params.Logger.V(DEBUGLEVEL).Enabled() {
301331
core.SetLoggingLevel(core.LevelDebug)
@@ -852,10 +882,13 @@ func (s *PowerVSClusterScope) createServiceInstance() (*resourcecontrollerv2.Res
852882
if zone == nil {
853883
return nil, fmt.Errorf("PowerVS zone is not set")
854884
}
885+
886+
tag := tagKey + s.Name()
855887
serviceInstance, _, err := s.ResourceClient.CreateResourceInstance(&resourcecontrollerv2.CreateResourceInstanceOptions{
856888
Name: s.GetServiceName(infrav1beta2.ResourceTypeServiceInstance),
857889
Target: zone,
858890
ResourceGroup: &resourceGroupID,
891+
Tags: append(make([]string, 0), tag),
859892
ResourcePlanID: ptr.To(resourcecontroller.PowerVSResourcePlanID),
860893
})
861894
if err != nil {
@@ -1072,6 +1105,24 @@ func (s *PowerVSClusterScope) createDHCPServer() (*string, error) {
10721105
return dhcpServer.ID, nil
10731106
}
10741107

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+
10751126
// ReconcileVPC reconciles VPC.
10761127
func (s *PowerVSClusterScope) ReconcileVPC() (bool, error) {
10771128
// if VPC server id is set means the VPC is already created
@@ -1172,6 +1223,11 @@ func (s *PowerVSClusterScope) createVPC() (*string, error) {
11721223
return nil, err
11731224
}
11741225

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+
11751231
// set security group for vpc
11761232
options := &vpcv1.CreateSecurityGroupRuleOptions{}
11771233
options.SetSecurityGroupID(*vpcDetails.DefaultSecurityGroup.ID)
@@ -1325,6 +1381,12 @@ func (s *PowerVSClusterScope) createVPCSubnet(subnet infrav1beta2.Subnet) (*stri
13251381
if subnetDetails == nil {
13261382
return nil, fmt.Errorf("create VPC subnet is nil")
13271383
}
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+
}
13281390
return subnetDetails.ID, nil
13291391
}
13301392

@@ -1542,7 +1604,12 @@ func (s *PowerVSClusterScope) createVPCSecurityGroup(spec infrav1beta2.VPCSecuri
15421604
if err != nil {
15431605
return nil, err
15441606
}
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+
}
15461613
return securityGroup.ID, nil
15471614
}
15481615

@@ -2002,6 +2069,12 @@ func (s *PowerVSClusterScope) createTransitGateway() error {
20022069
return err
20032070
}
20042071

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+
20052078
s.SetTransitGatewayStatus(tg.ID, ptr.To(true))
20062079

20072080
vpcCRN, err := s.fetchVPCCRN()
@@ -2217,6 +2290,12 @@ func (s *PowerVSClusterScope) createLoadBalancer(lb infrav1beta2.VPCLoadBalancer
22172290
if err != nil {
22182291
return nil, err
22192292
}
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+
22202299
lbState := infrav1beta2.VPCLoadBalancerState(*loadBalancer.ProvisioningStatus)
22212300
return &infrav1beta2.VPCLoadBalancerStatus{
22222301
ID: loadBalancer.ID,
@@ -2373,12 +2452,14 @@ func (s *PowerVSClusterScope) createCOSServiceInstance() (*resourcecontrollerv2.
23732452
return nil, fmt.Errorf("failed to fetch resource group ID for resource group %v, ID is empty", s.ResourceGroup())
23742453
}
23752454

2455+
tag := tagKey + s.Name()
23762456
target := "Global"
23772457
// create service instance
23782458
serviceInstance, _, err := s.ResourceClient.CreateResourceInstance(&resourcecontrollerv2.CreateResourceInstanceOptions{
23792459
Name: s.GetServiceName(infrav1beta2.ResourceTypeCOSInstance),
23802460
Target: &target,
23812461
ResourceGroup: &resourceGroupID,
2462+
Tags: append(make([]string, 0), tag),
23822463
ResourcePlanID: ptr.To(resourcecontroller.CosResourcePlanID),
23832464
})
23842465
if err != nil {

0 commit comments

Comments
 (0)