# A fresh GitHub token when the build starts A build needs a GitHub token to clone a private repository. GitHub App installation tokens last about an hour, so a run can reach the front of the queue with a token that has already expired. PaC checks the token before the run starts and saves any replacement in the same Kubernetes Secret. ## Creating the PipelineRun 1. GitHub sends a push or pull request event to the PaC controller. PaC authenticates the event and looks for a Kubernetes `Repository` CR whose `spec.url` matches the GitHub repository. That CR tells PaC which namespace to use and supplies repository settings. 2. The controller resolves the pipeline and gets its scoped GitHub App token. It replaces `{{ git_auth_secret }}` with a generated name, saves the token and its expiry under that name, and creates the PipelineRun. ```mermaid flowchart LR GH@{ img: "/icons/event.svg", label: "GitHub event", w: 48, h: 48, constraint: "on" } C@{ img: "/icons/controller.svg", label: "PaC controller", w: 48, h: 48, constraint: "on" } R@{ img: "/icons/config.svg", label: "Match Repository CR", w: 48, h: 48, constraint: "on" } P[Resolve pipeline and scope token] S@{ img: "/icons/secret.svg", label: "Create git-auth Secret", w: 48, h: 48, constraint: "on" } B@{ img: "/icons/pipeline.svg", label: "Create PipelineRun", w: 48, h: 48, constraint: "on" } Q@{ img: "/icons/queue.svg", label: "Wait in Kueue", w: 48, h: 48, constraint: "on" } GH --> C --> R --> P --> S --> B --> Q %% title: From a GitHub event to a queued run %% play: Create a private build %% GH -> C : event %% C -> R | The Repository CR connects the GitHub URL to a build namespace. %% R -> P %% P -> S : token | PaC saves the credentials before it submits the run, so the clone task can refer to the Secret by name. %% S -> B | After Kubernetes assigns the run a UID, PaC adds that ownership reference to the Secret. %% B -> Q | Kueue holds the PipelineRun in Pending until there is room to start it. ``` ## Releasing the PipelineRun Kueue asks Kubernetes to clear `PipelineRunPending`. The `queued-token` admission rule matches that update. “Review release” means the API server sends PaC the current and proposed PipelineRun and waits for an allow or deny response. An allowance lets Kubernetes save the release. A denial leaves the run pending. ```mermaid sequenceDiagram participant Q as Kueue participant A as Kubernetes API participant W as PaC webhook participant S as git-auth Secret participant G as GitHub participant T as Tekton Q->>A: Clear PipelineRunPending A->>W: Review release via queued-token admission rule W->>S: Read Secret through the API S-->>W: Current expiry opt Token expires within five minutes or has expired Note over W: Check the Secret owner and Repository CR W->>G: Request a scoped token G-->>W: Token and expiry W->>S: Update the same Secret through the API S-->>W: Write result end W-->>A: Admission decision alt Allowed A-->>T: Runnable PipelineRun T->>S: Read the same Secret for clone else Rejected A-->>Q: Release denied end %% title: From a build slot to the first clone %% play: Token expired in the queue %% Q -> A %% A -> W | Kubernetes sends PaC the current and proposed PipelineRun and waits for its decision. %% W -> S %% W -> G | PaC verifies the run owns the Secret and reads the Repository CR for its scope. %% G -> W %% W -> S : token | PaC writes the new token and expiry to the existing Secret. %% S -> W %% W -> A | The write succeeded, so PaC allows release. %% A -> T %% T -> S | The clone task still uses the name from its template. %% play: Token still valid %% Q -> A %% A -> W %% W -> S %% S -> W | More than five minutes remain, so PaC skips renewal. %% W -> A %% A -> T %% T -> S %% play: Secret update fails %% Q -> A %% A -> W %% W -> S %% W -> G %% G -> W %% W -> S %% S -> W | Kubernetes reports that the Secret write failed. %% W -> A %% A -> Q | The PipelineRun stays pending so Kueue can retry. ``` ## What the webhook checks PaC stores the expiry GitHub returns as a UTC timestamp in `pipelinesascode.tekton.dev/github-token-expiry` on the git-auth Secret. That is the only new annotation. At release, the webhook uses the run’s existing Secret reference. It skips renewal if the Secret or expiry is missing, or if more than five minutes remain. When renewal is due, PaC checks the Secret’s owner UID and reads the Repository CR in the run’s namespace. The CR must be active, use GitHub App authentication, and match the run and Secret URLs. ## How PaC renews the token PaC reads the run’s `controller-info` annotation for the GitHub App Secret, ConfigMap, and global Repository. This gives the webhook the same settings as the controller that created the run. If the annotation is absent, PaC uses its defaults. PaC verifies the installation, scopes the token, and updates `.git-credentials`, `git-provider-token`, and the expiry in one Secret write. A failed refresh keeps the run pending. A retry skips renewal if the saved token is still fresh. Dry runs skip renewal. ## What Tekton sees Once the API server saves the release, Tekton can start the run. The clone task reads the Secret name the controller put in its template before queueing. PaC’s watcher tracks the run and reports status back to GitHub. The token can still expire during a long build because the refresh happens at release time. ## Decisions ### ✅ Multiple GitHub Apps Refresh uses the run’s existing `controller-info` annotation to find the App that created it. The webhook and watcher share the parser. An admission test verifies the second controller’s App settings and repository scope. ### ✅ Repository access and metadata Trusted operators register repositories in their intended namespaces when the App gets access. The Repository CR supplies that mapping. Edit permissions and stronger identity verification are separate work. [Repository CR permissions follow-up](https://github.com/tektoncd/pipelines-as-code/issues/3104) Refresh is on for eligible GitHub.com App runs. The expiry on the existing git-auth Secret is the only new annotation. PaC scopes the replacement the same way it scoped the original token. ### ⏸️ Runs stuck pending A deleted or changed Repository CR, a wrong Secret owner, or a mismatched installation can leave a run pending on every retry and hold a queue slot. Parked for now. A follow-up can fail the run with a clear reason and free the slot. ### ⚠️ Older clusters Refresh requires Kubernetes 1.28 or later, or OpenShift 4.15 or later. Kubernetes 1.27 can drop `matchConditions`, which lets the API server call the webhook for every PipelineRun update. A webhook outage can then block unrelated updates and cancels. The release rule does not enforce the version requirement. A later `managed-by` selector could limit the impact to PaC runs. [Kubernetes 1.27 defaults](https://github.com/kubernetes/kubernetes/blob/v1.27.0/pkg/features/kube_features.go) · [Kubernetes 1.28 defaults](https://github.com/kubernetes/kubernetes/blob/v1.28.0/pkg/features/kube_features.go) ### 🧪 Timing and testing Slow GitHub calls can hit the admission deadline. If PaC already saved the token, the next attempt can use it. The five-minute margin applies at release, so a clone delayed past the expiry can still fail. The [test plan](konflux-16750-test-plan.md) tracks the live cluster checks. ## Why metrics startup changed PaC's watcher keeps a list of PipelineRuns so it can track builds. It also uses that list to report how many builds are running for each repository. Before this change, loading the metrics code automatically started a PipelineRun watch in any component using that code. Token refresh makes the webhook use the GitHub provider, which also uses the metrics code. On civuole, the automatic watch stopped the webhook starting because its service account cannot list PipelineRuns. The webhook never set up its certificate, so Kubernetes could not reach it to review the release. The watcher now starts the running-build metric itself and uses the PipelineRun list it already has. The webhook can start without a cluster-wide PipelineRun watch. ```mermaid sequenceDiagram participant K as Kubernetes participant W as PaC watcher participant M as Metrics participant P as Prometheus participant H as PaC webhook K->>W: PipelineRun updates W->>M: Start reporting from the watcher's list P->>M: Ask for running builds M-->>P: Count by namespace and repository H->>H: Start and set up its certificate K->>H: Review a queued build's release %% title: Metrics and webhook startup %% play: Report builds and review a release %% K -> W | The watcher keeps its existing list of PipelineRuns up to date. %% W -> M | The watcher starts the running-build metric using that list. %% P -> M %% M -> P : count | Monitoring receives the count with the same name and labels. %% H -> H | The webhook starts without watching PipelineRuns across the cluster. %% K -> H | Kubernetes can reach the webhook when a queued build is released. ``` The running-build metric keeps the same name and labels. Git provider API counters keep their existing reporting code. On civuole, the existing watcher reported one pending build; the updated metric code reported the same count and labels for that build locally. Focused tests passed. The updated watcher and webhook still need a live deployment check. The change removes exported Go helpers used to start the old reporter. PaC has no remaining callers, but an external Go program importing those helpers would need an update.