Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attach connections to existing transit gateway when its not there #1901

Merged
merged 1 commit into from
Sep 12, 2024

Conversation

dharaneeshvrd
Copy link
Contributor

What this PR does / why we need it:

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #1887

Special notes for your reviewer:

/area provider/ibmcloud

  1. Please confirm that if this PR changes any image versions, then that's the sole change this PR makes.

Release note:


@k8s-ci-robot k8s-ci-robot added area/provider/ibmcloud Issues or PRs related to ibmcloud provider cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 1, 2024
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 1, 2024
Copy link

netlify bot commented Aug 1, 2024

Deploy Preview for kubernetes-sigs-cluster-api-ibmcloud ready!

Name Link
🔨 Latest commit b24fe96
🔍 Latest deploy log https://app.netlify.com/sites/kubernetes-sigs-cluster-api-ibmcloud/deploys/66e2bb972c09f20007630d68
😎 Deploy Preview https://deploy-preview-1901--kubernetes-sigs-cluster-api-ibmcloud.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@dharaneeshvrd
Copy link
Contributor Author

/cc @Karthik-K-N

Copy link
Contributor

@Karthik-K-N Karthik-K-N left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few doubts, will take a look again

@@ -394,6 +394,35 @@ func (s *PowerVSClusterScope) GetServiceInstanceID() string {
return ""
}

