MTV-5240 | Annotate AAP hooks with job template name for kubectl#6845
MTV-5240 | Annotate AAP hooks with job template name for kubectl#6845gwencasey96 wants to merge 2 commits into
Conversation
Resolve AAP job template names on Hook reconcile and store them as annotations so kubectl users see the playbook name, not only the template ID. Add Hook CRD printer columns for terminal visibility. Ref: https://redhat.atlassian.net/browse/MTV-5240 Resolves: MTV-5240 Co-authored-by: Cursor <cursoragent@cursor.com> Signed-off-by: Gwen Casey <gcasey@redhat.com>
📝 WalkthroughWalkthroughHook resources now expose AAP job template information through Kubernetes display columns. The controller resolves template names via AAP API queries and stores them as annotations, enabling operators to see template details when listing hooks. ChangesAAP Template Information on Hook Resources
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@pkg/controller/hook/controller.go`:
- Around line 176-197: When exiting early on the skip/failure paths, clear any
stale AAP annotations so the printer column doesn't show outdated data: remove
annAAPJobTemplateID and annAAPJobTemplateName from the hook.Annotations before
returning in the branches where hook.Spec.AAP == nil and where name lookup fails
(the block around aap.JobTemplateNameForHook and the earlier curID/curName
check); apply the mutation via the controller client (use r.Client.Patch or
r.Client.Update on the hook) and handle/log errors from that patch with
r.Log.V(...) but still return the original boolean/result as before.
In `@pkg/lib/aap/client_test.go`:
- Around line 66-89: Add focused unit tests for JobTemplateNameForHook to cover
the new hook-resolution behavior: create an httptest.Server that returns
controlled responses for /api/ and /api/v2/job_templates/ (reuse NewClient to
point at the server) and write at least two subtests—one where a valid hook
(including a name with surrounding whitespace and an exact-ID string) resolves
successfully to the expected template name, and one where the hook cannot be
resolved so the function returns the not-found error; ensure the tests exercise
trimming, exact-ID matching, hook validation paths, and secret-loading behavior
by configuring the client inputs accordingly (use JobTemplateNameForHook and
ListAllJobTemplates symbols to locate where to call the helper).
In `@pkg/lib/aap/client.go`:
- Around line 101-111: The current code resolves hook.Spec.AAP.JobTemplateID by
scanning cl.ListAllJobTemplates limited by defaultMaxJobTemplatesList which can
miss templates past that cap; instead call the API to fetch the job template by
its ID (or use the API's ID filter) rather than ListAllJobTemplates — replace
the loop that compares t.ID to id with a direct get-by-ID call on the client (or
a filtered list request), handle and return the API error if not found, and
remove reliance on defaultMaxJobTemplatesList so the function reliably returns
the template name for hook.Spec.AAP.JobTemplateID.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 07d0ae7c-2bea-45dd-b584-318d976e0089
📒 Files selected for processing (7)
operator/.downstream_manifestsoperator/.kustomized_manifestsoperator/.upstream_manifestsoperator/config/crd/bases/forklift.konveyor.io_hooks.yamlpkg/controller/hook/controller.gopkg/lib/aap/client.gopkg/lib/aap/client_test.go
| if hook.Spec.AAP == nil { | ||
| return false, nil | ||
| } | ||
|
|
||
| wantID := strconv.Itoa(hook.Spec.AAP.JobTemplateID) | ||
| curID := "" | ||
| curName := "" | ||
| if hook.Annotations != nil { | ||
| curID = strings.TrimSpace(hook.Annotations[annAAPJobTemplateID]) | ||
| curName = strings.TrimSpace(hook.Annotations[annAAPJobTemplateName]) | ||
| } | ||
| if curID == wantID && curName != "" { | ||
| return false, nil | ||
| } | ||
|
|
||
| name, nameErr := aap.JobTemplateNameForHook(ctx, r.Client, hook) | ||
| if nameErr != nil || name == "" { | ||
| if nameErr != nil { | ||
| r.Log.V(1).Info("AAP job template name lookup skipped", "error", nameErr.Error(), "jobTemplateId", hook.Spec.AAP.JobTemplateID) | ||
| } | ||
| return false, nil | ||
| } |
There was a problem hiding this comment.
Clear stale AAP annotations on the skip/failure paths.
If a Hook already has these annotations and later spec.aap is removed, or the template ID changes but lookup now fails, this helper exits without patching and leaves the old forklift.konveyor.io/aap-job-template-name behind. That means the printer column can keep showing a template name that no longer matches the Hook. Please delete the AAP annotations before returning on those paths.
Possible direction
func (r *Reconciler) ensureAAPJobTemplateAnnotations(ctx context.Context, hook *api.Hook) (updated bool, err error) {
if hook.Spec.AAP == nil {
- return false, nil
+ return r.clearAAPJobTemplateAnnotations(ctx, hook)
}
...
name, nameErr := aap.JobTemplateNameForHook(ctx, r.Client, hook)
if nameErr != nil || name == "" {
if nameErr != nil {
r.Log.V(1).Info("AAP job template name lookup skipped", "error", nameErr.Error(), "jobTemplateId", hook.Spec.AAP.JobTemplateID)
}
- return false, nil
+ if curID != "" || curName != "" {
+ return r.clearAAPJobTemplateAnnotations(ctx, hook)
+ }
+ return false, nil
}🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@pkg/controller/hook/controller.go` around lines 176 - 197, When exiting early
on the skip/failure paths, clear any stale AAP annotations so the printer column
doesn't show outdated data: remove annAAPJobTemplateID and annAAPJobTemplateName
from the hook.Annotations before returning in the branches where hook.Spec.AAP
== nil and where name lookup fails (the block around aap.JobTemplateNameForHook
and the earlier curID/curName check); apply the mutation via the controller
client (use r.Client.Patch or r.Client.Update on the hook) and handle/log errors
from that patch with r.Log.V(...) but still return the original boolean/result
as before.
| func TestClientJobTemplateName(t *testing.T) { | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| switch r.URL.Path { | ||
| case "/api/", "/api": | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _, _ = w.Write([]byte(`{"current_version": "/api/v2/"}`)) | ||
| case "/api/v2/job_templates/": | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _, _ = w.Write([]byte(`{"count": 1, "next": null, "previous": null, "results": [{"id": 123, "name": "My Playbook Template"}]}`)) | ||
| default: | ||
| t.Fatalf("unexpected path: %s", r.URL.Path) | ||
| } | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| cl := NewClient(srv.URL, "tok", 0, nil) | ||
| templates, err := cl.ListAllJobTemplates(context.Background(), 10) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(templates) != 1 || templates[0].Name != "My Playbook Template" { | ||
| t.Fatalf("unexpected templates: %#v", templates) | ||
| } | ||
| } |
There was a problem hiding this comment.
🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win
This test doesn't cover the new hook-resolution behavior.
It only exercises ListAllJobTemplates, so JobTemplateNameForHook can break in hook validation, secret loading, exact-ID matching, or whitespace trimming without any new test failing. Please add a focused unit test for the new helper, ideally with both success and not-found cases.
As per coding guidelines, **/*.go: "coverage: Make sure that the code has unit tests."
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@pkg/lib/aap/client_test.go` around lines 66 - 89, Add focused unit tests for
JobTemplateNameForHook to cover the new hook-resolution behavior: create an
httptest.Server that returns controlled responses for /api/ and
/api/v2/job_templates/ (reuse NewClient to point at the server) and write at
least two subtests—one where a valid hook (including a name with surrounding
whitespace and an exact-ID string) resolves successfully to the expected
template name, and one where the hook cannot be resolved so the function returns
the not-found error; ensure the tests exercise trimming, exact-ID matching, hook
validation paths, and secret-loading behavior by configuring the client inputs
accordingly (use JobTemplateNameForHook and ListAllJobTemplates symbols to
locate where to call the helper).
| id := hook.Spec.AAP.JobTemplateID | ||
| templates, err := cl.ListAllJobTemplates(ctx, defaultMaxJobTemplatesList) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| for _, t := range templates { | ||
| if t.ID == id { | ||
| return strings.TrimSpace(t.Name), nil | ||
| } | ||
| } | ||
| return "", fmt.Errorf("job template %d not found in AAP", id) |
There was a problem hiding this comment.
Avoid resolving a specific template ID via a capped full-list scan.
This can return a false "not found" once the AAP instance has more than defaultMaxJobTemplatesList templates and the target ID falls on a later page. In this PR that failure is swallowed upstream, so larger installs will silently never get the kubectl annotation. Please resolve by ID directly, or query the API with an ID filter instead of scanning only the first 500 entries.
Possible direction
- templates, err := cl.ListAllJobTemplates(ctx, defaultMaxJobTemplatesList)
- if err != nil {
- return "", err
- }
- for _, t := range templates {
- if t.ID == id {
- return strings.TrimSpace(t.Name), nil
- }
- }
- return "", fmt.Errorf("job template %d not found in AAP", id)
+ t, err := cl.GetJobTemplate(ctx, id)
+ if err != nil {
+ return "", err
+ }
+ return strings.TrimSpace(t.Name), nil🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@pkg/lib/aap/client.go` around lines 101 - 111, The current code resolves
hook.Spec.AAP.JobTemplateID by scanning cl.ListAllJobTemplates limited by
defaultMaxJobTemplatesList which can miss templates past that cap; instead call
the API to fetch the job template by its ID (or use the API's ID filter) rather
than ListAllJobTemplates — replace the loop that compares t.ID to id with a
direct get-by-ID call on the client (or a filtered list request), handle and
return the API error if not found, and remove reliance on
defaultMaxJobTemplatesList so the function reliably returns the template name
for hook.Spec.AAP.JobTemplateID.
Add kubebuilder printcolumn markers for AAPTemplateID and AAPTemplate so make generate/manifests preserves kubectl columns instead of stripping manually edited CRD YAML. Ref: https://redhat.atlassian.net/browse/MTV-5240 Resolves: MTV-5240 Co-authored-by: Cursor <cursoragent@cursor.com> Signed-off-by: Gwen Casey <gcasey@redhat.com>
|
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6845 +/- ##
==========================================
- Coverage 15.45% 10.47% -4.99%
==========================================
Files 112 521 +409
Lines 23377 62038 +38661
==========================================
+ Hits 3613 6497 +2884
- Misses 19479 54992 +35513
- Partials 285 549 +264
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|



Resolve AAP job template names on Hook reconcile and store them as annotations so kubectl users see the playbook name, not only the template ID. Add Hook CRD printer columns for terminal visibility.
Ref: https://redhat.atlassian.net/browse/MTV-5240
Resolves: MTV-5240