Skip to content
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

feat: Role, ClusterRole, RoleBinding, ClusterRoleBinding L2s (#432) #686

Merged
merged 2 commits into from
May 7, 2022

Conversation

iliapolo
Copy link
Member

@iliapolo iliapolo commented May 7, 2022

Backport

This will backport the following commits from k8s-22/main to k8s-20/main:

Questions ?

Please refer to the Backport tool documentation

…432)

Fixes #24
Fixes #25
Fixes #26
Fixes #374

Adds Role and ClusterRole L2s with "allowXxx" methods that resemble those available in the AWS CDK. This allows you to write code like:

```ts
import * as kplus from 'cdk8s-plus-23';

declare const deployment: kplus.Deployment;

const role = new kplus.ClusterRole(chart, 'my-cluster-role');
role.allowRead(deployment, kplus.ApiResource.SECRETS, kplus.ApiResource.PODS);
```

...where the "read" syntactic sugar grants permissions to the "get", "list", and "watch" commands. There are other allow commands available that offer more granularity if needed.

RoleBinding and ClusterRoleBinding L2s have also been created, and they can be instantiated through their constructors, or created using the appropriate bind methods that will attach it to the role automatically:

```ts
declare const role: kplus.Role;
declare const clusterRole: kplus.ClusterRole;

const user = new kplus.User({ name: 'alice@example.com' });
const group = new kplus.Group({ name: 'frontend-devs' });

role.bind(user, group); // creates a RoleBinding associated with the same namespace `role` was defined with
clusterRole.bindInNamespace('development', user, group); // creates a RoleBinding associated with namespace "development"
clusterRole.bind(user, group); // creates a ClusterRoleBinding, not associated with any namespace
```

Future work:
- Add full `pod.grantRead(role)` and `Resources.fromTypes(ApiResource.POD).grantRead(role)` style APIs
- Add type checking or validation to prevent adding namespaced resources to ClusterRole or cluster-wide resources to Role's etc.
- Add more constants to `ApiResource` for granting permissions to [subresources](https://stackoverflow.com/questions/57872201/how-to-refer-to-all-subresources-in-a-role-definition)
- Add convenience APIs for adding permissions to the [default roles](https://kubernetes.io/docs/reference/access-authn-authz/rbac/#default-roles-and-role-bindings) using `aggregate-to-<default-role>` tags

BREAKING CHANGE: The interface `Resources` is now named `ContainerResources`.

Signed-off-by: Christopher Rybicki <rybickic@amazon.com>
(cherry picked from commit aeaba6e)
Signed-off-by: Eli Polonsky <epolon@amazon.com>

# Conflicts:
#	.projen/tasks.json
#	.projenrc.ts
#	package.json
#	src/ingress-v1beta1.ts
@iliapolo iliapolo added the auto-approve Pull requests that should be auto approved label May 7, 2022
Signed-off-by: Eli Polonsky <epolon@amazon.com>
@iliapolo iliapolo merged commit 1f2622d into k8s-20/main May 7, 2022
@iliapolo iliapolo deleted the backport/k8s-20/main/pr-432 branch May 7, 2022 14:19
@cdk8s-automation
Copy link
Contributor

⚪ Backport skipped

The pull request was not backported as there were no branches to backport to. If this is a mistake, please apply the desired version labels or run the backport tool manually.

Manual backport

To create the backport manually run:

backport --pr 686

Questions ?

Please refer to the Backport tool documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-approve Pull requests that should be auto approved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants