@@ -22,17 +22,37 @@ import { RoleBinding, Subject } from './gen/io.k8s.api.rbac.v1';
2222const ENVIRONMENTS = [ 'dev' , 'prod' ] ;
2323
2424export const expandTeamCr : KptFunc = ( configs ) => {
25+ // For each 'Team' custom resource in the input:
26+ // 1. Generate a per-enviroment Namespace.
27+ // 2. Generate RoleBindings in each Namespace.
2528 configs . get ( isTeam ) . forEach ( ( team ) => {
2629 const name = team . metadata . name ;
2730
2831 ENVIRONMENTS . forEach ( ( suffix ) => {
2932 const ns = `${ name } -${ suffix } ` ;
3033 configs . insert ( Namespace . named ( ns ) ) ;
31- configs . insert ( ...expandTeam ( team , ns ) ) ;
34+ configs . insert ( ...createRoleBindings ( team , ns ) ) ;
3235 } ) ;
3336 } ) ;
3437} ;
3538
39+ function createRoleBindings ( team : Team , namespace : string ) : RoleBinding [ ] {
40+ return ( team . spec . roles || [ ] ) . map ( ( item ) => {
41+ return new RoleBinding ( {
42+ metadata : {
43+ name : item . role ,
44+ namespace,
45+ } ,
46+ subjects : roleSubjects ( item ) ,
47+ roleRef : {
48+ kind : 'ClusterRole' ,
49+ name : item . role ,
50+ apiGroup : 'rbac.authorization.k8s.io' ,
51+ } ,
52+ } ) ;
53+ } ) ;
54+ }
55+
3656function roleSubjects ( item : Team . Spec . Item ) : Subject [ ] {
3757 const userSubjects : Subject [ ] = ( item . users || [ ] ) . map (
3858 ( user ) =>
@@ -51,23 +71,6 @@ function roleSubjects(item: Team.Spec.Item): Subject[] {
5171 return userSubjects . concat ( groupSubjects ) ;
5272}
5373
54- function expandTeam ( team : Team , namespace : string ) : RoleBinding [ ] {
55- return ( team . spec . roles || [ ] ) . map ( ( item ) => {
56- return new RoleBinding ( {
57- metadata : {
58- name : item . role ,
59- namespace,
60- } ,
61- subjects : roleSubjects ( item ) ,
62- roleRef : {
63- kind : 'ClusterRole' ,
64- name : item . role ,
65- apiGroup : 'rbac.authorization.k8s.io' ,
66- } ,
67- } ) ;
68- } ) ;
69- }
70-
7174expandTeamCr . usage = `
7275Generates per-environment Namespaces and RoleBindings from the 'Team' custom resource.
7376
0 commit comments