Skip to content

Commit

Permalink
fix: delete component also delete reports
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Mar 12, 2024
1 parent 1d32dc8 commit 2d9f1c7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 19 deletions.
5 changes: 3 additions & 2 deletions api/v1alpha1/rating.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import (
)

const (
RatingComponentLabel = "rating.component"
RatingRepositoryLabel = "rating.repository"
RatingComponentLabel = "rating.component"
RatingRepositoryLabel = "rating.repository"
RatingComponentVersion = "rating.version"

PipelineRun2RatingLabel = "rating.pipelinerun"
PipelineRun2ComponentLabel = "rating.pipelinerun.component"
Expand Down
61 changes: 44 additions & 17 deletions controllers/rating_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (r *RatingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
return reconcile.Result{}, err
}
if requeue {
return reconcile.Result{Requeue: true}, nil
return reconcile.Result{Requeue: requeue}, nil
}

// update status to false when rating is disabled
Expand Down Expand Up @@ -148,28 +148,55 @@ func (r RatingReconciler) updateLabels(ctx context.Context, instance *corev1alph
instance.Labels[corev1alpha1.RatingRepositoryLabel] = component.Labels[corev1alpha1.ComponentRepositoryLabel]
updateLabel = true
}
for _, p := range instance.Spec.PipelineParams {
shouldBreak := false
for _, pr := range p.Params {
if pr.Name == "VERSION" {
shouldBreak = true
if v, ok := instance.Labels[corev1alpha1.RatingComponentVersion]; !ok || v != pr.Value.StringVal {
instance.Labels[corev1alpha1.RatingComponentVersion] = pr.Value.StringVal
updateLabel = true
}
break
}
}
if shouldBreak {
break
}
}

if !updateLabel {
return false, nil
setOwner := true
for _, owner := range instance.OwnerReferences {
if owner.UID == component.UID {
setOwner = false
break
}
}
if setOwner {
_ = controllerutil.SetOwnerReference(component, instance, r.Scheme)
}

if err := r.Client.Update(ctx, instance); err != nil {
return false, err
if updateLabel || setOwner {
return true, r.Client.Update(ctx, instance)
}
// when update labels, we should patch a initial status
instanceDeepCopy := instance.DeepCopy()
instanceDeepCopy.Status.ConditionedStatus = corev1alpha1.ConditionedStatus{
Conditions: []corev1alpha1.Condition{
{
Status: v1.ConditionFalse,
LastTransitionTime: metav1.Now(),
Reason: corev1alpha1.ReasonCreated,
Message: "Rating is created.",
Type: corev1alpha1.TypeReady,

if len(instance.Status.Conditions) == 0 {
// when update labels, we should patch a initial status
instanceDeepCopy := instance.DeepCopy()
instanceDeepCopy.Status.ConditionedStatus = corev1alpha1.ConditionedStatus{
Conditions: []corev1alpha1.Condition{
{
Status: v1.ConditionFalse,
LastTransitionTime: metav1.Now(),
Reason: corev1alpha1.ReasonCreated,
Message: "Rating is created.",
Type: corev1alpha1.TypeReady,
},
},
},
}
return true, r.Client.Status().Patch(ctx, instanceDeepCopy, client.MergeFrom(instance))
}
return true, r.Client.Status().Patch(ctx, instanceDeepCopy, client.MergeFrom(instance))
return false, nil
}

func (r *RatingReconciler) CreatePipelineRun(logger logr.Logger, ctx context.Context, instance *corev1alpha1.Rating) error {
Expand Down

0 comments on commit 2d9f1c7

Please sign in to comment.