-
Notifications
You must be signed in to change notification settings - Fork 170
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
Changes from all commits
e794516
03f22f0
1c37593
5c31409
a4c6582
d73aa38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import ( | |
"github.com/Azure/ARO-RP/pkg/util/oidcbuilder" | ||
"github.com/Azure/ARO-RP/pkg/util/pointerutils" | ||
"github.com/Azure/ARO-RP/pkg/util/stringutils" | ||
utilwimi "github.com/Azure/ARO-RP/pkg/util/wimi" | ||
) | ||
|
||
var nsgNotReadyErrorRegex = regexp.MustCompile("Resource.*networkSecurityGroups.*referenced by resource.*not found") | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as before. |
||
return nil | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
"github.com/Azure/ARO-RP/pkg/frontend/middleware" | ||
"github.com/Azure/ARO-RP/pkg/operator" | ||
"github.com/Azure/ARO-RP/pkg/util/version" | ||
utilwimi "github.com/Azure/ARO-RP/pkg/util/wimi" | ||
) | ||
|
||
var errMissingIdentityParameter error = fmt.Errorf("identity parameter not provided but required for workload identity cluster") | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as before. |
||
if err := validateIdentityUrl(doc.OpenShiftCluster, putOrPatchClusterParameters.identityURL); err != nil { | ||
return nil, err | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ import ( | |
"github.com/Azure/ARO-RP/pkg/util/pullsecret" | ||
"github.com/Azure/ARO-RP/pkg/util/ready" | ||
"github.com/Azure/ARO-RP/pkg/util/restconfig" | ||
utilwimi "github.com/Azure/ARO-RP/pkg/util/wimi" | ||
) | ||
|
||
//go:embed staticresources | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as before. |
||
return nil | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package wimi | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
import "github.com/Azure/ARO-RP/pkg/api" | ||
|
||
// 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 | ||
} | ||
Comment on lines
+8
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 🙂 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice to see some unit tests 🙂 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package wimi | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/Azure/ARO-RP/pkg/api" | ||
) | ||
|
||
func TestIswimi(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suuuper NIT:- Since the function is named
|
||
tests := []*struct { | ||
name string | ||
oc api.OpenShiftCluster | ||
want bool | ||
}{ | ||
{ | ||
name: "Cluster is Workload Identity", | ||
oc: api.OpenShiftCluster{ | ||
Properties: api.OpenShiftClusterProperties{ | ||
PlatformWorkloadIdentityProfile: &api.PlatformWorkloadIdentityProfile{}, | ||
ServicePrincipalProfile: nil, | ||
}, | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "Cluster is Service Principal", | ||
oc: api.OpenShiftCluster{ | ||
Properties: api.OpenShiftClusterProperties{ | ||
PlatformWorkloadIdentityProfile: nil, | ||
ServicePrincipalProfile: &api.ServicePrincipalProfile{}, | ||
}, | ||
}, | ||
want: false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
got := IsWimi(&test.oc) | ||
if got != test.want { | ||
t.Error(fmt.Errorf("got != want: %v != %v", got, test.want)) | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
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:
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 🙂