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

shared utility function to check if a cluster is workload identity #3683

Merged
merged 6 commits into from
Jul 23, 2024

Conversation

slawande2
Copy link
Collaborator

@slawande2 slawande2 commented Jul 11, 2024

Which issue this PR addresses:

ARO-7856

What this PR does / why we need it:

Throughout the RP codebase and installer wrapper, we are using if platformworkloadidentityprofile != nil or ServicePrincipalProfile to indicate whether or not a cluster is workload identity or not. This PR includes a shared utility function to check if a function is a Workload Identity or Service Principal cluster, which can be imported in all parts of the codebase.

Test plan for issue:

Passed all the unit test cases.

@mociarain
Copy link
Collaborator

/azp run ci

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

mociarain
mociarain previously approved these changes Jul 12, 2024
Copy link
Collaborator

@mociarain mociarain left a comment

Choose a reason for hiding this comment

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

Nice

pkg/util/iswimi/iswimi.go Outdated Show resolved Hide resolved
pkg/util/iswimi/iswimi.go Outdated Show resolved Hide resolved
Copy link
Contributor

@kimorris27 kimorris27 left a comment

Choose a reason for hiding this comment

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

LGTM broadly speaking! I second Ayato's suggestions, and I have one other small change request to make sure some terminology usage is clear.

pkg/util/iswimi/iswimi.go Outdated Show resolved Hide resolved
pkg/util/iswimi/iswimi_test.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@cadenmarchese cadenmarchese left a comment

Choose a reason for hiding this comment

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

great work here!

pkg/util/iswimi/iswimi.go Outdated Show resolved Hide resolved
pkg/util/iswimi/iswimi.go Outdated Show resolved Hide resolved
pkg/cluster/deploybaseresources.go Outdated Show resolved Hide resolved
pkg/frontend/openshiftcluster_putorpatch.go Outdated Show resolved Hide resolved
pkg/frontend/openshiftcluster_putorpatch.go Outdated Show resolved Hide resolved
pkg/operator/deploy/deploy.go Outdated Show resolved Hide resolved
pkg/operator/deploy/deploy.go Outdated Show resolved Hide resolved
pkg/util/wimi/iswimi.go Outdated Show resolved Hide resolved
pkg/util/wimi/iswimi_test.go Outdated Show resolved Hide resolved
pkg/cluster/delete.go Outdated Show resolved Hide resolved
Copy link
Contributor

@kimorris27 kimorris27 left a comment

Choose a reason for hiding this comment

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

Thanks for being flexible in the review process! LGTM as long as E2E passes.

@kimorris27
Copy link
Contributor

/azp run ci, e2e

Copy link

Azure Pipelines successfully started running 2 pipeline(s).

pkg/util/wimi/iswimi.go Outdated Show resolved Hide resolved
SudoBrendan
SudoBrendan previously approved these changes Jul 17, 2024
Copy link
Collaborator

@SudoBrendan SudoBrendan left a comment

Choose a reason for hiding this comment

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

LGTM - feel free to action or dismiss my nit.

Comment on lines 10 to 13
if oc.Properties.PlatformWorkloadIdentityProfile == nil || oc.Properties.ServicePrincipalProfile != nil {
return false
}
return true
Copy link
Collaborator

Choose a reason for hiding this comment

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

suuuuuper nit on style - please feel free to tell me to go away :) - if you discover you are using an if block to both return true and return false, you can return the boolean expression of the if itself.

For example, this if block and two returns is equivalent to:

return !(oc.Properties.PlatformWorkloadIdentityProfile == nil || oc.Properties.ServicePrincipalProfile != nil)

@SudoBrendan
Copy link
Collaborator

/azp run e2e

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@kimorris27
Copy link
Contributor

/azp run e2e

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

kimorris27
kimorris27 previously approved these changes Jul 18, 2024
Copy link
Contributor

@kimorris27 kimorris27 left a comment

Choose a reason for hiding this comment

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

Nice work here.

I hadn't thought about it during my own reviews, but I think Ayato's suggestion here makes sense if you feel inclined to make it before we merge. I don't consider it blocking though.

@slawande2 slawande2 dismissed stale reviews from kimorris27 and SudoBrendan via d73aa38 July 18, 2024 20:11
@kimorris27
Copy link
Contributor

/azp run ci, e2e

Copy link

Azure Pipelines successfully started running 2 pipeline(s).

"github.com/Azure/ARO-RP/pkg/api"
)

