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

✨ Made changes for clusteradm accept to create role and policies on hub #819

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

alex0chan
Copy link

@alex0chan alex0chan commented Jan 31, 2025

Summary

This PR has the enhancement for clusteradm accept to create roles and policies on the hub

Related issue(s)

Ref: #514

Copy link
Contributor

openshift-ci bot commented Jan 31, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: alex0chan
Once this PR has been reviewed and has the lgtm label, please assign qiujian16 for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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

"Effect": "Allow",
"Action": [
"eks:DescribeCluster",
"eks:ListClusters"
Copy link
Member

Choose a reason for hiding this comment

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

I think these permissions are not needed as we are copying cert authority data from bootstrap kubeconfig.

Copy link
Contributor

Choose a reason for hiding this comment

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

Shall we not have any permissions policy attached then?
Theoretically seems fine but have not tested this yet.

Copy link
Member

Choose a reason for hiding this comment

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

If we have a similar method used by kluster-agent, can we reuse it?

Copy link
Contributor

Choose a reason for hiding this comment

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

We don't have this method anywhere else for the changes in this branch. If there will be more declarations coming up as a part of other PRs, We can con reuse there.

@@ -203,6 +210,21 @@ func (c *managedClusterController) sync(ctx context.Context, syncCtx factory.Syn
}
}

// Only create new IAM roles when status is not present
if !meta.IsStatusConditionTrue(managedCluster.Status.Conditions, v1.ManagedClusterConditionHubAccepted) {
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to remove this if condition and overwrite role, policy and access entry on every reconciliation? This would also ensure that the IAM resources are repaired if somebody accidentally or intentionally changes them manually.

Also suggesting to overwrite directly instead of making a get call first and them comparing, to save an api call.

Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to be a valid comment and we can take this in an upcoming change.
We will have to add some logic to check role, trust policy and permissions policy and if they have been modified or not. It might be a little bit more complicated as compared to checking kubernetes Roles and Rolebindings created because we can simply be reapply the yaml on them where as over here we might have to check the resources using sdk.

Copy link
Member

Choose a reason for hiding this comment

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

Is the intention to only create permission once? It might not work since the condition patch might fail, and the is called again. Can we ensure that the createPermission can be called multiple times when the accept=true which is better to tolerate error.

Copy link
Member

@mikeshng mikeshng left a comment

Choose a reason for hiding this comment

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

Could you check and maybe address some of the workflow errors if they are valid? Thanks.

@jaswalkiranavtar
Copy link
Member

Could you check and maybe address some of the workflow errors if they are valid? Thanks.

Yes I guess this is still WIP, we created PR to get early feedback.

@@ -203,6 +210,21 @@ func (c *managedClusterController) sync(ctx context.Context, syncCtx factory.Syn
}
}

