Skip to content

Commit

Permalink
Fix targetnamespace deletion
Browse files Browse the repository at this point in the history
This will add the fix for targetnamespace deletion
it will delete the namespace only if namespace contains
ownerreference of same reconcilation

Signed-off-by: Shiv Verma<[email protected]>
  • Loading branch information
pratap0007 committed Feb 8, 2024
1 parent 66069b9 commit 6b32b71
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pkg/reconciler/common/targetnamespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,20 @@ func ReconcileTargetNamespace(ctx context.Context, labels map[string]string, tek
_targetNamespace := namespace.DeepCopy()
targetNamespace = _targetNamespace
} else {
// delete irrelevant namespaces
// delete irrelevant namespaces if the owner is the same component
// if deletionTimestamp is not nil, that indicates, the namespace is in deletion state
if namespace.DeletionTimestamp == nil {
ownerReferenceName := namespace.GetOwnerReferences()[0].Name
if namespace.DeletionTimestamp == nil && ownerReferenceName == tektonComponent.GetName() {
if err := kubeClientSet.CoreV1().Namespaces().Delete(ctx, namespace.Name, metav1.DeleteOptions{}); err != nil {
logger.Errorw("error on deleting a namespace",
"namespace", namespace.Name,
err,
)
return err
}
} else {
logger.Infof("'%v' namespace is in deletion state", namespace.Name)
}
if namespace.DeletionTimestamp != nil {
logger.Debugf("'%v' namespace is in deletion state", namespace.Name)
namespaceDeletionInProgress = true
}
}
Expand Down
35 changes: 35 additions & 0 deletions pkg/reconciler/common/targetnamespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ func TestReconcileTargetNamespace(t *testing.T) {
},
err: nil,
},
{
name: "verify-custom-target-namespace-tekton-hub",
component: &v1alpha1.TektonHub{
ObjectMeta: metav1.ObjectMeta{Name: "hub"},
Spec: v1alpha1.TektonHubSpec{CommonSpec: v1alpha1.CommonSpec{TargetNamespace: "custom"}},
},
err: nil,
},
{
name: "verify-custom-target-namespace",
component: &v1alpha1.TektonConfig{
Expand Down Expand Up @@ -110,6 +118,32 @@ func TestReconcileTargetNamespace(t *testing.T) {
Labels: map[string]string{
labelKeyTargetNamespace: "true",
},
OwnerReferences: []metav1.OwnerReference{{Name: "config"}},
}}
_, err := fakeClientset.CoreV1().Namespaces().Create(context.TODO(), namespace, metav1.CreateOptions{})
assert.NilError(t, err)
},
postFunc: func(t *testing.T, fakeClientset *fake.Clientset, namespace *corev1.Namespace) {
// verify "custom-ns" is removed
_, err := fakeClientset.CoreV1().Namespaces().Get(context.TODO(), "custom-ns", metav1.GetOptions{})
assert.Equal(t, true, errors.IsNotFound(err), "'custom-ns' namespace should be deleted, but still found")
},
err: nil,
},
{
name: "verify-existing-non-target-namespace-deleted",
component: &v1alpha1.TektonConfig{
ObjectMeta: metav1.ObjectMeta{Name: "hub"},
Spec: v1alpha1.TektonConfigSpec{CommonSpec: v1alpha1.CommonSpec{TargetNamespace: "hello123"}},
},
preFunc: func(t *testing.T, fakeClientset *fake.Clientset) {
// create a namespace with different name and with "operator.tekton.dev/targetNamespace" label
namespace := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{
Name: "custom-ns",
Labels: map[string]string{
labelKeyTargetNamespace: "true",
},
OwnerReferences: []metav1.OwnerReference{{Name: "hub"}},
}}
_, err := fakeClientset.CoreV1().Namespaces().Create(context.TODO(), namespace, metav1.CreateOptions{})
assert.NilError(t, err)
Expand Down Expand Up @@ -152,6 +186,7 @@ func TestReconcileTargetNamespace(t *testing.T) {
Labels: map[string]string{
labelKeyTargetNamespace: "true",
},
OwnerReferences: []metav1.OwnerReference{{Name: "config"}},
DeletionTimestamp: &metav1.Time{Time: time.Now()},
}}
_, err := fakeClientset.CoreV1().Namespaces().Create(context.TODO(), namespace, metav1.CreateOptions{})
Expand Down

0 comments on commit 6b32b71

Please sign in to comment.