func TestIswimi(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suuuper NIT:- Since the function is named isWimi, this can be changed to TestIsWimi.
Please feel free to dismiss it or handle it accordingly. ```suggestion
func TestIsWimi(t *testing.T) {

Copy link

@AldoFusterTurpin AldoFusterTurpin left a comment

Choose a reason for hiding this comment

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

@slawande2 Thank you for this. LGTM! I just left an optional suggestion about moving the business logic closer to the struct where the data lives in to avoid having an anemic domain. No blocker at all, just a FYI 🙂

@@ -443,7 +444,7 @@ func (m *manager) Delete(ctx context.Context) error {
return err
}

if m.doc.OpenShiftCluster.Properties.ServicePrincipalProfile == nil && m.doc.OpenShiftCluster.Properties.PlatformWorkloadIdentityProfile != nil {
if utilwimi.IsWimi(m.doc.OpenShiftCluster) {
Copy link

@AldoFusterTurpin AldoFusterTurpin Jul 23, 2024

Choose a reason for hiding this comment

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

Just a small comment: If we move the definition of the IsWimi function to the OpenShift cluster, we obtain a richer domain where functions are closer where the data is used (to avoid having an anemic Domain Model, a concept of DDD that here could be interesting).
If we do that (check next comments for details) this line would look like this:

if m.doc.OpenShiftCluster.IsWimi() {

which is pretty neat as clearly states that the fact that a cluster is Wimi or not just depend on the cluster and reads a bit better 🙂

@@ -39,7 +40,7 @@ func (m *manager) createDNS(ctx context.Context) error {
}

func (m *manager) createOIDC(ctx context.Context) error {
if m.doc.OpenShiftCluster.Properties.ServicePrincipalProfile != nil || m.doc.OpenShiftCluster.Properties.PlatformWorkloadIdentityProfile == nil {
if !utilwimi.IsWimi(m.doc.OpenShiftCluster) {

Choose a reason for hiding this comment

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

Same as before.

@@ -135,8 +136,7 @@ func (f *frontend) _putOrPatchOpenShiftCluster(ctx context.Context, log *logrus.
if isCreate {
// Persist identity URL and tenant ID only for managed/workload identity cluster create
// We don't support updating cluster managed identity after cluster creation
// TODO - use a common function to check if the cluster is a managed/workload identity cluster
if !(doc.OpenShiftCluster.Properties.ServicePrincipalProfile != nil || doc.OpenShiftCluster.Identity == nil) {
if utilwimi.IsWimi(doc.OpenShiftCluster) {

Choose a reason for hiding this comment

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

Same as before.

@@ -468,8 +469,7 @@ func (o *operator) RenewMDSDCertificate(ctx context.Context) error {
}

func (o *operator) EnsureUpgradeAnnotation(ctx context.Context) error {
if o.oc.Properties.PlatformWorkloadIdentityProfile == nil ||
o.oc.Properties.ServicePrincipalProfile != nil {
if !utilwimi.IsWimi(o.oc) {

Choose a reason for hiding this comment

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

Same as before.

Comment on lines +8 to +14
// IsWimi checks whether a cluster is a Workload Identity cluster or Service Principal cluster
func IsWimi(oc *api.OpenShiftCluster) bool {
if oc.Properties.PlatformWorkloadIdentityProfile != nil && oc.Properties.ServicePrincipalProfile == nil {
return true
}
return false
}

Choose a reason for hiding this comment

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

Following my first comment in the PR, if we want to have a richer domain and avoid an anemic domain model, we can move this function definition to pkg/api/openshiftcluster.go and have on line 29 something like:

// IsWimi checks whether a cluster is a Workload Identity cluster or Service Principal cluster
func (oc *OpenShiftCluster) IsWimi() bool {
	if oc.Properties.PlatformWorkloadIdentityProfile != nil && oc.Properties.ServicePrincipalProfile == nil {
		return true
	}
	return false
}

which moves the business logic closer to where the data lives in 🙂

Choose a reason for hiding this comment

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

One last minor comment about it, we can simplify the function if we change it to one single return where the true condition should be met

// IsWimi checks whether a cluster is a Workload Identity cluster or Service Principal cluster
func (oc *OpenShiftCluster) IsWimi() bool {
	return oc.Properties.PlatformWorkloadIdentityProfile != nil && oc.Properties.ServicePrincipalProfile == nil
}

Just mentioning it in case you find it easier to read.

Choose a reason for hiding this comment

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

Nice to see some unit tests 🙂

@cadenmarchese
Copy link
Collaborator

@AldoFusterTurpin thanks a lot for the suggestion! We are going to implement it in a new PR.

@cadenmarchese cadenmarchese merged commit 53691de into master Jul 23, 2024
21 checks passed
@SudoBrendan SudoBrendan deleted the slawande/iswimi branch July 24, 2024 15:51
@AldoFusterTurpin
Copy link

AldoFusterTurpin commented Jul 25, 2024

@AldoFusterTurpin thanks a lot for the suggestion! We are going to implement it in a new PR.

Great! I see that PR is now open: #3721

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chainsaw Pull requests or issues owned by Team Chainsaw
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants