Skip to content

Commit

Permalink
feat: add counter for admissions
Browse files Browse the repository at this point in the history
Signed-off-by: David Weber <[email protected]>
Signed-off-by: David Weber <[email protected]>
  • Loading branch information
dweber019 committed Jun 9, 2024
1 parent c8bbd08 commit 4eb9628
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ Adding a new version? You'll need three changes:
[#6048](https://github.com/Kong/kubernetes-ingress-controller/pull/6048)
- Add support for Gateway API GRPCRoute and pass related Gateway API conformance test.
[#5776](https://github.com/Kong/kubernetes-ingress-controller/pull/5776)
- Added new metric for Prometheus called `ingress_controller_admission_count`. It's a counter and has two labels
`allowed` to indicate if the resource was allowed and `resource` to indicate the resource under admission.
[#6084](https://github.com/Kong/kubernetes-ingress-controller/issues/6084)

### Fixed

Expand Down
11 changes: 11 additions & 0 deletions internal/admission/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
ctrlref "github.com/kong/kubernetes-ingress-controller/v3/internal/controllers/reference"
"github.com/kong/kubernetes-ingress-controller/v3/internal/gatewayapi"
"github.com/kong/kubernetes-ingress-controller/v3/internal/labels"
"github.com/kong/kubernetes-ingress-controller/v3/internal/metrics"
"github.com/kong/kubernetes-ingress-controller/v3/internal/util"
kongv1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1"
kongv1alpha1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1alpha1"
Expand All @@ -37,6 +38,8 @@ type RequestHandler struct {
// referring the validated resource (Secret) to check the changes on
// referred Secret will produce invalid configuration of the plugins.
ReferenceIndexers ctrlref.CacheIndexers
// PromMetrics provides the Prometheus registry to record metrics
PromMetrics *metrics.CtrlFuncMetrics

Logger logr.Logger
}
Expand All @@ -63,6 +66,14 @@ func (h RequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

h.PromMetrics.RecordAdmissionCount(
response.Allowed,
fmt.Sprintf(
"%s.%s/%s",
review.Request.Resource.Resource, review.Request.Resource.Group, review.Request.Resource.Version,
),
)
review.Response = response

if err := json.NewEncoder(w).Encode(&review); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/dataplane/kong_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,13 @@ func NewKongClient(
kongConfigBuilder KongConfigBuilder,
cacheStores store.CacheStores,
fallbackConfigGenerator FallbackConfigGenerator,
prometheusMetrics *metrics.CtrlFuncMetrics,
) (*KongClient, error) {
c := &KongClient{
logger: logger,
requestTimeout: timeout,
diagnostic: diagnostic,
prometheusMetrics: metrics.NewCtrlFuncMetrics(),
prometheusMetrics: prometheusMetrics,
cache: &cacheStores,
kongConfig: kongConfig,
eventRecorder: eventRecorder,
Expand Down
3 changes: 3 additions & 0 deletions internal/dataplane/kong_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,7 @@ func setupTestKongClient(
configBuilder,
store.NewCacheStores(),
newMockFallbackConfigGenerator(),
metrics.NewCtrlFuncMetrics(),
)
require.NoError(t, err)
return kongClient
Expand Down Expand Up @@ -1386,6 +1387,7 @@ func TestKongClient_FallbackConfiguration_SkipMakingRedundantSnapshot(t *testing
configBuilder,
originalCache,
fallbackConfigGenerator,
metrics.NewCtrlFuncMetrics(),
)
require.NoError(t, err)

Expand Down Expand Up @@ -1451,6 +1453,7 @@ func TestKongClient_FallbackConfiguration_FailedRecovery(t *testing.T) {
configBuilder,
originalCache,
fallbackConfigGenerator,
metrics.NewCtrlFuncMetrics(),
)
require.NoError(t, err)

Expand Down
5 changes: 4 additions & 1 deletion internal/manager/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/kong/kubernetes-ingress-controller/v3/internal/manager/metadata"
"github.com/kong/kubernetes-ingress-controller/v3/internal/manager/telemetry"
"github.com/kong/kubernetes-ingress-controller/v3/internal/manager/utils/kongconfig"
"github.com/kong/kubernetes-ingress-controller/v3/internal/metrics"
"github.com/kong/kubernetes-ingress-controller/v3/internal/store"
"github.com/kong/kubernetes-ingress-controller/v3/internal/util"
"github.com/kong/kubernetes-ingress-controller/v3/internal/util/kubernetes/object/status"
Expand Down Expand Up @@ -185,7 +186,8 @@ func Run(
}

setupLog.Info("Starting Admission Server")
if err := setupAdmissionServer(ctx, c, clientsManager, referenceIndexers, mgr.GetClient(), logger, translatorFeatureFlags, storer); err != nil {
promMetrics := metrics.NewCtrlFuncMetrics()
if err := setupAdmissionServer(ctx, c, clientsManager, referenceIndexers, mgr.GetClient(), logger, translatorFeatureFlags, storer, promMetrics); err != nil {
return err
}

Expand All @@ -207,6 +209,7 @@ func Run(
configTranslator,
cache,
fallbackConfigGenerator,
promMetrics,
)
if err != nil {
return fmt.Errorf("failed to initialize kong data-plane client: %w", err)
Expand Down
3 changes: 3 additions & 0 deletions internal/manager/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
konnectLicense "github.com/kong/kubernetes-ingress-controller/v3/internal/konnect/license"
"github.com/kong/kubernetes-ingress-controller/v3/internal/license"
"github.com/kong/kubernetes-ingress-controller/v3/internal/manager/scheme"
"github.com/kong/kubernetes-ingress-controller/v3/internal/metrics"
"github.com/kong/kubernetes-ingress-controller/v3/internal/store"
"github.com/kong/kubernetes-ingress-controller/v3/internal/util"
"github.com/kong/kubernetes-ingress-controller/v3/internal/util/kubernetes/object/status"
Expand Down Expand Up @@ -195,6 +196,7 @@ func setupAdmissionServer(
logger logr.Logger,
translatorFeatures translator.FeatureFlags,
storer store.Storer,
promMetrics *metrics.CtrlFuncMetrics,
) error {
admissionLogger := logger.WithName("admission-server")

Expand All @@ -214,6 +216,7 @@ func setupAdmissionServer(
storer,
),
ReferenceIndexers: referenceIndexers,
PromMetrics: promMetrics,
Logger: admissionLogger,
}, admissionLogger)
if err != nil {
Expand Down
35 changes: 35 additions & 0 deletions internal/metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net"
"strconv"
"sync"
"time"

Expand All @@ -23,6 +24,7 @@ type CtrlFuncMetrics struct {
TranslationBrokenResources prometheus.Gauge
ConfigPushDuration *prometheus.HistogramVec
ConfigPushSuccessTime *prometheus.GaugeVec
AdmissionCount *prometheus.CounterVec

// Fallback config push metrics.
FallbackTranslationCount *prometheus.CounterVec
Expand Down Expand Up @@ -77,6 +79,16 @@ const (
DataplaneKey string = "dataplane"
)

const (
// AllowedKey defines the key of the metric label indicating admission was allowed.
AllowedKey string = "allowed"
)

const (
// AdmissionResourceKey defines the name of the metric label indicating which dataplane this time series is relevant for.
AdmissionResourceKey string = "resource"
)

// Regular config push metrics names.
const (
MetricNameConfigPushCount = "ingress_controller_configuration_push_count"
Expand All @@ -85,6 +97,7 @@ const (
MetricNameTranslationCount = "ingress_controller_translation_count"
MetricNameTranslationBrokenResources = "ingress_controller_translation_broken_resource_count"
MetricNameConfigPushDuration = "ingress_controller_configuration_push_duration_milliseconds"
MetricNameAdmissionCount = "ingress_controller_admission_count"
)

// Fallback config push metrics names.
Expand Down Expand Up @@ -189,6 +202,20 @@ func NewCtrlFuncMetrics() *CtrlFuncMetrics {
[]string{DataplaneKey},
)

controllerMetrics.AdmissionCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: MetricNameAdmissionCount,
Help: fmt.Sprintf(
"Count of admissions processed by Kong. "+
"`%s` describes whether an admission was allowed. "+
"`%s` describes the resource under admission. ",
AllowedKey,
AdmissionResourceKey,
),
},
[]string{AllowedKey, AdmissionResourceKey},
)

controllerMetrics.FallbackTranslationCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: MetricNameFallbackTranslationCount,
Expand Down Expand Up @@ -302,6 +329,7 @@ func NewCtrlFuncMetrics() *CtrlFuncMetrics {
controllerMetrics.TranslationBrokenResources,
controllerMetrics.ConfigPushDuration,
controllerMetrics.ConfigPushSuccessTime,
controllerMetrics.AdmissionCount,
controllerMetrics.FallbackTranslationBrokenResources,
controllerMetrics.FallbackTranslationCount,
controllerMetrics.FallbackConfigPushCount,
Expand Down Expand Up @@ -356,6 +384,13 @@ func (c *CtrlFuncMetrics) RecordTranslationBrokenResources(count int) {
c.TranslationBrokenResources.Set(float64(count))
}

func (c *CtrlFuncMetrics) RecordAdmissionCount(allowed bool, resource string) {
c.ConfigPushCount.With(prometheus.Labels{
AllowedKey: strconv.FormatBool(allowed),
AdmissionResourceKey: resource,
}).Inc()
}

// RecordFallbackTranslationFailure records a failed fallback configuration translation.
func (c *CtrlFuncMetrics) RecordFallbackTranslationFailure() {
c.FallbackTranslationCount.With(prometheus.Labels{
Expand Down
1 change: 1 addition & 0 deletions test/envtest/metrics_envtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func TestMetricsAreServed(t *testing.T) {
metrics.MetricNameTranslationBrokenResources,
metrics.MetricNameConfigPushDuration,
metrics.MetricNameConfigPushSuccessTime,
metrics.MetricNameAdmissionCount,
},
},
}
Expand Down

0 comments on commit 4eb9628

Please sign in to comment.