move iswimi function to openshiftcluster.go

This commit is contained in:
Sanjana Lawande 2024-07-24 11:54:39 -07:00
Родитель 0684a5af89
Коммит 2d97fea485
8 изменённых файлов: 51 добавлений и 72 удалений

Просмотреть файл

@ -27,6 +27,11 @@ type OpenShiftCluster struct {
Lock sync.Mutex `json:"-"`
}
// 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
}
// CreatedByType by defines user type, which executed the request
// This field should match common-types field names for swagger and sdk generation
type CreatedByType string

Просмотреть файл

@ -3,7 +3,10 @@ package api
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import "testing"
import (
"fmt"
"testing"
)
func TestIsTerminal(t *testing.T) {
for _, tt := range []struct {
@ -49,3 +52,41 @@ func TestIsTerminal(t *testing.T) {
})
}
}
func TestIsWimi(t *testing.T) {
tests := []*struct {
name string
oc OpenShiftCluster
want bool
}{
{
name: "Cluster is Workload Identity",
oc: OpenShiftCluster{
Properties: OpenShiftClusterProperties{
PlatformWorkloadIdentityProfile: &PlatformWorkloadIdentityProfile{},
ServicePrincipalProfile: nil,
},
},
want: true,
},
{
name: "Cluster is Service Principal",
oc: OpenShiftCluster{
Properties: OpenShiftClusterProperties{
PlatformWorkloadIdentityProfile: nil,
ServicePrincipalProfile: &ServicePrincipalProfile{},
},
},
want: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := test.oc.IsWimi()
if got != test.want {
t.Error(fmt.Errorf("got != want: %v != %v", got, test.want))
}
})
}
}

Просмотреть файл

@ -30,7 +30,6 @@ import (
"github.com/Azure/ARO-RP/pkg/util/oidcbuilder"
"github.com/Azure/ARO-RP/pkg/util/rbac"
"github.com/Azure/ARO-RP/pkg/util/stringutils"
utilwimi "github.com/Azure/ARO-RP/pkg/util/wimi"
)
// deleteNic deletes the network interface resource by first fetching the resource using the interface
@ -424,7 +423,7 @@ func (m *manager) Delete(ctx context.Context) error {
return err
}
if utilwimi.IsWimi(m.doc.OpenShiftCluster) {
if m.doc.OpenShiftCluster.IsWimi() {
m.log.Printf("deleting OIDC configuration")
blobContainerURL := oidcbuilder.GenerateBlobContainerURL(m.env)
azBlobClient, err := m.rpBlob.GetAZBlobClient(blobContainerURL, &azblob.ClientOptions{})

Просмотреть файл

@ -28,7 +28,6 @@ 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")
@ -40,7 +39,7 @@ func (m *manager) createDNS(ctx context.Context) error {
}
func (m *manager) createOIDC(ctx context.Context) error {
if !utilwimi.IsWimi(m.doc.OpenShiftCluster) {
if !m.doc.OpenShiftCluster.IsWimi() {
return nil
}

Просмотреть файл

@ -23,7 +23,6 @@ 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")
@ -141,7 +140,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
if utilwimi.IsWimi(doc.OpenShiftCluster) {
if doc.OpenShiftCluster.IsWimi() {
if err := validateIdentityUrl(doc.OpenShiftCluster, putOrPatchClusterParameters.identityURL); err != nil {
return nil, err
}

Просмотреть файл

@ -45,7 +45,6 @@ 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
@ -469,7 +468,7 @@ func (o *operator) RenewMDSDCertificate(ctx context.Context) error {
}
func (o *operator) EnsureUpgradeAnnotation(ctx context.Context) error {
if !utilwimi.IsWimi(o.oc) {
if !o.oc.IsWimi() {
return nil
}

Просмотреть файл

@ -1,14 +0,0 @@
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
}

Просмотреть файл

@ -1,49 +0,0 @@
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) {
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))
}
})
}
}