@@ -22,6 +22,7 @@ import (
2222 "fmt"
2323 "net/http"
2424 "reflect"
25+ "strconv"
2526 "strings"
2627 "time"
2728
@@ -43,6 +44,7 @@ import (
4344 cmstore "github.com/kubesphere/ks-devops/pkg/store/configmap"
4445 storeInter "github.com/kubesphere/ks-devops/pkg/store/store"
4546 "github.com/kubesphere/ks-devops/pkg/utils/k8sutil"
47+ "github.com/kubesphere/ks-devops/pkg/utils/sliceutil"
4648)
4749
4850// BuildNotExistMsg indicates the build with pipelinerun-id not exist in jenkins
@@ -86,16 +88,54 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
8688
8789 // DeletionTimestamp.IsZero() means copyPipeline has not been deleted.
8890 if ! pipelineRunCopied .ObjectMeta .DeletionTimestamp .IsZero () {
89- if err = jHandler .deleteJenkinsJobHistory (pipelineRunCopied ); err != nil {
91+ // if the annotation value is true, we should keep the record in Jenkins
92+ if keep , err := strconv .ParseBool (pipelineRunCopied .Annotations [v1alpha3 .PipelineRunKeepJenkinsRecordAnnoKey ]); err == nil && keep {
93+ klog .V (4 ).Infof ("try to delete PipelineRun: %s/%s, but need to keep Jenkins record" ,
94+ pipelineRunCopied .Namespace , pipelineRunCopied .Name )
95+ } else if err = jHandler .deleteJenkinsJobHistory (pipelineRunCopied ); err != nil {
9096 klog .V (4 ).Infof ("failed to delete Jenkins job history from PipelineRun: %s/%s, error: %v" ,
9197 pipelineRunCopied .Namespace , pipelineRunCopied .Name , err )
92- } else {
93- k8sutil .RemoveFinalizer (& pipelineRunCopied .ObjectMeta , v1alpha3 .PipelineRunFinalizerName )
94- err = r .Update (context .TODO (), pipelineRunCopied )
9598 }
99+
100+ err = retry .RetryOnConflict (retry .DefaultRetry , func () error {
101+ // Get the latest version of PipelineRun
102+ if err := r .Get (ctx , req .NamespacedName , pipelineRunCopied ); err != nil {
103+ // ignore not found error
104+ return client .IgnoreNotFound (err )
105+ }
106+
107+ // Remove the finalizer
108+ pipelineRunCopied .ObjectMeta .Finalizers = sliceutil .RemoveString (pipelineRunCopied .ObjectMeta .Finalizers , func (item string ) bool {
109+ return item == v1alpha3 .PipelineRunFinalizerName
110+ })
111+
112+ // Try to update
113+ return r .Update (ctx , pipelineRunCopied )
114+ })
115+
96116 return ctrl.Result {}, err
97117 }
98118
119+ // add finalizer if it does not exist
120+ if ! sliceutil .HasString (pipelineRunCopied .ObjectMeta .Finalizers , v1alpha3 .PipelineRunFinalizerName ) {
121+ err = retry .RetryOnConflict (retry .DefaultRetry , func () error {
122+ // Get the latest version of PipelineRun
123+ if err := r .Get (ctx , req .NamespacedName , pipelineRunCopied ); err != nil {
124+ return err
125+ }
126+ // Add the finalizer if it's still not there
127+ if ! sliceutil .HasString (pipelineRunCopied .ObjectMeta .Finalizers , v1alpha3 .PipelineRunFinalizerName ) {
128+ pipelineRunCopied .ObjectMeta .Finalizers = append (pipelineRunCopied .ObjectMeta .Finalizers , v1alpha3 .PipelineRunFinalizerName )
129+ // Try to update
130+ return r .Update (ctx , pipelineRunCopied )
131+ }
132+ return nil // Finalizer was added by another process, no need to update
133+ })
134+ if err != nil {
135+ return ctrl.Result {}, err
136+ }
137+ }
138+
99139 // the PipelineRun cannot allow building
100140 if ! pipelineRunCopied .Buildable () {
101141 return ctrl.Result {}, nil
0 commit comments