-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor CredentialsRequest reconciliation
- Loading branch information
Showing
20 changed files
with
452 additions
and
93 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
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
61 changes: 61 additions & 0 deletions
61
operator/controllers/loki/credentialsrequests_controller.go
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,61 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-logr/logr" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
|
||
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1" | ||
"github.com/grafana/loki/operator/controllers/loki/internal/lokistack" | ||
"github.com/grafana/loki/operator/controllers/loki/internal/management/state" | ||
"github.com/grafana/loki/operator/internal/handlers" | ||
) | ||
|
||
type CredentialsRequestsReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
Log logr.Logger | ||
} | ||
|
||
func (r *CredentialsRequestsReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
managed, err := state.IsManaged(ctx, req, r.Client) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
if !managed { | ||
r.Log.Info("Skipping reconciliation for unmanaged LokiStack resource", "name", req.String()) | ||
// Stop requeueing for unmanaged LokiStack custom resources | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
var stack *lokiv1.LokiStack | ||
if err = r.Client.Get(ctx, req.NamespacedName, stack); err != nil { | ||
if errors.IsNotFound(err) { | ||
return ctrl.Result{}, handlers.DeleteCredentialsRequest(ctx, r.Client, req.NamespacedName) | ||
} | ||
return ctrl.Result{}, err | ||
} | ||
|
||
secretRef, err := handlers.CreateCredentialsRequest(ctx, r.Client, req.NamespacedName) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
if err := lokistack.AnnotateForCredentialsRequest(ctx, r.Client, req.NamespacedName, secretRef); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager to only call this controller on create/delete/generic events. | ||
func (r *CredentialsRequestsReconciler) SetupWithManager(mgr manager.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&lokiv1.LokiStack{}, createOrDeletesPred). | ||
Complete(r) | ||
} |
33 changes: 33 additions & 0 deletions
33
operator/controllers/loki/internal/lokistack/credentialsrequest_discovery.go
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,33 @@ | ||
package lokistack | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/ViaQ/logerr/v2/kverrors" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/grafana/loki/operator/internal/external/k8s" | ||
"github.com/grafana/loki/operator/internal/manifests/storage" | ||
) | ||
|
||
var ErrAnnotationAlreadyExists = errors.New("credentialsRequestsSecretRef annotation already exists") | ||
|
||
// AnnotateForCredentialsRequest adds/updates the `loki.grafana.com/credentials-request-secret-ref` annotation | ||
// to the named Lokistack. If no LokiStack is found, then skip reconciliation. | ||
func AnnotateForCredentialsRequest(ctx context.Context, k k8s.Client, key client.ObjectKey, secretRef string) error { | ||
stack, err := getLokiStack(ctx, k, key) | ||
if stack == nil || err != nil { | ||
return err | ||
} | ||
|
||
if val, ok := stack.Annotations[storage.AnnotationCredentialsRequestsSecretRef]; ok && val == secretRef { | ||
return ErrAnnotationAlreadyExists | ||
} | ||
|
||
if err := updateAnnotation(ctx, k, stack, storage.AnnotationCredentialsRequestsSecretRef, secretRef); err != nil { | ||
return kverrors.Wrap(err, "failed to update lokistack `credentialsRequestSecretRef` annotation", "key", key) | ||
} | ||
|
||
return 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
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,40 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ViaQ/logerr/v2/kverrors" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/grafana/loki/operator/internal/external/k8s" | ||
"github.com/grafana/loki/operator/internal/manifests/openshift" | ||
) | ||
|
||
func CreateCredentialsRequest(ctx context.Context, k k8s.Client, stack client.ObjectKey) (string, error) { | ||
managedAuthEnv := openshift.DiscoverManagedAuthEnv() | ||
if managedAuthEnv == nil { | ||
return "", nil | ||
} | ||
|
||
opts := openshift.Options{ | ||
BuildOpts: openshift.BuildOptions{ | ||
LokiStackName: stack.Name, | ||
LokiStackNamespace: stack.Namespace, | ||
}, | ||
ManagedAuthEnv: managedAuthEnv, | ||
} | ||
|
||
credReq, err := openshift.BuildCredentialsRequest(opts) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if err := k.Create(ctx, credReq); err != nil { | ||
if !apierrors.IsAlreadyExists(err) { | ||
return "", kverrors.Wrap(err, "failed to create credentialsrequest", "key", client.ObjectKeyFromObject(credReq)) | ||
} | ||
} | ||
|
||
return credReq.Spec.SecretRef.Name, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ViaQ/logerr/v2/kverrors" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/grafana/loki/operator/internal/external/k8s" | ||
"github.com/grafana/loki/operator/internal/manifests/openshift" | ||
) | ||
|
||
func DeleteCredentialsRequest(ctx context.Context, k k8s.Client, stack client.ObjectKey) error { | ||
managedAuthEnv := openshift.DiscoverManagedAuthEnv() | ||
if managedAuthEnv == nil { | ||
return nil | ||
} | ||
|
||
opts := openshift.Options{ | ||
BuildOpts: openshift.BuildOptions{ | ||
LokiStackName: stack.Name, | ||
LokiStackNamespace: stack.Namespace, | ||
}, | ||
ManagedAuthEnv: managedAuthEnv, | ||
} | ||
|
||
credReq, err := openshift.BuildCredentialsRequest(opts) | ||
if err != nil { | ||
return kverrors.Wrap(err, "failed to build credentialsrequest", "key", stack) | ||
} | ||
|
||
if err := k.Delete(ctx, credReq); err != nil { | ||
return kverrors.Wrap(err, "failed to delete credentialsrequest", "key", client.ObjectKeyFromObject(credReq)) | ||
} | ||
|
||
return nil | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.