Cleanup services, deployments, mutating webhook configurations, secrets on every test startup
@@ -283,8 +283,12 @@ func NewK8sWithoutRoles(prepare bool) (*K8s, error) {
start := time.Now()
client.Prepare("nsmgr", "nsmd", "vppagent", "vpn", "icmp", "nsc", "source", "dest")
client.CleanupCRDs()
+ client.CleanupServices("nsm-admission-webhook-svc")
+ client.CleanupDeployments()
+ client.CleanupMutatingWebhookConfigurations()
+ client.CleanupSecrets("nsm-admission-webhook-certs")
client.CleanupConfigMaps()
- nsmrbac.DeleteAllRoles(client.clientset)
+ _ = nsmrbac.DeleteAllRoles(client.clientset)
logrus.Printf("Cleanup done: %v", time.Since(start))
}
return &client, nil
@@ -630,6 +634,12 @@ func (o *K8s) DeleteService(service *v1.Service, namespace string) error {
return o.clientset.CoreV1().Services(namespace).Delete(service.GetName(), &metaV1.DeleteOptions{})
}
+func (o *K8s) CleanupServices(services ...string) {
+ for _, s := range services {
+ _ = o.clientset.CoreV1().Services(o.namespace).Delete(s, &metaV1.DeleteOptions{})
+ }
+}
+
func (o *K8s) CreateDeployment(deployment *appsv1.Deployment, namespace string) (*appsv1.Deployment, error) {
d, err := o.clientset.AppsV1().Deployments(namespace).Create(deployment)
if err != nil {
@@ -643,6 +653,13 @@ func (o *K8s) DeleteDeployment(deployment *appsv1.Deployment, namespace string)
return o.clientset.AppsV1().Deployments(namespace).Delete(deployment.GetName(), &metaV1.DeleteOptions{})
}
+func (o *K8s) CleanupDeployments() {
+ deployments, _ := o.clientset.AppsV1().Deployments(o.namespace).List(metaV1.ListOptions{})
+ for _, d := range deployments.Items {
+ _ = o.DeleteDeployment(&d, o.namespace)
+ }
+}
+
func (o *K8s) CreateMutatingWebhookConfiguration(mutatingWebhookConf *arv1beta1.MutatingWebhookConfiguration) (*arv1beta1.MutatingWebhookConfiguration, error) {
awc, err := o.clientset.AdmissionregistrationV1beta1().MutatingWebhookConfigurations().Create(mutatingWebhookConf)
if err != nil {
@@ -656,6 +673,13 @@ func (o *K8s) DeleteMutatingWebhookConfiguration(mutatingWebhookConf *arv1beta1.
return o.clientset.AdmissionregistrationV1beta1().MutatingWebhookConfigurations().Delete(mutatingWebhookConf.GetName(), &metaV1.DeleteOptions{})
}
+func (o *K8s) CleanupMutatingWebhookConfigurations() {
+ mwConfigs, _ := o.clientset.AdmissionregistrationV1beta1().MutatingWebhookConfigurations().List(metaV1.ListOptions{})
+ for _, mwConfig := range mwConfigs.Items {
+ _ = o.DeleteMutatingWebhookConfiguration(&mwConfig)
+ }
+}
+
func (o *K8s) CreateSecret(secret *v1.Secret, namespace string) (*v1.Secret, error) {
s, err := o.clientset.CoreV1().Secrets(namespace).Create(secret)
if err != nil {
@@ -669,6 +693,12 @@ func (o *K8s) DeleteSecret(name string, namespace string) error {
return o.clientset.CoreV1().Secrets(namespace).Delete(name, &metaV1.DeleteOptions{})
}
+func (o *K8s) CleanupSecrets(secrets ...string) {
+ for _, s := range secrets {
+ _ = o.DeleteSecret(s, o.namespace)
+ }
+}
+
func (o *K8s) IsPodReady(pod *v1.Pod) bool {
return isPodReady(pod)
}
