Skip to content

Commit

Permalink
Fix reconcile json edit mode (#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
chilianyi authored Feb 15, 2023
1 parent b0ed588 commit 1c85b3b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rules:
- list
- update
- watch
- create
- apiGroups:
- ""
resources:
Expand Down
36 changes: 29 additions & 7 deletions controllers/jenkins/pipeline/json_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (r *JenkinsfileReconciler) Reconcile(ctx context.Context, req ctrl.Request)
case v1alpha3.PipelineJenkinsfileEditModeRaw:
result, err = r.reconcileJenkinsfileEditMode(pip, req.NamespacedName, coreClient)
case v1alpha3.PipelineJenkinsfileEditModeJSON:
result, err = r.reconcileJSONEditMode(pip, coreClient)
result, err = r.reconcileJSONEditMode(pip, req.NamespacedName, coreClient)
case "":
// Reconcile pipeline version <= v3.3.2
if _, ok := pip.Annotations[v1alpha3.PipelineJenkinsfileValueAnnoKey]; !ok {
Expand Down Expand Up @@ -142,23 +142,45 @@ func (r *JenkinsfileReconciler) updateAnnotations(annotations map[string]string,
})
}

func (r *JenkinsfileReconciler) reconcileJSONEditMode(pip *v1alpha3.Pipeline, coreClient core.Client) (
func (r *JenkinsfileReconciler) reconcileJSONEditMode(pip *v1alpha3.Pipeline, pipelineKey client.ObjectKey, coreClient core.Client) (
result ctrl.Result, err error) {
var jsonData string
if jsonData = pip.Annotations[v1alpha3.PipelineJenkinsfileValueAnnoKey]; jsonData != "" {
var toResult core.GenericResult
if toResult, err = coreClient.ToJenkinsfile(jsonData); err != nil || toResult.GetStatus() != "success" {
err = fmt.Errorf("failed to convert JSON format to Jenkinsfile, error: %v", err)
r.log.Error(err, "failed to convert json format to Jenkinsfile")
pip.Annotations[v1alpha3.PipelineJenkinsfileEditModeAnnoKey] = ""
pip.Annotations[v1alpha3.PipelineJenkinsfileValidateAnnoKey] = v1alpha3.PipelineJenkinsfileValidateFailure
err = r.updateAnnotations(pip.Annotations, pipelineKey)
return
}

pip.Annotations[v1alpha3.PipelineJenkinsfileEditModeAnnoKey] = ""
pip.Spec.Pipeline.Jenkinsfile = toResult.GetResult()
err = r.Update(context.Background(), pip)
pip.Annotations[v1alpha3.PipelineJenkinsfileValidateAnnoKey] = v1alpha3.PipelineJenkinsfileValidateSuccess
err = r.updateAnnotationsAndJenkinsfile(pip.Annotations, toResult.GetResult(), pipelineKey)
}
return
}

func (r *JenkinsfileReconciler) updateAnnotationsAndJenkinsfile(annotations map[string]string, jenkinsfile string, pipelineKey client.ObjectKey) error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
pipeline := &v1alpha3.Pipeline{}
if err := r.Get(context.Background(), pipelineKey, pipeline); err != nil {
return client.IgnoreNotFound(err)
}
if pipeline.Annotations[v1alpha3.PipelineJenkinsfileValidateAnnoKey] == annotations[v1alpha3.PipelineJenkinsfileValidateAnnoKey] &&
pipeline.Annotations[v1alpha3.PipelineJenkinsfileEditModeAnnoKey] == annotations[v1alpha3.PipelineJenkinsfileEditModeAnnoKey] &&
pipeline.Spec.Pipeline.Jenkinsfile == jenkinsfile {
return nil
}

// update annotations
pipeline.Annotations[v1alpha3.PipelineJenkinsfileEditModeAnnoKey] = annotations[v1alpha3.PipelineJenkinsfileEditModeAnnoKey]
pipeline.Annotations[v1alpha3.PipelineJenkinsfileValidateAnnoKey] = annotations[v1alpha3.PipelineJenkinsfileValidateAnnoKey]
pipeline.Spec.Pipeline.Jenkinsfile = jenkinsfile
return r.Update(context.Background(), pipeline)
})
}

// GetName returns the name of this controller
func (r *JenkinsfileReconciler) GetName() string {
return "JenkinsfileController"
Expand Down Expand Up @@ -193,7 +215,7 @@ var jenkinsfilePredicate = predicate.Funcs{
oldPipeline, okOld := ue.ObjectOld.(*v1alpha3.Pipeline)
newPipeline, okNew := ue.ObjectNew.(*v1alpha3.Pipeline)
if okOld && okNew {
if !reflect.DeepEqual(oldPipeline.Spec.Pipeline, newPipeline.Spec.Pipeline) {
if oldPipeline.Annotations[v1alpha3.PipelineJenkinsfileEditModeAnnoKey] != newPipeline.Annotations[v1alpha3.PipelineJenkinsfileEditModeAnnoKey] {
return true
}
}
Expand Down

0 comments on commit 1c85b3b

Please sign in to comment.