// Only create new IAM roles when status is not present
if !meta.IsStatusConditionTrue(managedCluster.Status.Conditions, v1.ManagedClusterConditionHubAccepted) {
clusterManager, err := c.operatorClient.OperatorV1().ClusterManagers().Get(context.TODO(), "cluster-manager", metav1.GetOptions{})
Copy link
Member

@jaswalkiranavtar jaswalkiranavtar Feb 4, 2025

Choose a reason for hiding this comment

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

Instead of making this API server call and adding another permission to clusterrole, can we instead pass this info from clustermanager CR to registration-controller on hub through these command line options similar to this?

@suvaanshkumar suvaanshkumar force-pushed the GWCP-65858-clusteradm-accept_bkp branch from 63faa0b to 76289f7 Compare February 5, 2025 03:38
@qiujian16
Copy link
Member

I am wondering whether we need to import aws sdk here, or we just ask user to install aws cli as the prereq?

@jaswalkiranavtar
Copy link
Member

I am wondering whether we need to import aws sdk here, or we just ask user to install aws cli as the prereq?

Yes we need to use aws sdk here to create IAM roles and policies when hub admin approves registration as part of registration controller on hub. Preferring sdk over cli as with cli, we would have to run shell commands from within go and handling errors would be complicated.

@suvaanshkumar suvaanshkumar force-pushed the GWCP-65858-clusteradm-accept_bkp branch 2 times, most recently from 9eda489 to 354054e Compare February 11, 2025 21:42
@jaswalkiranavtar jaswalkiranavtar force-pushed the GWCP-65858-clusteradm-accept_bkp branch from a6c455a to 3ae3841 Compare February 13, 2025 21:46
@jaswalkiranavtar jaswalkiranavtar force-pushed the GWCP-65858-clusteradm-accept_bkp branch from 3ae3841 to 6aa4d68 Compare February 13, 2025 21:47
Copy link

codecov bot commented Feb 13, 2025

Codecov Report

Attention: Patch coverage is 56.68016% with 107 lines in your changes missing coverage. Please review.

Project coverage is 63.74%. Comparing base (b6c2a84) to head (78d4bcd).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
pkg/registration/register/aws_irsa/aws_irsa.go 65.62% 58 Missing and 8 partials ⚠️
pkg/registration/hub/managedcluster/controller.go 12.50% 12 Missing and 2 partials ⚠️
pkg/registration/register/common.go 0.00% 11 Missing ⚠️
pkg/registration/register/csr/csr.go 0.00% 6 Missing ⚠️
...agercontroller/clustermanager_runtime_reconcile.go 0.00% 4 Missing and 1 partial ⚠️
pkg/common/helpers/helpers.go 0.00% 3 Missing ⚠️
pkg/registration/hub/manager.go 75.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #819      +/-   ##
==========================================
- Coverage   63.82%   63.74%   -0.09%     
==========================================
  Files         193      194       +1     
  Lines       18667    18897     +230     
==========================================
+ Hits        11914    12045     +131     
- Misses       5772     5859      +87     
- Partials      981      993      +12     
Flag Coverage Δ
unit 63.74% <56.68%> (-0.09%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Gaurav Jaswal <[email protected]>
// Only create new IAM roles when status is not present
if !meta.IsStatusConditionTrue(managedCluster.Status.Conditions, v1.ManagedClusterConditionHubAccepted) {
if err != nil {
log.Printf("Failed to get cluster manager %v", err)
Copy link
Member

Choose a reason for hiding this comment

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

we do not use log in controller. You have to use klog.FromContext to get the logger and set the correct log level. Actually, since it returns error, you might not need to output log.

@@ -87,3 +87,9 @@ type Approver interface {
// deletes rolebindings for the agent, and then this is the additional operation a driver should process.
Cleanup(ctx context.Context, cluster *clusterv1.ManagedCluster) error
}

type RegisterDriverForHub interface {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe only HubDriver, and also add some comment for this interface. And why it is not in the approver interface?


err = c.registerDriverForHub.CreatePermissions(ctx, managedCluster)
if err != nil {
log.Printf("Failed to create permissions %v", err)
Copy link
Member

Choose a reason for hiding this comment

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

ditto, do not use log here.

csrApprover, err := csr.NewCSRApprover(kubeClient, kubeInformers, m.ClusterAutoApprovalUsers, controllerContext.EventRecorder)
if err != nil {
return err
}

approver := register.NewAggregatedApprover(csrApprover)

awsIRSADriverForHub := awsirsa.NewAWSIRSADriverForHub(m.HubClusterArn)
csrDriverForHub := csr.NewCSRDriverForHub()
registerDriverForHub := register.NewAggregatedDriverForHub(csrDriverForHub, awsIRSADriverForHub)
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't we start this based on the flags of which drivers should be enabled?

// This function creates:
// 1. IAM Roles and Policies in the hub cluster IAM
// 2. Returns the hubclustername and the roleArn to be used for Access Entry creation
func CreateIAMRoleAndPolicy(ctx context.Context, hubClusterArn string, managedCluster *v1.ManagedCluster, iamClient *iam.Client) (string, string, error) {
Copy link
Member

Choose a reason for hiding this comment

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

why it is public? do you need to call it outside of this package?

return nil
}
//Creating config for aws
cfg, err := config.LoadDefaultConfig(context.TODO())
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to load the config everytime this func is called, or you can init the config when the driver is newed?

}
}
]
}
Copy link
Member

Choose a reason for hiding this comment

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

eof

@@ -203,6 +210,21 @@ func (c *managedClusterController) sync(ctx context.Context, syncCtx factory.Syn
}
}

// Only create new IAM roles when status is not present
if !meta.IsStatusConditionTrue(managedCluster.Status.Conditions, v1.ManagedClusterConditionHubAccepted) {
Copy link
Member

Choose a reason for hiding this comment

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

Is the intention to only create permission once? It might not work since the condition patch might fail, and the is called again. Can we ensure that the createPermission can be called multiple times when the accept=true which is better to tolerate error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants