Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete configmap when yurtstaticset is deleting #1640

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion pkg/controller/yurtstaticset/yurtstaticset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
65 changes: 63 additions & 2 deletions pkg/controller/yurtstaticset/yurtstaticset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}
})
}
}