func (s *PowerVSClusterScope) SetTransitGatewayStatus(id *string, controllerCreated *bool, powerVSConnResource *infrav1beta2.ResourceReference, vpcConnResource *infrav1beta2.ResourceReference) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (s *PowerVSClusterScope) SetTransitGatewayStatus(id *string, controllerCreated *bool, powerVSConnResource *infrav1beta2.ResourceReference, vpcConnResource *infrav1beta2.ResourceReference) {
func (s *PowerVSClusterScope) SetTransitGatewayStatus(id *string, controllerCreated *bool, powerVSConnResource , vpcConnResource *infrav1beta2.ResourceReference) {


s.IBMPowerVSCluster.Status.TransitGateway = &infrav1beta2.TransitGatewayStatus{
ID: id,
ControllerCreated: controllerCreated,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't it set ControllerCreated to false during second reconcilation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second time, we won't call the SetTransitgatewayStatus(). We would check status of TG and connections with the ID set in status and return.

func (s *PowerVSClusterScope) checkTransitGatewayConnections(id *string) (bool, error) {
requeue := false
// checkAndUpdateTransitGatewayConnections checks given transit gateway's connections status.
// // if update is set to true, it updates the transit gateway connections too if it is not exist already.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// // if update is set to true, it updates the transit gateway connections too if it is not exist already.
// if update is set to true, it updates the transit gateway connections too if it is not exist already.

Copy link
Contributor

@Karthik-K-N Karthik-K-N left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few questions. otherwise overall LGTM,

if powerVSAttached && vpcAttached {
if s.IBMPowerVSCluster.Status.TransitGateway == nil {
vpcConnResource = &infrav1beta2.ResourceReference{
ControllerCreated: ptr.To(false),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We intentionally not storing connections id here is status right? also if we dont set anything the default value for controller created is also false, but I think explicitly setting will enhance readability of code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes Agree, refactored with returning the ID and checking the attached status based on that. Ptal!

@dharaneeshvrd dharaneeshvrd force-pushed the tg-connections branch 3 times, most recently from 54b7576 to a261261 Compare August 5, 2024 06:13
Copy link
Contributor

@Amulyam24 Amulyam24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial comments on the create TG flow, will be taking a closer look

}
s.V(3).Info("VPC connection successfully attached to transit gateway", "name", *conn.Name)
vpcAttached = true
s.V(3).Info("Transit gateway VPC connection OK", "name", *conn.Name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a better logging statement?

@@ -1627,71 +1651,66 @@ func (s *PowerVSClusterScope) ReconcileTransitGateway() (bool, error) {
if err != nil {
return false, err
}
requeue, err := s.checkTransitGateway(tg.ID)
requeue, _, _, err := s.checkAndUpdateTransitGateway(tg, false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a case where TG pre-exists and no connections exist, if we pass false here, it won't create any connections right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified the logic to handle the existing resource passed via id in second block and taken care there to update the status. Ptal!

@dharaneeshvrd dharaneeshvrd force-pushed the tg-connections branch 3 times, most recently from 6229226 to 29d57ee Compare August 6, 2024 04:25
@mkumatag mkumatag added this to the v0.9.0 milestone Aug 6, 2024
if err != nil {
return "", false, err
return "", false, nil, nil, err
}
if transitGateway == nil || transitGateway.ID == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I dont remember, Do you know when can transitGateway can be nil, If we try to query a TG with invalid id or name which does not exist, It will throw an error right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, will remove this.

if err != nil {
return false, err
}
if tgID != "" {
s.V(3).Info("Transit gateway found in IBM Cloud")
s.SetStatus(infrav1beta2.ResourceTypeTransitGateway, infrav1beta2.ResourceReference{ID: &tgID, ControllerCreated: ptr.To(false)})
s.SetTransitGatewayStatus(&tgID, ptr.To(false), powerVSConn, vpcConn)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My another query was wont this set controllerCreated to false, In the second loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't come to this block on second reconciliation since status would have already generated and try to validate the resource's status in 1st block.

}
return *transitGateway.ID, requeue, nil
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a case where spec.TG.ID is nil and given TG name doesn't exist in cloud, GetTransitGatewayByName, returns nil. Without a nil check here, it would fail going further at checkTransitGatewayStatus where *tg.Status would be nil right? @dharaneeshvrd, Please correct me if my understanding is not right..

s.Info("Skipping transit gateway deletion as resource is not created by controller")
return false, nil
s.Info("Skipping transit gateway deletion as resource is not created by controller, but will check if connections are created by the controller.")
skipTGDeletion = true
}

if s.IBMPowerVSCluster.Status.TransitGateway.ID == nil {
Copy link
Contributor

@Amulyam24 Amulyam24 Sep 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dharaneesh, while testing, came across a scenario where for any reason if TG reconcilation fails and it doesn't set the s.IBMPowerVSCluster.Status.TransitGateway, it will return a nil pointer error during deletion as ID will be nil. Though the value of s.IBMPowerVSCluster.Status.TransitGateway is being checked during isResourceCreatedByController(infrav1beta2.ResourceTypeTransitGateway), we don't return after it to allow deletion of TG connections separately.

Copy link
Contributor

@Amulyam24 Amulyam24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM!

@dharaneeshvrd, hope you have tested all possible scenarios.

Copy link
Contributor

@Karthik-K-N Karthik-K-N left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 9, 2024
@hamzy
Copy link

hamzy commented Sep 11, 2024

When will this PR merge?

Name: connName,
})

return conn.ID, err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens when there is an error? don't we face a nil pointer dereference issue because conn is nil when err occurs.


// createTransitGatewayConnections creates PowerVS and VPC connections in the transit gateway.
func (s *PowerVSClusterScope) createTransitGatewayConnections(tg *tgapiv1.TransitGateway, pvsServiceInstanceCRN, vpcCRN *string) (*string, *string, error) {
powerVSConnID, err := s.createTransitGatewayConnection(tg.ID, ptr.To(fmt.Sprintf("%s-pvs-con", *tg.Name)), ptr.To(string(powervsNetworkConnectionType)), pvsServiceInstanceCRN)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this %s-pvs-con format can be placed somewhere so that we can reuse and easy to handle as well.

return nil, nil, fmt.Errorf("failed to create PowerVS connection in transit gateway: %w", err)
}

vpcConnID, err := s.createTransitGatewayConnection(tg.ID, ptr.To(fmt.Sprintf("%s-vpc-con", *tg.Name)), ptr.To(string(vpcNetworkConnectionType)), vpcCRN)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

}
if transitGateway == nil || transitGateway.ID == nil {

if transitGateway == nil || (transitGateway != nil && transitGateway.ID == nil) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if transitGateway == nil || (transitGateway != nil && transitGateway.ID == nil) {
if transitGateway == nil || transitGateway.ID == nil {

// checkTransitGateway checks transit gateway exist in cloud.
func (s *PowerVSClusterScope) isTransitGatewayExists() (string, bool, error) {
// isTransitGatewayExists checks transit gateway exist in cloud and if exist with unattached connections, it attaches the connection too.
func (s *PowerVSClusterScope) isTransitGatewayExists() (string, bool, *infrav1beta2.ResourceReference, *infrav1beta2.ResourceReference, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function name misleads as you are doing attach operation as well in this function.

@mkumatag
Copy link
Member

When will this PR merge?

soon, wondering if can test these changes with OCP directly pulling from the @dharaneeshvrd 's branch

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 12, 2024
Copy link
Member

@mkumatag mkumatag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 12, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dharaneeshvrd, mkumatag

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 12, 2024
@k8s-ci-robot k8s-ci-robot merged commit 632a722 into kubernetes-sigs:main Sep 12, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/provider/ibmcloud Issues or PRs related to ibmcloud provider cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Plan to handle exsiting TransitGateway and its connections
6 participants