2019-11-18 19:41:37 +00:00
|
|
|
/*
|
|
|
|
Cleans up stale webhookconfigurations created by kyverno that were not cleanedup
|
|
|
|
*/
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-10-06 18:25:38 +00:00
|
|
|
"context"
|
2019-11-18 19:41:37 +00:00
|
|
|
"flag"
|
2020-03-17 18:05:20 +00:00
|
|
|
"fmt"
|
2019-11-18 19:41:37 +00:00
|
|
|
"os"
|
|
|
|
"sync"
|
2019-12-16 20:55:44 +00:00
|
|
|
"time"
|
2019-11-18 19:41:37 +00:00
|
|
|
|
2021-10-06 18:25:38 +00:00
|
|
|
kyvernoclient "github.com/kyverno/kyverno/pkg/client/clientset/versioned"
|
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
2020-10-07 18:12:31 +00:00
|
|
|
client "github.com/kyverno/kyverno/pkg/dclient"
|
|
|
|
"github.com/kyverno/kyverno/pkg/signal"
|
|
|
|
"github.com/kyverno/kyverno/pkg/utils"
|
2019-11-18 19:41:37 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
2021-10-06 18:25:38 +00:00
|
|
|
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2019-11-18 19:41:37 +00:00
|
|
|
rest "k8s.io/client-go/rest"
|
|
|
|
clientcmd "k8s.io/client-go/tools/clientcmd"
|
2021-02-08 04:26:56 +00:00
|
|
|
"k8s.io/klog/v2"
|
|
|
|
"k8s.io/klog/v2/klogr"
|
2020-03-17 23:25:34 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
2019-11-18 19:41:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
kubeconfig string
|
2020-03-17 18:05:20 +00:00
|
|
|
setupLog = log.Log.WithName("setup")
|
2019-11-18 19:41:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-12-04 03:19:36 +00:00
|
|
|
policyReportKind string = "PolicyReport"
|
|
|
|
clusterPolicyReportKind string = "ClusterPolicyReport"
|
|
|
|
reportChangeRequestKind string = "ReportChangeRequest"
|
|
|
|
clusterReportChangeRequestKind string = "ClusterReportChangeRequest"
|
|
|
|
policyViolation string = "PolicyViolation"
|
|
|
|
clusterPolicyViolation string = "ClusterPolicyViolation"
|
2019-11-18 19:41:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-03-17 23:25:34 +00:00
|
|
|
klog.InitFlags(nil)
|
|
|
|
log.SetLogger(klogr.New())
|
|
|
|
// arguments
|
|
|
|
flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
|
|
|
|
if err := flag.Set("v", "2"); err != nil {
|
|
|
|
klog.Fatalf("failed to set log level: %v", err)
|
|
|
|
}
|
2021-01-24 19:34:02 +00:00
|
|
|
|
2020-03-17 23:25:34 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2019-11-18 19:41:37 +00:00
|
|
|
// os signal handler
|
|
|
|
stopCh := signal.SetupSignalHandler()
|
|
|
|
// create client config
|
|
|
|
clientConfig, err := createClientConfig(kubeconfig)
|
|
|
|
if err != nil {
|
2020-03-17 18:05:20 +00:00
|
|
|
setupLog.Error(err, "Failed to build kubeconfig")
|
|
|
|
os.Exit(1)
|
2019-11-18 19:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DYNAMIC CLIENT
|
|
|
|
// - client for all registered resources
|
2020-05-28 02:51:34 +00:00
|
|
|
client, err := client.NewClient(clientConfig, 15*time.Minute, stopCh, log.Log)
|
2019-11-18 19:41:37 +00:00
|
|
|
if err != nil {
|
2020-03-17 18:05:20 +00:00
|
|
|
setupLog.Error(err, "Failed to create client")
|
|
|
|
os.Exit(1)
|
2019-11-18 19:41:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 18:25:38 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-02-15 02:12:28 +00:00
|
|
|
// Exit for unsupported version of kubernetes cluster
|
2021-06-08 20:14:28 +00:00
|
|
|
if !utils.HigherThanKubernetesVersion(client, log.Log, 1, 16, 0) {
|
2020-05-19 00:00:52 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-02-15 02:12:28 +00:00
|
|
|
|
2019-11-18 19:41:37 +00:00
|
|
|
requests := []request{
|
2020-11-09 19:26:12 +00:00
|
|
|
{policyReportKind, ""},
|
|
|
|
{clusterPolicyReportKind, ""},
|
2020-12-04 03:19:36 +00:00
|
|
|
|
|
|
|
{reportChangeRequestKind, ""},
|
|
|
|
{clusterReportChangeRequestKind, ""},
|
|
|
|
|
|
|
|
// clean up policy violation CRD
|
2020-11-09 19:26:12 +00:00
|
|
|
{policyViolation, ""},
|
|
|
|
{clusterPolicyViolation, ""},
|
2019-11-18 19:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
failure := false
|
|
|
|
// use pipline to pass request to cleanup resources
|
|
|
|
// generate requests
|
|
|
|
in := gen(done, stopCh, requests...)
|
|
|
|
// process requests
|
|
|
|
// processing routine count : 2
|
2021-10-06 18:25:38 +00:00
|
|
|
p1 := process(client, pclient, done, stopCh, in)
|
|
|
|
p2 := process(client, pclient, done, stopCh, in)
|
2019-11-18 19:41:37 +00:00
|
|
|
// merge results from processing routines
|
|
|
|
for err := range merge(done, stopCh, p1, p2) {
|
|
|
|
if err != nil {
|
|
|
|
failure = true
|
2020-03-17 18:05:20 +00:00
|
|
|
log.Log.Error(err, "failed to cleanup resource")
|
2019-11-18 19:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-24 19:34:02 +00:00
|
|
|
|
2019-11-18 19:41:37 +00:00
|
|
|
// if there is any failure then we fail process
|
|
|
|
if failure {
|
2021-01-24 19:34:02 +00:00
|
|
|
log.Log.Info("failed to cleanup prior configurations")
|
2019-11-18 19:41:37 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 18:25:38 +00:00
|
|
|
func executeRequest(client *client.Client, pclient *kyvernoclient.Clientset, req request) error {
|
2020-11-09 19:26:12 +00:00
|
|
|
switch req.kind {
|
|
|
|
case policyReportKind:
|
2021-10-06 18:25:38 +00:00
|
|
|
return removePolicyReport(client, pclient, req.kind)
|
2020-11-09 19:26:12 +00:00
|
|
|
case clusterPolicyReportKind:
|
|
|
|
return removeClusterPolicyReport(client, req.kind)
|
2020-12-04 03:19:36 +00:00
|
|
|
case reportChangeRequestKind:
|
|
|
|
return removeReportChangeRequest(client, req.kind)
|
|
|
|
case clusterReportChangeRequestKind:
|
|
|
|
return removeClusterReportChangeRequest(client, req.kind)
|
2020-11-09 19:26:12 +00:00
|
|
|
case policyViolation, clusterPolicyViolation:
|
|
|
|
return removeViolationCRD(client)
|
2019-11-18 19:41:37 +00:00
|
|
|
}
|
2021-01-24 19:34:02 +00:00
|
|
|
|
2019-11-18 19:41:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createClientConfig(kubeconfig string) (*rest.Config, error) {
|
2020-03-17 18:05:20 +00:00
|
|
|
logger := log.Log
|
2019-11-18 19:41:37 +00:00
|
|
|
if kubeconfig == "" {
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.Info("Using in-cluster configuration")
|
2019-11-18 19:41:37 +00:00
|
|
|
return rest.InClusterConfig()
|
|
|
|
}
|
2021-01-24 19:34:02 +00:00
|
|
|
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.Info(fmt.Sprintf("Using configuration from '%s'", kubeconfig))
|
2019-11-18 19:41:37 +00:00
|
|
|
return clientcmd.BuildConfigFromFlags("", kubeconfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
type request struct {
|
|
|
|
kind string
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Processing Pipeline
|
|
|
|
-> Process Requests
|
|
|
|
Generate Requests -> Process Requests -> Merge Results
|
|
|
|
-> Process Requests
|
|
|
|
- number of processes can be controlled
|
|
|
|
- stop processing on SIGTERM OR SIGNKILL signal
|
|
|
|
- stop processing if any process fails(supported)
|
|
|
|
*/
|
|
|
|
// Generates requests to be processed
|
|
|
|
func gen(done <-chan struct{}, stopCh <-chan struct{}, requests ...request) <-chan request {
|
|
|
|
out := make(chan request)
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
for _, req := range requests {
|
|
|
|
select {
|
|
|
|
case out <- req:
|
|
|
|
case <-done:
|
|
|
|
println("done generate")
|
|
|
|
return
|
|
|
|
case <-stopCh:
|
|
|
|
println("shutting down generate")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// processes the requests
|
2021-10-06 18:25:38 +00:00
|
|
|
func process(client *client.Client, pclient *kyvernoclient.Clientset, done <-chan struct{}, stopCh <-chan struct{}, requests <-chan request) <-chan error {
|
2020-03-17 18:05:20 +00:00
|
|
|
logger := log.Log.WithName("process")
|
2019-11-18 19:41:37 +00:00
|
|
|
out := make(chan error)
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
for req := range requests {
|
|
|
|
select {
|
2021-10-06 18:25:38 +00:00
|
|
|
case out <- executeRequest(client, pclient, req):
|
2019-11-18 19:41:37 +00:00
|
|
|
case <-done:
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.Info("done")
|
2019-11-18 19:41:37 +00:00
|
|
|
return
|
|
|
|
case <-stopCh:
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.Info("shutting down")
|
2019-11-18 19:41:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// waits for all processes to be complete and merges result
|
|
|
|
func merge(done <-chan struct{}, stopCh <-chan struct{}, processes ...<-chan error) <-chan error {
|
2020-03-17 18:05:20 +00:00
|
|
|
logger := log.Log.WithName("merge")
|
2019-11-18 19:41:37 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
out := make(chan error)
|
|
|
|
// gets the output from each process
|
|
|
|
output := func(ch <-chan error) {
|
|
|
|
defer wg.Done()
|
|
|
|
for err := range ch {
|
|
|
|
select {
|
|
|
|
case out <- err:
|
|
|
|
case <-done:
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.Info("done")
|
2019-11-18 19:41:37 +00:00
|
|
|
return
|
|
|
|
case <-stopCh:
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.Info("shutting down")
|
2019-11-18 19:41:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(len(processes))
|
|
|
|
for _, process := range processes {
|
|
|
|
go output(process)
|
|
|
|
}
|
|
|
|
|
|
|
|
// close when all the process goroutines are done
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(out)
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
2020-11-09 19:26:12 +00:00
|
|
|
|
|
|
|
func removeClusterPolicyReport(client *client.Client, kind string) error {
|
|
|
|
logger := log.Log.WithName("removeClusterPolicyReport")
|
|
|
|
|
|
|
|
cpolrs, err := client.ListResource("", kind, "", nil)
|
2021-01-22 02:58:53 +00:00
|
|
|
if err != nil {
|
2020-11-09 19:26:12 +00:00
|
|
|
logger.Error(err, "failed to list clusterPolicyReport")
|
2020-12-04 03:19:36 +00:00
|
|
|
return nil
|
2020-11-09 19:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, cpolr := range cpolrs.Items {
|
2021-10-06 18:25:38 +00:00
|
|
|
deleteResource(client, cpolr.GetAPIVersion(), cpolr.GetKind(), "", cpolr.GetName())
|
2020-11-09 19:26:12 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-06 18:25:38 +00:00
|
|
|
func removePolicyReport(client *client.Client, pclient *kyvernoclient.Clientset, kind string) error {
|
2020-11-09 19:26:12 +00:00
|
|
|
logger := log.Log.WithName("removePolicyReport")
|
|
|
|
|
|
|
|
namespaces, err := client.ListResource("", "Namespace", "", nil)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "failed to list namespaces")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ns := range namespaces.Items {
|
2021-10-06 18:25:38 +00:00
|
|
|
logger.Info("Removing policy reports", "namespace", ns.GetName())
|
|
|
|
err := pclient.Wgpolicyk8sV1alpha2().PolicyReports(ns.GetName()).DeleteCollection(context.TODO(), v1.DeleteOptions{}, v1.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "Failed to delete policy reports", "namespace", ns.GetName())
|
2020-12-04 03:19:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeReportChangeRequest(client *client.Client, kind string) error {
|
|
|
|
logger := log.Log.WithName("removeReportChangeRequest")
|
|
|
|
|
|
|
|
ns := getKyvernoNameSpace()
|
|
|
|
rcrList, err := client.ListResource("", kind, ns, nil)
|
2021-01-22 02:58:53 +00:00
|
|
|
if err != nil {
|
2020-12-04 03:19:36 +00:00
|
|
|
logger.Error(err, "failed to list reportChangeRequest")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rcr := range rcrList.Items {
|
2021-10-06 18:25:38 +00:00
|
|
|
deleteResource(client, rcr.GetAPIVersion(), rcr.GetKind(), rcr.GetNamespace(), rcr.GetName())
|
2020-11-09 19:26:12 +00:00
|
|
|
}
|
2020-12-21 19:04:19 +00:00
|
|
|
|
2020-12-04 03:19:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeClusterReportChangeRequest(client *client.Client, kind string) error {
|
|
|
|
crcrList, err := client.ListResource("", kind, "", nil)
|
2021-01-22 02:58:53 +00:00
|
|
|
if err != nil {
|
2020-12-09 17:29:52 +00:00
|
|
|
log.Log.Error(err, "failed to list clusterReportChangeRequest")
|
2020-12-04 03:19:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-09 19:26:12 +00:00
|
|
|
|
2020-12-04 03:19:36 +00:00
|
|
|
for _, crcr := range crcrList.Items {
|
2021-10-06 18:25:38 +00:00
|
|
|
deleteResource(client, crcr.GetAPIVersion(), crcr.GetKind(), "", crcr.GetName())
|
2020-12-04 03:19:36 +00:00
|
|
|
}
|
2020-11-09 19:26:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeViolationCRD(client *client.Client) error {
|
|
|
|
if err := client.DeleteResource("", "CustomResourceDefinition", "", "policyviolations.kyverno.io", false); err != nil {
|
|
|
|
if !errors.IsNotFound(err) {
|
2020-12-09 17:29:52 +00:00
|
|
|
log.Log.Error(err, "failed to delete CRD policyViolation")
|
2020-11-09 19:26:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := client.DeleteResource("", "CustomResourceDefinition", "", "clusterpolicyviolations.kyverno.io", false); err != nil {
|
|
|
|
if !errors.IsNotFound(err) {
|
2020-12-09 17:29:52 +00:00
|
|
|
log.Log.Error(err, "failed to delete CRD clusterPolicyViolation")
|
2020-11-09 19:26:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-04 03:19:36 +00:00
|
|
|
|
|
|
|
// getKubePolicyNameSpace - setting default KubePolicyNameSpace
|
|
|
|
func getKyvernoNameSpace() string {
|
|
|
|
kyvernoNamespace := os.Getenv("KYVERNO_NAMESPACE")
|
|
|
|
if kyvernoNamespace == "" {
|
|
|
|
kyvernoNamespace = "kyverno"
|
|
|
|
}
|
|
|
|
return kyvernoNamespace
|
|
|
|
}
|
2020-12-09 07:04:16 +00:00
|
|
|
|
2021-10-06 18:25:38 +00:00
|
|
|
func deleteResource(client *client.Client, apiversion, kind, ns, name string) {
|
2020-12-09 07:04:16 +00:00
|
|
|
err := client.DeleteResource(apiversion, kind, ns, name, false)
|
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
2020-12-09 17:29:52 +00:00
|
|
|
log.Log.Error(err, "failed to delete resource", "kind", kind, "name", name)
|
2020-12-09 07:04:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-09 17:29:52 +00:00
|
|
|
log.Log.Info("successfully cleaned up resource", "kind", kind, "name", name)
|
2020-12-09 07:04:16 +00:00
|
|
|
}
|