1/* 2Copyright 2018 The Kubernetes Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package exec 18 19import ( 20 "sync" 21 "time" 22 23 "k8s.io/client-go/tools/metrics" 24) 25 26type certificateExpirationTracker struct { 27 mu sync.RWMutex 28 m map[*Authenticator]time.Time 29 metricSet func(*time.Time) 30} 31 32var 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. 41func (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} 61