Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2018 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 exec |
| 18 | |
| 19 | import ( |
| 20 | "sync" |
| 21 | "time" |
| 22 | |
| 23 | "k8s.io/client-go/tools/metrics" |
| 24 | ) |
| 25 | |
| 26 | type certificateExpirationTracker struct { |
| 27 | mu sync.RWMutex |
| 28 | m map[*Authenticator]time.Time |
| 29 | metricSet func(*time.Time) |
| 30 | } |
| 31 | |
| 32 | var expirationMetrics = &certificateExpirationTracker{ |
| 33 | m: map[*Authenticator]time.Time{}, |
| 34 | metricSet: func(e *time.Time) { |
| 35 | metrics.ClientCertExpiry.Set(e) |
| 36 | }, |
| 37 | } |
| 38 | |
| 39 | // set stores the given expiration time and updates the updates the certificate |
| 40 | // expiry metric to the earliest expiration time. |
| 41 | func (c *certificateExpirationTracker) set(a *Authenticator, t time.Time) { |
| 42 | c.mu.Lock() |
| 43 | defer c.mu.Unlock() |
| 44 | c.m[a] = t |
| 45 | |
| 46 | earliest := time.Time{} |
| 47 | for _, t := range c.m { |
| 48 | if t.IsZero() { |
| 49 | continue |
| 50 | } |
| 51 | if earliest.IsZero() || earliest.After(t) { |
| 52 | earliest = t |
| 53 | } |
| 54 | } |
| 55 | if earliest.IsZero() { |
| 56 | c.metricSet(nil) |
| 57 | } else { |
| 58 | c.metricSet(&earliest) |
| 59 | } |
| 60 | } |