-
-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #310 from jordiprats/master
Add support for ClusterRule aggregationRule
- Loading branch information
Showing
8 changed files
with
161 additions
and
5 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 |
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