mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
Improve init container to use DeleteCollection to remove policy reports (#2477)
* Improve init container to use DeleteCollection to remove policy reports Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu> * Do not use go routine for each namespace Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu>
This commit is contained in:
parent
c2751828d1
commit
b460490984
1 changed files with 30 additions and 28 deletions
|
@ -4,16 +4,20 @@ Cleans up stale webhookconfigurations created by kyverno that were not cleanedup
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
kyvernoclient "github.com/kyverno/kyverno/pkg/client/clientset/versioned"
|
||||||
|
"github.com/kyverno/kyverno/pkg/config"
|
||||||
client "github.com/kyverno/kyverno/pkg/dclient"
|
client "github.com/kyverno/kyverno/pkg/dclient"
|
||||||
"github.com/kyverno/kyverno/pkg/signal"
|
"github.com/kyverno/kyverno/pkg/signal"
|
||||||
"github.com/kyverno/kyverno/pkg/utils"
|
"github.com/kyverno/kyverno/pkg/utils"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
|
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
rest "k8s.io/client-go/rest"
|
rest "k8s.io/client-go/rest"
|
||||||
clientcmd "k8s.io/client-go/tools/clientcmd"
|
clientcmd "k8s.io/client-go/tools/clientcmd"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
@ -63,6 +67,17 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pclientConfig, err := config.CreateClientConfig(kubeconfig, log.Log)
|
||||||
|
if err != nil {
|
||||||
|
setupLog.Error(err, "Failed to build client config")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
pclient, err := kyvernoclient.NewForConfig(pclientConfig)
|
||||||
|
if err != nil {
|
||||||
|
setupLog.Error(err, "Failed to create client")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
// Exit for unsupported version of kubernetes cluster
|
// Exit for unsupported version of kubernetes cluster
|
||||||
if !utils.HigherThanKubernetesVersion(client, log.Log, 1, 16, 0) {
|
if !utils.HigherThanKubernetesVersion(client, log.Log, 1, 16, 0) {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -88,8 +103,8 @@ func main() {
|
||||||
in := gen(done, stopCh, requests...)
|
in := gen(done, stopCh, requests...)
|
||||||
// process requests
|
// process requests
|
||||||
// processing routine count : 2
|
// processing routine count : 2
|
||||||
p1 := process(client, done, stopCh, in)
|
p1 := process(client, pclient, done, stopCh, in)
|
||||||
p2 := process(client, done, stopCh, in)
|
p2 := process(client, pclient, done, stopCh, in)
|
||||||
// merge results from processing routines
|
// merge results from processing routines
|
||||||
for err := range merge(done, stopCh, p1, p2) {
|
for err := range merge(done, stopCh, p1, p2) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -105,10 +120,10 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func executeRequest(client *client.Client, req request) error {
|
func executeRequest(client *client.Client, pclient *kyvernoclient.Clientset, req request) error {
|
||||||
switch req.kind {
|
switch req.kind {
|
||||||
case policyReportKind:
|
case policyReportKind:
|
||||||
return removePolicyReport(client, req.kind)
|
return removePolicyReport(client, pclient, req.kind)
|
||||||
case clusterPolicyReportKind:
|
case clusterPolicyReportKind:
|
||||||
return removeClusterPolicyReport(client, req.kind)
|
return removeClusterPolicyReport(client, req.kind)
|
||||||
case reportChangeRequestKind:
|
case reportChangeRequestKind:
|
||||||
|
@ -167,14 +182,14 @@ func gen(done <-chan struct{}, stopCh <-chan struct{}, requests ...request) <-ch
|
||||||
}
|
}
|
||||||
|
|
||||||
// processes the requests
|
// processes the requests
|
||||||
func process(client *client.Client, done <-chan struct{}, stopCh <-chan struct{}, requests <-chan request) <-chan error {
|
func process(client *client.Client, pclient *kyvernoclient.Clientset, done <-chan struct{}, stopCh <-chan struct{}, requests <-chan request) <-chan error {
|
||||||
logger := log.Log.WithName("process")
|
logger := log.Log.WithName("process")
|
||||||
out := make(chan error)
|
out := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(out)
|
defer close(out)
|
||||||
for req := range requests {
|
for req := range requests {
|
||||||
select {
|
select {
|
||||||
case out <- executeRequest(client, req):
|
case out <- executeRequest(client, pclient, req):
|
||||||
case <-done:
|
case <-done:
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
return
|
return
|
||||||
|
@ -231,12 +246,12 @@ func removeClusterPolicyReport(client *client.Client, kind string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, cpolr := range cpolrs.Items {
|
for _, cpolr := range cpolrs.Items {
|
||||||
deleteResource(client, cpolr.GetAPIVersion(), cpolr.GetKind(), "", cpolr.GetName(), nil)
|
deleteResource(client, cpolr.GetAPIVersion(), cpolr.GetKind(), "", cpolr.GetName())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func removePolicyReport(client *client.Client, kind string) error {
|
func removePolicyReport(client *client.Client, pclient *kyvernoclient.Clientset, kind string) error {
|
||||||
logger := log.Log.WithName("removePolicyReport")
|
logger := log.Log.WithName("removePolicyReport")
|
||||||
|
|
||||||
namespaces, err := client.ListResource("", "Namespace", "", nil)
|
namespaces, err := client.ListResource("", "Namespace", "", nil)
|
||||||
|
@ -245,21 +260,12 @@ func removePolicyReport(client *client.Client, kind string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// name of namespace policy report follows the name convention
|
|
||||||
// pr-ns-<namespace name>
|
|
||||||
for _, ns := range namespaces.Items {
|
for _, ns := range namespaces.Items {
|
||||||
reportNames := []string{
|
logger.Info("Removing policy reports", "namespace", ns.GetName())
|
||||||
fmt.Sprintf("policyreport-ns-%s", ns.GetName()),
|
err := pclient.Wgpolicyk8sV1alpha2().PolicyReports(ns.GetName()).DeleteCollection(context.TODO(), v1.DeleteOptions{}, v1.ListOptions{})
|
||||||
fmt.Sprintf("pr-ns-%s", ns.GetName()),
|
if err != nil {
|
||||||
fmt.Sprintf("polr-ns-%s", ns.GetName()),
|
logger.Error(err, "Failed to delete policy reports", "namespace", ns.GetName())
|
||||||
}
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
wg.Add(len(reportNames))
|
|
||||||
for _, reportName := range reportNames {
|
|
||||||
go deleteResource(client, "", kind, ns.GetName(), reportName, &wg)
|
|
||||||
}
|
|
||||||
wg.Wait()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -276,7 +282,7 @@ func removeReportChangeRequest(client *client.Client, kind string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, rcr := range rcrList.Items {
|
for _, rcr := range rcrList.Items {
|
||||||
deleteResource(client, rcr.GetAPIVersion(), rcr.GetKind(), rcr.GetNamespace(), rcr.GetName(), nil)
|
deleteResource(client, rcr.GetAPIVersion(), rcr.GetKind(), rcr.GetNamespace(), rcr.GetName())
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -290,7 +296,7 @@ func removeClusterReportChangeRequest(client *client.Client, kind string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, crcr := range crcrList.Items {
|
for _, crcr := range crcrList.Items {
|
||||||
deleteResource(client, crcr.GetAPIVersion(), crcr.GetKind(), "", crcr.GetName(), nil)
|
deleteResource(client, crcr.GetAPIVersion(), crcr.GetKind(), "", crcr.GetName())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -319,11 +325,7 @@ func getKyvernoNameSpace() string {
|
||||||
return kyvernoNamespace
|
return kyvernoNamespace
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteResource(client *client.Client, apiversion, kind, ns, name string, wg *sync.WaitGroup) {
|
func deleteResource(client *client.Client, apiversion, kind, ns, name string) {
|
||||||
if wg != nil {
|
|
||||||
defer wg.Done()
|
|
||||||
}
|
|
||||||
|
|
||||||
err := client.DeleteResource(apiversion, kind, ns, name, false)
|
err := client.DeleteResource(apiversion, kind, ns, name, false)
|
||||||
if err != nil && !errors.IsNotFound(err) {
|
if err != nil && !errors.IsNotFound(err) {
|
||||||
log.Log.Error(err, "failed to delete resource", "kind", kind, "name", name)
|
log.Log.Error(err, "failed to delete resource", "kind", kind, "name", name)
|
||||||
|
|
Loading…
Add table
Reference in a new issue