From 08b7781373a280eaee56933f9ddfa72c32329fdb Mon Sep 17 00:00:00 2001 From: JameKeal <413621396@qq.com> Date: Wed, 9 Aug 2023 10:23:31 +0800 Subject: [PATCH] delete configmap when yurtstaticset is deleting --- .../yurtstaticset/yurtstaticset_controller.go | 25 ++++++- .../yurtstaticset_controller_test.go | 65 ++++++++++++++++++- 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/pkg/controller/yurtstaticset/yurtstaticset_controller.go b/pkg/controller/yurtstaticset/yurtstaticset_controller.go index 0d59909e3f8..c3469a05645 100644 --- a/pkg/controller/yurtstaticset/yurtstaticset_controller.go +++ b/pkg/controller/yurtstaticset/yurtstaticset_controller.go @@ -283,12 +283,18 @@ func (r *ReconcileYurtStaticSet) Reconcile(_ context.Context, request reconcile. // Fetch the YurtStaticSet instance instance := &appsv1alpha1.YurtStaticSet{} if err := r.Get(context.TODO(), request.NamespacedName, instance); err != nil { + // if the yurtStaticSet does not exist, delete the specified configmap if exist. + if kerr.IsNotFound(err) { + return reconcile.Result{}, r.deleteConfigMap(request.Name, request.Namespace) + } klog.Errorf("Fail to get YurtStaticSet %v, %v", request.NamespacedName, err) return ctrl.Result{}, client.IgnoreNotFound(err) } if instance.DeletionTimestamp != nil { - return reconcile.Result{}, nil + // handle the deletion event + // delete the configMap which is created by yurtStaticSet + return reconcile.Result{}, r.deleteConfigMap(request.Name, request.Namespace) } var ( @@ -541,3 +547,20 @@ func (r *ReconcileYurtStaticSet) updateYurtStaticSetStatus(instance *appsv1alpha return reconcile.Result{}, nil } + +// deleteConfigMap delete the configMap if YurtStaticSet is deleting +func (r *ReconcileYurtStaticSet) deleteConfigMap(name, namespace string) error { + cmName := util.WithConfigMapPrefix(name) + configMap := &corev1.ConfigMap{} + if err := r.Get(context.TODO(), types.NamespacedName{Name: cmName, Namespace: namespace}, configMap); err != nil { + if kerr.IsNotFound(err) { + return nil + } + return err + } + if err := r.Delete(context.TODO(), configMap, &client.DeleteOptions{}); err != nil { + return err + } + klog.Infof(Format("Delete ConfigMap %s from YurtStaticSet %s", configMap.Name, name)) + return nil +} diff --git a/pkg/controller/yurtstaticset/yurtstaticset_controller_test.go b/pkg/controller/yurtstaticset/yurtstaticset_controller_test.go index f46556561d2..76eece0d502 100644 --- a/pkg/controller/yurtstaticset/yurtstaticset_controller_test.go +++ b/pkg/controller/yurtstaticset/yurtstaticset_controller_test.go @@ -27,7 +27,7 @@ import ( "k8s.io/apimachinery/pkg/util/intstr" clientgoscheme "k8s.io/client-go/kubernetes/scheme" "sigs.k8s.io/controller-runtime/pkg/client" - fakeclint "sigs.k8s.io/controller-runtime/pkg/client/fake" + fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -113,7 +113,7 @@ func TestReconcile(t *testing.T) { for _, s := range strategy { instance.Spec.UpgradeStrategy = s - c := fakeclint.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(instance).WithObjects(staticPods...).WithObjects(nodes...).Build() + c := fakeclient.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(instance).WithObjects(staticPods...).WithObjects(nodes...).Build() var req = reconcile.Request{NamespacedName: types.NamespacedName{Namespace: metav1.NamespaceDefault, Name: TestStaticPodName}} rsp := ReconcileYurtStaticSet{ @@ -157,3 +157,64 @@ func Test_nodeTurnReady(t *testing.T) { } }) } + +func TestReconcileYurtStaticSetDeleteConfigMap(t *testing.T) { + staticPods := prepareStaticPods() + instance := &appsv1alpha1.YurtStaticSet{ + ObjectMeta: metav1.ObjectMeta{ + Name: TestStaticPodName, + Namespace: metav1.NamespaceDefault, + }, + Spec: appsv1alpha1.YurtStaticSetSpec{ + StaticPodManifest: "nginx", + Template: corev1.PodTemplateSpec{}, + }, + } + cmList := []client.Object{ + &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: "yurt-static-set-nginx", + Namespace: metav1.NamespaceDefault, + }, + }, + } + + scheme := runtime.NewScheme() + if err := appsv1alpha1.AddToScheme(scheme); err != nil { + t.Fatal("Fail to add yurt custom resource") + } + if err := clientgoscheme.AddToScheme(scheme); err != nil { + t.Fatal("Fail to add kubernetes clint-go custom resource") + } + c := fakeclient.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(instance).WithObjects(staticPods...).WithObjects(cmList...).Build() + + tests := []struct { + name string + yssName string + namespace string + wantErr bool + }{ + { + name: "test1", + yssName: TestStaticPodName, + namespace: metav1.NamespaceDefault, + wantErr: false, + }, + { + name: "test2", + yssName: TestStaticPodName, + namespace: metav1.NamespaceDefault, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := &ReconcileYurtStaticSet{ + Client: c, + } + if err := r.deleteConfigMap(tt.yssName, tt.namespace); (err != nil) != tt.wantErr { + t.Errorf("deleteConfigMap() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}