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

STONEBLD-1844 Add controller for PipelineRuns #210

Merged
merged 1 commit into from
Dec 14, 2023

Conversation

stuartwdouglas
Copy link
Contributor

This controller looks for build PipelineRuns and will record an event that a downstream component should be 'nudged'.

This is the first part of STONEBLD-1756.

@openshift-ci openshift-ci bot requested review from mmorhun and psturc November 3, 2023 01:53
@stuartwdouglas stuartwdouglas force-pushed the STONEBLD-1844 branch 3 times, most recently from e98daf6 to 6a7cb16 Compare November 3, 2023 05:10
main.go Outdated Show resolved Hide resolved
go.mod Outdated Show resolved Hide resolved
controllers/component_pipelinerun_nudge_controller.go Outdated Show resolved Hide resolved
controllers/component_pipelinerun_nudge_controller.go Outdated Show resolved Hide resolved
controllers/component_pipelinerun_nudge_controller.go Outdated Show resolved Hide resolved
controllers/component_pipelinerun_nudge_controller.go Outdated Show resolved Hide resolved
controllers/component_pipelinerun_nudge_controller.go Outdated Show resolved Hide resolved
controllers/component_pipelinerun_nudge_controller.go Outdated Show resolved Hide resolved
controllers/component_pipelinerun_nudge_controller.go Outdated Show resolved Hide resolved
for i := range components.Items {
comp := components.Items[i]
if comp.Spec.Application == updatedComponent.Spec.Application && slices.Contains(updatedComponent.Spec.BuildNudgesRef, comp.Name) {
//TODO: Do the nudge
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nudge would be a complicated operation that takes some time and involves dealing with git provider API.
What if nudge for one of the components fails? What if it's transient/consistent error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This story was just about getting it to the point where we can print the message in the logs. I assume we will need a retry count mechanism with back off, we don't want to retry indefinitely, but we also don't want a single transient failure to stop the update.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a question for the future implementation, but how would we distinguish already nudged components from not nudged one in case if error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to add an annotation with the state information. Also nudging should be idempotent, if you run the same nudge again it should figure out that there is an existing PR, and the update to the PR would result in the same commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added support for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something to bear in mind with error handling too is that the actual updates will be async, and error handling will also need to be done through watching the job/pipeline that is doing the update.

@stuartwdouglas stuartwdouglas force-pushed the STONEBLD-1844 branch 2 times, most recently from db77258 to b835e5a Compare November 4, 2023 00:00
@stuartwdouglas
Copy link
Contributor Author

@mmorhun I think I have addressed everything

@stuartwdouglas stuartwdouglas force-pushed the STONEBLD-1844 branch 3 times, most recently from 785a945 to ea5e6b5 Compare November 4, 2023 01:32
@stuartwdouglas
Copy link
Contributor Author

Given that both @tkdchen and @mmorhun asked about failure handling I have added failure handling and the retry mechanism to this PR. Currently it won't really do anything as printing log messages won't fail, but there are unit tests for artificial failure.

controllers/component_dependency_update_controller.go Outdated Show resolved Hide resolved
controllers/component_dependency_update_controller.go Outdated Show resolved Hide resolved
controllers/component_dependency_update_controller.go Outdated Show resolved Hide resolved
controllers/component_dependency_update_controller.go Outdated Show resolved Hide resolved
controllers/component_dependency_update_controller.go Outdated Show resolved Hide resolved
controllers/component_dependency_update_controller.go Outdated Show resolved Hide resolved
controllers/component_dependency_update_controller.go Outdated Show resolved Hide resolved
controllers/component_dependency_update_controller.go Outdated Show resolved Hide resolved
go.mod Outdated Show resolved Hide resolved
@stuartwdouglas stuartwdouglas force-pushed the STONEBLD-1844 branch 4 times, most recently from 8d27a99 to b611547 Compare November 7, 2023 01:46
@stuartwdouglas stuartwdouglas force-pushed the STONEBLD-1844 branch 2 times, most recently from 7e37caa to faae7fa Compare November 7, 2023 04:34
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"strings"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: probably "strings" should go to the "standard" block and the block moved to the beginning.

alreadyProcessed := map[string]bool{}
if pipelineRun.Annotations != nil && pipelineRun.Annotations[AlreadyNudgedAnnotationName] != "" {
list := strings.Split(pipelineRun.Annotations[AlreadyNudgedAnnotationName], ",")
for _, i := range list {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using i as a string key is confusing

controllers/component_dependency_update_controller.go Outdated Show resolved Hide resolved
}
val = val + i
}
pipelineRun.Annotations[AlreadyNudgedAnnotationName] = val
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if a new build will start in meantime?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is what I mentioned above about builds going stale. When I perform the 'nudge' I could check for currently running pipelines for the same component that are older than the current state, and if they exist and are still running mark them as already nudged and remove the finalizer.

The other approach would be that when a pipeline finishes we check if a newer pipeline exists and has finished, however I don't think that approach will work very well as the pruner may have removed already completed pipelines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have implemented the stale check logic

@stuartwdouglas stuartwdouglas force-pushed the STONEBLD-1844 branch 2 times, most recently from 132430a to daa0e1c Compare November 8, 2023 03:34
@stuartwdouglas
Copy link
Contributor Author

/retest

1 similar comment
@stuartwdouglas
Copy link
Contributor Author

/retest

@stuartwdouglas
Copy link
Contributor Author

/retest

@stuartwdouglas stuartwdouglas force-pushed the STONEBLD-1844 branch 11 times, most recently from 2487aab to 1822dc9 Compare December 13, 2023 01:22
controllers/component_dependency_update_controller.go Outdated Show resolved Hide resolved
controllers/component_dependency_update_controller.go Outdated Show resolved Hide resolved
controllers/component_dependency_update_controller.go Outdated Show resolved Hide resolved
controllers/component_dependency_update_controller.go Outdated Show resolved Hide resolved
if err != nil {
return ctrl.Result{}, err
}
log.Error(nudgeErr, fmt.Sprintf("component update of as a result of a build of %s failed", updatedComponent.Name), l.Audit, "true")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this log be moved before Get?

controllers/component_dependency_update_controller.go Outdated Show resolved Hide resolved
controllers/renovate_util.go Outdated Show resolved Hide resolved
@@ -65,7 +65,7 @@ func (r *PaCPipelineRunPrunerReconciler) SetupWithManager(mgr ctrl.Manager) erro
Complete(r)
}

//+kubebuilder:rbac:groups=tekton.dev,resources=pipelineruns,verbs=get;list;watch;delete;deletecollection
//+kubebuilder:rbac:groups=tekton.dev,resources=pipelineruns,verbs=get;list;watch;delete;deletecollection;update;patch
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid adding these permissions here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently not, I tried adding them to the new controller and the ones here took precedence. Seemed to be an issue with the controller gen.

pkg/git/github/github_app.go Outdated Show resolved Hide resolved
pkg/git/github/github_app.go Outdated Show resolved Hide resolved
@chmeliik
Copy link
Contributor

I had some comments here before they got lost in the github history, not sure if you've had a chance to address them: #210 (comment) (and the following ones)

Comment on lines 28 to 31
func GetAllGithubInstallations(ctx context.Context, client client.Client, eventRecorder record.EventRecorder, componentList []v1alpha1.Component) (string, []installationStruct, error) {
log := log2.FromContext(ctx)
// Check if GitHub Application is used, if not then skip
pacSecret := v1.Secret{}
globalPaCSecretKey := types.NamespacedName{Namespace: buildServiceNamespaceName, Name: prepare.PipelinesAsCodeSecretName}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General review-ability observation: next time, it would be nice to split the PR into commits that make it easier to review. Specifically, some of the changes in this PR just move code from one place to another - it would have been nice to do that in a separate commit and explain that there's no change in functionality, that it's just moving code around. (I'm not actually sure that there are no changes in the code that got moved, just assuming that's true).

Having it all in a single commit makes it hard to focus on the things that matter, first one has to figure out what is or isn't a relevant change

@stuartwdouglas stuartwdouglas force-pushed the STONEBLD-1844 branch 2 times, most recently from 8e2cb96 to f93c48d Compare December 13, 2023 22:47
This controller looks for build PipelineRuns and will record an event
that a downstream component should be 'nudged'.

The controller then uses renovate to perform the 'nudge' and updates the
downstream repositories with the new hash.
Copy link

Quality Gate Passed Quality Gate passed

The SonarCloud Quality Gate passed, but some issues were introduced.

5 New issues
0 Security Hotspots
No data about Coverage
1.9% Duplication on New Code

See analysis details on SonarCloud

@stuartwdouglas
Copy link
Contributor Author

/retest

1 similar comment
@stuartwdouglas
Copy link
Contributor Author

/retest

Comment on lines +238 to +239
index := strings.LastIndex(image, ":")
if index != -1 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To not to have func wide variable index, you can do:

if index := strings.LastIndex(image, ":"); index != -1 {

@openshift-ci openshift-ci bot added the lgtm label Dec 14, 2023
@stuartwdouglas stuartwdouglas merged commit b4956a3 into konflux-ci:main Dec 14, 2023
12 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants