Skip to content

Commit 76988bb

Browse files
committed
fix: return early in DiscoverRunner to avoid unnecessary env checks
Stop iterating through all runner factories after the first matching runner is found. Previously, DiscoverRunner would check every registered runner (GitHub, GitLab, Azure, Jenkins, CircleCI, Dagger, TeamCity, Tekton) even after finding a match. Some runners perform expensive operations in CheckEnv() — opening k8s config files (Tekton) or making API calls (GitHub) — making attestation init slower as more runners are added. Fixes #2834
1 parent 7b0148e commit 76988bb

1 file changed

Lines changed: 7 additions & 23 deletions

File tree

pkg/attestation/crafter/runner.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -114,35 +114,19 @@ func NewRunner(t schemaapi.CraftingSchema_Runner_RunnerType, authToken string, l
114114

115115
// DiscoverRunner the runner environment
116116
// This method does a simple check to see which runner is available in the environment
117-
// by iterating over the different runners and performing duck-typing checks
118-
// If more than one runner is detected, we default to generic since its an incongruent result
117+
// by iterating over the different runners and performing duck-typing checks.
118+
// It returns the first matching runner immediately to avoid unnecessary environment
119+
// checks (e.g., opening k8s config files, making API calls) from other runners.
119120
func DiscoverRunner(authToken string, logger zerolog.Logger) SupportedRunner {
120-
detected := []SupportedRunner{}
121-
122-
// Create all runners and check their environment
123-
for _, factory := range RunnerFactories {
121+
for runnerType, factory := range RunnerFactories {
124122
r := factory(authToken, &logger)
125123
if r.CheckEnv() {
126-
detected = append(detected, r)
127-
}
128-
}
129-
130-
// if we don't detect any runner or more than one, we default to generic
131-
if len(detected) == 0 {
132-
return runners.NewGeneric()
133-
}
134-
135-
if len(detected) > 1 {
136-
var detectedStr []string
137-
for _, d := range detected {
138-
detectedStr = append(detectedStr, d.ID().String())
124+
logger.Debug().Str("runner", runnerType.String()).Msg("runner detected")
125+
return r
139126
}
140-
141-
logger.Warn().Strs("detected", detectedStr).Msg("multiple runners detected, incongruent environment")
142-
return runners.NewGeneric()
143127
}
144128

145-
return detected[0]
129+
return runners.NewGeneric()
146130
}
147131

148132
func DiscoverAndEnforceRunner(enforcedRunnerType schemaapi.CraftingSchema_Runner_RunnerType, dryRun bool, authToken string, logger zerolog.Logger) (SupportedRunner, error) {

0 commit comments

Comments
 (0)