Skip to content

Commit

Permalink
add metric to emit number of pods using v1 or v2
Browse files Browse the repository at this point in the history
  • Loading branch information
minj131 committed Oct 24, 2023
1 parent 767ebad commit a575739
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
11 changes: 10 additions & 1 deletion pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import (
"encoding/csv"
"encoding/json"
"fmt"
"github.com/aws/amazon-eks-pod-identity-webhook/pkg/containercredentials"
"io/ioutil"
"net/http"
"path/filepath"
"strconv"
"strings"

"github.com/aws/amazon-eks-pod-identity-webhook/pkg/containercredentials"
"github.com/prometheus/client_golang/prometheus"

"github.com/aws/amazon-eks-pod-identity-webhook/pkg"
"github.com/aws/amazon-eks-pod-identity-webhook/pkg/cache"
"k8s.io/api/admission/v1beta1"
Expand All @@ -40,6 +42,7 @@ import (
func init() {
_ = corev1.AddToScheme(runtimeScheme)
_ = admissionregistrationv1beta1.AddToScheme(runtimeScheme)
prometheus.MustRegister(webhookPodCount)
}

var (
Expand Down Expand Up @@ -404,6 +407,9 @@ func (m *Modifier) buildPodPatchConfig(pod *corev1.Pod) *podPatchConfig {
if containerCredentialsPatchConfig != nil {
regionalSTS, tokenExpiration := m.Cache.GetCommonConfigurations(pod.Spec.ServiceAccountName, pod.Namespace)
tokenExpiration, containersToSkip := m.parsePodAnnotations(pod, tokenExpiration)

webhookPodCount.WithLabelValues("container_credentials").Inc()

return &podPatchConfig{
ContainersToSkip: containersToSkip,
TokenExpiration: tokenExpiration,
Expand All @@ -418,6 +424,9 @@ func (m *Modifier) buildPodPatchConfig(pod *corev1.Pod) *podPatchConfig {
roleArn, audience, regionalSTS, tokenExpiration := m.Cache.Get(pod.Spec.ServiceAccountName, pod.Namespace)
if roleArn != "" {
tokenExpiration, containersToSkip := m.parsePodAnnotations(pod, tokenExpiration)

webhookPodCount.WithLabelValues("sts_web_identity").Inc()

return &podPatchConfig{
ContainersToSkip: containersToSkip,
TokenExpiration: tokenExpiration,
Expand Down
20 changes: 13 additions & 7 deletions pkg/handler/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,19 @@ var (
},
[]string{"verb", "path"},
)
webhookPodCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "pod_identity_webhook_pod_count",
Help: "Indicator to how many pods are using sts web identity or container credentials",
}, []string{"method"},
)
)

func register() {
prometheus.MustRegister(requestCounter)
prometheus.MustRegister(requestLatencies)
prometheus.MustRegister(requestLatenciesSummary)
prometheus.MustRegister(webhookPodCount)
}

func monitor(verb, path string, httpCode int, reqStart time.Time) {
Expand Down Expand Up @@ -100,13 +107,12 @@ func (w *statusLoggingResponseWriter) Write(data []byte) (int, error) {
// InstrumentRoute is a middleware for adding the following metrics for each
// route:
//
// # Counter
// http_request_count{"verb", "path", "code}
// # Histogram
// http_request_latencies{"verb", "path"}
// # Summary
// http_request_duration_microseconds{"verb", "path", "code}
//
// # Counter
// http_request_count{"verb", "path", "code}
// # Histogram
// http_request_latencies{"verb", "path"}
// # Summary
// http_request_duration_microseconds{"verb", "path", "code}
func InstrumentRoute() Middleware {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit a575739

Please sign in to comment.