2019-11-18 19:41:37 +00:00
|
|
|
/*
|
|
|
|
Cleans up stale webhookconfigurations created by kyverno that were not cleanedup
|
|
|
|
*/
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
|
2020-10-07 18:12:31 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
|
|
|
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"
|
|
|
|
rest "k8s.io/client-go/rest"
|
|
|
|
clientcmd "k8s.io/client-go/tools/clientcmd"
|
2020-03-17 23:25:34 +00:00
|
|
|
"k8s.io/klog"
|
|
|
|
"k8s.io/klog/klogr"
|
|
|
|
"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
|
|
|
mutatingWebhookConfigKind string = "MutatingWebhookConfiguration"
|
|
|
|
validatingWebhookConfigKind string = "ValidatingWebhookConfiguration"
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-15 02:12:28 +00:00
|
|
|
// Exit for unsupported version of kubernetes cluster
|
2020-11-03 06:14:36 +00:00
|
|
|
if !utils.HigherThanKubernetesVersion(client, log.Log, 1, 14, 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-02-06 02:44:35 +00:00
|
|
|
{validatingWebhookConfigKind, config.ValidatingWebhookConfigurationName},
|
|
|
|
{validatingWebhookConfigKind, config.ValidatingWebhookConfigurationDebugName},
|
2020-02-03 21:38:24 +00:00
|
|
|
{mutatingWebhookConfigKind, config.MutatingWebhookConfigurationName},
|
|
|
|
{mutatingWebhookConfigKind, config.MutatingWebhookConfigurationDebugName},
|
2020-12-04 03:19:36 +00:00
|
|
|
|
2020-02-03 21:38:24 +00:00
|
|
|
{validatingWebhookConfigKind, config.PolicyValidatingWebhookConfigurationName},
|
|
|
|
{validatingWebhookConfigKind, config.PolicyValidatingWebhookConfigurationDebugName},
|
|
|
|
{mutatingWebhookConfigKind, config.PolicyMutatingWebhookConfigurationName},
|
|
|
|
{mutatingWebhookConfigKind, config.PolicyMutatingWebhookConfigurationDebugName},
|
2020-12-04 03:19:36 +00:00
|
|
|
|
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
|
|
|
|
p1 := process(client, done, stopCh, in)
|
|
|
|
p2 := process(client, done, stopCh, in)
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 19:26:12 +00:00
|
|
|
func executeRequest(client *client.Client, req request) error {
|
|
|
|
switch req.kind {
|
|
|
|
case mutatingWebhookConfigKind, validatingWebhookConfigKind:
|
|
|
|
return removeWebhookIfExists(client, req.kind, req.name)
|
|
|
|
case policyReportKind:
|
|
|
|
return removePolicyReport(client, req.kind)
|
|
|
|
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
|
|
|
|
func process(client *client.Client, 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 {
|
2020-11-09 19:26:12 +00:00
|
|
|
case out <- executeRequest(client, 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 removeWebhookIfExists(client *client.Client, kind string, name string) error {
|
|
|
|
logger := log.Log.WithName("removeExistingWebhook").WithValues("kind", kind, "name", name)
|
|
|
|
var err error
|
|
|
|
// Get resource
|
|
|
|
_, err = client.GetResource("", kind, "", name)
|
|
|
|
if errors.IsNotFound(err) {
|
|
|
|
logger.V(4).Info("resource not found")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "failed to get resource")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Delete resource
|
|
|
|
err = client.DeleteResource("", kind, "", name, false)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "failed to delete resource")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logger.Info("removed the resource")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-12-09 07:04:16 +00:00
|
|
|
deleteResource(client, cpolr.GetAPIVersion(), cpolr.GetKind(), "", cpolr.GetName(), nil)
|
2020-11-09 19:26:12 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removePolicyReport(client *client.Client, kind string) error {
|
|
|
|
logger := log.Log.WithName("removePolicyReport")
|
|
|
|
|
|
|
|
namespaces, err := client.ListResource("", "Namespace", "", nil)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "failed to list namespaces")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// name of namespace policy report follows the name convention
|
2020-12-04 03:19:36 +00:00
|
|
|
// pr-ns-<namespace name>
|
2020-11-09 19:26:12 +00:00
|
|
|
for _, ns := range namespaces.Items {
|
2020-12-04 03:19:36 +00:00
|
|
|
reportNames := []string{
|
|
|
|
fmt.Sprintf("policyreport-ns-%s", ns.GetName()),
|
|
|
|
fmt.Sprintf("pr-ns-%s", ns.GetName()),
|
2021-01-05 07:17:17 +00:00
|
|
|
fmt.Sprintf("polr-ns-%s", ns.GetName()),
|
2020-12-04 03:19:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 07:04:16 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(reportNames))
|
2020-12-04 03:19:36 +00:00
|
|
|
for _, reportName := range reportNames {
|
2020-12-09 07:04:16 +00:00
|
|
|
go deleteResource(client, "", kind, ns.GetName(), reportName, &wg)
|
2020-12-04 03:19:36 +00:00
|
|
|
}
|
2020-12-09 07:04:16 +00:00
|
|
|
wg.Wait()
|
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 {
|
2020-12-21 19:04:19 +00:00
|
|
|
deleteResource(client, rcr.GetAPIVersion(), rcr.GetKind(), rcr.GetNamespace(), rcr.GetName(), nil)
|
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 {
|
2020-12-09 07:04:16 +00:00
|
|
|
deleteResource(client, crcr.GetAPIVersion(), crcr.GetKind(), "", crcr.GetName(), nil)
|
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
|
|
|
|
|
|
|
func deleteResource(client *client.Client, apiversion, kind, ns, name string, wg *sync.WaitGroup) {
|
|
|
|
if wg != nil {
|
|
|
|
defer wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|