From 0f1d27f1e6d49110de53b3f7c55e54983d40096e Mon Sep 17 00:00:00 2001
From: Stefan Prodan <stefan.prodan@gmail.com>
Date: Thu, 11 Feb 2021 18:52:45 +0200
Subject: [PATCH] Remove network policies on uninstall

Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
---
 cmd/flux/uninstall.go       | 13 +++++++++++++
 docs/guides/installation.md |  1 +
 internal/utils/utils.go     |  2 ++
 3 files changed, 16 insertions(+)

diff --git a/cmd/flux/uninstall.go b/cmd/flux/uninstall.go
index 08e3290f..f6826b26 100644
--- a/cmd/flux/uninstall.go
+++ b/cmd/flux/uninstall.go
@@ -24,6 +24,7 @@ import (
 	"github.com/spf13/cobra"
 	appsv1 "k8s.io/api/apps/v1"
 	corev1 "k8s.io/api/core/v1"
+	networkingv1 "k8s.io/api/networking/v1"
 	rbacv1 "k8s.io/api/rbac/v1"
 	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -130,6 +131,18 @@ func uninstallComponents(ctx context.Context, kubeClient client.Client, namespac
 			}
 		}
 	}
+	{
+		var list networkingv1.NetworkPolicyList
+		if err := kubeClient.List(ctx, &list, client.InNamespace(namespace), selector); err == nil {
+			for _, r := range list.Items {
+				if err := kubeClient.Delete(ctx, &r, opts); err != nil {
+					logger.Failuref("NetworkPolicy/%s/%s deletion failed: %s", r.Namespace, r.Name, err.Error())
+				} else {
+					logger.Successf("NetworkPolicy/%s/%s deleted %s", r.Namespace, r.Name, dryRunStr)
+				}
+			}
+		}
+	}
 	{
 		var list corev1.ServiceAccountList
 		if err := kubeClient.List(ctx, &list, client.InNamespace(namespace), selector); err == nil {
diff --git a/docs/guides/installation.md b/docs/guides/installation.md
index 5608f19a..fc303e19 100644
--- a/docs/guides/installation.md
+++ b/docs/guides/installation.md
@@ -617,6 +617,7 @@ flux uninstall --namespace=flux-system
 The above command performs the following operations:
 
 - deletes Flux components (deployments and services)
+- deletes Flux network policies
 - deletes Flux RBAC (service accounts, cluster roles and cluster role bindings)
 - removes the Kubernetes finalizers from Flux custom resources
 - deletes Flux custom resource definitions and custom resources
diff --git a/internal/utils/utils.go b/internal/utils/utils.go
index 0acddf35..5e8f20d5 100644
--- a/internal/utils/utils.go
+++ b/internal/utils/utils.go
@@ -33,6 +33,7 @@ import (
 	"github.com/olekukonko/tablewriter"
 	appsv1 "k8s.io/api/apps/v1"
 	corev1 "k8s.io/api/core/v1"
+	networkingv1 "k8s.io/api/networking/v1"
 	rbacv1 "k8s.io/api/rbac/v1"
 	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
 	apiruntime "k8s.io/apimachinery/pkg/runtime"
@@ -169,6 +170,7 @@ func KubeClient(kubeConfigPath string, kubeContext string) (client.Client, error
 	_ = corev1.AddToScheme(scheme)
 	_ = rbacv1.AddToScheme(scheme)
 	_ = appsv1.AddToScheme(scheme)
+	_ = networkingv1.AddToScheme(scheme)
 	_ = sourcev1.AddToScheme(scheme)
 	_ = kustomizev1.AddToScheme(scheme)
 	_ = helmv2.AddToScheme(scheme)
-- 
GitLab