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

Add annotation for an agent metrics listener #698

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions agent-inject/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ type Agent struct {
// template_config specific configuration
VaultAgentTemplateConfig VaultAgentTemplateConfig

// VaultAgentMetricsListenerPort is the port used to server the Vault agent metrics
VaultAgentMetricsListenerPort uint16

// RunAsUser is the user ID to run the Vault agent container(s) as.
RunAsUser int64

Expand Down Expand Up @@ -544,6 +547,17 @@ func New(pod *corev1.Pod) (*Agent, error) {
return nil, err
}

if pod.Annotations[AnnotationAgentMetricsListenerPort] != "" {
vaultAgentMetricsListenerPort, err := parseutil.SafeParseInt(pod.Annotations[AnnotationAgentMetricsListenerPort])
if err != nil {
return agent, err
}
if vaultAgentMetricsListenerPort < 1 || vaultAgentMetricsListenerPort > 65535 {
return agent, errors.New("invalid port number: must be in the range 1 to 65535")
}
agent.VaultAgentMetricsListenerPort = uint16(vaultAgentMetricsListenerPort)
}

return agent, nil
}

Expand Down
3 changes: 3 additions & 0 deletions agent-inject/agent/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ const (
// should map to the same unique value provided in
// "vault.hashicorp.com/agent-inject-secret-". Defaults to false
AnnotationErrorOnMissingKey = "vault.hashicorp.com/error-on-missing-key"

// AnnotationAgentMetricsListenerPort configures the port the agent metrics server should listen on
AnnotationAgentMetricsListenerPort = "vault.hashicorp.com/agent-metrics-listener-port"
)

type AgentConfig struct {
Expand Down
35 changes: 24 additions & 11 deletions agent-inject/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ type Template struct {
ErrMissingKey bool `json:"error_on_missing_key,omitempty"`
}

// Listener defines the configuration for Vault Agent Cache Listener
// Listener defines the configuration for Vault Agent Cache Listener and/ or Vault Agent Metrics Listener
type Listener struct {
Type string `json:"type"`
Address string `json:"address"`
TLSDisable bool `json:"tls_disable"`
AgentAPI *AgentAPI `json:"agent_api,omitempty"`
Role string `json:"role,omitempty"`
}

// AgentAPI defines the agent_api stanza for a listener
Expand Down Expand Up @@ -271,7 +272,7 @@ func (a *Agent) newConfig(init bool) ([]byte, error) {

cacheListener := makeCacheListener(a.VaultAgentCache.ListenerPort)
if a.VaultAgentCache.Persist {
config.Listener = cacheListener
config.Listener = append(config.Listener, &cacheListener)
config.Cache = &Cache{
UseAutoAuthToken: a.VaultAgentCache.UseAutoAuthToken,
Persist: &CachePersist{
Expand All @@ -281,7 +282,7 @@ func (a *Agent) newConfig(init bool) ([]byte, error) {
},
}
} else if a.VaultAgentCache.Enable && !a.PrePopulateOnly && !init {
config.Listener = cacheListener
config.Listener = append(config.Listener, &cacheListener)
config.Cache = &Cache{
UseAutoAuthToken: a.VaultAgentCache.UseAutoAuthToken,
}
Expand All @@ -296,7 +297,7 @@ func (a *Agent) newConfig(init bool) ([]byte, error) {
EnableQuit: a.EnableQuit,
}
} else {
config.Listener = makeCacheListener(a.VaultAgentCache.ListenerPort)
config.Listener = append(config.Listener, &cacheListener)
config.Listener[0].AgentAPI = &AgentAPI{
EnableQuit: a.EnableQuit,
}
Expand All @@ -307,19 +308,31 @@ func (a *Agent) newConfig(init bool) ([]byte, error) {
}
}

if a.VaultAgentMetricsListenerPort > 0 {
metricsListener := makeMetricsListener(a.VaultAgentMetricsListenerPort)
config.Listener = append(config.Listener, &metricsListener)
}

return config.render()
}

func (c *Config) render() ([]byte, error) {
return json.Marshal(c)
}

func makeCacheListener(port string) []*Listener {
return []*Listener{
{
Type: "tcp",
Address: fmt.Sprintf("127.0.0.1:%s", port),
TLSDisable: true,
},
func makeCacheListener(port string) Listener {
return Listener{
Type: "tcp",
Address: fmt.Sprintf("127.0.0.1:%s", port),
TLSDisable: true,
}
}

func makeMetricsListener(port uint16) Listener {
return Listener{
Type: "tcp",
Address: fmt.Sprintf("0.0.0.0:%d", port),
TLSDisable: true,
Role: "metrics_only",
}
}
7 changes: 7 additions & 0 deletions agent-inject/agent/container_init_sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ func (a *Agent) ContainerInitSidecar() (corev1.Container, error) {
if a.SetSecurityContext {
newContainer.SecurityContext = a.securityContext()
}
if a.VaultAgentMetricsListenerPort > 0 {
containerPort := corev1.ContainerPort{
Name: "agent-metrics",
ContainerPort: int32(a.VaultAgentMetricsListenerPort),
}
newContainer.Ports = append(newContainer.Ports, containerPort)
}

// apply any JSON patch requested
if a.InitJsonPatch == "" {
Expand Down
7 changes: 7 additions & 0 deletions agent-inject/agent/container_sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ func (a *Agent) ContainerSidecar() (corev1.Container, error) {
if a.SetSecurityContext {
newContainer.SecurityContext = a.securityContext()
}
if a.VaultAgentMetricsListenerPort > 0 {
containerPort := corev1.ContainerPort{
Name: "agent-metrics",
ContainerPort: int32(a.VaultAgentMetricsListenerPort),
}
newContainer.Ports = append(newContainer.Ports, containerPort)
}

// apply any JSON patch requested
if a.JsonPatch == "" {
Expand Down