@@ -26,7 +26,7 @@ import {MatTableDataSource} from '@angular/material/table';
2626import { KyvernoService } from '@app/core/services/kyverno' ;
2727import { NotificationService } from '@app/core/services/notification' ;
2828import { UserService } from '@app/core/services/user' ;
29- import { PolicyTemplate } from '@app/shared/entity/kyverno' ;
29+ import { PolicyBinding , PolicyTemplate } from '@app/shared/entity/kyverno' ;
3030import { Group } from '@app/shared/utils/member' ;
3131import { filter , Subject , switchMap , take , takeUntil } from 'rxjs' ;
3232import _ from 'lodash' ;
@@ -39,10 +39,6 @@ import {
3939 ConfirmationDialogConfig ,
4040} from '@app/shared/components/confirmation-dialog/component' ;
4141
42- interface templatesBinding {
43- bindingName : string ;
44- namespace : string ;
45- }
4642@Component ( {
4743 selector : 'km-kyverno-cluster-policies-list' ,
4844 templateUrl : './template.html' ,
@@ -56,14 +52,13 @@ export class KyvernoClusterPoliciesListComponent implements OnInit, OnDestroy {
5652 @ViewChild ( MatSort , { static : true } ) sort : MatSort ;
5753 @ViewChild ( MatPaginator , { static : true } ) paginator : MatPaginator ;
5854 private readonly _unsubscribe = new Subject < void > ( ) ;
59- dataSource = new MatTableDataSource < PolicyTemplate > ( ) ;
55+ dataSource = new MatTableDataSource < PolicyBinding > ( ) ;
6056 policyTemplates : PolicyTemplate [ ] = [ ] ;
61- policiesWithBinding : PolicyTemplate [ ] = [ ] ;
57+ policyBindings : PolicyBinding [ ] = [ ] ;
6258 columns = [ 'name' , 'category' , 'namespace' , 'view' ] ;
6359 loadingTemplates = false ;
6460 hasOwnerRole = false ;
6561 nameSpaces : string [ ] = [ ] ;
66- policyBindings : Record < string , templatesBinding > = { } ;
6762
6863 constructor (
6964 private readonly _kyvernoService : KyvernoService ,
@@ -103,12 +98,13 @@ export class KyvernoClusterPoliciesListComponent implements OnInit, OnDestroy {
10398 }
10499
105100 openAddPolicyDialog ( ) : void {
106- const noneBindingPolicies = this . policyTemplates . filter ( template => ! this . policyBindings [ template . name ] ) ;
101+ const templatesToExclude = new Set ( this . policyBindings . map ( binding => binding . spec . policyTemplateRef . name ) ) ;
102+ const templatesWithNoBinding = this . policyTemplates . filter ( template => ! templatesToExclude . has ( template . name ) ) ;
107103 const config : MatDialogConfig = {
108104 data : {
109105 projectID : this . projectID ,
110106 clusterID : this . cluster . id ,
111- templates : noneBindingPolicies ,
107+ templates : templatesWithNoBinding ,
112108 namespaces : this . nameSpaces ,
113109 } as AddPolicyDialogConfig ,
114110 } ;
@@ -120,8 +116,7 @@ export class KyvernoClusterPoliciesListComponent implements OnInit, OnDestroy {
120116 . subscribe ( _ => this . _getPolicyBindings ( ) ) ;
121117 }
122118
123- deletePolicyBinding ( templateName : string ) : void {
124- const bindingName = this . policyBindings [ templateName ] . bindingName ;
119+ deletePolicyBinding ( bindingName : string ) : void {
125120 const config : MatDialogConfig = {
126121 data : {
127122 title : 'Delete Policy' ,
@@ -137,13 +132,26 @@ export class KyvernoClusterPoliciesListComponent implements OnInit, OnDestroy {
137132 . pipe ( switchMap ( _ => this . _kyvernoService . deletePolicyBinding ( bindingName , this . projectID , this . cluster . id ) ) )
138133 . subscribe ( _ => {
139134 this . _notificationService . success ( `Deleting the ${ bindingName } policy` ) ;
140- delete this . policyBindings [ templateName ] ;
141- this . policiesWithBinding = this . policiesWithBinding . filter ( template => template . name !== templateName ) ;
142- this . dataSource . data = this . policiesWithBinding ;
135+ this . policyBindings = this . policyBindings . filter ( binding => binding . name !== bindingName ) ;
136+ this . dataSource . data = this . policyBindings ;
143137 } ) ;
144138 }
145139
146- viewTemplateSpec ( template : PolicyTemplate ) : void {
140+ canDeletePolicy ( templateName : string ) : boolean {
141+ const template = this . policyTemplates . find ( template => template . name === templateName ) ;
142+
143+ if ( ! template ) {
144+ return this . hasOwnerRole && this . isClusterRunning ;
145+ }
146+ return this . hasOwnerRole && ! template ?. spec . enforced && this . isClusterRunning ;
147+ }
148+
149+ viewTemplateSpec ( templateName : string ) : void {
150+ const template = this . policyTemplates . find ( template => template . name === templateName ) ;
151+ if ( ! template ) {
152+ return ;
153+ }
154+
147155 const config : MatDialogConfig = {
148156 data : {
149157 template : template ,
@@ -152,21 +160,23 @@ export class KyvernoClusterPoliciesListComponent implements OnInit, OnDestroy {
152160 this . _matDialog . open ( ViewTemplateDialogComponent , config ) ;
153161 }
154162
155- canDeletePolicy ( enforced : boolean ) : boolean {
156- return this . hasOwnerRole && ! enforced && this . isClusterRunning ;
163+ canViewTemplate ( templateName : string ) : boolean {
164+ const template = this . policyTemplates . find ( template => template . name === templateName ) ;
165+ return ! ! template ;
166+ }
167+
168+ getCategory ( policyTemplateName : string ) : string {
169+ const template = this . policyTemplates . find ( template => template . name === policyTemplateName ) ;
170+ return template ? template . spec . category : '-' ;
157171 }
158172
159173 private _getPolicyBindings ( ) : void {
160174 this . _kyvernoService
161175 . listPolicyBindings ( this . projectID , this . cluster . id )
162176 . pipe ( takeUntil ( this . _unsubscribe ) )
163177 . subscribe ( bindings => {
164- bindings . forEach ( binding => {
165- this . policyBindings [ binding . spec . policyTemplateRef . name ] = {
166- bindingName : binding ?. name ,
167- namespace : binding ?. spec ?. kyvernoPolicyNamespace ?. name ,
168- } ;
169- } ) ;
178+ this . policyBindings = bindings ;
179+ this . dataSource . data = bindings ;
170180 this . _getPolicyTemplates ( ) ;
171181 } ) ;
172182 }
@@ -180,14 +190,15 @@ export class KyvernoClusterPoliciesListComponent implements OnInit, OnDestroy {
180190 if ( _ . isEmpty ( template . spec ?. target ?. clusterSelector ) ) {
181191 return true ;
182192 }
193+ if ( this . policyBindings . some ( binding => binding . spec . policyTemplateRef . name === template . name ) ) {
194+ return true ;
195+ }
183196 const labelKeys : string [ ] = Object . keys ( template . spec . target . clusterSelector . matchLabels ) ;
184197 const hasMatchedLabels = ! ! labelKeys . find ( key =>
185198 this . _isMatchedLabel ( key , template . spec . target . clusterSelector . matchLabels [ key ] )
186199 ) ;
187200 return hasMatchedLabels ;
188201 } ) ;
189- this . policiesWithBinding = this . policyTemplates . filter ( template => ! ! this . policyBindings [ template . name ] ) ;
190- this . dataSource . data = this . policiesWithBinding ;
191202 this . loadingTemplates = false ;
192203 } ) ;
193204 }
0 commit comments