// validateBuildRunOwnerReferences defines or removes the ownerReference for the BuildRun based on
// an annotation value
validateBuildRunOwnerReferences defines or removes the ownerReference for the BuildRun based on
an annotation value
// validateBuildRunOwnerReferences defines or removes the ownerReference for the BuildRun based on // an annotation value
func (r *ReconcileBuild) validateBuildRunOwnerReferences(ctx context.Context, b *build.Build) error {
	buildRunList, err := r.retrieveBuildRunsfromBuild(ctx, b)
	if err != nil {
		return err
	}
	switch b.GetAnnotations()[build.AnnotationBuildRunDeletion] {
	case "true":
		for _, buildRun := range buildRunList.Items {
			if index := r.validateBuildOwnerReference(buildRun.OwnerReferences, b); index == -1 {
				if err := r.setOwnerReferenceFunc(b, &buildRun, r.scheme); err != nil {
					b.Status.Reason = fmt.Sprintf("unexpected error when trying to set the ownerreference: %v", err)
					if err := r.client.Status().Update(ctx, b); err != nil {
						return err
					}
				}
				if err = r.client.Update(ctx, &buildRun); err != nil {
					return err
				}
				ctxlog.Info(ctx, fmt.Sprintf("successfully updated BuildRun %s", buildRun.Name), namespace, buildRun.Namespace, name, buildRun.Name)
			}
		}
	case "", "false":
		for _, buildRun := range buildRunList.Items {
			if index := r.validateBuildOwnerReference(buildRun.OwnerReferences, b); index != -1 {
				buildRun.OwnerReferences = removeOwnerReferenceByIndex(buildRun.OwnerReferences, index)
				if err := r.client.Update(ctx, &buildRun); err != nil {
					return err
				}
				ctxlog.Info(ctx, fmt.Sprintf("successfully updated BuildRun %s", buildRun.Name), namespace, buildRun.Namespace, name, buildRun.Name)
			}
		}
	default:
		ctxlog.Info(ctx, fmt.Sprintf("the annotation %s was not properly defined, supported values are true or false", build.AnnotationBuildRunDeletion), namespace, b.Namespace, name, b.Name)
		return fmt.Errorf("the annotation %s was not properly defined, supported values are true or false", build.AnnotationBuildRunDeletion)
	}
	return nil
}
func ( r * ReconcileBuild ) validateBuildRunOwnerReferences ( ctx context . Context , b * build . Build ) error { buildRunList , err := r . retrieveBuildRunsfromBuild ( ctx , b ) if err != nil { return err } switch b . GetAnnotations ( ) [ build . AnnotationBuildRunDeletion ] { case " " : for _ , buildRun := range buildRunList . Items { if index := r . validateBuildOwnerReference ( buildRun . OwnerReferences , b ) ; index == - 1 { if err := r . setOwnerReferenceFunc ( b , & buildRun , r . scheme ) ; err != nil { b . Status . Reason = fmt . Sprintf ( " " , err ) if err := r . client . Status ( ) . Update ( ctx , b ) ; err != nil { return err } } if err = r . client . Update ( ctx , & buildRun ) ; err != nil { return err } ctxlog . Info ( ctx , fmt . Sprintf ( " " , buildRun . Name ) , namespace , buildRun . Namespace , name , buildRun . Name ) } } case " " , " " : for _ , buildRun := range buildRunList . Items { if index := r . validateBuildOwnerReference ( buildRun . OwnerReferences , b ) ; index != - 1 { buildRun . OwnerReferences = removeOwnerReferenceByIndex ( buildRun . OwnerReferences , index ) if err := r . client . Update ( ctx , & buildRun ) ; err != nil { return err } ctxlog . Info ( ctx , fmt . Sprintf ( " " , buildRun . Name ) , namespace , buildRun . Namespace , name , buildRun . Name ) } } default : ctxlog . Info ( ctx , fmt . Sprintf ( " " , build . AnnotationBuildRunDeletion ) , namespace , b . Namespace , name , b . Name ) return fmt . Errorf ( " " , build . AnnotationBuildRunDeletion ) } return nil }
validateBuildRunOwnerReferences defines or removes the ownerReference for the BuildRun based on
an annotation value
validateBuildRunOwnerReferences defines or removes the ownerReference for the BuildRun based on an annotation value
// if the buildRun does not have an ownerreference to the Build, lets add it. // if the buildRun have an ownerreference to the Build, lets remove it