Skip to content

feat: allow overriding spiffe-helper health-check port via annotation#87

Open
ErvalhouS wants to merge 2 commits intocofide:mainfrom
ErvalhouS:signalwire/configurable-helper-health-port
Open

feat: allow overriding spiffe-helper health-check port via annotation#87
ErvalhouS wants to merge 2 commits intocofide:mainfrom
ErvalhouS:signalwire/configurable-helper-health-port

Conversation

@ErvalhouS
Copy link
Copy Markdown

@ErvalhouS ErvalhouS commented Feb 26, 2026

Problem

When the spiffe-helper sidecar is injected into a pod that already has
another container binding port 8081, the health-check server fails
to start:

Error serving health checks: listen tcp :8081: bind: address already in use

Because the health server never comes up, the startup probe always hits
the other process and receives a non-2xx response, causing an infinite
CrashLoopBackOff on the spiffe-helper container.

Solution

Add a new pod annotation spiffe.cofide.io/helper-health-port that lets
users override the port used for:

  1. The bind_port in the injected spiffe-helper HCL config
  2. The startupProbe, livenessProbe, and readinessProbe on the
    injected sidecar container
annotations:
  spiffe.cofide.io/inject: "helper"
  spiffe.cofide.io/helper-health-port: "8088"

The default remains 8081 — no behaviour change for existing pods.
An invalid annotation value (non-integer, out of range) is logged and
the default is used as a fallback.

Changes

File Change
internal/const/const.go Add SPIFFEHelperHealthPortAnnotation constant
internal/helper/config.go Thread HealthCheckPort through SPIFFEHelperConfigParamsSPIFFEHelper struct → GetSidecarContainer() probes and HCL config
internal/webhook/webhook.go Read + validate annotation, pass value to SPIFFEHelperConfigParams

Testing

go test ./internal/...
ok  github.com/cofide/spiffe-enable/internal/helper   0.013s
ok  github.com/cofide/spiffe-enable/internal/webhook  0.073s

🤖 Generated with Claude Code

Add a new pod annotation `spiffe.cofide.io/helper-health-port` that lets
users override the port spiffe-helper binds its health-check server on.

The default remains 8081, but pods where that port is already taken by
another container (e.g. FreeSwitch JSON RPC API) can now set a different
port:

  annotations:
    spiffe.cofide.io/helper-health-port: "8088"

The annotation value is validated (must be a valid 1–65535 integer); an
invalid value is logged and the default is used instead.

Changes:
- internal/const/const.go: add SPIFFEHelperHealthPortAnnotation constant
- internal/helper/config.go: thread HealthCheckPort through
  SPIFFEHelperConfigParams → SPIFFEHelper → GetSidecarContainer() so the
  injected HCL config and all three probes use the overridden port
- internal/webhook/webhook.go: read and validate the annotation, pass
  the value to SPIFFEHelperConfigParams
Copilot AI review requested due to automatic review settings February 26, 2026 12:44
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @ErvalhouS, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the spiffe-enable admission webhook by providing a mechanism to customize the health-check port used by the injected spiffe-helper sidecar. This change resolves potential port conflicts in environments where the default port 8081 is already occupied, improving the robustness and deployability of spiffe-enabled applications without altering existing configurations.

Highlights

  • Problem Solved: Addressed an issue where the spiffe-helper sidecar's health-check server would fail to start if port 8081 was already in use by another container in the same pod's network namespace, leading to CrashLoopBackOff.
  • New Feature: Introduced a new pod annotation, spiffe.cofide.io/helper-health-port, allowing users to override the default health-check port (8081) for the spiffe-helper sidecar.
  • Configuration Impact: The specified health-check port is now used for the bind_port in the injected spiffe-helper HCL configuration and for the startupProbe, livenessProbe, and readinessProbe of the sidecar container.
  • Backward Compatibility: The default health-check port remains 8081, ensuring no behavior change for existing pods that do not use the new annotation. Invalid annotation values are logged, and the default port is used as a fallback.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • internal/const/const.go
    • Added SPIFFEHelperHealthPortAnnotation constant.
  • internal/helper/config.go
    • Updated SPIFFEHelperConfigParams to include HealthCheckPort.
    • Modified NewSPIFFEHelper to use the provided HealthCheckPort or default to SPIFFEHelperHealthCheckPort.
    • Configured SPIFFEHelperHealthConfig to use the specified BindPort.
    • Updated SPIFFEHelper struct to store the HealthCheckPort.
    • Modified sidecar container probes (readiness, liveness, startup) to use the configurable health check port.
  • internal/webhook/webhook.go
    • Imported strconv package.
    • Implemented logic to read and validate the spiffe.cofide.io/helper-health-port annotation from pod annotations.
    • Passed the resolved healthCheckPort to helper.SPIFFEHelperConfigParams.
