Skip to content

Commit

Permalink
cleanup logging, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Cooper committed Feb 8, 2020
1 parent dbb2e17 commit 96795d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,12 @@ Generate a Kubernetes role with every available resource type on a cluster.
Arguments:

-n,--name - specify the name of the emitted Role. Default is 'foo-role'
--include-deprecated - include API groups that are deprecated. By default, API groups such as "extensions" are excluded.
```

The resulting `Role` resource will be printed to stdout in YAML format.

```bash
$ kube-role-gen
2020-01-03 11:41:54,534 - INFO - Gathering core API resource details
2020-01-03 11:41:54,534 - INFO - Gathering API groups & resource details
2020-01-03 11:41:59,661 - INFO - Resource discovery complete. Found 76 resources in 19 API groups
2020-01-03 11:41:59,661 - INFO - Converting resources to rbac.authorization.k8s.io/v1/Role
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
Expand All @@ -64,10 +59,17 @@ You can also redirect the output to a file and create your new Roles from the ge

```bash
$ kube-role-gen > foo-role.yaml
2020-01-03 11:42:07,417 - INFO - Gathering core API resource details
2020-01-03 11:42:07,417 - INFO - Gathering API groups & resource details
2020-01-03 11:42:12,676 - INFO - Resource discovery complete. Found 76 resources in 19 API groups
2020-01-03 11:42:12,677 - INFO - Converting resources to rbac.authorization.k8s.io/v1/Role
2020/02/07 22:42:54 Group: v1
2020/02/07 22:42:54 Resource: bindings - Verbs: [create]
2020/02/07 22:42:54 Resource: componentstatuses - Verbs: [get list]
2020/02/07 22:42:54 Resource: configmaps - Verbs: [create delete deletecollection get list patch update watch]
2020/02/07 22:42:54 Resource: endpoints - Verbs: [create delete deletecollection get list patch update watch]
2020/02/07 22:42:54 Resource: events - Verbs: [create delete deletecollection get list patch update watch]
2020/02/07 22:42:54 Resource: limitranges - Verbs: [create delete deletecollection get list patch update watch]
2020/02/07 22:42:54 Resource: namespaces - Verbs: [create delete get list patch update watch]
2020/02/07 22:42:54 Resource: namespaces/finalize - Verbs: [update]
2020/02/07 22:42:54 Resource: namespaces/status - Verbs: [get patch update]
...

$ kubeval foo-role.yaml
PASS - foo-role.yaml contains a valid Role
Expand Down
22 changes: 13 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func main() {

for _, apiResourceList := range apiResourceListArray {

log.Printf("\n\tAPI Group: %s", apiResourceList.GroupVersion)
log.Printf("Group: %s", apiResourceList.GroupVersion)
// rbac rules only look at API group names, not name & version
groupOnly := strings.Split(apiResourceList.GroupVersion, "/")[0]
// core API doesn't have a group "name". In rbac policy rules, its a blank string
Expand All @@ -59,8 +59,7 @@ func main() {
resourceList := make([]string, 0)
uniqueVerbs := make(map[string]bool)
for _, apiResource := range apiResourceList.APIResources {
log.Printf("\n\tGroup Name: %s\n\tResource: %s\n\tVerbs: %s\n",
groupOnly,
log.Printf("Resource: %s - Verbs: %s",
apiResource.Name,
apiResource.Verbs.String())

Expand All @@ -70,12 +69,7 @@ func main() {
}
}

verbList := make([]string, len(uniqueVerbs))
i := 0
for k := range uniqueVerbs {
verbList[i] = k
i++
}
verbList := mapSetToList(uniqueVerbs)

newPolicyRule := &rbacv1.PolicyRule{
APIGroups: []string{groupOnly},
Expand Down Expand Up @@ -108,6 +102,16 @@ func main() {
fmt.Println(writer.String())
}

func mapSetToList(initialMap map[string]bool) []string {
list := make([]string, len(initialMap))
i := 0
for k := range initialMap {
list[i] = k
i++
}
return list
}

func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
Expand Down

0 comments on commit 96795d7

Please sign in to comment.