| 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 |
"hash" |
| 23 |
"sync" |
| 24 |
|
| 25 |
"k8s.io/component-base/metrics" |
| 26 |
"k8s.io/component-base/metrics/legacyregistry" |
| 27 |
) |
| 28 |
|
| 29 |
const ( |
| 30 |
namespace = "apiserver" |
| 31 |
subsystem = "authorization_config_controller" |
| 32 |
) |
| 33 |
|
| 34 |
var ( |
| 35 |
authorizationConfigAutomaticReloadsTotal = metrics.NewCounterVec( |
| 36 |
&metrics.CounterOpts{ |
| 37 |
Namespace: namespace, |
| 38 |
Subsystem: subsystem, |
| 39 |
Name: "automatic_reloads_total", |
| 40 |
Help: "Total number of automatic reloads of authorization configuration split by status and apiserver identity.", |
| 41 |
StabilityLevel: metrics.ALPHA, |
| 42 |
}, |
| 43 |
[]string{"status", "apiserver_id_hash"}, |
| 44 |
) |
| 45 |
|
| 46 |
authorizationConfigAutomaticReloadLastTimestampSeconds = metrics.NewGaugeVec( |
| 47 |
&metrics.GaugeOpts{ |
| 48 |
Namespace: namespace, |
| 49 |
Subsystem: subsystem, |
| 50 |
Name: "automatic_reload_last_timestamp_seconds", |
| 51 |
Help: "Timestamp of the last automatic reload of authorization configuration split by status and apiserver identity.", |
| 52 |
StabilityLevel: metrics.ALPHA, |
| 53 |
}, |
| 54 |
[]string{"status", "apiserver_id_hash"}, |
| 55 |
) |
| 56 |
) |
| 57 |
|
| 58 |
var registerMetrics sync.Once |
| 59 |
var hashPool *sync.Pool |
| 60 |
|
| 61 |
func RegisterMetrics() { |
| 62 |
registerMetrics.Do(func() { |
| 63 |
hashPool = &sync.Pool{ |
| 64 |
New: func() interface{} { |
| 65 |
return sha256.New() |
| 66 |
}, |
| 67 |
} |
| 68 |
legacyregistry.MustRegister(authorizationConfigAutomaticReloadsTotal) |
| 69 |
legacyregistry.MustRegister(authorizationConfigAutomaticReloadLastTimestampSeconds) |
| 70 |
}) |
| 71 |
} |
| 72 |
|
| 73 |
func ResetMetricsForTest() { |
| 74 |
authorizationConfigAutomaticReloadsTotal.Reset() |
| 75 |
authorizationConfigAutomaticReloadLastTimestampSeconds.Reset() |
| 76 |
} |
| 77 |
|
| 78 |
func RecordAuthorizationConfigAutomaticReloadFailure(apiServerID string) { |
| 79 |
apiServerIDHash := getHash(apiServerID) |
| 80 |
authorizationConfigAutomaticReloadsTotal.WithLabelValues("failure", apiServerIDHash).Inc() |
| 81 |
authorizationConfigAutomaticReloadLastTimestampSeconds.WithLabelValues("failure", apiServerIDHash).SetToCurrentTime() |
| 82 |
} |
| 83 |
|
| 84 |
func RecordAuthorizationConfigAutomaticReloadSuccess(apiServerID string) { |
| 85 |
apiServerIDHash := getHash(apiServerID) |
| 86 |
authorizationConfigAutomaticReloadsTotal.WithLabelValues("success", apiServerIDHash).Inc() |
| 87 |
authorizationConfigAutomaticReloadLastTimestampSeconds.WithLabelValues("success", apiServerIDHash).SetToCurrentTime() |
| 88 |
} |
| 89 |
|
| 90 |
func getHash(data string) string { |
| 91 |
if len(data) == 0 { |
| 92 |
return "" |
| 93 |
} |
| 94 |
h := hashPool.Get().(hash.Hash) |
| 95 |
h.Reset() |
| 96 |
h.Write([]byte(data)) |
| 97 |
dataHash := fmt.Sprintf("sha256:%x", h.Sum(nil)) |
| 98 |
hashPool.Put(h) |
| 99 |
return dataHash |
| 100 |
} |