| 1 |
/* |
| 2 |
Copyright 2024 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package metrics |
| 18 |
|
| 19 |
import ( |
| 20 |
"crypto/sha256" |
| 21 |
"fmt" |
| 22 |
"sync" |
| 23 |
|
| 24 |
"k8s.io/component-base/metrics" |
| 25 |
"k8s.io/component-base/metrics/legacyregistry" |
| 26 |
) |
| 27 |
|
| 28 |
const ( |
| 29 |
namespace = "apiserver" |
| 30 |
subsystem = "authentication_config_controller" |
| 31 |
) |
| 32 |
|
| 33 |
var ( |
| 34 |
authenticationConfigAutomaticReloadsTotal = metrics.NewCounterVec( |
| 35 |
&metrics.CounterOpts{ |
| 36 |
Namespace: namespace, |
| 37 |
Subsystem: subsystem, |
| 38 |
Name: "automatic_reloads_total", |
| 39 |
Help: "Total number of automatic reloads of authentication configuration split by status and apiserver identity.", |
| 40 |
StabilityLevel: metrics.ALPHA, |
| 41 |
}, |
| 42 |
[]string{"status", "apiserver_id_hash"}, |
| 43 |
) |
| 44 |
|
| 45 |
authenticationConfigAutomaticReloadLastTimestampSeconds = metrics.NewGaugeVec( |
| 46 |
&metrics.GaugeOpts{ |
| 47 |
Namespace: namespace, |
| 48 |
Subsystem: subsystem, |
| 49 |
Name: "automatic_reload_last_timestamp_seconds", |
| 50 |
Help: "Timestamp of the last automatic reload of authentication configuration split by status and apiserver identity.", |
| 51 |
StabilityLevel: metrics.ALPHA, |
| 52 |
}, |
| 53 |
[]string{"status", "apiserver_id_hash"}, |
| 54 |
) |
| 55 |
) |
| 56 |
|
| 57 |
var registerMetrics sync.Once |
| 58 |
|
| 59 |
func RegisterMetrics() { |
| 60 |
registerMetrics.Do(func() { |
| 61 |
legacyregistry.MustRegister(authenticationConfigAutomaticReloadsTotal) |
| 62 |
legacyregistry.MustRegister(authenticationConfigAutomaticReloadLastTimestampSeconds) |
| 63 |
}) |
| 64 |
} |
| 65 |
|
| 66 |
func ResetMetricsForTest() { |
| 67 |
authenticationConfigAutomaticReloadsTotal.Reset() |
| 68 |
authenticationConfigAutomaticReloadLastTimestampSeconds.Reset() |
| 69 |
} |
| 70 |
|
| 71 |
func RecordAuthenticationConfigAutomaticReloadFailure(apiServerID string) { |
| 72 |
apiServerIDHash := getHash(apiServerID) |
| 73 |
authenticationConfigAutomaticReloadsTotal.WithLabelValues("failure", apiServerIDHash).Inc() |
| 74 |
authenticationConfigAutomaticReloadLastTimestampSeconds.WithLabelValues("failure", apiServerIDHash).SetToCurrentTime() |
| 75 |
} |
| 76 |
|
| 77 |
func RecordAuthenticationConfigAutomaticReloadSuccess(apiServerID string) { |
| 78 |
apiServerIDHash := getHash(apiServerID) |
| 79 |
authenticationConfigAutomaticReloadsTotal.WithLabelValues("success", apiServerIDHash).Inc() |
| 80 |
authenticationConfigAutomaticReloadLastTimestampSeconds.WithLabelValues("success", apiServerIDHash).SetToCurrentTime() |
| 81 |
} |
| 82 |
|
| 83 |
func getHash(data string) string { |
| 84 |
if len(data) == 0 { |
| 85 |
return "" |
| 86 |
} |
| 87 |
return fmt.Sprintf("sha256:%x", sha256.Sum256([]byte(data))) |
| 88 |
} |