Skip to content

Commit

Permalink
fix: avoid panic when reconciling namespace without owner reference
Browse files Browse the repository at this point in the history
  • Loading branch information
l-qing committed Jan 20, 2025
1 parent f547f09 commit 23a76a7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/reconciler/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func PipelineReady(informer informer.TektonPipelineInformer) (*v1alpha1.TektonPi
}
return nil, err
}
if ppln.GetStatus() != nil && strings.Contains(ppln.GetStatus().GetCondition(apis.ConditionReady).Message, v1alpha1.UpgradePending) {
readyCondition := ppln.GetStatus().GetCondition(apis.ConditionReady)
if readyCondition != nil && strings.Contains(readyCondition.Message, v1alpha1.UpgradePending) {
return nil, v1alpha1.DEPENDENCY_UPGRADE_PENDING_ERR
}
if !ppln.Status.IsReady() {
Expand Down
4 changes: 3 additions & 1 deletion pkg/reconciler/common/targetnamespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func ReconcileTargetNamespace(ctx context.Context, labels map[string]string, ann
if namespace.Name == tektonComponent.GetSpec().GetTargetNamespace() && namespace.DeletionTimestamp == nil {
_targetNamespace := namespace.DeepCopy()
targetNamespace = _targetNamespace
} else {
} else if len(namespace.GetOwnerReferences()) > 0 {
// delete irrelevant namespaces if the owner is the same component
// if deletionTimestamp is not nil, that indicates, the namespace is in deletion state
ownerReferenceName := namespace.GetOwnerReferences()[0].Name
Expand All @@ -73,6 +73,8 @@ func ReconcileTargetNamespace(ctx context.Context, labels map[string]string, ann
logger.Debugf("'%v' namespace is in deletion state", namespace.Name)
namespaceDeletionInProgress = true
}
} else {
logger.Infof("'%v' namespace is not owned by any component", namespace.Name)
}
}

Expand Down
26 changes: 26 additions & 0 deletions pkg/reconciler/common/targetnamespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,32 @@ func TestReconcileTargetNamespace(t *testing.T) {
// ReconcileTargetNamespace requeue event for the namespace is in deletion state
err: v1alpha1.REQUEUE_EVENT_AFTER,
},
{
name: "verify-namespace-in-deletion-state-without-owner-reference",
component: &v1alpha1.TektonConfig{
ObjectMeta: metav1.ObjectMeta{Name: "config"},
Spec: v1alpha1.TektonConfigSpec{CommonSpec: v1alpha1.CommonSpec{TargetNamespace: namespaceTektonPipelines}},
},
additionalLabels: map[string]string{},
ownerReferences: nil,
preFunc: func(t *testing.T, fakeClientset *fake.Clientset) {
// create a namespace with deletionTimestamp, it means the namespace is in deletion state
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceTektonPipelines,
Labels: map[string]string{
labelKeyTargetNamespace: "true",
},
OwnerReferences: nil,
DeletionTimestamp: &metav1.Time{Time: time.Now()},
},
}
_, err := fakeClientset.CoreV1().Namespaces().Create(context.TODO(), namespace, metav1.CreateOptions{})
assert.NilError(t, err)
},
// ReconcileTargetNamespace requeue event for the namespace is in deletion state
err: v1alpha1.REQUEUE_EVENT_AFTER,
},
{
name: "verify-existing-namespace",
component: &v1alpha1.TektonConfig{
Expand Down

0 comments on commit 23a76a7

Please sign in to comment.