-
Notifications
You must be signed in to change notification settings - Fork 4k
fix(operator): move authorization RBAC to dedicated cluster_scoped controller #16712
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
Open
JoaoBraveCoding
wants to merge
10
commits into
grafana:main
Choose a base branch
from
JoaoBraveCoding:LOG-6083
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8a0e3a9
fix(operator): move authorization RBAC to dedicated cluster_scoped
JoaoBraveCoding 3838f75
Merge branch 'main' into LOG-6083
JoaoBraveCoding 0908fd8
clean-up code and added delete path for rbac resources new & legacy
JoaoBraveCoding e2d129f
remove RBAC for gateway
JoaoBraveCoding 456b663
added tests for rbac
JoaoBraveCoding 7796c21
added feature flags to enable SSR & SSAR
JoaoBraveCoding 2aa6b8a
Merge branch 'main' into LOG-6083
JoaoBraveCoding 73de57a
Merge branch 'main' into LOG-6083
JoaoBraveCoding 4ea7fa2
address review comments
JoaoBraveCoding 60e1d77
Merge branch 'main' into LOG-6083
JoaoBraveCoding 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
operator/internal/handlers/lokistack_cluster_scope_resources_create.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,87 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/ViaQ/logerr/v2/kverrors" | ||
| "github.com/go-logr/logr" | ||
| rbacv1 "k8s.io/api/rbac/v1" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" //nolint:typecheck | ||
| ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
|
|
||
| lokiv1 "github.com/grafana/loki/operator/api/loki/v1" | ||
| "github.com/grafana/loki/operator/internal/external/k8s" | ||
| "github.com/grafana/loki/operator/internal/manifests" | ||
| "github.com/grafana/loki/operator/internal/manifests/openshift" | ||
| ) | ||
|
|
||
| // CreateClusterScopedResources handles the LokiStack cluster scoped create events. | ||
| func CreateClusterScopedResources(ctx context.Context, log logr.Logger, dashboards bool, operatorNs string, k k8s.Client, s *runtime.Scheme, stacks lokiv1.LokiStackList) error { | ||
| // This has to be done here as to not introduce a circular dependency. | ||
| rulerSubjects := make([]rbacv1.Subject, 0, len(stacks.Items)) | ||
| for _, stack := range stacks.Items { | ||
| rulerSubjects = append(rulerSubjects, rbacv1.Subject{ | ||
| Kind: "ServiceAccount", | ||
| Name: manifests.RulerName(stack.Name), | ||
| Namespace: stack.Namespace, | ||
| }) | ||
| } | ||
| opts := openshift.NewOptionsClusterScope(operatorNs, manifests.ClusterScopeLabels(), rulerSubjects) | ||
|
|
||
| objs := openshift.BuildRBAC(opts) | ||
| if dashboards { | ||
| objs = append(objs, openshift.BuildDashboards(opts)...) | ||
| } | ||
|
|
||
| var errCount int32 | ||
| for _, obj := range objs { | ||
| desired := obj.DeepCopyObject().(client.Object) | ||
| mutateFn := manifests.MutateFuncFor(obj, desired, nil) | ||
|
|
||
| op, err := ctrl.CreateOrUpdate(ctx, k, obj, mutateFn) | ||
| if err != nil { | ||
| log.Error(err, "failed to configure resource") | ||
| errCount++ | ||
| continue | ||
| } | ||
|
|
||
| msg := fmt.Sprintf("Resource has been %s", op) | ||
| switch op { | ||
| case ctrlutil.OperationResultNone: | ||
| log.V(1).Info(msg) | ||
| default: | ||
| log.Info(msg) | ||
| } | ||
| } | ||
|
|
||
| if errCount > 0 { | ||
| return kverrors.New("failed to configure lokistack cluster-scoped resources") | ||
| } | ||
|
|
||
| // Delete legacy RBAC resources | ||
| // This needs to live here and not in DeleteClusterScopedResources as we want to | ||
| // delete the legacy RBAC resources when LokiStack is reconciled and not on delete. | ||
| var legacyObjs []client.Object | ||
| for _, stack := range stacks.Items { | ||
| // This name would clash with the new cluster-scoped resources. Skip it. | ||
| if stack.Name == "lokistack" { | ||
| continue | ||
| } | ||
| legacyObjs = append(legacyObjs, openshift.LegacyRBAC(manifests.GatewayName(stack.Name), manifests.RulerName(stack.Name))...) | ||
| } | ||
| for _, obj := range legacyObjs { | ||
| key := client.ObjectKeyFromObject(obj) | ||
| if err := k.Delete(ctx, obj, &client.DeleteOptions{}); err != nil { | ||
| if apierrors.IsNotFound(err) { | ||
| continue | ||
| } | ||
| return kverrors.Wrap(err, "failed to delete resource", "kind", obj.GetObjectKind(), "key", key) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } |
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
35 changes: 35 additions & 0 deletions
35
operator/internal/handlers/lokistack_cluster_scope_resources_delete.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,35 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/ViaQ/logerr/v2/kverrors" | ||
| rbacv1 "k8s.io/api/rbac/v1" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| lokiv1 "github.com/grafana/loki/operator/api/loki/v1" | ||
| "github.com/grafana/loki/operator/internal/external/k8s" | ||
| "github.com/grafana/loki/operator/internal/manifests" | ||
| "github.com/grafana/loki/operator/internal/manifests/openshift" | ||
| ) | ||
|
|
||
| // DeleteClusterScopedResources removes all cluster-scoped resources. | ||
| func DeleteClusterScopedResources(ctx context.Context, k k8s.Client, operatorNs string, stacks lokiv1.LokiStackList) error { | ||
JoaoBraveCoding marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Since we are deleting we don't need to worry about the subjects. | ||
| opts := openshift.NewOptionsClusterScope(operatorNs, manifests.ClusterScopeLabels(), []rbacv1.Subject{}) | ||
|
|
||
| objs := openshift.BuildRBAC(opts) | ||
| objs = append(objs, openshift.BuildDashboards(opts)...) | ||
|
|
||
| for _, obj := range objs { | ||
| key := client.ObjectKeyFromObject(obj) | ||
JoaoBraveCoding marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if err := k.Delete(ctx, obj, &client.DeleteOptions{}); err != nil { | ||
| if apierrors.IsNotFound(err) { | ||
| continue | ||
| } | ||
| return kverrors.Wrap(err, "failed to delete dashboard", "kind", obj.GetObjectKind(), "key", key) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.