-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support of Azure authentication for ASO
- Loading branch information
1 parent
e52424e
commit 5343a11
Showing
9 changed files
with
281 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
resources: | ||
- ../capz | ||
components: | ||
- ../aso |
This file contains 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 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 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package controllers | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
corev1 "k8s.io/api/core/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/client-go/tools/record" | ||
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" | ||
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler" | ||
"sigs.k8s.io/cluster-api-provider-azure/util/tele" | ||
"sigs.k8s.io/cluster-api/util/predicates" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
// AzureClusterIdentityReconciler reconciles ASO identity secrets objects. | ||
type AzureClusterIdentityReconciler struct { | ||
client.Client | ||
Recorder record.EventRecorder | ||
ReconcileTimeout time.Duration | ||
WatchFilterValue string | ||
} | ||
|
||
// SetupWithManager initializes this controller with a manager. | ||
func (acir *AzureClusterIdentityReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { | ||
_, log, done := tele.StartSpanWithLogger(ctx, | ||
"controllers.AzureClusterIdentityReconciler.SetupWithManager", | ||
tele.KVP("controller", "AzureClusterIdentity"), | ||
) | ||
defer done() | ||
|
||
_, err := ctrl.NewControllerManagedBy(mgr). | ||
WithOptions(options). | ||
For(&infrav1.AzureClusterIdentity{}). | ||
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(log, acir.WatchFilterValue)). | ||
WithEventFilter(predicates.ResourceIsNotExternallyManaged(log)). | ||
Named("AzureClusterIdentity"). | ||
Build(acir) | ||
if err != nil { | ||
return errors.Wrap(err, "error creating controller") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azureclusteridentities,verbs=get;list;watch | ||
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete | ||
|
||
// Reconcile reconciles the AzureClusterIdentity objects. | ||
func (acir *AzureClusterIdentityReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) { | ||
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultedLoopTimeout(acir.ReconcileTimeout)) | ||
defer cancel() | ||
|
||
ctx, log, done := tele.StartSpanWithLogger(ctx, "controllers.AzureClusterIdentityReconciler.Reconcile", | ||
tele.KVP("namespace", req.Namespace), | ||
tele.KVP("name", req.Name), | ||
tele.KVP("kind", "AzureClusterIdentity"), | ||
) | ||
defer done() | ||
|
||
log = log.WithValues("namespace", req.Namespace, "AzureClusterIdentity", req.Name) | ||
|
||
// Fetch the AzureClusterIdentity instance | ||
azureClusterIdentity := &infrav1.AzureClusterIdentity{} | ||
err := acir.Get(ctx, req.NamespacedName, azureClusterIdentity) | ||
if err != nil { | ||
if apierrors.IsNotFound(err) { | ||
log.Info("object was not found") | ||
return reconcile.Result{}, nil | ||
} | ||
return reconcile.Result{}, err | ||
} | ||
|
||
// Handle deleted azureClusterIdentity | ||
if !azureClusterIdentity.DeletionTimestamp.IsZero() { | ||
// Cleanup ASO related secrets | ||
opt1 := client.InNamespace(azureClusterIdentity.Namespace) | ||
opt2 := client.MatchingLabels(map[string]string{ | ||
infrav1.ASOSecretLabel: azureClusterIdentity.Name, | ||
}) | ||
var asoSecrets corev1.SecretList | ||
if err := acir.List(ctx, &asoSecrets, opt1, opt2); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
log.Info("ASO related secrets not found") | ||
} else { | ||
return ctrl.Result{}, errors.Wrap(err, "failed to list ASO secrets") | ||
} | ||
} | ||
|
||
for _, secret := range asoSecrets.Items { | ||
secret := secret | ||
if err := acir.Client.Delete(ctx, &secret); err != nil { | ||
acir.Recorder.Eventf(azureClusterIdentity, corev1.EventTypeWarning, "Error reconciling AzureClusterIdentity", err.Error()) | ||
log.Error(err, "failed to delete ASO secrets") | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
} | ||
return ctrl.Result{}, nil | ||
} |
This file contains 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 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