forked from derailed/popeye
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ClusterRole: add aggregationRule support
- Loading branch information
1 parent
0917ef7
commit 3a15288
Showing
6 changed files
with
159 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Popeye | ||
|
||
package cache | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/derailed/popeye/internal" | ||
"github.com/derailed/popeye/internal/db" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
) | ||
|
||
// ClusterRole represents ClusterRole cache. | ||
type ClusterRole struct { | ||
db *db.DB | ||
} | ||
|
||
// NewClusterRole returns a new ClusterRole cache. | ||
func NewClusterRole(db *db.DB) *ClusterRole { | ||
return &ClusterRole{db: db} | ||
} | ||
|
||
// RoleRefs computes all role external references. | ||
func (r *ClusterRole) AggregationMatchers(refs *sync.Map) { | ||
txn, it := r.db.MustITFor(internal.Glossary[internal.CR]) | ||
defer txn.Abort() | ||
for o := it.Next(); o != nil; o = it.Next() { | ||
cr := o.(*rbacv1.ClusterRole) | ||
if cr.AggregationRule != nil { | ||
for _, lbs := range cr.AggregationRule.ClusterRoleSelectors { | ||
for k, v := range lbs.MatchLabels { | ||
refs.Store(k, v) | ||
} | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Popeye | ||
|
||
package cache_test | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/derailed/popeye/internal" | ||
"github.com/derailed/popeye/internal/cache" | ||
"github.com/derailed/popeye/internal/db" | ||
"github.com/derailed/popeye/internal/test" | ||
"github.com/stretchr/testify/assert" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
) | ||
|
||
func TestClusterRoleAggregation(t *testing.T) { | ||
dba, err := test.NewTestDB() | ||
assert.NoError(t, err) | ||
l := db.NewLoader(dba) | ||
|
||
ctx := test.MakeCtx(t) | ||
assert.NoError(t, test.LoadDB[*rbacv1.ClusterRole](ctx, l.DB, "auth/cr/1.yaml", internal.Glossary[internal.CR])) | ||
|
||
cr := cache.NewClusterRole(dba) | ||
var aRefs sync.Map | ||
cr.AggregationMatchers(&aRefs) | ||
|
||
value, ok := aRefs.Load("rbac.authorization.k8s.io/aggregate-to-cr4") | ||
assert.True(t, ok) | ||
assert.Equal(t, "true", value) | ||
} |
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,27 @@ | ||
--- | ||
apiVersion: v1 | ||
kind: List | ||
items: | ||
- apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
annotations: | ||
rbac.authorization.kubernetes.io/autoupdate: "true" | ||
name: cr4 | ||
aggregationRule: | ||
clusterRoleSelectors: | ||
- matchLabels: | ||
rbac.authorization.k8s.io/aggregate-to-cr4: "true" | ||
- apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
labels: | ||
rbac.authorization.k8s.io/aggregate-to-cr4: "true" | ||
name: cr5 | ||
rules: | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- pods | ||
verbs: | ||
- list |
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,27 @@ | ||
--- | ||
apiVersion: v1 | ||
kind: List | ||
items: | ||
- apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
annotations: | ||
rbac.authorization.kubernetes.io/autoupdate: "true" | ||
name: cr4 | ||
aggregationRule: | ||
clusterRoleSelectors: | ||
- matchLabels: | ||
rbac.authorization.k8s.io/aggregate-to-cr4: "true" | ||
- apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
labels: | ||
rbac.authorization.k8s.io/aggregate-to-cr4: "true" | ||
name: cr5 | ||
rules: | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- pods | ||
verbs: | ||
- list |