mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-09 01:16:55 +00:00
* add report in cli * policy report crd added * policy report added * configmap added * added jobs * added jobs * bug fixed * added logic for cli * common function added * sub command added for policy report * subcommand added for report * common package changed * configmap added * added logic for kyverno cli * added logic for jobs * added logic for jobs * added logic for jobs * added logic for cli * buf fix * cli changes * count bug fix * docs added for command * go fmt * refactor codebase * remove policy controller for policyreport * policy report removed * bug fixes * bug fixes * added job trigger if needed * job deletation logic added * build failed fix * fixed e2e test * remove hard coded variables * packages adde * improvment added in jobs sheduler * policy report yaml added * cronjob added * small fixes * remove background sync * documentation added for report command * remove extra log * small improvement * tested policy report * revert hardcoded changes * changes for demo * demo changes * resource aggrigation added * More changes * More changes * - resolve PR comments; - refactor jobs controller * set rbac for jobs * add clean up in job controller * add short names * remove application scope for policyreport * move job controller to policyreport * add report logic in command apply * - update policy report types; - upgrade k8s library; - update code gen * temporarily comment out code to pass CI build * generate / update policyreport to cluster * add unit test for CLI report * add test for apply - generate policy report * fix unit test * - remove job controller; - remove in-memory configmap; - clean up kustomize manifest * remove dependency * add reportRequest / clusterReportRequest * clean up policy report * generate report request * update crd clusterReportRequest * - update json tag of report summary; - update definition manifests; - fix dclient creation * aggregate reportRequest into policy report * fix unit tests * - update report summary to optional; - generate clusterPolicyReport; - remove reportRequests after merged to report * remove * generate reportRequest in kyverno namespace * update resource filter in helm chart * - rename reportRequest to reportChangeRequest; -rename clusterReportRequest to clusterReportChangeRequest * generate policy report in background scan * skip generating report change request if there's entry results * fix results entry removal when policy / rule gets deleted * rename apiversion from policy.kubernetes.io to policy.k8s.io * update summary.* to lower case * move reportChangeRequest to kyverno.io/v1alpha1 * remove policy report flag * fix report update * clean up policy violation CRD * remove violation CRD from manifest * clean up policy violation code - remove pvGenerator * change severity fields to lower case * update import library * set report category Co-authored-by: Yuvraj <yuvraj.yad001@gmail.com> Co-authored-by: Yuvraj <10830562+evalsocket@users.noreply.github.com> Co-authored-by: Jim Bugwadia <jim@nirmata.com>
176 lines
5 KiB
Go
176 lines
5 KiB
Go
package policystatus
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
v1 "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
|
"github.com/kyverno/kyverno/pkg/client/clientset/versioned"
|
|
kyvernolister "github.com/kyverno/kyverno/pkg/client/listers/kyverno/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
log "sigs.k8s.io/controller-runtime/pkg/log"
|
|
)
|
|
|
|
// Policy status implementation works in the following way,
|
|
//Currently policy status maintains a cache of the status of
|
|
//each policy.
|
|
//Every x unit of time the status of policy is updated using
|
|
//the data from the cache.
|
|
//The sync exposes a listener which accepts a statusUpdater
|
|
//interface which dictates how the status should be updated.
|
|
//The status is updated by a worker that receives the interface
|
|
//on a channel.
|
|
//The worker then updates the current status using the methods
|
|
//exposed by the interface.
|
|
//Current implementation is designed to be threadsafe with optimised
|
|
//locking for each policy.
|
|
|
|
// statusUpdater defines a type to have a method which
|
|
//updates the given status
|
|
type statusUpdater interface {
|
|
PolicyName() string
|
|
UpdateStatus(status v1.PolicyStatus) v1.PolicyStatus
|
|
}
|
|
|
|
type Listener chan statusUpdater
|
|
|
|
func (l Listener) Send(s statusUpdater) {
|
|
l <- s
|
|
}
|
|
|
|
// Sync is the object which is used to initialize
|
|
//the policyStatus sync, can be considered the parent object
|
|
//since it contains access to all the persistant data present
|
|
//in this package.
|
|
type Sync struct {
|
|
cache *cache
|
|
Listener Listener
|
|
client *versioned.Clientset
|
|
lister kyvernolister.ClusterPolicyLister
|
|
nsLister kyvernolister.PolicyLister
|
|
}
|
|
|
|
type cache struct {
|
|
dataMu sync.RWMutex
|
|
data map[string]v1.PolicyStatus
|
|
keyToMutex *keyToMutex
|
|
}
|
|
|
|
func NewSync(c *versioned.Clientset, lister kyvernolister.ClusterPolicyLister, nsLister kyvernolister.PolicyLister) *Sync {
|
|
return &Sync{
|
|
cache: &cache{
|
|
dataMu: sync.RWMutex{},
|
|
data: make(map[string]v1.PolicyStatus),
|
|
keyToMutex: newKeyToMutex(),
|
|
},
|
|
client: c,
|
|
lister: lister,
|
|
nsLister: nsLister,
|
|
Listener: make(chan statusUpdater, 20),
|
|
}
|
|
}
|
|
|
|
func (s *Sync) Run(workers int, stopCh <-chan struct{}) {
|
|
for i := 0; i < workers; i++ {
|
|
go s.updateStatusCache(stopCh)
|
|
}
|
|
|
|
wait.Until(s.updatePolicyStatus, 10*time.Second, stopCh)
|
|
<-stopCh
|
|
}
|
|
|
|
// updateStatusCache is a worker which updates the current status
|
|
//using the statusUpdater interface
|
|
func (s *Sync) updateStatusCache(stopCh <-chan struct{}) {
|
|
for {
|
|
select {
|
|
case statusUpdater := <-s.Listener:
|
|
s.cache.keyToMutex.Get(statusUpdater.PolicyName()).Lock()
|
|
|
|
s.cache.dataMu.RLock()
|
|
status, exist := s.cache.data[statusUpdater.PolicyName()]
|
|
s.cache.dataMu.RUnlock()
|
|
if !exist {
|
|
policy, _ := s.lister.Get(statusUpdater.PolicyName())
|
|
if policy != nil {
|
|
status = policy.Status
|
|
}
|
|
}
|
|
updatedStatus := statusUpdater.UpdateStatus(status)
|
|
|
|
s.cache.dataMu.Lock()
|
|
s.cache.data[statusUpdater.PolicyName()] = updatedStatus
|
|
s.cache.dataMu.Unlock()
|
|
|
|
s.cache.keyToMutex.Get(statusUpdater.PolicyName()).Unlock()
|
|
oldStatus, _ := json.Marshal(status)
|
|
newStatus, _ := json.Marshal(updatedStatus)
|
|
log.Log.V(4).Info(fmt.Sprintf("\nupdated status of policy - %v\noldStatus:\n%v\nnewStatus:\n%v\n", statusUpdater.PolicyName(), string(oldStatus), string(newStatus)))
|
|
case <-stopCh:
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// updatePolicyStatus updates the status in the policy resource definition
|
|
//from the status cache, syncing them
|
|
func (s *Sync) updatePolicyStatus() {
|
|
s.cache.dataMu.Lock()
|
|
var nameToStatus = make(map[string]v1.PolicyStatus, len(s.cache.data))
|
|
for k, v := range s.cache.data {
|
|
nameToStatus[k] = v
|
|
}
|
|
s.cache.dataMu.Unlock()
|
|
|
|
for policyName, status := range nameToStatus {
|
|
// Identify Policy and ClusterPolicy based on namespace in key
|
|
// key = <namespace>/<name> for namespacepolicy and key = <name> for clusterpolicy
|
|
// and update the respective policies
|
|
namespace := ""
|
|
isNamespacedPolicy := false
|
|
key := policyName
|
|
index := strings.Index(policyName, "/")
|
|
if index != -1 {
|
|
namespace = policyName[:index]
|
|
isNamespacedPolicy = true
|
|
policyName = policyName[index+1:]
|
|
}
|
|
if !isNamespacedPolicy {
|
|
policy, err := s.lister.Get(policyName)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
policy.Status = status
|
|
_, err = s.client.KyvernoV1().ClusterPolicies().UpdateStatus(context.TODO(), policy, metav1.UpdateOptions{})
|
|
if err != nil {
|
|
s.cache.dataMu.Lock()
|
|
delete(s.cache.data, policyName)
|
|
s.cache.dataMu.Unlock()
|
|
log.Log.Error(err, "failed to update policy status")
|
|
}
|
|
} else {
|
|
policy, err := s.nsLister.Policies(namespace).Get(policyName)
|
|
if err != nil {
|
|
s.cache.dataMu.Lock()
|
|
delete(s.cache.data, key)
|
|
s.cache.dataMu.Unlock()
|
|
continue
|
|
}
|
|
policy.Status = status
|
|
_, err = s.client.KyvernoV1().Policies(namespace).UpdateStatus(context.TODO(), policy, metav1.UpdateOptions{})
|
|
if err != nil {
|
|
s.cache.dataMu.Lock()
|
|
delete(s.cache.data, key)
|
|
s.cache.dataMu.Unlock()
|
|
log.Log.Error(err, "failed to update namespace policy status")
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|