1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-07 08:26:53 +00:00
kyverno/pkg/policyStatus/0_main.go

88 lines
1.7 KiB
Go
Raw Normal View History

2020-02-25 20:55:07 +05:30
package policyStatus
import (
"sync"
"time"
"github.com/golang/glog"
"github.com/nirmata/kyverno/pkg/policystore"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/nirmata/kyverno/pkg/client/clientset/versioned"
v1 "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
)
type Sync struct {
cache *cache
listener chan statusUpdater
stop <-chan struct{}
client *versioned.Clientset
policyStore *policystore.PolicyStore
}
type cache struct {
mutex sync.RWMutex
data map[string]v1.PolicyStatus
}
func NewSync(c *versioned.Clientset, sc <-chan struct{}, pms *policystore.PolicyStore) *Sync {
return &Sync{
cache: &cache{
mutex: sync.RWMutex{},
data: make(map[string]v1.PolicyStatus),
},
stop: sc,
client: c,
policyStore: pms,
2020-02-25 23:57:16 +05:30
listener: make(chan statusUpdater),
2020-02-25 20:55:07 +05:30
}
}
2020-02-25 21:07:00 +05:30
func (s *Sync) Run(workers int) {
for i := 0; i < workers; i++ {
go s.updateStatusCache()
}
2020-02-26 10:22:11 +05:30
wait.Until(s.updatePolicyStatus, 2*time.Second, s.stop)
2020-02-25 20:55:07 +05:30
<-s.stop
s.updatePolicyStatus()
}
func (s *Sync) updateStatusCache() {
for {
select {
case statusUpdater := <-s.listener:
statusUpdater.updateStatus()
case <-s.stop:
return
}
}
}
func (s *Sync) updatePolicyStatus() {
s.cache.mutex.Lock()
var nameToStatus = make(map[string]v1.PolicyStatus, len(s.cache.data))
for k, v := range s.cache.data {
nameToStatus[k] = v
}
s.cache.mutex.Unlock()
for policyName, status := range nameToStatus {
policy, err := s.policyStore.Get(policyName)
if err != nil {
continue
}
policy.Status = status
_, err = s.client.KyvernoV1().ClusterPolicies().UpdateStatus(policy)
if err != nil {
2020-02-26 10:22:11 +05:30
s.cache.mutex.Lock()
delete(s.cache.data, policyName)
s.cache.mutex.Unlock()
2020-02-25 20:55:07 +05:30
glog.V(4).Info(err)
}
}
}