Activity
  • The author performed unit tests on the internal packages, confirming github.com/cofide/spiffe-enable/internal/helper and github.com/cofide/spiffe-enable/internal/webhook passed successfully.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request effectively addresses the problem of port conflicts for the spiffe-helper health-check server by introducing a configurable port via a pod annotation. The implementation is robust, including proper validation of the annotation value and defaulting to the standard port if the annotation is invalid or not provided. The changes are well-integrated across the const, helper, and webhook packages, ensuring the new port is used consistently for both the spiffe-helper configuration and Kubernetes probes. This is a well-executed feature.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds support for overriding the SPIFFE helper sidecar’s health-check server port via a pod annotation to avoid port collisions (default remains 8081).

Changes:

  • Introduces spiffe.cofide.io/helper-health-port annotation constant.
  • Parses/validates the annotation in the mutating webhook and passes the resolved port into helper config generation.
  • Threads the resolved health-check port through SPIFFE helper config generation so both HCL bind_port and container probes use it.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
internal/webhook/webhook.go Reads/validates new annotation and passes HealthCheckPort into helper config params.
internal/helper/config.go Adds HealthCheckPort parameter/field and uses it for HCL bind_port and probe ports.
internal/const/const.go Defines the new SPIFFEHelperHealthPortAnnotation constant and docs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/webhook/webhook.go Outdated
Comment on lines +178 to +187
healthCheckPort := 0
if portStr, ok := pod.Annotations[constants.SPIFFEHelperHealthPortAnnotation]; ok {
if p, err := strconv.Atoi(portStr); err != nil || p <= 0 || p > 65535 {
logger.Error(fmt.Errorf("invalid annotation value %q", portStr),
"Ignoring invalid helper-health-port annotation, using default",
"annotation", constants.SPIFFEHelperHealthPortAnnotation)
} else {
healthCheckPort = p
}
}
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

Consider trimming whitespace from the annotation value before parsing (e.g., users may accidentally set "8088 " in YAML). Also, when strconv.Atoi fails, logging the parsing error (or including it via %w) would make the log more actionable than a generic "invalid annotation value" error.

Copilot uses AI. Check for mistakes.
Comment thread internal/webhook/webhook.go Outdated
Comment on lines 177 to 195
// Resolve optional health-check port override
healthCheckPort := 0
if portStr, ok := pod.Annotations[constants.SPIFFEHelperHealthPortAnnotation]; ok {
if p, err := strconv.Atoi(portStr); err != nil || p <= 0 || p > 65535 {
logger.Error(fmt.Errorf("invalid annotation value %q", portStr),
"Ignoring invalid helper-health-port annotation, using default",
"annotation", constants.SPIFFEHelperHealthPortAnnotation)
} else {
healthCheckPort = p
}
}

// Generate the spiffe-helper configuration
configParams := helper.SPIFFEHelperConfigParams{
AgentAddress: constants.SPIFFEWLSocketPath,
CertPath: constants.SPIFFEEnableCertDirectory,
IncludeIntermediateBundle: incIntermediateBundle,
HealthCheckPort: healthCheckPort,
}
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

The new helper-health-port override logic isn't covered by tests. Please add webhook tests that (a) set the annotation to a valid port and assert the injected spiffe-helper container probes use that port, and (b) set an invalid value and assert the default port is used (and the pod mutation still succeeds).

Copilot uses AI. Check for mistakes.
Comment thread internal/helper/config.go
Comment on lines 87 to 119
@@ -97,6 +105,7 @@ func NewSPIFFEHelper(params SPIFFEHelperConfigParams) (*SPIFFEHelper, error) {
SVIDBundleFilename: "ca.pem",
HealthCheck: SPIFFEHelperHealthConfig{
ListenerEnabled: true,
BindPort: healthCheckPort,
},
}

@@ -106,7 +115,7 @@ func NewSPIFFEHelper(params SPIFFEHelperConfigParams) (*SPIFFEHelper, error) {
hclBytes := hclFile.Bytes()
hclString := string(hclBytes)

return &SPIFFEHelper{Config: hclString}, nil
return &SPIFFEHelper{Config: hclString, HealthCheckPort: healthCheckPort}, nil
}
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

The health check port defaulting/override is newly introduced but isn't asserted in unit tests for NewSPIFFEHelper. Consider adding tests that verify (1) HealthCheck.BindPort defaults to 8081 when params.HealthCheckPort is zero, and (2) when a custom port is provided, both the generated HCL bind_port and the returned SPIFFEHelper.HealthCheckPort (used by probes) reflect that value.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants