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: delete component also delete reports #401

Merged
merged 1 commit into from
Mar 12, 2024
Merged
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
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
Loading