|
| 1 | +/* |
| 2 | +Copyright 2019 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 endpoint |
| 18 | + |
| 19 | +import ( |
| 20 | + "sync" |
| 21 | + "time" |
| 22 | + |
| 23 | + "k8s.io/api/core/v1" |
| 24 | + podutil "k8s.io/kubernetes/pkg/api/v1/pod" |
| 25 | +) |
| 26 | + |
| 27 | +// TriggerTimeTracker is a util used to compute the EndpointsLastChangeTriggerTime annotation which |
| 28 | +// is exported in the endpoints controller's sync function. |
| 29 | +// See the documentation of the EndpointsLastChangeTriggerTime annotation for more details. |
| 30 | +// |
| 31 | +// Please note that this util may compute a wrong EndpointsLastChangeTriggerTime if a same object |
| 32 | +// changes multiple times between two consecutive syncs. We're aware of this limitation but we |
| 33 | +// decided to accept it, as fixing it would require a major rewrite of the endpoints controller and |
| 34 | +// Informer framework. Such situations, i.e. frequent updates of the same object in a single sync |
| 35 | +// period, should be relatively rare and therefore this util should provide a good approximation of |
| 36 | +// the EndpointsLastChangeTriggerTime. |
| 37 | +// TODO(mm4tt): Implement a more robust mechanism that is not subject to the above limitations. |
| 38 | +type TriggerTimeTracker struct { |
| 39 | + // endpointsStates is a map, indexed by Endpoints object key, storing the last known Endpoints |
| 40 | + // object state observed during the most recent call of the ComputeEndpointsLastChangeTriggerTime |
| 41 | + // function. |
| 42 | + endpointsStates map[endpointsKey]endpointsState |
| 43 | + |
| 44 | + // mutex guarding the endpointsStates map. |
| 45 | + mutex sync.Mutex |
| 46 | +} |
| 47 | + |
| 48 | +// NewTriggerTimeTracker creates a new instance of the TriggerTimeTracker. |
| 49 | +func NewTriggerTimeTracker() *TriggerTimeTracker { |
| 50 | + return &TriggerTimeTracker{ |
| 51 | + endpointsStates: make(map[endpointsKey]endpointsState), |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// endpointsKey is a key uniquely identifying an Endpoints object. |
| 56 | +type endpointsKey struct { |
| 57 | + // namespace, name composing a namespaced name - an unique identifier of every Endpoints object. |
| 58 | + namespace, name string |
| 59 | +} |
| 60 | + |
| 61 | +// endpointsState represents a state of an Endpoints object that is known to this util. |
| 62 | +type endpointsState struct { |
| 63 | + // lastServiceTriggerTime is a service trigger time observed most recently. |
| 64 | + lastServiceTriggerTime time.Time |
| 65 | + // lastPodTriggerTimes is a map (Pod name -> time) storing the pod trigger times that were |
| 66 | + // observed during the most recent call of the ComputeEndpointsLastChangeTriggerTime function. |
| 67 | + lastPodTriggerTimes map[string]time.Time |
| 68 | +} |
| 69 | + |
| 70 | +// ComputeEndpointsLastChangeTriggerTime updates the state of the Endpoints object being synced |
| 71 | +// and returns the time that should be exported as the EndpointsLastChangeTriggerTime annotation. |
| 72 | +// |
| 73 | +// If the method returns a 'zero' time the EndpointsLastChangeTriggerTime annotation shouldn't be |
| 74 | +// exported. |
| 75 | +// |
| 76 | +// Please note that this function may compute a wrong EndpointsLastChangeTriggerTime value if the |
| 77 | +// same object (pod/service) changes multiple times between two consecutive syncs. |
| 78 | +// |
| 79 | +// Important: This method is go-routing safe but only when called for different keys. The method |
| 80 | +// shouldn't be called concurrently for the same key! This contract is fulfilled in the current |
| 81 | +// implementation of the endpoints controller. |
| 82 | +func (t *TriggerTimeTracker) ComputeEndpointsLastChangeTriggerTime( |
| 83 | + namespace, name string, service *v1.Service, pods []*v1.Pod) time.Time { |
| 84 | + |
| 85 | + key := endpointsKey{namespace: namespace, name: name} |
| 86 | + // As there won't be any concurrent calls for the same key, we need to guard access only to the |
| 87 | + // endpointsStates map. |
| 88 | + t.mutex.Lock() |
| 89 | + state, wasKnown := t.endpointsStates[key] |
| 90 | + t.mutex.Unlock() |
| 91 | + |
| 92 | + // Update the state before returning. |
| 93 | + defer func() { |
| 94 | + t.mutex.Lock() |
| 95 | + t.endpointsStates[key] = state |
| 96 | + t.mutex.Unlock() |
| 97 | + }() |
| 98 | + |
| 99 | + // minChangedTriggerTime is the min trigger time of all trigger times that have changed since the |
| 100 | + // last sync. |
| 101 | + var minChangedTriggerTime time.Time |
| 102 | + // TODO(mm4tt): If memory allocation / GC performance impact of recreating map in every call |
| 103 | + // turns out to be too expensive, we should consider rewriting this to reuse the existing map. |
| 104 | + podTriggerTimes := make(map[string]time.Time) |
| 105 | + for _, pod := range pods { |
| 106 | + if podTriggerTime := getPodTriggerTime(pod); !podTriggerTime.IsZero() { |
| 107 | + podTriggerTimes[pod.Name] = podTriggerTime |
| 108 | + if podTriggerTime.After(state.lastPodTriggerTimes[pod.Name]) { |
| 109 | + // Pod trigger time has changed since the last sync, update minChangedTriggerTime. |
| 110 | + minChangedTriggerTime = min(minChangedTriggerTime, podTriggerTime) |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + serviceTriggerTime := getServiceTriggerTime(service) |
| 115 | + if serviceTriggerTime.After(state.lastServiceTriggerTime) { |
| 116 | + // Service trigger time has changed since the last sync, update minChangedTriggerTime. |
| 117 | + minChangedTriggerTime = min(minChangedTriggerTime, serviceTriggerTime) |
| 118 | + } |
| 119 | + |
| 120 | + state.lastPodTriggerTimes = podTriggerTimes |
| 121 | + state.lastServiceTriggerTime = serviceTriggerTime |
| 122 | + |
| 123 | + if !wasKnown { |
| 124 | + // New Endpoints object / new Service, use Service creationTimestamp. |
| 125 | + return service.CreationTimestamp.Time |
| 126 | + } else { |
| 127 | + // Regular update of the Endpoints object, return min of changed trigger times. |
| 128 | + return minChangedTriggerTime |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// DeleteEndpoints deletes endpoints state stored in this util. |
| 133 | +func (t *TriggerTimeTracker) DeleteEndpoints(namespace, name string) { |
| 134 | + key := endpointsKey{namespace: namespace, name: name} |
| 135 | + t.mutex.Lock() |
| 136 | + defer t.mutex.Unlock() |
| 137 | + delete(t.endpointsStates, key) |
| 138 | +} |
| 139 | + |
| 140 | +// getPodTriggerTime returns the time of the pod change (trigger) that resulted or will result in |
| 141 | +// the endpoints object change. |
| 142 | +func getPodTriggerTime(pod *v1.Pod) (triggerTime time.Time) { |
| 143 | + if readyCondition := podutil.GetPodReadyCondition(pod.Status); readyCondition != nil { |
| 144 | + triggerTime = readyCondition.LastTransitionTime.Time |
| 145 | + } |
| 146 | + // TODO(mm4tt): Implement missing cases: deletionTime set, pod label change |
| 147 | + return triggerTime |
| 148 | +} |
| 149 | + |
| 150 | +// getServiceTriggerTime returns the time of the service change (trigger) that resulted or will |
| 151 | +// result in the endpoints object change. |
| 152 | +func getServiceTriggerTime(service *v1.Service) (triggerTime time.Time) { |
| 153 | + // TODO(mm4tt): Ideally we should look at service.LastUpdateTime, but such thing doesn't exist. |
| 154 | + return service.CreationTimestamp.Time |
| 155 | +} |
| 156 | + |
| 157 | +// min returns minimum of the currentMin and newValue or newValue if the currentMin is not set. |
| 158 | +func min(currentMin, newValue time.Time) time.Time { |
| 159 | + if currentMin.IsZero() || newValue.Before(currentMin) { |
| 160 | + return newValue |
| 161 | + } |
| 162 | + return currentMin |
| 163 | +} |
0 commit comments