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

feat: Add prometheus endpoint #19

Merged
merged 7 commits into from
Sep 19, 2024
Merged
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
3 changes: 2 additions & 1 deletion charts/eks-pod-identity-agent/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ helm.sh/chart: {{ include "eks-pod-identity-agent.chart" . }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
app: eks-pod-identiy-agent
{{- end }}

{{/*
Expand All @@ -59,4 +60,4 @@ The eks-pod-identity-agent image to use
{{- else }}
{{- printf "%s/eks/eks-pod-identity-agent:%s" .Values.image.containerRegistry .Values.image.tag }}
{{- end }}
{{- end }}
{{- end }}
47 changes: 47 additions & 0 deletions charts/eks-pod-identity-agent/templates/metrics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{{- if .Values.metrics.enabled -}}
---
apiVersion: v1
kind: Service
metadata:
name: eks-pod-identity-agent
namespace: {{ .Release.Namespace }}
labels:
{{- include "eks-pod-identity-agent.labels" . | nindent 4 }}
{{- with .Values.metrics.extraLabels -}}
{{ toYaml . | nindent 4 }}
{{- end }}
spec:
selector:
app: eks-pod-identiy-agent
ports:
- name: metrics
port: {{ .Values.metrics.port }}
targetPort: {{ .Values.metrics.port }}
type: ClusterIP

---
{{- if .Values.metrics.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: eks-pod-identity-agent
namespace: {{ .Release.Namespace }}
labels:
{{- include "eks-pod-identity-agent.labels" . | nindent 4 }}
{{- with .Values.metrics.serviceMonitor.extraLabels -}}
{{ toYaml . | nindent 4 }}
{{- end }}
spec:
selector:
matchLabels:
app: eks-pod-identiy-agent
namespaceSelector:
matchNames:
- {{ .Release.Namespace }}
endpoints:
- targetPort: {{ .Values.metrics.port }}
path: {{ .Values.metrics.path }}
interval: {{ .Values.metrics.serviceMonitor.interval | default "15s"}}

{{- end }}
{{- end }}
19 changes: 17 additions & 2 deletions charts/eks-pod-identity-agent/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ agent:
probePort: 2703
readinessEndpoint: /readyz


metrics:
enabled: false
port: 2705
path: /metrics
extraLabels:
prometheus.io/scrape: "true"
prometheus.io/port: 2705
serviceMonitor:
enabled: false
extraLabels: {}
interval: "15s"

clusterName: EKS_CLUSTER_NAME

env:
Expand Down Expand Up @@ -57,7 +70,9 @@ nameOverride: "eks-pod-identity-agent"

nodeSelector: {}

podAnnotations: {}
podAnnotations:
prometheus.io/port: '2705'
prometheus.io/scrape: 'true'
pkruk marked this conversation as resolved.
Show resolved Hide resolved

priorityClassName: system-node-critical

Expand All @@ -79,4 +94,4 @@ tolerations:
updateStrategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: "10%"
maxUnavailable: "10%"
7 changes: 5 additions & 2 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
var (
serverPort uint16
probePort uint16
metricsPort uint16
bindHosts []string
clusterName string
overrideEksAuthEndpoint string
Expand All @@ -36,8 +37,8 @@ var serverCmd = &cobra.Command{
Short: "A proxy server that exchanges kubernetes service account token with temporary AWS credentials by calling EKS Auth APIs",
Long: fmt.Sprintf(`This command initalizes a proxy server that will listen by default on port %d.

Request that are sent to the credential path (/v1/credentials) will be proxied to EKS to fetch temporary
AWS credentials. The AWS SDKs used from within EKS workloads can be configured to invoke this endpoint
Request that are sent to the credential path (/v1/credentials) will be proxied to EKS to fetch temporary
AWS credentials. The AWS SDKs used from within EKS workloads can be configured to invoke this endpoint
for granular IAM permissions.

Example use: './eks-pod-identity-agent server'`, serverPort),
Expand Down Expand Up @@ -97,6 +98,7 @@ func createServers(cfg aws.Config) []*server.Server {

// add health probes listening on host's network
servers = append(servers, server.NewProbeServer(fmt.Sprintf("localhost:%d", probePort), bindHosts, serverPort))
servers = append(servers, server.NewMetricsServer(fmt.Sprintf("0.0.0.0:%d", metricsPort), bindHosts, serverPort))
return servers
}

Expand Down Expand Up @@ -126,6 +128,7 @@ func init() {
// Setup the port where the proxy server will listen to connections
serverCmd.Flags().Uint16VarP(&serverPort, "port", "p", 80, "Listening port of the proxy server")
serverCmd.Flags().Uint16Var(&probePort, "probe-port", 2703, "Health and readiness listening port")
serverCmd.Flags().Uint16Var(&metricsPort, "metrics-port", 2705, "Metrics listening port")
serverCmd.Flags().DurationVar(&maxCredentialRenewal, "max-credential-retention-before-renewal", 3*time.Hour,
"Maximum amount of time that agent waits before renewing credentials. Set 0 to disable caching.")
serverCmd.Flags().IntVar(&maxCacheSize, "max-cache-size", 2000,
Expand Down
19 changes: 14 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ require (
github.com/onsi/gomega v1.27.8
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.9.0
github.com/vishvananda/netlink v1.1.0
go.uber.org/mock v0.3.0
golang.org/x/sys v0.18.0
golang.org/x/sys v0.25.0
golang.org/x/time v0.3.0
)

Expand All @@ -29,13 +29,22 @@ require (
github.com/aws/aws-sdk-go-v2/service/sso v1.20.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.4 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.28.6 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.20.3 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
32 changes: 32 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.28.6 h1:cwIxeBttqPN3qkaAjcEcsh8NYr8n
github.com/aws/aws-sdk-go-v2/service/sts v1.28.6/go.mod h1:FZf1/nKNEkHdGGJP/cI2MoIMquumuRK6ol3QQJNDxmw=
github.com/aws/smithy-go v1.20.2 h1:tbp628ireGtzcHDDmLT/6ADHidqnwgF57XOXZe6tp4Q=
github.com/aws/smithy-go v1.20.2/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -38,16 +42,30 @@ github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1
github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/onsi/ginkgo/v2 v2.9.7 h1:06xGQy5www2oN160RtEZoTvnP2sPhEfePYmCDc2szss=
github.com/onsi/ginkgo/v2 v2.9.7/go.mod h1:cxrmXWykAwTwhQsJOPfdIDiJ+l2RYq7U8hFU+M/1uw0=
github.com/onsi/gomega v1.27.8 h1:gegWiwZjBsf2DgiSbf5hpokZ98JVDMcWkUiigk6/KXc=
github.com/onsi/gomega v1.27.8/go.mod h1:2J8vzI/s+2shY9XHRApDkdgPo1TKT7P2u6fXeJKFnNQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.20.3 h1:oPksm4K8B+Vt35tUhw6GbSNSgVlVSBH0qELP/7u83l4=
github.com/prometheus/client_golang v1.20.3/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
Expand All @@ -58,6 +76,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0=
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df h1:OviZH7qLw/7ZovXvuNyL3XQl8UFofeikI1NW1Gypu7k=
Expand All @@ -66,16 +85,29 @@ go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo=
go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.9.1 h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo=
golang.org/x/tools v0.9.1/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
21 changes: 19 additions & 2 deletions internal/credsretriever/refreshing_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"math/rand"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.amzn.com/eks/eks-pod-identity-agent/internal/cache/expiring"
"go.amzn.com/eks/eks-pod-identity-agent/internal/cloud/eksauth"
"go.amzn.com/eks/eks-pod-identity-agent/internal/middleware/logger"
Expand Down Expand Up @@ -52,6 +54,20 @@ type internalClock func() time.Time
// type assertion
var _ credentials.CredentialRetriever = &cachedCredentialRetriever{}

var (
promCacheError = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "pod_identity_cache_errors",
Help: "Removing credentials from cache, got non recoverable error",
}, []string{"type"},
)

promCacheState = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "pod_identity_cache_state",
Help: "The state of credential in cache",
}, []string{"state"},
)
)

const (
// defaultCleanupInterval sets how often we go over the cache to check if
// there are expired credentials requiring renewal
Expand Down Expand Up @@ -145,7 +161,6 @@ func (r *cachedCredentialRetriever) GetIamCredentials(ctx context.Context,
func (r *cachedCredentialRetriever) callDelegateAndCache(ctx context.Context,
request *credentials.EksCredentialsRequest) (cacheEntry, credentials.ResponseMetadata, error) {
log := logger.FromContext(ctx)

newCacheEntry, err := r.fetchCredentialsFromDelegate(ctx, request)
if err != nil {
return cacheEntry{}, nil, fmt.Errorf("error getting credentials to cache: %w", err)
Expand Down Expand Up @@ -194,7 +209,6 @@ func (r *cachedCredentialRetriever) onCredentialRenewal(key string, entry cacheE
logger.ContextWithField(entry.requestLogCtx, "from", "renewal-thread"), renewalTimeout)
defer cancel()
log := logger.FromContext(ctx)

if r.refreshRateLimiter.Allow() {
err := r.refreshRateLimiter.Wait(ctx)
if err != nil {
Expand All @@ -204,11 +218,13 @@ func (r *cachedCredentialRetriever) onCredentialRenewal(key string, entry cacheE
_, _, err = r.callDelegateAndCache(ctx, entry.originatingRequest)
if err == nil {
// if we retrieved the credentials successfully, exit we don't need to do anything else
promCacheState.WithLabelValues("hit").Inc()
return
}

if eksauth.IsIrrecoverableApiError(err) {
log.Infof("Removing credentials from cache, got non recoverable error: %s", err.Error())
promCacheError.WithLabelValues("NonRecoverable").Inc()
r.internalCache.Delete(entry.originatingRequest.ServiceAccountToken)
return
}
Expand All @@ -227,6 +243,7 @@ func (r *cachedCredentialRetriever) onCredentialRenewal(key string, entry cacheE
Infof("Credentials still valid for at least %0.2fs, keeping them will try again after ttl expires", oldCredsDuration.Seconds())
r.internalCache.SetWithRefreshExpire(key, entry, newRefreshTtl, oldCredsDuration)
} else {
promCacheState.WithLabelValues("evicted").Inc()
log.Infof("Evicting credentials since they are too old")
}
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/handlers/eks_credential_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"context"
"encoding/json"
"net/http"
"strconv"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.amzn.com/eks/eks-pod-identity-agent/internal/cloud/eksauth"
"go.amzn.com/eks/eks-pod-identity-agent/internal/credsretriever"
"go.amzn.com/eks/eks-pod-identity-agent/internal/middleware/logger"
Expand Down Expand Up @@ -35,6 +38,13 @@ type EksCredentialHandlerOpts struct {
RefreshQPS int
}

var (
promHttpStatus = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "pod_identity_http_response",
Help: "Pod Identity http response code",
}, []string{"code"})
)

func NewEksCredentialHandler(opts EksCredentialHandlerOpts) *EksCredentialHandler {
credentialsRetriever := eksauth.NewService(opts.Cfg)
if opts.CredentialRenewal != 0 && opts.MaxCacheSize != 0 {
Expand Down Expand Up @@ -72,18 +82,21 @@ func (h *EksCredentialHandler) HandleRequest(resp http.ResponseWriter, req *http
creds, err := h.GetEksCredentials(ctx, eksCredentialsRequest)
if err != nil {
msg, code := errors.HandleCredentialFetchingError(ctx, err)
promHttpStatus.WithLabelValues(strconv.Itoa(code)).Inc()
http.Error(resp, msg, code)
return
}

jsonOutput, err := json.Marshal(creds)
if err != nil {
promHttpStatus.WithLabelValues(strconv.Itoa(http.StatusInternalServerError)).Inc()
http.Error(resp, "Unable to serialize credentials", http.StatusInternalServerError)
return
}

// send the response
resp.Header().Add("Content-Type", "application/json")
promHttpStatus.WithLabelValues("200").Inc()
_, err = resp.Write(jsonOutput)
if err != nil {
log.Errorf("failed to write response: %v", err)
Expand Down
8 changes: 8 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"
"go.amzn.com/eks/eks-pod-identity-agent/configuration"
"go.amzn.com/eks/eks-pod-identity-agent/internal/middleware/logger"
ratelimiter "go.amzn.com/eks/eks-pod-identity-agent/internal/middleware/rate_limiter"
Expand Down Expand Up @@ -63,6 +64,13 @@ func NewEksCredentialServer(addr string, opts handlers.EksCredentialHandlerOpts)
return srv
}

func NewMetricsServer(addr string, hosts []string, port uint16) *Server {
srv := newBaseServer(addr)
srv.configurer = handlers.NewProbeHandler(hosts, port)
srv.mux.Handle("/metrics", promhttp.Handler())
return srv
}

func (p *Server) ListenUntilContextCancelled(ctx context.Context) {
log := logger.FromContext(ctx)
p.configureHandler()
Expand Down