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

fix: avoid panic when reconciling namespace without owner reference #2516

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion pkg/reconciler/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func PipelineReady(informer informer.TektonPipelineInformer) (*v1alpha1.TektonPi
}
return nil, err
}
if ppln.GetStatus() != nil && strings.Contains(ppln.GetStatus().GetCondition(apis.ConditionReady).Message, v1alpha1.UpgradePending) {
if isUpgradePending(ppln.GetStatus()) {
return nil, v1alpha1.DEPENDENCY_UPGRADE_PENDING_ERR
}
if !ppln.Status.IsReady() {
Expand All @@ -52,6 +52,18 @@ func PipelineReady(informer informer.TektonPipelineInformer) (*v1alpha1.TektonPi
return ppln, nil
}

// isUpgradePending checks if the component status indicates an upgrade is pending
func isUpgradePending(status v1alpha1.TektonComponentStatus) bool {
if status == nil {
return false
}
readyCondition := status.GetCondition(apis.ConditionReady)
if readyCondition == nil {
return false
}
return strings.Contains(readyCondition.Message, v1alpha1.UpgradePending)
}

func PipelineTargetNamspace(informer informer.TektonPipelineInformer) (string, error) {
ppln, err := getPipelineRes(informer)
if err != nil {
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I manually created the ns, which resulted in entering here, causing a panic.

I understand that we should not panic under any circumstances.

// 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
Loading