-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update DynamicRoles and DynamicClusterRoles when an inherited Role or…
… ClusterRole is modified (#7)
- Loading branch information
Showing
7 changed files
with
202 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
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" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/redhat-cop/dynamic-rbac-operator/helpers" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
rbacv1 "k8s.io/api/rbac/v1" | ||
) | ||
|
||
// ClusterRoleReconciler reconciles a ClusterRole object | ||
type ClusterRoleReconciler struct { | ||
client.Client | ||
Log logr.Logger | ||
Scheme *runtime.Scheme | ||
Cache *helpers.ResourceCache | ||
} | ||
|
||
// +kubebuilder:rbac:groups=rbac.redhatcop.redhat.io,resources=clusterroles,verbs=get;list;watch;create;update;patch;delete | ||
// +kubebuilder:rbac:groups=rbac.redhatcop.redhat.io,resources=clusterroles/status,verbs=get;update;patch | ||
|
||
func (r *ClusterRoleReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { | ||
_ = context.Background() | ||
_ = r.Log.WithValues("clusterrole", req.NamespacedName) | ||
|
||
result := ctrl.Result{} | ||
var err error | ||
|
||
if _, exists := r.Cache.WatchedClusterRoles[req.NamespacedName]; exists { | ||
r.Log.Info("A cluster role referenced by a dynamic resource has been updated - reconciling now") | ||
result, err = UpdateAllDynamicResources(r.Client, r.Log, r.Scheme, r.Cache) | ||
} | ||
|
||
return result, err | ||
} | ||
|
||
func (r *ClusterRoleReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&rbacv1.ClusterRole{}). | ||
Complete(r) | ||
} |
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,48 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-logr/logr" | ||
rbacv1alpha1 "github.com/redhat-cop/dynamic-rbac-operator/api/v1alpha1" | ||
"github.com/redhat-cop/dynamic-rbac-operator/helpers" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
// UpdateAllDynamicResources loops through all DynamicRoles and DynamicClusterRoles and updates their rules/specs as required based on current cache info | ||
func UpdateAllDynamicResources(client client.Client, log logr.Logger, scheme *runtime.Scheme, cache *helpers.ResourceCache) (ctrl.Result, error) { | ||
// Clear the watched roles cache maps since we're about to recreate them anyway - gets rid of anything we used to care about but no longer need | ||
cache.WatchedRoles = map[types.NamespacedName]bool{} | ||
cache.WatchedClusterRoles = map[types.NamespacedName]bool{} | ||
|
||
dynamicRoleList := &rbacv1alpha1.DynamicRoleList{} | ||
err := client.List(context.TODO(), dynamicRoleList) | ||
if err != nil { | ||
log.Error(err, "could not list Dynamic Roles") | ||
return reconcile.Result{}, err | ||
} | ||
for _, dynamicRole := range dynamicRoleList.Items { | ||
_, err := ReconcileDynamicRole(&dynamicRole, client, scheme, log, cache) | ||
if err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
} | ||
dynamicClusterRoleList := &rbacv1alpha1.DynamicClusterRoleList{} | ||
err = client.List(context.TODO(), dynamicClusterRoleList) | ||
if err != nil { | ||
log.Error(err, "could not list Dynamic Cluster Roles") | ||
return reconcile.Result{}, err | ||
} | ||
for _, dynamicClusterRole := range dynamicClusterRoleList.Items { | ||
_, err := ReconcileDynamicClusterRole(&dynamicClusterRole, client, scheme, log, cache) | ||
if err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
} | ||
log.Info("All computed roles have been reconciled") | ||
return reconcile.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
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" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/redhat-cop/dynamic-rbac-operator/helpers" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
rbacv1 "k8s.io/api/rbac/v1" | ||
) | ||
|
||
// RoleReconciler reconciles a Role object | ||
type RoleReconciler struct { | ||
client.Client | ||
Log logr.Logger | ||
Scheme *runtime.Scheme | ||
Cache *helpers.ResourceCache | ||
} | ||
|
||
// +kubebuilder:rbac:groups=rbac.redhatcop.redhat.io,resources=roles,verbs=get;list;watch;create;update;patch;delete | ||
// +kubebuilder:rbac:groups=rbac.redhatcop.redhat.io,resources=roles/status,verbs=get;update;patch | ||
|
||
func (r *RoleReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { | ||
_ = context.Background() | ||
_ = r.Log.WithValues("role", req.NamespacedName) | ||
|
||
result := ctrl.Result{} | ||
var err error | ||
|
||
if _, exists := r.Cache.WatchedRoles[req.NamespacedName]; exists { | ||
r.Log.Info("A role referenced by a dynamic resource has been updated - reconciling now") | ||
result, err = UpdateAllDynamicResources(r.Client, r.Log, r.Scheme, r.Cache) | ||
} | ||
|
||
return result, err | ||
} | ||
|
||
func (r *RoleReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&rbacv1.Role{}). | ||
Complete(r) | ||
} |
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