-
Notifications
You must be signed in to change notification settings - Fork 191
Operator: Expose Cluster SP as Condition #1230
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package checker | ||
|
|
||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the Apache License 2.0. | ||
|
|
||
| const ( | ||
| azureCredentialSecretNamespace = "kube-system" | ||
| azureCredentialSecretName = "azure-credentials" | ||
| machineSetsNamespace = "openshift-machine-api" | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
pkg/operator/controllers/checker/serviceprincipalchecker.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package checker | ||
|
|
||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the Apache License 2.0. | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/Azure/go-autorest/autorest/azure" | ||
| maoclient "github.com/openshift/machine-api-operator/pkg/generated/clientset/versioned" | ||
| "github.com/operator-framework/operator-sdk/pkg/status" | ||
| "github.com/sirupsen/logrus" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/kubernetes" | ||
|
|
||
| "github.com/Azure/ARO-RP/pkg/api" | ||
| "github.com/Azure/ARO-RP/pkg/api/validate/dynamic" | ||
| arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" | ||
| aroclient "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned" | ||
| "github.com/Azure/ARO-RP/pkg/operator/controllers" | ||
| "github.com/Azure/ARO-RP/pkg/util/aad" | ||
| ) | ||
|
|
||
| type ServicePrincipalChecker struct { | ||
| log *logrus.Entry | ||
| clustercli maoclient.Interface | ||
| arocli aroclient.Interface | ||
| kubernetescli kubernetes.Interface | ||
| role string | ||
| } | ||
|
|
||
| func NewServicePrincipalChecker(log *logrus.Entry, maocli maoclient.Interface, arocli aroclient.Interface, kubernetescli kubernetes.Interface, role string) *ServicePrincipalChecker { | ||
| return &ServicePrincipalChecker{ | ||
| log: log, | ||
| clustercli: maocli, | ||
| arocli: arocli, | ||
| kubernetescli: kubernetescli, | ||
| role: role, | ||
| } | ||
| } | ||
|
|
||
| func (r *ServicePrincipalChecker) Name() string { | ||
| return "ServicePrincipalChecker" | ||
| } | ||
|
|
||
| func (r *ServicePrincipalChecker) Check(ctx context.Context) error { | ||
| cond := &status.Condition{ | ||
| Type: arov1alpha1.ServicePrincipalValid, | ||
| Status: corev1.ConditionTrue, | ||
| Message: "service principal is valid", | ||
| Reason: "CheckDone", | ||
| } | ||
|
|
||
| cluster, err := r.arocli.AroV1alpha1().Clusters().Get(ctx, arov1alpha1.SingletonClusterName, metav1.GetOptions{}) | ||
mjudeikis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| resource, err := azure.ParseResourceID(cluster.Spec.ResourceID) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| azEnv, err := azure.EnvironmentFromName(cluster.Spec.AZEnvironment) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| azCred, err := azCredentials(ctx, r.kubernetescli) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| _, err = aad.GetToken(ctx, r.log, azCred.clientID, azCred.clientSecret, azCred.tenantID, azEnv.ActiveDirectoryEndpoint, azEnv.ResourceManagerEndpoint) | ||
| if err != nil { | ||
| updateFailedCondition(cond, err) | ||
| } | ||
|
|
||
| spDynamic, err := dynamic.NewValidator(r.log, &azEnv, resource.SubscriptionID, nil, dynamic.AuthorizerClusterServicePrincipal) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = spDynamic.ValidateServicePrincipal(ctx, azCred.clientID, azCred.clientSecret, azCred.tenantID) | ||
| if err != nil { | ||
| updateFailedCondition(cond, err) | ||
| } | ||
|
|
||
| return controllers.SetCondition(ctx, r.arocli, cond, r.role) | ||
| } | ||
|
|
||
| type credentials struct { | ||
| clientID string | ||
| clientSecret string | ||
| tenantID string | ||
| } | ||
|
|
||
| func azCredentials(ctx context.Context, kubernetescli kubernetes.Interface) (*credentials, error) { | ||
| var creds credentials | ||
|
|
||
| mysec, err := kubernetescli.CoreV1().Secrets(azureCredentialSecretNamespace).Get(ctx, azureCredentialSecretName, metav1.GetOptions{}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| creds.clientID = string(mysec.Data["azure_client_id"]) | ||
| creds.clientSecret = string(mysec.Data["azure_client_secret"]) | ||
| creds.tenantID = string(mysec.Data["azure_tenant_id"]) | ||
|
|
||
| return &creds, nil | ||
| } | ||
|
|
||
| func updateFailedCondition(cond *status.Condition, err error) { | ||
| cond.Status = corev1.ConditionFalse | ||
| if tErr, ok := err.(*api.CloudError); ok { | ||
| cond.Message = tErr.Message | ||
| } else { | ||
| cond.Message = err.Error() | ||
| } | ||
| } | ||
93 changes: 93 additions & 0 deletions
93
pkg/operator/controllers/checker/serviceprincipalchecker_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package checker | ||
|
|
||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the Apache License 2.0. | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| azuretypes "github.com/openshift/installer/pkg/types/azure" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/kubernetes/fake" | ||
|
|
||
| arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" | ||
| arofake "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned/fake" | ||
| ) | ||
|
|
||
| // TODO - once aad.GetToken is mockable add tests for other cases | ||
| func TestServicePrincipalValid(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| for _, tt := range []struct { | ||
| name string | ||
| aroCluster *arov1alpha1.Cluster | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "fail: aro cluster resource doesn't exist", | ||
| wantErr: `clusters.aro.openshift.io "cluster" not found`, | ||
| }, | ||
| { | ||
| name: "fail: invalid cluster resource", | ||
| aroCluster: &arov1alpha1.Cluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: arov1alpha1.SingletonClusterName, | ||
| }, | ||
| Spec: arov1alpha1.ClusterSpec{ | ||
| AZEnvironment: azuretypes.PublicCloud.Name(), | ||
| ResourceID: "invalid_resource", | ||
| }, | ||
| }, | ||
| wantErr: `parsing failed for invalid_resource. Invalid resource Id format`, | ||
| }, | ||
| { | ||
| name: "fail: invalid az environment", | ||
| aroCluster: &arov1alpha1.Cluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: arov1alpha1.SingletonClusterName, | ||
| }, | ||
| Spec: arov1alpha1.ClusterSpec{ | ||
| AZEnvironment: "NEVERLAND", | ||
| ResourceID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.RedHatOpenShift/openShiftClusters/mycluster", | ||
| }, | ||
| }, | ||
| wantErr: `autorest/azure: There is no cloud environment matching the name "NEVERLAND"`, | ||
| }, | ||
| { | ||
| name: "fail: azure-credential secret doesn't exist", | ||
| aroCluster: &arov1alpha1.Cluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: arov1alpha1.SingletonClusterName, | ||
| }, | ||
| Spec: arov1alpha1.ClusterSpec{ | ||
| AZEnvironment: azuretypes.PublicCloud.Name(), | ||
| ResourceID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.RedHatOpenShift/openShiftClusters/mycluster", | ||
| }, | ||
| }, | ||
| wantErr: `secrets "azure-credentials" not found`, | ||
| }, | ||
| } { | ||
| arocli := arofake.NewSimpleClientset() | ||
| kubernetescli := fake.NewSimpleClientset() | ||
|
|
||
| if tt.aroCluster != nil { | ||
| arocli = arofake.NewSimpleClientset(tt.aroCluster) | ||
| } | ||
|
|
||
| sp := &ServicePrincipalChecker{ | ||
| arocli: arocli, | ||
| kubernetescli: kubernetescli, | ||
| } | ||
|
|
||
| t.Run(tt.name, func(t *testing.T) { | ||
| err := sp.Check(ctx) | ||
|
|
||
| if err != nil && err.Error() != tt.wantErr || | ||
| err == nil && tt.wantErr != "" { | ||
| t.Error(fmt.Errorf("\n%s\n !=\n%s", err.Error(), tt.wantErr)) | ||
| } | ||
| }) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.