-
Notifications
You must be signed in to change notification settings - Fork 42.6k
Add metrics for authentication config reload #123793
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
Merged
k8s-ci-robot
merged 1 commit into
kubernetes:master
from
aramase:aramase/f/authn_config_reload_metrics
Mar 9, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
staging/src/k8s.io/apiserver/pkg/server/options/authenticationconfig/metrics/metrics.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| Copyright 2024 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package metrics | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "fmt" | ||
| "sync" | ||
|
|
||
| "k8s.io/component-base/metrics" | ||
| "k8s.io/component-base/metrics/legacyregistry" | ||
| ) | ||
|
|
||
| const ( | ||
| namespace = "apiserver" | ||
| subsystem = "authentication_config_controller" | ||
| ) | ||
|
|
||
| var ( | ||
| authenticationConfigAutomaticReloadsTotal = metrics.NewCounterVec( | ||
| &metrics.CounterOpts{ | ||
| Namespace: namespace, | ||
| Subsystem: subsystem, | ||
| Name: "automatic_reloads_total", | ||
| Help: "Total number of automatic reloads of authentication configuration split by status and apiserver identity.", | ||
| StabilityLevel: metrics.ALPHA, | ||
| }, | ||
| []string{"status", "apiserver_id_hash"}, | ||
| ) | ||
|
|
||
| authenticationConfigAutomaticReloadLastTimestampSeconds = metrics.NewGaugeVec( | ||
| &metrics.GaugeOpts{ | ||
| Namespace: namespace, | ||
| Subsystem: subsystem, | ||
| Name: "automatic_reload_last_timestamp_seconds", | ||
| Help: "Timestamp of the last automatic reload of authentication configuration split by status and apiserver identity.", | ||
| StabilityLevel: metrics.ALPHA, | ||
| }, | ||
| []string{"status", "apiserver_id_hash"}, | ||
| ) | ||
| ) | ||
|
|
||
| var registerMetrics sync.Once | ||
|
|
||
| func RegisterMetrics() { | ||
| registerMetrics.Do(func() { | ||
| legacyregistry.MustRegister(authenticationConfigAutomaticReloadsTotal) | ||
| legacyregistry.MustRegister(authenticationConfigAutomaticReloadLastTimestampSeconds) | ||
| }) | ||
| } | ||
|
|
||
| func ResetMetricsForTest() { | ||
| authenticationConfigAutomaticReloadsTotal.Reset() | ||
| authenticationConfigAutomaticReloadLastTimestampSeconds.Reset() | ||
| } | ||
|
|
||
| func RecordAuthenticationConfigAutomaticReloadFailure(apiServerID string) { | ||
| apiServerIDHash := getHash(apiServerID) | ||
| authenticationConfigAutomaticReloadsTotal.WithLabelValues("failure", apiServerIDHash).Inc() | ||
| authenticationConfigAutomaticReloadLastTimestampSeconds.WithLabelValues("failure", apiServerIDHash).SetToCurrentTime() | ||
| } | ||
|
|
||
| func RecordAuthenticationConfigAutomaticReloadSuccess(apiServerID string) { | ||
| apiServerIDHash := getHash(apiServerID) | ||
| authenticationConfigAutomaticReloadsTotal.WithLabelValues("success", apiServerIDHash).Inc() | ||
| authenticationConfigAutomaticReloadLastTimestampSeconds.WithLabelValues("success", apiServerIDHash).SetToCurrentTime() | ||
| } | ||
|
|
||
| func getHash(data string) string { | ||
| if len(data) == 0 { | ||
| return "" | ||
| } | ||
| return fmt.Sprintf("sha256:%x", sha256.Sum256([]byte(data))) | ||
| } |
109 changes: 109 additions & 0 deletions
109
staging/src/k8s.io/apiserver/pkg/server/options/authenticationconfig/metrics/metrics_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| Copyright 2024 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package metrics | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "k8s.io/component-base/metrics/legacyregistry" | ||
| "k8s.io/component-base/metrics/testutil" | ||
| ) | ||
|
|
||
| const ( | ||
| testAPIServerID = "testAPIServerID" | ||
| testAPIServerIDHash = "sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37" | ||
| ) | ||
|
|
||
| func TestRecordAuthenticationConfigAutomaticReloadFailure(t *testing.T) { | ||
| expectedValue := ` | ||
| # HELP apiserver_authentication_config_controller_automatic_reloads_total [ALPHA] Total number of automatic reloads of authentication configuration split by status and apiserver identity. | ||
| # TYPE apiserver_authentication_config_controller_automatic_reloads_total counter | ||
| apiserver_authentication_config_controller_automatic_reloads_total {apiserver_id_hash="sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37",status="failure"} 1 | ||
| ` | ||
| metrics := []string{ | ||
| namespace + "_" + subsystem + "_automatic_reloads_total", | ||
| } | ||
|
|
||
| authenticationConfigAutomaticReloadsTotal.Reset() | ||
| RegisterMetrics() | ||
|
|
||
| RecordAuthenticationConfigAutomaticReloadFailure(testAPIServerID) | ||
| if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expectedValue), metrics...); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| func TestRecordAuthenticationConfigAutomaticReloadSuccess(t *testing.T) { | ||
| expectedValue := ` | ||
| # HELP apiserver_authentication_config_controller_automatic_reloads_total [ALPHA] Total number of automatic reloads of authentication configuration split by status and apiserver identity. | ||
| # TYPE apiserver_authentication_config_controller_automatic_reloads_total counter | ||
| apiserver_authentication_config_controller_automatic_reloads_total {apiserver_id_hash="sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37",status="success"} 1 | ||
| ` | ||
| metrics := []string{ | ||
| namespace + "_" + subsystem + "_automatic_reloads_total", | ||
| } | ||
|
|
||
| authenticationConfigAutomaticReloadsTotal.Reset() | ||
| RegisterMetrics() | ||
|
|
||
| RecordAuthenticationConfigAutomaticReloadSuccess(testAPIServerID) | ||
| if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expectedValue), metrics...); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| func TestAuthenticationConfigAutomaticReloadLastTimestampSeconds(t *testing.T) { | ||
| testCases := []struct { | ||
| expectedValue string | ||
| resultLabel string | ||
| timestamp int64 | ||
| }{ | ||
| { | ||
| expectedValue: ` | ||
| # HELP apiserver_authentication_config_controller_automatic_reload_last_timestamp_seconds [ALPHA] Timestamp of the last automatic reload of authentication configuration split by status and apiserver identity. | ||
| # TYPE apiserver_authentication_config_controller_automatic_reload_last_timestamp_seconds gauge | ||
| apiserver_authentication_config_controller_automatic_reload_last_timestamp_seconds{apiserver_id_hash="sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37",status="failure"} 1.689101941e+09 | ||
| `, | ||
| resultLabel: "failure", | ||
| timestamp: 1689101941, | ||
| }, | ||
| { | ||
| expectedValue: ` | ||
| # HELP apiserver_authentication_config_controller_automatic_reload_last_timestamp_seconds [ALPHA] Timestamp of the last automatic reload of authentication configuration split by status and apiserver identity. | ||
| # TYPE apiserver_authentication_config_controller_automatic_reload_last_timestamp_seconds gauge | ||
| apiserver_authentication_config_controller_automatic_reload_last_timestamp_seconds{apiserver_id_hash="sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37",status="success"} 1.689101941e+09 | ||
| `, | ||
| resultLabel: "success", | ||
| timestamp: 1689101941, | ||
| }, | ||
| } | ||
|
|
||
| metrics := []string{ | ||
| namespace + "_" + subsystem + "_automatic_reload_last_timestamp_seconds", | ||
| } | ||
| RegisterMetrics() | ||
|
|
||
| for _, tc := range testCases { | ||
| authenticationConfigAutomaticReloadLastTimestampSeconds.Reset() | ||
| authenticationConfigAutomaticReloadLastTimestampSeconds.WithLabelValues(tc.resultLabel, testAPIServerIDHash).Set(float64(tc.timestamp)) | ||
|
|
||
| if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(tc.expectedValue), metrics...); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.