Skip to content

Commit 69a0af9

Browse files
authored
Merge pull request #401 from 0xff-dev/main
fix: delete component also delete reports
2 parents 1d32dc8 + 2d9f1c7 commit 69a0af9

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

api/v1alpha1/rating.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ import (
2525
)
2626

2727
const (
28-
RatingComponentLabel = "rating.component"
29-
RatingRepositoryLabel = "rating.repository"
28+
RatingComponentLabel = "rating.component"
29+
RatingRepositoryLabel = "rating.repository"
30+
RatingComponentVersion = "rating.version"
3031

3132
PipelineRun2RatingLabel = "rating.pipelinerun"
3233
PipelineRun2ComponentLabel = "rating.pipelinerun.component"

controllers/rating_controller.go

+44-17
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (r *RatingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
100100
return reconcile.Result{}, err
101101
}
102102
if requeue {
103-
return reconcile.Result{Requeue: true}, nil
103+
return reconcile.Result{Requeue: requeue}, nil
104104
}
105105

106106
// update status to false when rating is disabled
@@ -148,28 +148,55 @@ func (r RatingReconciler) updateLabels(ctx context.Context, instance *corev1alph
148148
instance.Labels[corev1alpha1.RatingRepositoryLabel] = component.Labels[corev1alpha1.ComponentRepositoryLabel]
149149
updateLabel = true
150150
}
151+
for _, p := range instance.Spec.PipelineParams {
152+
shouldBreak := false
153+
for _, pr := range p.Params {
154+
if pr.Name == "VERSION" {
155+
shouldBreak = true
156+
if v, ok := instance.Labels[corev1alpha1.RatingComponentVersion]; !ok || v != pr.Value.StringVal {
157+
instance.Labels[corev1alpha1.RatingComponentVersion] = pr.Value.StringVal
158+
updateLabel = true
159+
}
160+
break
161+
}
162+
}
163+
if shouldBreak {
164+
break
165+
}
166+
}
151167

152-
if !updateLabel {
153-
return false, nil
168+
setOwner := true
169+
for _, owner := range instance.OwnerReferences {
170+
if owner.UID == component.UID {
171+
setOwner = false
172+
break
173+
}
174+
}
175+
if setOwner {
176+
_ = controllerutil.SetOwnerReference(component, instance, r.Scheme)
154177
}
155178

156-
if err := r.Client.Update(ctx, instance); err != nil {
157-
return false, err
179+
if updateLabel || setOwner {
180+
return true, r.Client.Update(ctx, instance)
158181
}
159-
// when update labels, we should patch a initial status
160-
instanceDeepCopy := instance.DeepCopy()
161-
instanceDeepCopy.Status.ConditionedStatus = corev1alpha1.ConditionedStatus{
162-
Conditions: []corev1alpha1.Condition{
163-
{
164-
Status: v1.ConditionFalse,
165-
LastTransitionTime: metav1.Now(),
166-
Reason: corev1alpha1.ReasonCreated,
167-
Message: "Rating is created.",
168-
Type: corev1alpha1.TypeReady,
182+
183+
if len(instance.Status.Conditions) == 0 {
184+
// when update labels, we should patch a initial status
185+
instanceDeepCopy := instance.DeepCopy()
186+
instanceDeepCopy.Status.ConditionedStatus = corev1alpha1.ConditionedStatus{
187+
Conditions: []corev1alpha1.Condition{
188+
{
189+
Status: v1.ConditionFalse,
190+
LastTransitionTime: metav1.Now(),
191+
Reason: corev1alpha1.ReasonCreated,
192+
Message: "Rating is created.",
193+
Type: corev1alpha1.TypeReady,
194+
},
169195
},
170-
},
196+
}
197+
return true, r.Client.Status().Patch(ctx, instanceDeepCopy, client.MergeFrom(instance))
171198
}
172-
return true, r.Client.Status().Patch(ctx, instanceDeepCopy, client.MergeFrom(instance))
199+
return false, nil
173200
}
174201

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

0 commit comments

Comments
 (0)